
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp
- Committer:
- baxterja
- Date:
- 2017-05-31
- Revision:
- 73:b059b5bdc664
- Parent:
- 72:0b554f5026b9
- Child:
- 74:ebc9f09fda11
File content as of revision 73:b059b5bdc664:
#include "mbed.h" // Sampling #include "DMA_sampling/adc.h" #include "DMA_sampling/dac.h" #include "DMA_sampling/pdb.h" #include "Jareds_DSP/filters_jared.h" // DSP #include "dsp.h" #define PRINT_BUFFER_LENGTH 10000 #define GATHER_STATISTICS 1 // for debug purposes Serial pc(USBTX, USBRX); DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); DigitalOut status_0(D0); DigitalOut status_1(D1); DigitalIn sw2(SW2);//Button 2 DigitalIn sw3(SW3);//Button 3 // defined in dma.cpp extern int len; extern uint16_t static_input_array0[];//ADC 0(swaps between a0 and a1. Used to measure current) extern uint16_t static_input_array1[];//ADC 1(measures the voltage between the probe and ground) extern uint16_t static_output_array0[];//DAC outputs whatever wave form we want. extern uint16_t sampling_status;//used to determine when adc's are done reading. #define INPUT_ARRAY_SIZE 32 #define DECIMATION_FACTOR 8 #define DEMODULATED_SIGNAL_LENGTH 64 float *input_50k[8]; /* // To set up FIR filtering #define TOTAL_TAPS 20 #define STATE_LENGTH 51 #define BUFFER_LENGTH 32 uint16_t numTaps = TOTAL_TAPS; float States_50k[4][STATE_LENGTH]= {0}; float States_6250[8][STATE_LENGTH]= {0}; float States_781[8][STATE_LENGTH]= {0}; 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}; //arm_fir_instance_f32 S = {numTaps, State, Coeffs}; arm_fir_decimate_instance_f32 filterStruct_50k[4] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[0], DECIMATION_FACTOR, numTaps, Coeffs, States_50k[1], DECIMATION_FACTOR, numTaps, Coeffs, States_50k[2], DECIMATION_FACTOR, numTaps, Coeffs, States_50k[3]}; arm_fir_decimate_instance_f32 filterStruct_6250[8] = {DECIMATION_FACTOR, numTaps, Coeffs, States_6250[0], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[1], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[2], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[3], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[4], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[5], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[6], DECIMATION_FACTOR, numTaps, Coeffs, States_6250[7]}; arm_fir_decimate_instance_f32 filterStruct_781[8] = {DECIMATION_FACTOR, numTaps, Coeffs, States_781[0], DECIMATION_FACTOR, numTaps, Coeffs, States_781[1], DECIMATION_FACTOR, numTaps, Coeffs, States_781[2], DECIMATION_FACTOR, numTaps, Coeffs, States_781[3], DECIMATION_FACTOR, numTaps, Coeffs, States_781[4], DECIMATION_FACTOR, numTaps, Coeffs, States_781[5], DECIMATION_FACTOR, numTaps, Coeffs, States_781[6], DECIMATION_FACTOR, numTaps, Coeffs, States_781[7]}; //filterStruct_50k[0] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[0]}; //filterStruct_50k[1] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[1]}; //filterStruct_50k[2] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[2]}; //filterStruct_50k[3] = {DECIMATION_FACTOR, numTaps, Coeffs, States_50k[3]}; float filter_input_array[BUFFER_LENGTH] = {0}; float filter_output_array[BUFFER_LENGTH] = {0}; */ #define pre_compute_length 500 #define demodulation_length 125 #define CarrierFrequency 200 #define SAMPLEFREQUENCY 100000 //float i_mod_pre[demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR)]; //float q_mod_pre[demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR)]; uint16_t out_val_pre[pre_compute_length]; //used to write values to the dac float twopi = 3.14159265359 * 2; void pre_compute_tables() { // This function will precompute the cos and sin tables used in the rest of the program for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){ out_val_pre[precompute_counter] = (int) (cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter) * 2046.0 + 2048.0);//12 bit cos wave } //float decimated_frequency = 6250; //for(int precompute_counter = 0; precompute_counter < demodulation_length+(INPUT_ARRAY_SIZE/DECIMATION_FACTOR); precompute_counter++){ // i_mod_pre[precompute_counter] = (cos(twopi * CarrierFrequency / decimated_frequency * precompute_counter)); // q_mod_pre[precompute_counter] = (-sin(twopi * CarrierFrequency / decimated_frequency * precompute_counter)); } int main() { pre_compute_tables(); //turn off all LEDs led_blue = 1; led_green = 1; led_red = 1; int DAC_COUNTER = 0; pc.baud(230400); pc.printf("Starting\r\n"); 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}; 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}; for(int i = 0; i < 8; i++) input_50k[i] = new float[32];//each array represents the input of the adcs filters f3 = filters(8, 8, NULL, 8, 20, Coeffs2,false); //6250->781 filters f2 = filters(8, 8, &f3, 8, 20, Coeffs,false); //50,000->6250 filters f1 = filters(4, 8, &f2, 32, 20, Coeffs,true); //float output_print_buffer[PRINT_BUFFER_LENGTH];//used to store the adc0 values(current measurment) //float output_print_buffer2[PRINT_BUFFER_LENGTH];//used to store the adc0 values(voltage measurment) int print_buffer_count = 0; pdb_init(); // Initalize PDB dac_init(); // initializes DAC adc_init(); // always initialize adc before dma pc.printf("ADC Initialized\r\n"); dma_init(); // initializes DMAs dma_reset(); // This clears any DMA triggers that may have gotten things into a different state pc.printf("Buffer Size: %i\r\n", len); led_green = 1; pc.printf("\r\n\r\n\r\n"); pdb_start(); //while(print_buffer_count<PRINT_BUFFER_LENGTH) while(!GATHER_STATISTICS||print_buffer_count<PRINT_BUFFER_LENGTH) { while(sampling_status == 0)//wait until the ADCs read a new value { status_0 = 1; } sampling_status = 0;//sets sampling status to 0. DMA sets it to one once ADCs sample status_0 = 0;//Tied to D0. use an O-scope to measure how much free time there is to play with. status_1 = 1;//Tied to D1. use an O-scope to measure how much time the processing takes for(int i = 0; i < len; i+=2) { static_output_array0[i] = out_val_pre[DAC_COUNTER];//DAC output DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter static_output_array0[i+1] = out_val_pre[DAC_COUNTER];//DAC output DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter input_50k[1][i/2]=static_input_array1[i]; input_50k[3][i/2]=static_input_array1[i+1]; input_50k[0][i/2]=static_input_array0[i]; input_50k[2][i/2]=static_input_array0[i+1]; }//End of for loop going throught the buffer of adc samples f1.input(input_50k,32); status_1 = 0;//turn off D1 used in deterimining how long processing is taking //filter_input_array[i] = (float) (((int)static_input_array0[i]) - 0x8000); //arm_fir_f32(&S, filter_input_array, filter_output_array, len); }//end of while loop //for(int i = 0; i<PRINT_BUFFER_LENGTH; i++)//print all the adc values measured //{ // printf("%.1f %.1f\n\r",output_print_buffer[i],output_print_buffer2[i]); //} }