Martin Simpson
/
ELEC347_STM32F303K8_FIR
DSP Module
main.cpp@0:2470e37cc502, 2017-10-13 (annotated)
- Committer:
- martinsimpson
- Date:
- Fri Oct 13 09:29:43 2017 +0000
- Revision:
- 0:2470e37cc502
First Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martinsimpson | 0:2470e37cc502 | 1 | #include "mbed.h" |
martinsimpson | 0:2470e37cc502 | 2 | |
martinsimpson | 0:2470e37cc502 | 3 | //Just Comment out the Dataset you DO NOT wish to use and UN-Comment the one that you wish to use |
martinsimpson | 0:2470e37cc502 | 4 | //and then 'Build' <F7> and once complete 'Load' to the STM-Discovery board <F8> |
martinsimpson | 0:2470e37cc502 | 5 | |
martinsimpson | 0:2470e37cc502 | 6 | #include "LP_2KHz_Fc_6KHz_Fs_37N.h" |
martinsimpson | 0:2470e37cc502 | 7 | //#include "LP_2KHz_Fc_40KHz_Fs_37N.h" |
martinsimpson | 0:2470e37cc502 | 8 | //#include "LP_2KHz_Fc_40KHz_Fs_100N.h" |
martinsimpson | 0:2470e37cc502 | 9 | |
martinsimpson | 0:2470e37cc502 | 10 | #define ON 1 |
martinsimpson | 0:2470e37cc502 | 11 | #define OFF 0 |
martinsimpson | 0:2470e37cc502 | 12 | |
martinsimpson | 0:2470e37cc502 | 13 | float x[N]={0}; |
martinsimpson | 0:2470e37cc502 | 14 | float yn; |
martinsimpson | 0:2470e37cc502 | 15 | |
martinsimpson | 0:2470e37cc502 | 16 | void sampler(void); //Ticker routine PROTOTYPE |
martinsimpson | 0:2470e37cc502 | 17 | |
martinsimpson | 0:2470e37cc502 | 18 | //MBED Class Instances follows |
martinsimpson | 0:2470e37cc502 | 19 | DigitalOut SampLED(LED1); //Digital Output (GREEN LED is PB_3, D13 You can use an Oscilloscope on This pin to confirm sample rate) |
martinsimpson | 0:2470e37cc502 | 20 | |
martinsimpson | 0:2470e37cc502 | 21 | AnalogIn Ain(PA_1); //Analog Input (Signal Input 0 to +3 Volts) |
martinsimpson | 0:2470e37cc502 | 22 | AnalogOut Aout(PA_4); //Analog Output (Signal Input 0 to +3 Volts) NB PA_4 because Arduin Nano Compatability |
martinsimpson | 0:2470e37cc502 | 23 | //on STM32F303k8 uses I2C PA_5 to PB7 |
martinsimpson | 0:2470e37cc502 | 24 | |
martinsimpson | 0:2470e37cc502 | 25 | Ticker sample_timer; //Ticker class instance called sample_timer |
martinsimpson | 0:2470e37cc502 | 26 | |
martinsimpson | 0:2470e37cc502 | 27 | PwmOut test(PWM_OUT); |
martinsimpson | 0:2470e37cc502 | 28 | |
martinsimpson | 0:2470e37cc502 | 29 | |
martinsimpson | 0:2470e37cc502 | 30 | int main() { |
martinsimpson | 0:2470e37cc502 | 31 | float duty=0.1f; |
martinsimpson | 0:2470e37cc502 | 32 | test.period_ms(1); |
martinsimpson | 0:2470e37cc502 | 33 | test=duty; |
martinsimpson | 0:2470e37cc502 | 34 | |
martinsimpson | 0:2470e37cc502 | 35 | float sample_rate=(1.0/Fs)*1000000.0; //Calculate the number of uS required for a Frequency Sampling Rate |
martinsimpson | 0:2470e37cc502 | 36 | //Fs held in *.h |
martinsimpson | 0:2470e37cc502 | 37 | |
martinsimpson | 0:2470e37cc502 | 38 | sample_timer.attach_us(&sampler,(int)sample_rate); |
martinsimpson | 0:2470e37cc502 | 39 | //Ticker Instance serviced by routine at a repeat rate in microseconds |
martinsimpson | 0:2470e37cc502 | 40 | |
martinsimpson | 0:2470e37cc502 | 41 | while(1) { |
martinsimpson | 0:2470e37cc502 | 42 | sleep(); |
martinsimpson | 0:2470e37cc502 | 43 | } |
martinsimpson | 0:2470e37cc502 | 44 | } |
martinsimpson | 0:2470e37cc502 | 45 | |
martinsimpson | 0:2470e37cc502 | 46 | void sampler(void){ //Ticker routine |
martinsimpson | 0:2470e37cc502 | 47 | |
martinsimpson | 0:2470e37cc502 | 48 | SampLED = ON; //LED Indicates start of sampling |
martinsimpson | 0:2470e37cc502 | 49 | int i; //Initialise local variable i |
martinsimpson | 0:2470e37cc502 | 50 | x[0]=Ain; //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!! |
martinsimpson | 0:2470e37cc502 | 51 | yn=0.0; //output accumulation, start as zero |
martinsimpson | 0:2470e37cc502 | 52 | |
martinsimpson | 0:2470e37cc502 | 53 | for(i=0; i<N; i++) yn+=x[i]*b[i]; //generate output from filter components FIR a=0 |
martinsimpson | 0:2470e37cc502 | 54 | for(i=N-1; i!=0; i--) x[i]=x[i-1]; //shift data |
martinsimpson | 0:2470e37cc502 | 55 | |
martinsimpson | 0:2470e37cc502 | 56 | Aout=yn; //Output resultant to DAC. Again MBED uses 0.0 to 1.0 float!!!!!! |
martinsimpson | 0:2470e37cc502 | 57 | |
martinsimpson | 0:2470e37cc502 | 58 | SampLED = OFF; //LED Indicates end of sampling |
martinsimpson | 0:2470e37cc502 | 59 | } |
martinsimpson | 0:2470e37cc502 | 60 |