Chirp Signal Generator

Dependencies:   mbed

Fork of TAU_ZOOLOG_Playback_Rev1_1 by Yossi_Students

Committer:
TauZoolog
Date:
Sun Dec 06 19:39:37 2015 +0000
Revision:
0:e239fd599412
Child:
1:0b510109a099
adding filter

Who changed what in which revision?

UserRevisionLine numberNew 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 0:e239fd599412 37 //nadav - declare simple filter
TauZoolog 0:e239fd599412 38 inline float simpleFilterFloat(float Variable);
TauZoolog 0:e239fd599412 39
TauZoolog 0:e239fd599412 40 // Variables
TauZoolog 0:e239fd599412 41 uint16_t ADCValue=0;
TauZoolog 0:e239fd599412 42 float ADCFloat=0;
TauZoolog 0:e239fd599412 43 float OutputADCFloat=0;
TauZoolog 0:e239fd599412 44 //amplitude scale is normalized such that 1 equals 3.3V, -1 equals 0V, and 0 equals 3.3/2V
TauZoolog 0:e239fd599412 45 float ADC2Float=(2.0f/4095.0f); //ADCvalue*(2/0xFFF)-1.0f // 12 bits range
TauZoolog 0:e239fd599412 46 float Float2ADC=(4095.0f/2.0f); //(ADCvalue+1.0f)*(0xFFF/2) // 12 bits range
TauZoolog 0:e239fd599412 47
TauZoolog 0:e239fd599412 48
TauZoolog 0:e239fd599412 49 bool PinD11_state=0;
TauZoolog 0:e239fd599412 50 bool PinD12_state=0;
TauZoolog 0:e239fd599412 51 bool PinD13_state=0;
TauZoolog 0:e239fd599412 52
TauZoolog 0:e239fd599412 53 ////////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 54 //// Define Your Variables ////////////////////////////////////////////// Define Your Variables ////
TauZoolog 0:e239fd599412 55 ////////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 56
TauZoolog 0:e239fd599412 57 float example=0.0f; // example
TauZoolog 0:e239fd599412 58
TauZoolog 0:e239fd599412 59 //nadav - Simple First order filter
TauZoolog 0:e239fd599412 60 //get this from matlab script "simple_SimpleFilterExample.m"
TauZoolog 0:e239fd599412 61 //float ALPF=0.152641;
TauZoolog 0:e239fd599412 62 //float AHPF=0.999815;
TauZoolog 0:e239fd599412 63 //float AHPF=0.999815; //10Hz cut off fs=340
TauZoolog 0:e239fd599412 64 //float AHPF=0.574978; //40KHz cut off fs=340
TauZoolog 0:e239fd599412 65 //float AHPF=0.782963; //15KHz cut off fs=340
TauZoolog 0:e239fd599412 66 //float AHPF=0.844025; //10KHz cut off fs=340
TauZoolog 0:e239fd599412 67 //float AHPF=0.871202; //8KHz cut off fs=340
TauZoolog 0:e239fd599412 68 //float AHPF=0.915416; //5KHz cut off fs=340
TauZoolog 0:e239fd599412 69 //float AHPF=0.900187; //6KHz cut off fs=340
TauZoolog 0:e239fd599412 70 //float AHPF=0.885458; //7KHz cut off fs=340
TauZoolog 0:e239fd599412 71 //float AHPF=0.931168; //8KHz cut off fs=340*2
TauZoolog 0:e239fd599412 72 float AHPF=0.999128; //100Hz cut off fs=340*2
TauZoolog 0:e239fd599412 73 float LastY=0;
TauZoolog 0:e239fd599412 74 float CurY=0;
TauZoolog 0:e239fd599412 75 float LastU=0;
TauZoolog 0:e239fd599412 76 float CurU=0;
TauZoolog 0:e239fd599412 77
TauZoolog 0:e239fd599412 78
TauZoolog 0:e239fd599412 79 ////////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 80 //// Define Your Variables ////////////////////////////////////////////// Define Your Variables ////
TauZoolog 0:e239fd599412 81 ////////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 82
TauZoolog 0:e239fd599412 83
TauZoolog 0:e239fd599412 84
TauZoolog 0:e239fd599412 85
TauZoolog 0:e239fd599412 86 // Main code
TauZoolog 0:e239fd599412 87 int main()
TauZoolog 0:e239fd599412 88 {
TauZoolog 0:e239fd599412 89 // System Clock Configuration (Peripherial clocks)
TauZoolog 0:e239fd599412 90 RCC_PeriphCLKInitTypeDef PeriphClkInit;
TauZoolog 0:e239fd599412 91 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
TauZoolog 0:e239fd599412 92 PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1;
TauZoolog 0:e239fd599412 93 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
TauZoolog 0:e239fd599412 94
TauZoolog 0:e239fd599412 95 MX_ADC1_Init();
TauZoolog 0:e239fd599412 96 MX_DAC1_Init();
TauZoolog 0:e239fd599412 97
TauZoolog 0:e239fd599412 98 HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0x000);
TauZoolog 0:e239fd599412 99 HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
TauZoolog 0:e239fd599412 100
TauZoolog 0:e239fd599412 101 HAL_ADC_Start(&hadc1);
TauZoolog 0:e239fd599412 102
TauZoolog 0:e239fd599412 103 // more direct method to setting DAC value`s
TauZoolog 0:e239fd599412 104 //define Dac Register.
TauZoolog 0:e239fd599412 105 __IO uint32_t Dac_Reg = 0;
TauZoolog 0:e239fd599412 106 Dac_Reg = (uint32_t) (hdac1.Instance);
TauZoolog 0:e239fd599412 107 Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R);
TauZoolog 0:e239fd599412 108
TauZoolog 0:e239fd599412 109 /* Set the DAC channel1 selected data holding register */
TauZoolog 0:e239fd599412 110 *(__IO uint32_t *) Dac_Reg = ADCValue;
TauZoolog 0:e239fd599412 111
TauZoolog 0:e239fd599412 112 // more direct method to reading ADC value's:
TauZoolog 0:e239fd599412 113 /* Clear regular group conversion flag */
TauZoolog 0:e239fd599412 114
TauZoolog 0:e239fd599412 115 //__HAL_ADC_CLEAR_FLAG(&hadc1, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
TauZoolog 0:e239fd599412 116
TauZoolog 0:e239fd599412 117 /* Return ADC converted value */
TauZoolog 0:e239fd599412 118 //ADCValue=(uint16_t)(hadc1.Instance->DR);
TauZoolog 0:e239fd599412 119
TauZoolog 0:e239fd599412 120 // Infinite loop
TauZoolog 0:e239fd599412 121
TauZoolog 0:e239fd599412 122 // ADC to float convertion and back.
TauZoolog 0:e239fd599412 123
TauZoolog 0:e239fd599412 124 while(1) {
TauZoolog 0:e239fd599412 125 // more direct method to reading ADC value's:
TauZoolog 0:e239fd599412 126 /* Return ADC converted value */
TauZoolog 0:e239fd599412 127 ADCValue=(uint16_t)(hadc1.Instance->DR);
TauZoolog 0:e239fd599412 128
TauZoolog 0:e239fd599412 129 // read ADC value , Alternative more slow method
TauZoolog 0:e239fd599412 130 //ADCValue=HAL_ADC_GetValue(&hadc1);
TauZoolog 0:e239fd599412 131
TauZoolog 0:e239fd599412 132 // convert to float for filter
TauZoolog 0:e239fd599412 133 //amplitude scale is normalized such that 1 equals 3.3V -1 euqales 0V, and 0 equales 3.3/2V
TauZoolog 0:e239fd599412 134 ADCFloat=((float)ADCValue * ADC2Float)-1.0f;
TauZoolog 0:e239fd599412 135
TauZoolog 0:e239fd599412 136 // toggle pin, Loop frequency indicator
TauZoolog 0:e239fd599412 137 PinD12_state=!PinD12_state;
TauZoolog 0:e239fd599412 138 if (PinD12_state) {GPIOA->BSRRL = GPIO_PIN_6;}else{GPIOA->BSRRH = GPIO_PIN_6;}
TauZoolog 0:e239fd599412 139
TauZoolog 0:e239fd599412 140 ////////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 141 //// Change Code from here ////////////////////////////////////////////// Change Code from here ////
TauZoolog 0:e239fd599412 142 ////////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 143
TauZoolog 0:e239fd599412 144 //////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 145 // Apply Filter to reading before further processing//
TauZoolog 0:e239fd599412 146 //////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 147
TauZoolog 0:e239fd599412 148 ADCFloat=simpleFilterFloat(ADCFloat); //nadav - filtering input
TauZoolog 0:e239fd599412 149
TauZoolog 0:e239fd599412 150
TauZoolog 0:e239fd599412 151 ////////////////////////////
TauZoolog 0:e239fd599412 152 // Apply Filter to Output //
TauZoolog 0:e239fd599412 153 ////////////////////////////
TauZoolog 0:e239fd599412 154
TauZoolog 0:e239fd599412 155 OutputADCFloat=OutputFilterFloat(ADCFloat);
TauZoolog 0:e239fd599412 156
TauZoolog 0:e239fd599412 157 // Set Condition of Pin D13 (LED2) Status
TauZoolog 0:e239fd599412 158 if (ADCFloat>0.5f){
TauZoolog 0:e239fd599412 159 GPIOA->BSRRL = GPIO_PIN_5;
TauZoolog 0:e239fd599412 160 }else{
TauZoolog 0:e239fd599412 161 GPIOA->BSRRH = GPIO_PIN_5;
TauZoolog 0:e239fd599412 162 }
TauZoolog 0:e239fd599412 163
TauZoolog 0:e239fd599412 164 ///////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 165 //// Change Code Up To Here /////////////////////////////////////////// Change Code Up To Here ////
TauZoolog 0:e239fd599412 166 ///////////////////////////////////////////////////////////////////////////////////////////////////
TauZoolog 0:e239fd599412 167
TauZoolog 0:e239fd599412 168
TauZoolog 0:e239fd599412 169 // Apply limits to max dac values [0..1]
TauZoolog 0:e239fd599412 170 // Convert filtered data back to uint16_t,
TauZoolog 0:e239fd599412 171 if (OutputADCFloat < -1.0f) {
TauZoolog 0:e239fd599412 172 ADCValue=0; // Min value
TauZoolog 0:e239fd599412 173 } else if (OutputADCFloat > 1.0f) {
TauZoolog 0:e239fd599412 174 ADCValue=0xFFF; // Max value
TauZoolog 0:e239fd599412 175 } else {
TauZoolog 0:e239fd599412 176 ADCValue=(uint16_t)((OutputADCFloat+1.0f) * Float2ADC);
TauZoolog 0:e239fd599412 177 }
TauZoolog 0:e239fd599412 178
TauZoolog 0:e239fd599412 179 // Update Dac value
TauZoolog 0:e239fd599412 180 /* Set the DAC channel1 selected data holding register */
TauZoolog 0:e239fd599412 181 *(__IO uint32_t *) Dac_Reg = ADCValue;
TauZoolog 0:e239fd599412 182 //HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValue); //Alternative more slow method
TauZoolog 0:e239fd599412 183
TauZoolog 0:e239fd599412 184 }
TauZoolog 0:e239fd599412 185 }
TauZoolog 0:e239fd599412 186 ///////////////////////////////////////////
TauZoolog 0:e239fd599412 187 // Filter Function ////////////////////////
TauZoolog 0:e239fd599412 188 ///////////////////////////////////////////
TauZoolog 0:e239fd599412 189 inline void NOP() { __ASM volatile ("nop"); } // one tick operation, Use to adjust frequency by slowing down the proccess
TauZoolog 0:e239fd599412 190 //nadav - simple filter implementation
TauZoolog 0:e239fd599412 191 inline float simpleFilterFloat(float Variable){
TauZoolog 0:e239fd599412 192 // Buffer variables
TauZoolog 0:e239fd599412 193 LastU=CurU;
TauZoolog 0:e239fd599412 194 CurU=Variable;
TauZoolog 0:e239fd599412 195 LastY=CurY;
TauZoolog 0:e239fd599412 196
TauZoolog 0:e239fd599412 197 // Simple Filter LFP
TauZoolog 0:e239fd599412 198 //CurY=(1-ALPF)*LastY+ALPF*CurU;
TauZoolog 0:e239fd599412 199
TauZoolog 0:e239fd599412 200 // Simple Filter HPF
TauZoolog 0:e239fd599412 201 CurY=AHPF*(LastY+CurU-LastU);
TauZoolog 0:e239fd599412 202
TauZoolog 0:e239fd599412 203
TauZoolog 0:e239fd599412 204 return CurY;
TauZoolog 0:e239fd599412 205 }// end output filter function
TauZoolog 0:e239fd599412 206
TauZoolog 0:e239fd599412 207 ///////////////////////////////////////////
TauZoolog 0:e239fd599412 208 // Output Filter Function /////////////////
TauZoolog 0:e239fd599412 209 ///////////////////////////////////////////
TauZoolog 0:e239fd599412 210 inline float OutputFilterFloat(float Variable){
TauZoolog 0:e239fd599412 211 Variable*=1.0f; // Example of Math operation, Make sure to use float operations and not Double.
TauZoolog 0:e239fd599412 212 return Variable;
TauZoolog 0:e239fd599412 213 }// end output filter function
TauZoolog 0:e239fd599412 214
TauZoolog 0:e239fd599412 215
TauZoolog 0:e239fd599412 216
TauZoolog 0:e239fd599412 217
TauZoolog 0:e239fd599412 218 ////////////////////////
TauZoolog 0:e239fd599412 219 // Settings functions //
TauZoolog 0:e239fd599412 220 ////////////////////////
TauZoolog 0:e239fd599412 221 /* ADC1 init function */
TauZoolog 0:e239fd599412 222 void MX_ADC1_Init(void)
TauZoolog 0:e239fd599412 223 {
TauZoolog 0:e239fd599412 224
TauZoolog 0:e239fd599412 225 ADC_ChannelConfTypeDef sConfig;
TauZoolog 0:e239fd599412 226
TauZoolog 0:e239fd599412 227 /**Common config
TauZoolog 0:e239fd599412 228 */
TauZoolog 0:e239fd599412 229 hadc1.Instance = ADC1;
TauZoolog 0:e239fd599412 230 hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
TauZoolog 0:e239fd599412 231 hadc1.Init.Resolution = ADC_RESOLUTION12b;
TauZoolog 0:e239fd599412 232 hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
TauZoolog 0:e239fd599412 233 hadc1.Init.ContinuousConvMode = ENABLE;
TauZoolog 0:e239fd599412 234 hadc1.Init.DiscontinuousConvMode = DISABLE;
TauZoolog 0:e239fd599412 235 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
TauZoolog 0:e239fd599412 236 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
TauZoolog 0:e239fd599412 237 hadc1.Init.NbrOfConversion = 1;
TauZoolog 0:e239fd599412 238 hadc1.Init.DMAContinuousRequests = DISABLE;
TauZoolog 0:e239fd599412 239 hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
TauZoolog 0:e239fd599412 240 hadc1.Init.LowPowerAutoWait = DISABLE;
TauZoolog 0:e239fd599412 241 hadc1.Init.Overrun = OVR_DATA_OVERWRITTEN;
TauZoolog 0:e239fd599412 242 HAL_ADC_Init(&hadc1);
TauZoolog 0:e239fd599412 243
TauZoolog 0:e239fd599412 244 /**Configure Regular Channel
TauZoolog 0:e239fd599412 245 */
TauZoolog 0:e239fd599412 246 sConfig.Channel = ADC_CHANNEL_1;
TauZoolog 0:e239fd599412 247 sConfig.Rank = 1;
TauZoolog 0:e239fd599412 248 sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED ;//ADC_DIFFERENTIAL_ENDED; //ADC_SINGLE_ENDED
TauZoolog 0:e239fd599412 249 sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; //ADC_SAMPLETIME_1CYCLE_5;
TauZoolog 0:e239fd599412 250 sConfig.OffsetNumber = ADC_OFFSET_NONE;
TauZoolog 0:e239fd599412 251 sConfig.Offset = 0;
TauZoolog 0:e239fd599412 252 HAL_ADC_ConfigChannel(&hadc1, &sConfig);
TauZoolog 0:e239fd599412 253
TauZoolog 0:e239fd599412 254 }
TauZoolog 0:e239fd599412 255
TauZoolog 0:e239fd599412 256 /* DAC1 init function */
TauZoolog 0:e239fd599412 257 void MX_DAC1_Init(void)
TauZoolog 0:e239fd599412 258 {
TauZoolog 0:e239fd599412 259
TauZoolog 0:e239fd599412 260 DAC_ChannelConfTypeDef sConfig;
TauZoolog 0:e239fd599412 261
TauZoolog 0:e239fd599412 262 /**DAC Initialization
TauZoolog 0:e239fd599412 263 */
TauZoolog 0:e239fd599412 264 hdac1.Instance = DAC1;
TauZoolog 0:e239fd599412 265 HAL_DAC_Init(&hdac1);
TauZoolog 0:e239fd599412 266
TauZoolog 0:e239fd599412 267 /**DAC channel OUT1 config
TauZoolog 0:e239fd599412 268 */
TauZoolog 0:e239fd599412 269 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
TauZoolog 0:e239fd599412 270 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
TauZoolog 0:e239fd599412 271 HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1);
TauZoolog 0:e239fd599412 272
TauZoolog 0:e239fd599412 273 }