Chirp Signal Generator
Dependencies: mbed
Fork of TAU_ZOOLOG_Playback_Rev1_1 by
main.cpp@2:1d99e9b70f0d, 2015-12-08 (annotated)
- Committer:
- TauZoolog
- Date:
- Tue Dec 08 12:15:51 2015 +0000
- Revision:
- 2:1d99e9b70f0d
- Parent:
- 1:0b510109a099
simple filter removed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TauZoolog | 0:e239fd599412 | 1 | /* |
TauZoolog | 0:e239fd599412 | 2 | Digital Filter Implementation: arkadiraf@gmail.com 21/10/2015 |
TauZoolog | 0:e239fd599412 | 3 | ADC --> Filter --> DAC , Based on NUCLEO - F303RE |
TauZoolog | 0:e239fd599412 | 4 | |
TauZoolog | 0:e239fd599412 | 5 | Pinout: |
TauZoolog | 0:e239fd599412 | 6 | ADC -- PA_0 -- A0 |
TauZoolog | 0:e239fd599412 | 7 | DAC -- PA_4 -- A2 |
TauZoolog | 0:e239fd599412 | 8 | |
TauZoolog | 0:e239fd599412 | 9 | I/O -- PA_5 -- D13 (Status LED, Condition) |
TauZoolog | 0:e239fd599412 | 10 | I/O -- PA_6 -- D12 (Toggle Pin, Loop Freq) |
TauZoolog | 0:e239fd599412 | 11 | I/O -- PA_7 -- D11 (General Output Pin ) |
TauZoolog | 0:e239fd599412 | 12 | |
TauZoolog | 0:e239fd599412 | 13 | Loop Running at up to 1 MHz, depending on filter lenght. |
TauZoolog | 0:e239fd599412 | 14 | 250 proccessor operations at 250 kHz |
TauZoolog | 0:e239fd599412 | 15 | Make sure to use float variables in filter (Not double!). |
TauZoolog | 0:e239fd599412 | 16 | */ |
TauZoolog | 0:e239fd599412 | 17 | #include "mbed.h" |
TauZoolog | 0:e239fd599412 | 18 | // mbed variables, Settings |
TauZoolog | 0:e239fd599412 | 19 | AnalogOut my_output(PA_4); |
TauZoolog | 0:e239fd599412 | 20 | AnalogIn my_input(PA_0); |
TauZoolog | 0:e239fd599412 | 21 | DigitalOut myled(PA_5); |
TauZoolog | 0:e239fd599412 | 22 | DigitalOut mytoggle(PA_6); |
TauZoolog | 0:e239fd599412 | 23 | DigitalOut mytrigger(PA_7); |
TauZoolog | 0:e239fd599412 | 24 | |
TauZoolog | 0:e239fd599412 | 25 | // Low level declerations |
TauZoolog | 0:e239fd599412 | 26 | ADC_HandleTypeDef hadc1; |
TauZoolog | 0:e239fd599412 | 27 | DAC_HandleTypeDef hdac1; |
TauZoolog | 0:e239fd599412 | 28 | |
TauZoolog | 0:e239fd599412 | 29 | // initialize low level dac, adc |
TauZoolog | 0:e239fd599412 | 30 | static void MX_ADC1_Init(void); |
TauZoolog | 0:e239fd599412 | 31 | static void MX_DAC1_Init(void); |
TauZoolog | 0:e239fd599412 | 32 | |
TauZoolog | 0:e239fd599412 | 33 | // declare filter |
TauZoolog | 0:e239fd599412 | 34 | inline float FilterFloat(float Variable); |
TauZoolog | 0:e239fd599412 | 35 | // declare output filter |
TauZoolog | 0:e239fd599412 | 36 | inline float OutputFilterFloat(float Variable); |
TauZoolog | 1:0b510109a099 | 37 | // declare first filter |
TauZoolog | 1:0b510109a099 | 38 | inline float FirstFilterFloat(float Variable); |
TauZoolog | 1:0b510109a099 | 39 | // declare second filter |
TauZoolog | 1:0b510109a099 | 40 | inline float SecondFilterFloat(float Variable); |
TauZoolog | 0:e239fd599412 | 41 | //nadav - declare simple filter |
TauZoolog | 2:1d99e9b70f0d | 42 | //inline float simpleFilterFloat(float Variable); |
TauZoolog | 0:e239fd599412 | 43 | |
TauZoolog | 0:e239fd599412 | 44 | // Variables |
TauZoolog | 0:e239fd599412 | 45 | uint16_t ADCValue=0; |
TauZoolog | 0:e239fd599412 | 46 | float ADCFloat=0; |
TauZoolog | 0:e239fd599412 | 47 | float OutputADCFloat=0; |
TauZoolog | 0:e239fd599412 | 48 | //amplitude scale is normalized such that 1 equals 3.3V, -1 equals 0V, and 0 equals 3.3/2V |
TauZoolog | 0:e239fd599412 | 49 | float ADC2Float=(2.0f/4095.0f); //ADCvalue*(2/0xFFF)-1.0f // 12 bits range |
TauZoolog | 0:e239fd599412 | 50 | float Float2ADC=(4095.0f/2.0f); //(ADCvalue+1.0f)*(0xFFF/2) // 12 bits range |
TauZoolog | 0:e239fd599412 | 51 | |
TauZoolog | 0:e239fd599412 | 52 | |
TauZoolog | 0:e239fd599412 | 53 | bool PinD11_state=0; |
TauZoolog | 0:e239fd599412 | 54 | bool PinD12_state=0; |
TauZoolog | 0:e239fd599412 | 55 | bool PinD13_state=0; |
TauZoolog | 0:e239fd599412 | 56 | |
TauZoolog | 0:e239fd599412 | 57 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 58 | //// Define Your Variables ////////////////////////////////////////////// Define Your Variables //// |
TauZoolog | 0:e239fd599412 | 59 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 60 | |
TauZoolog | 0:e239fd599412 | 61 | float example=0.0f; // example |
TauZoolog | 0:e239fd599412 | 62 | |
TauZoolog | 0:e239fd599412 | 63 | //nadav - Simple First order filter |
TauZoolog | 0:e239fd599412 | 64 | //get this from matlab script "simple_SimpleFilterExample.m" |
TauZoolog | 0:e239fd599412 | 65 | //float ALPF=0.152641; |
TauZoolog | 0:e239fd599412 | 66 | //float AHPF=0.999815; |
TauZoolog | 0:e239fd599412 | 67 | //float AHPF=0.999815; //10Hz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 68 | //float AHPF=0.574978; //40KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 69 | //float AHPF=0.782963; //15KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 70 | //float AHPF=0.844025; //10KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 71 | //float AHPF=0.871202; //8KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 72 | //float AHPF=0.915416; //5KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 73 | //float AHPF=0.900187; //6KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 74 | //float AHPF=0.885458; //7KHz cut off fs=340 |
TauZoolog | 0:e239fd599412 | 75 | //float AHPF=0.931168; //8KHz cut off fs=340*2 |
TauZoolog | 0:e239fd599412 | 76 | float AHPF=0.999128; //100Hz cut off fs=340*2 |
TauZoolog | 1:0b510109a099 | 77 | float SOSMat[6]= { 1.000000, -1.970399, 1.000000, 1.000000, -1.942826, 0.946079}; |
TauZoolog | 1:0b510109a099 | 78 | float Gscale=0.097947; |
TauZoolog | 1:0b510109a099 | 79 | //nrackoz ydagon hpf |
TauZoolog | 1:0b510109a099 | 80 | float SOSMat_H[6]= { 1.000000, -2.000000, 1.000000, 1.000000, -1.949235, 0.950492}; |
TauZoolog | 1:0b510109a099 | 81 | float Gscale_H=0.974932; |
TauZoolog | 1:0b510109a099 | 82 | //nrakocz ydagon - simple filter variables |
TauZoolog | 1:0b510109a099 | 83 | //float AHPF=0.730141; |
TauZoolog | 1:0b510109a099 | 84 | //filter 1 bus |
TauZoolog | 1:0b510109a099 | 85 | float LLastY=0; |
TauZoolog | 0:e239fd599412 | 86 | float LastY=0; |
TauZoolog | 0:e239fd599412 | 87 | float CurY=0; |
TauZoolog | 1:0b510109a099 | 88 | float LLastU=0; |
TauZoolog | 0:e239fd599412 | 89 | float LastU=0; |
TauZoolog | 0:e239fd599412 | 90 | float CurU=0; |
TauZoolog | 1:0b510109a099 | 91 | //filter 2 bus |
TauZoolog | 1:0b510109a099 | 92 | float LLastY_H=0; |
TauZoolog | 1:0b510109a099 | 93 | float LastY_H=0; |
TauZoolog | 1:0b510109a099 | 94 | float CurY_H=0; |
TauZoolog | 1:0b510109a099 | 95 | float LLastU_H=0; |
TauZoolog | 1:0b510109a099 | 96 | float LastU_H=0; |
TauZoolog | 1:0b510109a099 | 97 | float CurU_H=0; |
TauZoolog | 0:e239fd599412 | 98 | |
TauZoolog | 0:e239fd599412 | 99 | |
TauZoolog | 0:e239fd599412 | 100 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 101 | //// Define Your Variables ////////////////////////////////////////////// Define Your Variables //// |
TauZoolog | 0:e239fd599412 | 102 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 103 | |
TauZoolog | 0:e239fd599412 | 104 | |
TauZoolog | 0:e239fd599412 | 105 | |
TauZoolog | 0:e239fd599412 | 106 | |
TauZoolog | 0:e239fd599412 | 107 | // Main code |
TauZoolog | 0:e239fd599412 | 108 | int main() |
TauZoolog | 0:e239fd599412 | 109 | { |
TauZoolog | 0:e239fd599412 | 110 | // System Clock Configuration (Peripherial clocks) |
TauZoolog | 0:e239fd599412 | 111 | RCC_PeriphCLKInitTypeDef PeriphClkInit; |
TauZoolog | 0:e239fd599412 | 112 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12; |
TauZoolog | 0:e239fd599412 | 113 | PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; |
TauZoolog | 0:e239fd599412 | 114 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); |
TauZoolog | 0:e239fd599412 | 115 | |
TauZoolog | 0:e239fd599412 | 116 | MX_ADC1_Init(); |
TauZoolog | 0:e239fd599412 | 117 | MX_DAC1_Init(); |
TauZoolog | 0:e239fd599412 | 118 | |
TauZoolog | 0:e239fd599412 | 119 | HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0x000); |
TauZoolog | 0:e239fd599412 | 120 | HAL_DAC_Start(&hdac1, DAC_CHANNEL_1); |
TauZoolog | 0:e239fd599412 | 121 | |
TauZoolog | 0:e239fd599412 | 122 | HAL_ADC_Start(&hadc1); |
TauZoolog | 0:e239fd599412 | 123 | |
TauZoolog | 0:e239fd599412 | 124 | // more direct method to setting DAC value`s |
TauZoolog | 0:e239fd599412 | 125 | //define Dac Register. |
TauZoolog | 0:e239fd599412 | 126 | __IO uint32_t Dac_Reg = 0; |
TauZoolog | 0:e239fd599412 | 127 | Dac_Reg = (uint32_t) (hdac1.Instance); |
TauZoolog | 0:e239fd599412 | 128 | Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R); |
TauZoolog | 0:e239fd599412 | 129 | |
TauZoolog | 0:e239fd599412 | 130 | /* Set the DAC channel1 selected data holding register */ |
TauZoolog | 0:e239fd599412 | 131 | *(__IO uint32_t *) Dac_Reg = ADCValue; |
TauZoolog | 0:e239fd599412 | 132 | |
TauZoolog | 0:e239fd599412 | 133 | // more direct method to reading ADC value's: |
TauZoolog | 0:e239fd599412 | 134 | /* Clear regular group conversion flag */ |
TauZoolog | 0:e239fd599412 | 135 | |
TauZoolog | 0:e239fd599412 | 136 | //__HAL_ADC_CLEAR_FLAG(&hadc1, (ADC_FLAG_EOC | ADC_FLAG_EOS) ); |
TauZoolog | 0:e239fd599412 | 137 | |
TauZoolog | 0:e239fd599412 | 138 | /* Return ADC converted value */ |
TauZoolog | 0:e239fd599412 | 139 | //ADCValue=(uint16_t)(hadc1.Instance->DR); |
TauZoolog | 0:e239fd599412 | 140 | |
TauZoolog | 0:e239fd599412 | 141 | // Infinite loop |
TauZoolog | 0:e239fd599412 | 142 | |
TauZoolog | 0:e239fd599412 | 143 | // ADC to float convertion and back. |
TauZoolog | 0:e239fd599412 | 144 | |
TauZoolog | 0:e239fd599412 | 145 | while(1) { |
TauZoolog | 0:e239fd599412 | 146 | // more direct method to reading ADC value's: |
TauZoolog | 0:e239fd599412 | 147 | /* Return ADC converted value */ |
TauZoolog | 0:e239fd599412 | 148 | ADCValue=(uint16_t)(hadc1.Instance->DR); |
TauZoolog | 0:e239fd599412 | 149 | |
TauZoolog | 0:e239fd599412 | 150 | // read ADC value , Alternative more slow method |
TauZoolog | 0:e239fd599412 | 151 | //ADCValue=HAL_ADC_GetValue(&hadc1); |
TauZoolog | 0:e239fd599412 | 152 | |
TauZoolog | 0:e239fd599412 | 153 | // convert to float for filter |
TauZoolog | 0:e239fd599412 | 154 | //amplitude scale is normalized such that 1 equals 3.3V -1 euqales 0V, and 0 equales 3.3/2V |
TauZoolog | 0:e239fd599412 | 155 | ADCFloat=((float)ADCValue * ADC2Float)-1.0f; |
TauZoolog | 0:e239fd599412 | 156 | |
TauZoolog | 0:e239fd599412 | 157 | // toggle pin, Loop frequency indicator |
TauZoolog | 0:e239fd599412 | 158 | PinD12_state=!PinD12_state; |
TauZoolog | 0:e239fd599412 | 159 | if (PinD12_state) {GPIOA->BSRRL = GPIO_PIN_6;}else{GPIOA->BSRRH = GPIO_PIN_6;} |
TauZoolog | 0:e239fd599412 | 160 | |
TauZoolog | 0:e239fd599412 | 161 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 162 | //// Change Code from here ////////////////////////////////////////////// Change Code from here //// |
TauZoolog | 0:e239fd599412 | 163 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 164 | |
TauZoolog | 0:e239fd599412 | 165 | ////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 166 | // Apply Filter to reading before further processing// |
TauZoolog | 0:e239fd599412 | 167 | ////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 168 | |
TauZoolog | 2:1d99e9b70f0d | 169 | //ADCFloat=simpleFilterFloat(ADCFloat); //nadav - filtering input |
TauZoolog | 0:e239fd599412 | 170 | |
TauZoolog | 1:0b510109a099 | 171 | ADCFloat=FirstFilterFloat(ADCFloat); |
TauZoolog | 1:0b510109a099 | 172 | ADCFloat=SecondFilterFloat(ADCFloat); |
TauZoolog | 0:e239fd599412 | 173 | |
TauZoolog | 0:e239fd599412 | 174 | //////////////////////////// |
TauZoolog | 0:e239fd599412 | 175 | // Apply Filter to Output // |
TauZoolog | 0:e239fd599412 | 176 | //////////////////////////// |
TauZoolog | 0:e239fd599412 | 177 | |
TauZoolog | 0:e239fd599412 | 178 | OutputADCFloat=OutputFilterFloat(ADCFloat); |
TauZoolog | 0:e239fd599412 | 179 | |
TauZoolog | 0:e239fd599412 | 180 | // Set Condition of Pin D13 (LED2) Status |
TauZoolog | 0:e239fd599412 | 181 | if (ADCFloat>0.5f){ |
TauZoolog | 0:e239fd599412 | 182 | GPIOA->BSRRL = GPIO_PIN_5; |
TauZoolog | 0:e239fd599412 | 183 | }else{ |
TauZoolog | 0:e239fd599412 | 184 | GPIOA->BSRRH = GPIO_PIN_5; |
TauZoolog | 0:e239fd599412 | 185 | } |
TauZoolog | 0:e239fd599412 | 186 | |
TauZoolog | 0:e239fd599412 | 187 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 188 | //// Change Code Up To Here /////////////////////////////////////////// Change Code Up To Here //// |
TauZoolog | 0:e239fd599412 | 189 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 190 | |
TauZoolog | 0:e239fd599412 | 191 | |
TauZoolog | 0:e239fd599412 | 192 | // Apply limits to max dac values [0..1] |
TauZoolog | 0:e239fd599412 | 193 | // Convert filtered data back to uint16_t, |
TauZoolog | 0:e239fd599412 | 194 | if (OutputADCFloat < -1.0f) { |
TauZoolog | 0:e239fd599412 | 195 | ADCValue=0; // Min value |
TauZoolog | 0:e239fd599412 | 196 | } else if (OutputADCFloat > 1.0f) { |
TauZoolog | 0:e239fd599412 | 197 | ADCValue=0xFFF; // Max value |
TauZoolog | 0:e239fd599412 | 198 | } else { |
TauZoolog | 0:e239fd599412 | 199 | ADCValue=(uint16_t)((OutputADCFloat+1.0f) * Float2ADC); |
TauZoolog | 0:e239fd599412 | 200 | } |
TauZoolog | 0:e239fd599412 | 201 | |
TauZoolog | 0:e239fd599412 | 202 | // Update Dac value |
TauZoolog | 0:e239fd599412 | 203 | /* Set the DAC channel1 selected data holding register */ |
TauZoolog | 0:e239fd599412 | 204 | *(__IO uint32_t *) Dac_Reg = ADCValue; |
TauZoolog | 0:e239fd599412 | 205 | //HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValue); //Alternative more slow method |
TauZoolog | 0:e239fd599412 | 206 | |
TauZoolog | 0:e239fd599412 | 207 | } |
TauZoolog | 0:e239fd599412 | 208 | } |
TauZoolog | 0:e239fd599412 | 209 | /////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 210 | // Filter Function //////////////////////// |
TauZoolog | 0:e239fd599412 | 211 | /////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 212 | inline void NOP() { __ASM volatile ("nop"); } // one tick operation, Use to adjust frequency by slowing down the proccess |
TauZoolog | 0:e239fd599412 | 213 | //nadav - simple filter implementation |
TauZoolog | 2:1d99e9b70f0d | 214 | //inline float simpleFilterFloat(float Variable){ |
TauZoolog | 0:e239fd599412 | 215 | // Buffer variables |
TauZoolog | 2:1d99e9b70f0d | 216 | // LastU=CurU; |
TauZoolog | 2:1d99e9b70f0d | 217 | // CurU=Variable; |
TauZoolog | 2:1d99e9b70f0d | 218 | // LastY=CurY; |
TauZoolog | 0:e239fd599412 | 219 | |
TauZoolog | 0:e239fd599412 | 220 | // Simple Filter LFP |
TauZoolog | 0:e239fd599412 | 221 | //CurY=(1-ALPF)*LastY+ALPF*CurU; |
TauZoolog | 0:e239fd599412 | 222 | |
TauZoolog | 0:e239fd599412 | 223 | // Simple Filter HPF |
TauZoolog | 2:1d99e9b70f0d | 224 | // CurY=AHPF*(LastY+CurU-LastU); |
TauZoolog | 0:e239fd599412 | 225 | |
TauZoolog | 0:e239fd599412 | 226 | |
TauZoolog | 2:1d99e9b70f0d | 227 | // return CurY; |
TauZoolog | 2:1d99e9b70f0d | 228 | //}// end output filter function |
TauZoolog | 0:e239fd599412 | 229 | |
TauZoolog | 1:0b510109a099 | 230 | |
TauZoolog | 1:0b510109a099 | 231 | inline float FirstFilterFloat(float Variable){ |
TauZoolog | 1:0b510109a099 | 232 | LLastU_H=LastU_H; |
TauZoolog | 1:0b510109a099 | 233 | LastU_H=CurU_H; |
TauZoolog | 1:0b510109a099 | 234 | CurU_H=Variable; |
TauZoolog | 1:0b510109a099 | 235 | LLastY_H=LastY_H; |
TauZoolog | 1:0b510109a099 | 236 | LastY_H=CurY_H; |
TauZoolog | 1:0b510109a099 | 237 | |
TauZoolog | 1:0b510109a099 | 238 | // IIR biquad filter direct form 1 // y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2] |
TauZoolog | 1:0b510109a099 | 239 | CurY_H=SOSMat_H[0]*CurU_H + SOSMat_H[1]*LastU_H + SOSMat_H[2]*LLastU_H - SOSMat_H[4]*LastY_H - SOSMat_H[5]*LLastY_H; |
TauZoolog | 1:0b510109a099 | 240 | return CurY_H*Gscale_H; // scale output |
TauZoolog | 1:0b510109a099 | 241 | }// end filter function |
TauZoolog | 1:0b510109a099 | 242 | |
TauZoolog | 1:0b510109a099 | 243 | |
TauZoolog | 0:e239fd599412 | 244 | /////////////////////////////////////////// |
TauZoolog | 0:e239fd599412 | 245 | // Output Filter Function ///////////////// |
TauZoolog | 0:e239fd599412 | 246 | /////////////////////////////////////////// |
TauZoolog | 1:0b510109a099 | 247 | |
TauZoolog | 1:0b510109a099 | 248 | /////////////////////////////////////////// |
TauZoolog | 1:0b510109a099 | 249 | inline float SecondFilterFloat(float Variable, int LPF_flag){ |
TauZoolog | 1:0b510109a099 | 250 | |
TauZoolog | 1:0b510109a099 | 251 | // Buffer variables |
TauZoolog | 1:0b510109a099 | 252 | LLastU=LastU; |
TauZoolog | 1:0b510109a099 | 253 | LastU=CurU; |
TauZoolog | 1:0b510109a099 | 254 | CurU=Variable; |
TauZoolog | 1:0b510109a099 | 255 | LLastY=LastY; |
TauZoolog | 1:0b510109a099 | 256 | LastY=CurY; |
TauZoolog | 1:0b510109a099 | 257 | |
TauZoolog | 1:0b510109a099 | 258 | // IIR biquad filter direct form 1 // y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2] |
TauZoolog | 1:0b510109a099 | 259 | CurY=SOSMat[0]*CurU + SOSMat[1]*LastU + SOSMat[2]*LLastU - SOSMat[4]*LastY - SOSMat[5]*LLastY; |
TauZoolog | 1:0b510109a099 | 260 | return CurY*Gscale; |
TauZoolog | 1:0b510109a099 | 261 | |
TauZoolog | 1:0b510109a099 | 262 | //IIR biquad filter direct form 1 // y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a2 * y[n-2] |
TauZoolog | 1:0b510109a099 | 263 | CurY=SOSMat_H[0]*CurU + SOSMat_H[1]*LastU + SOSMat_H[2]*LLastU - SOSMat_H[4]*LastY - SOSMat_H[5]*LLastY; |
TauZoolog | 1:0b510109a099 | 264 | return CurY*Gscale_H; // scale output |
TauZoolog | 1:0b510109a099 | 265 | |
TauZoolog | 1:0b510109a099 | 266 | // end output filter function |
TauZoolog | 1:0b510109a099 | 267 | } |
TauZoolog | 1:0b510109a099 | 268 | |
TauZoolog | 1:0b510109a099 | 269 | /////gain filter///// |
TauZoolog | 0:e239fd599412 | 270 | inline float OutputFilterFloat(float Variable){ |
TauZoolog | 0:e239fd599412 | 271 | Variable*=1.0f; // Example of Math operation, Make sure to use float operations and not Double. |
TauZoolog | 0:e239fd599412 | 272 | return Variable; |
TauZoolog | 0:e239fd599412 | 273 | }// end output filter function |
TauZoolog | 0:e239fd599412 | 274 | |
TauZoolog | 0:e239fd599412 | 275 | |
TauZoolog | 0:e239fd599412 | 276 | |
TauZoolog | 0:e239fd599412 | 277 | |
TauZoolog | 0:e239fd599412 | 278 | //////////////////////// |
TauZoolog | 0:e239fd599412 | 279 | // Settings functions // |
TauZoolog | 0:e239fd599412 | 280 | //////////////////////// |
TauZoolog | 0:e239fd599412 | 281 | /* ADC1 init function */ |
TauZoolog | 0:e239fd599412 | 282 | void MX_ADC1_Init(void) |
TauZoolog | 0:e239fd599412 | 283 | { |
TauZoolog | 0:e239fd599412 | 284 | |
TauZoolog | 0:e239fd599412 | 285 | ADC_ChannelConfTypeDef sConfig; |
TauZoolog | 0:e239fd599412 | 286 | |
TauZoolog | 0:e239fd599412 | 287 | /**Common config |
TauZoolog | 0:e239fd599412 | 288 | */ |
TauZoolog | 0:e239fd599412 | 289 | hadc1.Instance = ADC1; |
TauZoolog | 0:e239fd599412 | 290 | hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC; |
TauZoolog | 0:e239fd599412 | 291 | hadc1.Init.Resolution = ADC_RESOLUTION12b; |
TauZoolog | 0:e239fd599412 | 292 | hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; |
TauZoolog | 0:e239fd599412 | 293 | hadc1.Init.ContinuousConvMode = ENABLE; |
TauZoolog | 0:e239fd599412 | 294 | hadc1.Init.DiscontinuousConvMode = DISABLE; |
TauZoolog | 0:e239fd599412 | 295 | hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
TauZoolog | 0:e239fd599412 | 296 | hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
TauZoolog | 0:e239fd599412 | 297 | hadc1.Init.NbrOfConversion = 1; |
TauZoolog | 0:e239fd599412 | 298 | hadc1.Init.DMAContinuousRequests = DISABLE; |
TauZoolog | 0:e239fd599412 | 299 | hadc1.Init.EOCSelection = EOC_SINGLE_CONV; |
TauZoolog | 0:e239fd599412 | 300 | hadc1.Init.LowPowerAutoWait = DISABLE; |
TauZoolog | 0:e239fd599412 | 301 | hadc1.Init.Overrun = OVR_DATA_OVERWRITTEN; |
TauZoolog | 0:e239fd599412 | 302 | HAL_ADC_Init(&hadc1); |
TauZoolog | 0:e239fd599412 | 303 | |
TauZoolog | 0:e239fd599412 | 304 | /**Configure Regular Channel |
TauZoolog | 0:e239fd599412 | 305 | */ |
TauZoolog | 0:e239fd599412 | 306 | sConfig.Channel = ADC_CHANNEL_1; |
TauZoolog | 0:e239fd599412 | 307 | sConfig.Rank = 1; |
TauZoolog | 0:e239fd599412 | 308 | sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED ;//ADC_DIFFERENTIAL_ENDED; //ADC_SINGLE_ENDED |
TauZoolog | 0:e239fd599412 | 309 | sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; //ADC_SAMPLETIME_1CYCLE_5; |
TauZoolog | 0:e239fd599412 | 310 | sConfig.OffsetNumber = ADC_OFFSET_NONE; |
TauZoolog | 0:e239fd599412 | 311 | sConfig.Offset = 0; |
TauZoolog | 0:e239fd599412 | 312 | HAL_ADC_ConfigChannel(&hadc1, &sConfig); |
TauZoolog | 0:e239fd599412 | 313 | |
TauZoolog | 0:e239fd599412 | 314 | } |
TauZoolog | 0:e239fd599412 | 315 | |
TauZoolog | 0:e239fd599412 | 316 | /* DAC1 init function */ |
TauZoolog | 0:e239fd599412 | 317 | void MX_DAC1_Init(void) |
TauZoolog | 0:e239fd599412 | 318 | { |
TauZoolog | 0:e239fd599412 | 319 | |
TauZoolog | 0:e239fd599412 | 320 | DAC_ChannelConfTypeDef sConfig; |
TauZoolog | 0:e239fd599412 | 321 | |
TauZoolog | 0:e239fd599412 | 322 | /**DAC Initialization |
TauZoolog | 0:e239fd599412 | 323 | */ |
TauZoolog | 0:e239fd599412 | 324 | hdac1.Instance = DAC1; |
TauZoolog | 0:e239fd599412 | 325 | HAL_DAC_Init(&hdac1); |
TauZoolog | 0:e239fd599412 | 326 | |
TauZoolog | 0:e239fd599412 | 327 | /**DAC channel OUT1 config |
TauZoolog | 0:e239fd599412 | 328 | */ |
TauZoolog | 0:e239fd599412 | 329 | sConfig.DAC_Trigger = DAC_TRIGGER_NONE; |
TauZoolog | 0:e239fd599412 | 330 | sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; |
TauZoolog | 0:e239fd599412 | 331 | HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1); |
TauZoolog | 0:e239fd599412 | 332 | |
TauZoolog | 0:e239fd599412 | 333 | } |