Jared Baxter
/
Impedance_Fast_Circuitry
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp@74:ebc9f09fda11, 2017-06-01 (annotated)
- Committer:
- baxterja
- Date:
- Thu Jun 01 22:12:31 2017 +0000
- Revision:
- 74:ebc9f09fda11
- Parent:
- 73:b059b5bdc664
- Child:
- 75:8bb94685c80b
Working after updating mbed.bld
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
timmey9 | 22:523e316cbe70 | 2 | |
timmey9 | 52:5a40cc58c4c2 | 3 | // Sampling |
bmazzeo | 53:83a90a47c1fd | 4 | #include "DMA_sampling/adc.h" |
bmazzeo | 54:1697dc574b96 | 5 | #include "DMA_sampling/dac.h" |
bmazzeo | 55:2526b3317bc8 | 6 | #include "DMA_sampling/pdb.h" |
baxterja | 73:b059b5bdc664 | 7 | #include "Jareds_DSP/filters_jared.h" |
timmey9 | 22:523e316cbe70 | 8 | |
bmazzeo | 67:ec0c58490ce6 | 9 | // DSP |
bmazzeo | 67:ec0c58490ce6 | 10 | #include "dsp.h" |
bmazzeo | 67:ec0c58490ce6 | 11 | |
baxterja | 72:0b554f5026b9 | 12 | #define PRINT_BUFFER_LENGTH 10000 |
baxterja | 73:b059b5bdc664 | 13 | #define GATHER_STATISTICS 1 |
bmazzeo | 67:ec0c58490ce6 | 14 | |
baxterja | 74:ebc9f09fda11 | 15 | |
timmey9 | 22:523e316cbe70 | 16 | // for debug purposes |
timmey9 | 18:b17ddeeb1c09 | 17 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 18 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 19 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 20 | DigitalOut led_blue(LED_BLUE); |
bmazzeo | 58:4bee89daccff | 21 | DigitalOut status_0(D0); |
bmazzeo | 58:4bee89daccff | 22 | DigitalOut status_1(D1); |
baxterja | 73:b059b5bdc664 | 23 | DigitalIn sw2(SW2);//Button 2 |
baxterja | 73:b059b5bdc664 | 24 | DigitalIn sw3(SW3);//Button 3 |
timmey9 | 51:43143a3fc2d7 | 25 | |
timmey9 | 52:5a40cc58c4c2 | 26 | // defined in dma.cpp |
timmey9 | 45:d591d138cdeb | 27 | extern int len; |
baxterja | 73:b059b5bdc664 | 28 | extern uint16_t static_input_array0[];//ADC 0(swaps between a0 and a1. Used to measure current) |
baxterja | 73:b059b5bdc664 | 29 | extern uint16_t static_input_array1[];//ADC 1(measures the voltage between the probe and ground) |
baxterja | 73:b059b5bdc664 | 30 | extern uint16_t static_output_array0[];//DAC outputs whatever wave form we want. |
baxterja | 73:b059b5bdc664 | 31 | extern uint16_t sampling_status;//used to determine when adc's are done reading. |
bmazzeo | 57:7b8c49e1c1f6 | 32 | |
baxterja | 73:b059b5bdc664 | 33 | #define INPUT_ARRAY_SIZE 32 |
baxterja | 73:b059b5bdc664 | 34 | #define DECIMATION_FACTOR 8 |
baxterja | 73:b059b5bdc664 | 35 | #define DEMODULATED_SIGNAL_LENGTH 64 |
baxterja | 73:b059b5bdc664 | 36 | |
baxterja | 73:b059b5bdc664 | 37 | float *input_50k[8]; |
baxterja | 73:b059b5bdc664 | 38 | |
baxterja | 73:b059b5bdc664 | 39 | |
baxterja | 73:b059b5bdc664 | 40 | |
baxterja | 73:b059b5bdc664 | 41 | /* |
bmazzeo | 67:ec0c58490ce6 | 42 | // To set up FIR filtering |
baxterja | 73:b059b5bdc664 | 43 | #define TOTAL_TAPS 20 |
baxterja | 73:b059b5bdc664 | 44 | #define STATE_LENGTH 51 |
baxterja | 73:b059b5bdc664 | 45 | #define BUFFER_LENGTH 32 |
bmazzeo | 67:ec0c58490ce6 | 46 | uint16_t numTaps = TOTAL_TAPS; |
baxterja | 73:b059b5bdc664 | 47 | float States_50k[4][STATE_LENGTH]= {0}; |
baxterja | 73:b059b5bdc664 | 48 | float States_6250[8][STATE_LENGTH]= {0}; |
baxterja | 73:b059b5bdc664 | 49 | float States_781[8][STATE_LENGTH]= {0}; |
baxterja | 73:b059b5bdc664 | 50 | |
baxterja | 73:b059b5bdc664 | 51 | float Coeffs[TOTAL_TAPS] = {0.0380723866161221, 0.0263930577547015, 0.0332622132972336, 0.0408057838845535, 0.0482178949738844, 0.0551211071268984, 0.0611830030337647, 0.0660392721230197, 0.0694611630329689, 0.0711942672973351, 0.0711942672973351, 0.0694611630329688, 0.0660392721230197, 0.0611830030337647, 0.0551211071268984, 0.0482178949738844, 0.0408057838845535, 0.0332622132972336, 0.0263930577547015, 0.0380723866161221}; |
baxterja | 73:b059b5bdc664 | 52 | //arm_fir_instance_f32 S = {numTaps, State, Coeffs}; |
baxterja | 73:b059b5bdc664 | 53 | arm_fir_decimate_instance_f32 filterStruct_50k[4] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[0], |
baxterja | 73:b059b5bdc664 | 54 | DECIMATION_FACTOR, numTaps, Coeffs, States_50k[1], |
baxterja | 73:b059b5bdc664 | 55 | DECIMATION_FACTOR, numTaps, Coeffs, States_50k[2], |
baxterja | 73:b059b5bdc664 | 56 | DECIMATION_FACTOR, numTaps, Coeffs, States_50k[3]}; |
baxterja | 73:b059b5bdc664 | 57 | |
baxterja | 73:b059b5bdc664 | 58 | arm_fir_decimate_instance_f32 filterStruct_6250[8] = {DECIMATION_FACTOR, numTaps, Coeffs, States_6250[0], |
baxterja | 73:b059b5bdc664 | 59 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[1], |
baxterja | 73:b059b5bdc664 | 60 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[2], |
baxterja | 73:b059b5bdc664 | 61 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[3], |
baxterja | 73:b059b5bdc664 | 62 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[4], |
baxterja | 73:b059b5bdc664 | 63 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[5], |
baxterja | 73:b059b5bdc664 | 64 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[6], |
baxterja | 73:b059b5bdc664 | 65 | DECIMATION_FACTOR, numTaps, Coeffs, States_6250[7]}; |
baxterja | 73:b059b5bdc664 | 66 | |
baxterja | 73:b059b5bdc664 | 67 | arm_fir_decimate_instance_f32 filterStruct_781[8] = {DECIMATION_FACTOR, numTaps, Coeffs, States_781[0], |
baxterja | 73:b059b5bdc664 | 68 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[1], |
baxterja | 73:b059b5bdc664 | 69 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[2], |
baxterja | 73:b059b5bdc664 | 70 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[3], |
baxterja | 73:b059b5bdc664 | 71 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[4], |
baxterja | 73:b059b5bdc664 | 72 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[5], |
baxterja | 73:b059b5bdc664 | 73 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[6], |
baxterja | 73:b059b5bdc664 | 74 | DECIMATION_FACTOR, numTaps, Coeffs, States_781[7]}; |
baxterja | 73:b059b5bdc664 | 75 | //filterStruct_50k[0] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[0]}; |
baxterja | 73:b059b5bdc664 | 76 | //filterStruct_50k[1] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[1]}; |
baxterja | 73:b059b5bdc664 | 77 | //filterStruct_50k[2] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[2]}; |
baxterja | 73:b059b5bdc664 | 78 | //filterStruct_50k[3] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[3]}; |
bmazzeo | 67:ec0c58490ce6 | 79 | |
bmazzeo | 67:ec0c58490ce6 | 80 | float filter_input_array[BUFFER_LENGTH] = {0}; |
bmazzeo | 67:ec0c58490ce6 | 81 | float filter_output_array[BUFFER_LENGTH] = {0}; |
baxterja | 73:b059b5bdc664 | 82 | */ |
baxterja | 69:014d4bbd4e03 | 83 | #define pre_compute_length 500 |
baxterja | 73:b059b5bdc664 | 84 | #define demodulation_length 125 |
baxterja | 69:014d4bbd4e03 | 85 | #define CarrierFrequency 200 |
baxterja | 69:014d4bbd4e03 | 86 | #define SAMPLEFREQUENCY 100000 |
baxterja | 73:b059b5bdc664 | 87 | //float i_mod_pre[demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR)]; |
baxterja | 73:b059b5bdc664 | 88 | //float q_mod_pre[demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR)]; |
baxterja | 73:b059b5bdc664 | 89 | uint16_t out_val_pre[pre_compute_length]; //used to write values to the dac |
baxterja | 69:014d4bbd4e03 | 90 | float twopi = 3.14159265359 * 2; |
baxterja | 69:014d4bbd4e03 | 91 | |
baxterja | 69:014d4bbd4e03 | 92 | |
baxterja | 69:014d4bbd4e03 | 93 | void pre_compute_tables() { |
baxterja | 69:014d4bbd4e03 | 94 | // This function will precompute the cos and sin tables used in the rest of the program |
baxterja | 69:014d4bbd4e03 | 95 | for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ |
baxterja | 73:b059b5bdc664 | 96 | out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0);//12 bit cos wave |
baxterja | 69:014d4bbd4e03 | 97 | } |
baxterja | 73:b059b5bdc664 | 98 | |
baxterja | 73:b059b5bdc664 | 99 | //float decimated_frequency = 6250; |
baxterja | 73:b059b5bdc664 | 100 | //for(int precompute_counter = 0; precompute_counter < demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR); precompute_counter++){ |
baxterja | 73:b059b5bdc664 | 101 | // i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency / decimated_frequency * precompute_counter)); |
baxterja | 73:b059b5bdc664 | 102 | // q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency / decimated_frequency * precompute_counter)); |
baxterja | 73:b059b5bdc664 | 103 | |
baxterja | 69:014d4bbd4e03 | 104 | } |
baxterja | 69:014d4bbd4e03 | 105 | |
baxterja | 69:014d4bbd4e03 | 106 | |
emilmont | 7:65188f4a8c25 | 107 | int main() { |
baxterja | 73:b059b5bdc664 | 108 | pre_compute_tables(); |
baxterja | 73:b059b5bdc664 | 109 | //turn off all LEDs |
timmey9 | 22:523e316cbe70 | 110 | led_blue = 1; |
timmey9 | 35:df40c4566826 | 111 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 112 | led_red = 1; |
timmey9 | 34:44cc9b76a507 | 113 | |
baxterja | 73:b059b5bdc664 | 114 | |
baxterja | 69:014d4bbd4e03 | 115 | int DAC_COUNTER = 0; |
baxterja | 73:b059b5bdc664 | 116 | |
baxterja | 73:b059b5bdc664 | 117 | pc.baud(230400); |
baxterja | 73:b059b5bdc664 | 118 | pc.printf("Starting\r\n"); |
baxterja | 73:b059b5bdc664 | 119 | float Coeffs[20] = {0.0328368433284673, 0.0237706090075265, 0.0309894695180997, 0.0385253568846695, 0.0459996974310349, 0.0530165318016261, 0.0591943866845610, 0.0641755708098907, 0.0676960677594849, 0.0694621149975389, 0.0694621149975389, 0.0676960677594849, 0.0641755708098907, 0.0591943866845610, 0.0530165318016261, 0.0459996974310349, 0.0385253568846695, 0.0309894695180997, 0.0237706090075265, 0.0328368433284673}; |
baxterja | 73:b059b5bdc664 | 120 | float Coeffs2[20] = {-0.00506451294187997, -0.00824932319607346, -0.00986370066237912, -0.00518913235010027, 0.00950858650162284, 0.0357083149022659, 0.0711557142019980, 0.109659494661247, 0.142586101123340, 0.161603335553589, 0.161603335553589, 0.142586101123340, 0.109659494661247, 0.0711557142019980, 0.0357083149022659, 0.00950858650162284, -0.00518913235010027, -0.00986370066237912, -0.00824932319607346, -0.00506451294187997}; |
baxterja | 73:b059b5bdc664 | 121 | |
baxterja | 73:b059b5bdc664 | 122 | |
baxterja | 73:b059b5bdc664 | 123 | for(int i = 0; i < 8; i++) |
baxterja | 73:b059b5bdc664 | 124 | input_50k[i] = new float[32];//each array represents the input of the adcs |
baxterja | 73:b059b5bdc664 | 125 | |
baxterja | 73:b059b5bdc664 | 126 | filters f3 = filters(8, 8, NULL, 8, 20, Coeffs2,false); |
baxterja | 73:b059b5bdc664 | 127 | |
baxterja | 73:b059b5bdc664 | 128 | //6250->781 |
baxterja | 73:b059b5bdc664 | 129 | filters f2 = filters(8, 8, &f3, 8, 20, Coeffs,false); |
baxterja | 73:b059b5bdc664 | 130 | |
baxterja | 73:b059b5bdc664 | 131 | //50,000->6250 |
baxterja | 73:b059b5bdc664 | 132 | filters f1 = filters(4, 8, &f2, 32, 20, Coeffs,true); |
baxterja | 69:014d4bbd4e03 | 133 | |
baxterja | 69:014d4bbd4e03 | 134 | |
baxterja | 73:b059b5bdc664 | 135 | //float output_print_buffer[PRINT_BUFFER_LENGTH];//used to store the adc0 values(current measurment) |
baxterja | 73:b059b5bdc664 | 136 | //float output_print_buffer2[PRINT_BUFFER_LENGTH];//used to store the adc0 values(voltage measurment) |
baxterja | 70:8cd7a8a2c153 | 137 | int print_buffer_count = 0; |
baxterja | 70:8cd7a8a2c153 | 138 | |
bmazzeo | 66:72c5c24e532c | 139 | pdb_init(); // Initalize PDB |
bmazzeo | 66:72c5c24e532c | 140 | dac_init(); // initializes DAC |
timmey9 | 45:d591d138cdeb | 141 | adc_init(); // always initialize adc before dma |
bmazzeo | 53:83a90a47c1fd | 142 | pc.printf("ADC Initialized\r\n"); |
baxterja | 74:ebc9f09fda11 | 143 | dma_init2(); // initializes DMAs |
bmazzeo | 55:2526b3317bc8 | 144 | dma_reset(); // This clears any DMA triggers that may have gotten things into a different state |
bmazzeo | 67:ec0c58490ce6 | 145 | pc.printf("Buffer Size: %i\r\n", len); |
bmazzeo | 54:1697dc574b96 | 146 | |
timmey9 | 46:a015ebf4663b | 147 | led_green = 1; |
timmey9 | 38:ec3b16c130d7 | 148 | |
timmey9 | 40:bd6d8c35e822 | 149 | pc.printf("\r\n\r\n\r\n"); |
timmey9 | 37:8bdc71f3e874 | 150 | |
bmazzeo | 64:bb4a4bd57681 | 151 | pdb_start(); |
baxterja | 70:8cd7a8a2c153 | 152 | //while(print_buffer_count<PRINT_BUFFER_LENGTH) |
baxterja | 73:b059b5bdc664 | 153 | while(!GATHER_STATISTICS||print_buffer_count<PRINT_BUFFER_LENGTH) |
baxterja | 73:b059b5bdc664 | 154 | { |
baxterja | 73:b059b5bdc664 | 155 | while(sampling_status == 0)//wait until the ADCs read a new value |
baxterja | 73:b059b5bdc664 | 156 | { |
baxterja | 73:b059b5bdc664 | 157 | status_0 = 1; |
baxterja | 73:b059b5bdc664 | 158 | } |
baxterja | 73:b059b5bdc664 | 159 | sampling_status = 0;//sets sampling status to 0. DMA sets it to one once ADCs sample |
baxterja | 73:b059b5bdc664 | 160 | status_0 = 0;//Tied to D0. use an O-scope to measure how much free time there is to play with. |
baxterja | 73:b059b5bdc664 | 161 | status_1 = 1;//Tied to D1. use an O-scope to measure how much time the processing takes |
baxterja | 73:b059b5bdc664 | 162 | |
baxterja | 73:b059b5bdc664 | 163 | for(int i = 0; i < len; i+=2) |
bmazzeo | 61:a56cca07d4a6 | 164 | { |
baxterja | 73:b059b5bdc664 | 165 | static_output_array0[i] = out_val_pre[DAC_COUNTER];//DAC output |
baxterja | 73:b059b5bdc664 | 166 | DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table |
baxterja | 73:b059b5bdc664 | 167 | if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter |
baxterja | 73:b059b5bdc664 | 168 | static_output_array0[i+1] = out_val_pre[DAC_COUNTER];//DAC output |
baxterja | 73:b059b5bdc664 | 169 | DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table |
baxterja | 73:b059b5bdc664 | 170 | if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter |
baxterja | 73:b059b5bdc664 | 171 | |
baxterja | 73:b059b5bdc664 | 172 | input_50k[1][i/2]=static_input_array1[i]; |
baxterja | 73:b059b5bdc664 | 173 | input_50k[3][i/2]=static_input_array1[i+1]; |
baxterja | 73:b059b5bdc664 | 174 | input_50k[0][i/2]=static_input_array0[i]; |
baxterja | 73:b059b5bdc664 | 175 | input_50k[2][i/2]=static_input_array0[i+1]; |
baxterja | 73:b059b5bdc664 | 176 | |
baxterja | 73:b059b5bdc664 | 177 | |
baxterja | 73:b059b5bdc664 | 178 | |
baxterja | 73:b059b5bdc664 | 179 | }//End of for loop going throught the buffer of adc samples |
baxterja | 73:b059b5bdc664 | 180 | f1.input(input_50k,32); |
baxterja | 70:8cd7a8a2c153 | 181 | |
baxterja | 73:b059b5bdc664 | 182 | |
baxterja | 73:b059b5bdc664 | 183 | |
baxterja | 70:8cd7a8a2c153 | 184 | |
baxterja | 73:b059b5bdc664 | 185 | status_1 = 0;//turn off D1 used in deterimining how long processing is taking |
baxterja | 73:b059b5bdc664 | 186 | //filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000); |
baxterja | 73:b059b5bdc664 | 187 | //arm_fir_f32(&S, filter_input_array, filter_output_array, len); |
baxterja | 73:b059b5bdc664 | 188 | |
baxterja | 73:b059b5bdc664 | 189 | }//end of while loop |
baxterja | 73:b059b5bdc664 | 190 | |
baxterja | 73:b059b5bdc664 | 191 | //for(int i = 0; i<PRINT_BUFFER_LENGTH; i++)//print all the adc values measured |
baxterja | 73:b059b5bdc664 | 192 | //{ |
baxterja | 73:b059b5bdc664 | 193 | // printf("%.1f %.1f\n\r",output_print_buffer[i],output_print_buffer2[i]); |
baxterja | 73:b059b5bdc664 | 194 | //} |
baxterja | 73:b059b5bdc664 | 195 | |
baxterja | 70:8cd7a8a2c153 | 196 | |
bmazzeo | 61:a56cca07d4a6 | 197 | } |
bmazzeo | 63:7903a33e2fd4 | 198 |