Impedance Fast Circuitry Software

Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Committer:
baxterja
Date:
Wed Jun 27 19:23:32 2018 +0000
Revision:
87:80c9169acb85
Parent:
86:b3520f4453cf
Changed frequencies for different probe number.  I also ordered frequencies from smallest to largest (I don't know if this is a good idea or not)

Who changed what in which revision?

UserRevisionLine numberNew 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"
baxterja 75:8bb94685c80b 8 #include "Jareds_DSP/demodulate.h"
bmazzeo 67:ec0c58490ce6 9 // DSP
bmazzeo 67:ec0c58490ce6 10 #include "dsp.h"
baxterja 76:704fc58ffcd0 11 #include "print_data.h"
bmazzeo 67:ec0c58490ce6 12
baxterja 87:80c9169acb85 13 #define PROBE_NUMBER 6
baxterja 87:80c9169acb85 14
baxterja 87:80c9169acb85 15 #if PROBE_NUMBER == 1
baxterja 87:80c9169acb85 16 #define PRE_COMPUTE_LENGTH 528
baxterja 87:80c9169acb85 17 #elif PROBE_NUMBER == 2
baxterja 87:80c9169acb85 18 #define PRE_COMPUTE_LENGTH 368
baxterja 87:80c9169acb85 19 #elif PROBE_NUMBER == 3
baxterja 87:80c9169acb85 20 #define PRE_COMPUTE_LENGTH 288
baxterja 87:80c9169acb85 21 #elif PROBE_NUMBER == 4
baxterja 87:80c9169acb85 22 #define PRE_COMPUTE_LENGTH 232
baxterja 87:80c9169acb85 23 #elif PROBE_NUMBER == 5
baxterja 87:80c9169acb85 24 #define PRE_COMPUTE_LENGTH 200
baxterja 87:80c9169acb85 25 #elif PROBE_NUMBER == 6
baxterja 87:80c9169acb85 26 #define PRE_COMPUTE_LENGTH 176
baxterja 87:80c9169acb85 27 #endif
baxterja 87:80c9169acb85 28
baxterja 87:80c9169acb85 29 /*
baxterja 85:f1be018aacac 30 #if PROBE_NUMBER == 1
baxterja 85:f1be018aacac 31 #define PRE_COMPUTE_LENGTH 232
baxterja 85:f1be018aacac 32 #elif PROBE_NUMBER == 2
baxterja 85:f1be018aacac 33 #define PRE_COMPUTE_LENGTH 528
baxterja 85:f1be018aacac 34 #elif PROBE_NUMBER == 3
baxterja 85:f1be018aacac 35 #define PRE_COMPUTE_LENGTH 176
baxterja 85:f1be018aacac 36 #elif PROBE_NUMBER == 4
baxterja 85:f1be018aacac 37 #define PRE_COMPUTE_LENGTH 320
baxterja 85:f1be018aacac 38 #elif PROBE_NUMBER == 5
baxterja 85:f1be018aacac 39 #define PRE_COMPUTE_LENGTH 208
baxterja 85:f1be018aacac 40 #elif PROBE_NUMBER == 6
baxterja 85:f1be018aacac 41 #define PRE_COMPUTE_LENGTH 528
baxterja 85:f1be018aacac 42 #endif
baxterja 87:80c9169acb85 43 6/27/18 switched the frequency and final LPF
baxterja 87:80c9169acb85 44 LPF now has a pass band of 40 instead of 60 and a stop band of 70 instead of 120.
baxterja 87:80c9169acb85 45 This allows for more frequency in the desired range.
baxterja 85:f1be018aacac 46 //190Hz 528
baxterja 85:f1be018aacac 47 //312Hz 320
baxterja 85:f1be018aacac 48 //431Hz 232
baxterja 85:f1be018aacac 49 //481Hz 208
baxterja 85:f1be018aacac 50 //568Hz 176
baxterja 87:80c9169acb85 51 */
baxterja 87:80c9169acb85 52 //#define probe_number 1 //Probe number
baxterja 87:80c9169acb85 53 const int probe_number = PROBE_NUMBER;
baxterja 87:80c9169acb85 54 //190Hz 528
baxterja 87:80c9169acb85 55 //272Hz 368
baxterja 87:80c9169acb85 56 //347Hz 288
baxterja 87:80c9169acb85 57 //431Hz 232
baxterja 87:80c9169acb85 58 //500Hz 200
baxterja 87:80c9169acb85 59 //568Hz 176
baxterja 85:f1be018aacac 60
baxterja 72:0b554f5026b9 61 #define PRINT_BUFFER_LENGTH 10000
baxterja 73:b059b5bdc664 62 #define GATHER_STATISTICS 1
bmazzeo 67:ec0c58490ce6 63
baxterja 85:f1be018aacac 64 Serial pc(USBTX, USBRX);
baxterja 74:ebc9f09fda11 65
timmey9 22:523e316cbe70 66 // for debug purposes
timmey9 18:b17ddeeb1c09 67 DigitalOut led_red(LED_RED);
timmey9 18:b17ddeeb1c09 68 DigitalOut led_green(LED_GREEN);
timmey9 18:b17ddeeb1c09 69 DigitalOut led_blue(LED_BLUE);
bmazzeo 58:4bee89daccff 70 DigitalOut status_0(D0);
bmazzeo 58:4bee89daccff 71 DigitalOut status_1(D1);
baxterja 77:1ee17a9e9f8b 72 DigitalOut status_3(D3);
baxterja 85:f1be018aacac 73 DigitalOut Start_sync(D4);// Currently not being used.
baxterja 85:f1be018aacac 74 DigitalIn sw2(SW2);//Button 2 Currently not being used
baxterja 85:f1be018aacac 75 DigitalIn sw3(SW3);//Button 3 Currently not being used
timmey9 51:43143a3fc2d7 76
baxterja 86:b3520f4453cf 77
baxterja 86:b3520f4453cf 78 Timer timer;
baxterja 86:b3520f4453cf 79
timmey9 52:5a40cc58c4c2 80 // defined in dma.cpp
timmey9 45:d591d138cdeb 81 extern int len;
baxterja 73:b059b5bdc664 82 extern uint16_t static_input_array0[];//ADC 0(swaps between a0 and a1. Used to measure current)
baxterja 73:b059b5bdc664 83 extern uint16_t static_input_array1[];//ADC 1(measures the voltage between the probe and ground)
baxterja 73:b059b5bdc664 84 extern uint16_t static_output_array0[];//DAC outputs whatever wave form we want.
baxterja 73:b059b5bdc664 85 extern uint16_t sampling_status;//used to determine when adc's are done reading.
bmazzeo 57:7b8c49e1c1f6 86
baxterja 85:f1be018aacac 87 //#define INPUT_ARRAY_SIZE 32
baxterja 85:f1be018aacac 88 //#define DECIMATION_FACTOR 8
baxterja 85:f1be018aacac 89 //#define DEMODULATED_SIGNAL_LENGTH 64
baxterja 73:b059b5bdc664 90
baxterja 73:b059b5bdc664 91 float *input_50k[8];
baxterja 73:b059b5bdc664 92
baxterja 85:f1be018aacac 93
baxterja 85:f1be018aacac 94
baxterja 73:b059b5bdc664 95
baxterja 73:b059b5bdc664 96
baxterja 84:5b4466dd2326 97 //190Hz 528
baxterja 84:5b4466dd2326 98 //312Hz 320
baxterja 84:5b4466dd2326 99 //431Hz 232
baxterja 84:5b4466dd2326 100 //481Hz 208
baxterja 84:5b4466dd2326 101 //568Hz 176
baxterja 85:f1be018aacac 102 //#define pre_compute_length 208
baxterja 85:f1be018aacac 103 //#define CarrierFrequency (100000/pre_compute_length)
baxterja 85:f1be018aacac 104 const int pre_compute_length = PRE_COMPUTE_LENGTH;
baxterja 85:f1be018aacac 105 const float CarrierFrequency = (100000/pre_compute_length);
baxterja 69:014d4bbd4e03 106 #define SAMPLEFREQUENCY 100000
baxterja 85:f1be018aacac 107
baxterja 73:b059b5bdc664 108 uint16_t out_val_pre[pre_compute_length]; //used to write values to the dac
baxterja 69:014d4bbd4e03 109 float twopi = 3.14159265359 * 2;
baxterja 69:014d4bbd4e03 110
baxterja 69:014d4bbd4e03 111
baxterja 69:014d4bbd4e03 112 void pre_compute_tables() {
baxterja 69:014d4bbd4e03 113 // This function will precompute the cos and sin tables used in the rest of the program
baxterja 69:014d4bbd4e03 114 for(int precompute_counter = 0; precompute_counter < pre_compute_length; precompute_counter++){
baxterja 84:5b4466dd2326 115 out_val_pre[precompute_counter] = (int) ((cos(twopi * CarrierFrequency /SAMPLEFREQUENCY * precompute_counter)) * 2046.0 + 2048.0);//12 bit cos wave
baxterja 69:014d4bbd4e03 116 }
baxterja 73:b059b5bdc664 117
baxterja 73:b059b5bdc664 118
baxterja 69:014d4bbd4e03 119 }
baxterja 69:014d4bbd4e03 120
baxterja 85:f1be018aacac 121
emilmont 7:65188f4a8c25 122 int main() {
baxterja 79:5f24cfd685d8 123
timmey9 22:523e316cbe70 124 led_blue = 1;
timmey9 35:df40c4566826 125 led_green = 1;
timmey9 18:b17ddeeb1c09 126 led_red = 1;
timmey9 34:44cc9b76a507 127
baxterja 79:5f24cfd685d8 128 pre_compute_tables();
baxterja 79:5f24cfd685d8 129 precompute_tables();
baxterja 79:5f24cfd685d8 130 //turn off all LEDs
baxterja 73:b059b5bdc664 131
baxterja 84:5b4466dd2326 132 //slight change.
baxterja 79:5f24cfd685d8 133
baxterja 79:5f24cfd685d8 134
baxterja 73:b059b5bdc664 135
baxterja 73:b059b5bdc664 136 pc.baud(230400);
baxterja 85:f1be018aacac 137 /*
baxterja 79:5f24cfd685d8 138 //pc.printf("High: %x Mid: %x Low: %x",SIM->UIDH,SIM->UIDML,SIM->UIDL);
baxterja 84:5b4466dd2326 139 if(SIM->UIDH == 0x2effff && SIM->UIDML == 0x4e453154 && SIM->UIDL == 0x1004001e) probe_number = 6;
baxterja 80:7a4856c596fc 140 else if(SIM->UIDH == 0x1cffff && SIM->UIDML == 0x4e453215 && SIM->UIDL == 0x700b0003) probe_number = 2;
baxterja 80:7a4856c596fc 141 else if(SIM->UIDH == 0x2effff && SIM->UIDML == 0x4e453215 && SIM->UIDL == 0x700b0021) probe_number = 3;
baxterja 80:7a4856c596fc 142 else if(SIM->UIDH == 0x14ffff && SIM->UIDML == 0x4e453154 && SIM->UIDL == 0x5009002b) probe_number = 4;
baxterja 84:5b4466dd2326 143 else if(SIM->UIDH == 0x12ffff && SIM->UIDML == 0x4e453103 && SIM->UIDL == 0x60010030) probe_number = 1;
baxterja 84:5b4466dd2326 144 else if(SIM->UIDH == 0x10ffff && SIM->UIDML == 0x4e453105 && SIM->UIDL == 0x100b0031) probe_number = 5;
baxterja 80:7a4856c596fc 145 else if(SIM->UIDH == 0x19ffff && SIM->UIDML == 0x4d441504 && SIM->UIDL == 0x90110031) probe_number = 7;//this FRDM board had a bad usb connection
baxterja 80:7a4856c596fc 146 else
baxterja 80:7a4856c596fc 147 {
baxterja 80:7a4856c596fc 148 pc.printf("FRDM BOARD NOT RECOGNIZED. Here is the new ID:\n\r");
baxterja 80:7a4856c596fc 149 pc.printf("High: %x Mid: %x Low: %x\n\r",SIM->UIDH,SIM->UIDML,SIM->UIDL);
baxterja 80:7a4856c596fc 150 probe_number = 6;
baxterja 80:7a4856c596fc 151 }
baxterja 85:f1be018aacac 152 */
baxterja 80:7a4856c596fc 153 pc.printf("PROBE #%d",probe_number);
baxterja 80:7a4856c596fc 154 wait(1.0f);
baxterja 85:f1be018aacac 155
baxterja 79:5f24cfd685d8 156 int DAC_COUNTER = 0;
baxterja 85:f1be018aacac 157
baxterja 78:10b2916b8f5c 158
baxterja 85:f1be018aacac 159 //Filters used in program. Coefficients generated using matlab FDA tool
baxterja 78:10b2916b8f5c 160 //Fs = 100000, FPass = 1500, FStop = 4500, -40db
baxterja 78:10b2916b8f5c 161 float Coeffs_100k[64] = {0.00368762746013400, -0.00390835182721185, -0.00352466159836192, -0.00378044968164769, -0.00434746630849834, -0.00502920744118478, -0.00570543677799210, -0.00628464650365093, -0.00668691824262464, -0.00683917139561193, -0.00667505410229485, -0.00613341508706847, -0.00516232410283893, -0.00371868951723358, -0.00177417571286934, 0.000685809876396031, 0.00365648516386919, 0.00711658039739992, 0.0110234409358280, 0.0153179248880130, 0.0199204447074274, 0.0247385052308702, 0.0296636617979935, 0.0345813441342327, 0.0393672306749833, 0.0438989152744108, 0.0480511039644129, 0.0517108728403463, 0.0547710075854812, 0.0571509837193129, 0.0587848911618005, 0.0596038010741317, 0.0596038010741317, 0.0587848911618005, 0.0571509837193129, 0.0547710075854812, 0.0517108728403463, 0.0480511039644129, 0.0438989152744108, 0.0393672306749833, 0.0345813441342327, 0.0296636617979935, 0.0247385052308702, 0.0199204447074274, 0.0153179248880130, 0.0110234409358280, 0.00711658039739992, 0.00365648516386919, 0.000685809876396031, -0.00177417571286934, -0.00371868951723358, -0.00516232410283893, -0.00613341508706847, -0.00667505410229485, -0.00683917139561193, -0.00668691824262464, -0.00628464650365093, -0.00570543677799210, -0.00502920744118478, -0.00434746630849834, -0.00378044968164769, -0.00352466159836192, -0.00390835182721185, 0.00368762746013400};
baxterja 78:10b2916b8f5c 162 //FS = 12500, Fpass = 100 Fstop 595 -50 db, picks on 1KHz signal
baxterja 78:10b2916b8f5c 163 float Coeffs_12500[64] = {-0.00222124676855946, -0.00163176345559825, -0.00214372257722720, -0.00268653504526241, -0.00323192624665395, -0.00374368158669702, -0.00417907979321931, -0.00449328006488225, -0.00463658803501384, -0.00455550760053878, -0.00419848205106894, -0.00352024663122325, -0.00247317556122392, -0.00102679426345247, 0.000845020041268567, 0.00315252023354445, 0.00589238992243332, 0.00904676286706929, 0.0125796442526745, 0.0164391331165775, 0.0205582919104753, 0.0248562683452381, 0.0292399202971496, 0.0336089813533200, 0.0378556202824368, 0.0418719268554270, 0.0455508704073759, 0.0487915537931733, 0.0515035460002020, 0.0536094934833149, 0.0550483048257919, 0.0557785148825623, 0.0557785148825623, 0.0550483048257919, 0.0536094934833149, 0.0515035460002020, 0.0487915537931733, 0.0455508704073759, 0.0418719268554270, 0.0378556202824368, 0.0336089813533200, 0.0292399202971496, 0.0248562683452381, 0.0205582919104753, 0.0164391331165775, 0.0125796442526745, 0.00904676286706929, 0.00589238992243332, 0.00315252023354445, 0.000845020041268567, -0.00102679426345247, -0.00247317556122392, -0.00352024663122325, -0.00419848205106894, -0.00455550760053878, -0.00463658803501384, -0.00449328006488225, -0.00417907979321931, -0.00374368158669702, -0.00323192624665395, -0.00268653504526241, -0.00214372257722720, -0.00163176345559825, -0.00222124676855946};
baxterja 78:10b2916b8f5c 164 //Fs = 1562.5 Fpass = 100, FStop = 195.6, null at 200 Hz
baxterja 78:10b2916b8f5c 165 float Coeffs_1563[32] = {-0.00296990567211633, 0.00784631357544163, 0.00955642274001412, 0.0105078670942049, 0.00746432577935901, -0.000715888274492355, -0.0127656342089486, -0.0248578255670040, -0.0313597093257934, -0.0264804428976321, -0.00639056527392095, 0.0289495639622991, 0.0749846381319702, 0.123239880157668, 0.163403327438059, 0.186215627611441, 0.186215627611441, 0.163403327438059, 0.123239880157668, 0.0749846381319702, 0.0289495639622991, -0.00639056527392095, -0.0264804428976321, -0.0313597093257934, -0.0248578255670040, -0.0127656342089486, -0.000715888274492355, 0.00746432577935901, 0.0105078670942049, 0.00955642274001412, 0.00784631357544163, -0.00296990567211633};
baxterja 78:10b2916b8f5c 166 //Fs = 781.25, Fpass = 60, Fstop = 120, -50 Db
baxterja 87:80c9169acb85 167 //float Coeffs_782[32] = {-0.00423132648651131, -0.00413085595827858, -0.00212955364173648, 0.00345345707266939, 0.0105870277845968, 0.0147191914630408, 0.0108878516242339, -0.00283458314983071, -0.0224398052018669, -0.0377493805765767, -0.0358474685211045, -0.00721456868509791, 0.0483564412276129, 0.119338204671425, 0.185468453617916, 0.225354002914126, 0.225354002914126, 0.185468453617916, 0.119338204671425, 0.0483564412276129, -0.00721456868509791, -0.0358474685211045, -0.0377493805765767, -0.0224398052018669, -0.00283458314983071, 0.0108878516242339, 0.0147191914630408, 0.0105870277845968, 0.00345345707266939, -0.00212955364173648, -0.00413085595827858, -0.00423132648651131};
baxterja 87:80c9169acb85 168 //Fs = 781.25, Fpass = 40, Fstop = 70, -80 Db Wstop = 1000
baxterja 87:80c9169acb85 169 float Coeffs_782[64] = {9.75137900147270e-06, 0.000242116460111013, 0.000587759650222984, 0.00121948229641138, 0.00218692996477935, 0.00352877877192510, 0.00522776253657456, 0.00719094075913147, 0.00923733657506023, 0.0110994562722364, 0.0124427726825193, 0.0129046836285611, 0.0121508175170645, 0.00994240690006920, 0.00620512977988183, 0.00108717079481820, -0.00500601108796552, -0.0114117467388055, -0.0172511672001640, -0.0215122218794239, -0.0231679949423431, -0.0213174230933399, -0.0153286015860647, -0.00496376773300694, 0.00953605359350501, 0.0274232655747699, 0.0474831945783405, 0.0681394765484662, 0.0876167028664372, 0.104139083136827, 0.116139722609181, 0.122451402975443, 0.122451402975443, 0.116139722609181, 0.104139083136827, 0.0876167028664372, 0.0681394765484662, 0.0474831945783405, 0.0274232655747699, 0.00953605359350501, -0.00496376773300694, -0.0153286015860647, -0.0213174230933399, -0.0231679949423431, -0.0215122218794239, -0.0172511672001640, -0.0114117467388055, -0.00500601108796552, 0.00108717079481820, 0.00620512977988183, 0.00994240690006920, 0.0121508175170645, 0.0129046836285611, 0.0124427726825193, 0.0110994562722364, 0.00923733657506023, 0.00719094075913147, 0.00522776253657456, 0.00352877877192510, 0.00218692996477935, 0.00121948229641138, 0.000587759650222984, 0.000242116460111013, 9.75137900147270e-06};
baxterja 76:704fc58ffcd0 170
baxterja 87:80c9169acb85 171 //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 87:80c9169acb85 172 //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 173
baxterja 85:f1be018aacac 174 //note I allocate space for 4 adc inputs, but usually I only use 2
baxterja 73:b059b5bdc664 175 for(int i = 0; i < 8; i++)
baxterja 77:1ee17a9e9f8b 176 input_50k[i] = new float[64];//each array represents the input of the adcs
baxterja 78:10b2916b8f5c 177
baxterja 85:f1be018aacac 178 //filters(const int num_filters, const int DECIMATION, filters * f, const int block_size, const int numTaps, float coeffs[], int demodulate_after)
baxterja 85:f1be018aacac 179 //num filters: usually 4. In-phase and quadrature phase for both voltage and current
baxterja 85:f1be018aacac 180 //DECIMATION: Decimation Factor
baxterja 85:f1be018aacac 181 //f: the filter after current filter.
baxterja 85:f1be018aacac 182 //block_size: block size of each filter command should be an integer multiple of DECIMATION
baxterja 85:f1be018aacac 183 //Flag noting rather or not to demodulate, and if so what frequency to demodulate at
baxterja 87:80c9169acb85 184 filters f4 = filters(4, 8, NULL, 8, 64, Coeffs_782,DEMOD_No_Demod);
baxterja 78:10b2916b8f5c 185 filters f3 = filters(4, 2, &f4, 4, 32, Coeffs_1563,DEMOD_No_Demod);
baxterja 78:10b2916b8f5c 186 filters f2 = filters(4, 8, &f3, 8, 64, Coeffs_12500,DEMOD_No_Demod);
baxterja 78:10b2916b8f5c 187 filters f1 = filters(2, 8, &f2, 64, 64, Coeffs_100k,DEMOD_200HZ);
baxterja 73:b059b5bdc664 188
baxterja 84:5b4466dd2326 189 /*
baxterja 78:10b2916b8f5c 190 filters f4_b = filters(4, 8, NULL, 8, 32, Coeffs_782,DEMOD_No_Demod);
baxterja 78:10b2916b8f5c 191 filters f3_b = filters(4, 2, &f4_b, 4, 32, Coeffs_1563,DEMOD_No_Demod);
baxterja 78:10b2916b8f5c 192 filters f2_b = filters(4, 8, &f3_b, 8, 64, Coeffs_12500,DEMOD_No_Demod);
baxterja 78:10b2916b8f5c 193 filters f1_b = filters(2, 8, &f2_b, 64, 64, Coeffs_100k,DEMOD_1000HZ);
baxterja 84:5b4466dd2326 194 */
baxterja 78:10b2916b8f5c 195 //filters f_pre = filters(2, 2, &f1, 64, 64, Coeffs_50k,false);
baxterja 69:014d4bbd4e03 196
baxterja 73:b059b5bdc664 197 //float output_print_buffer[PRINT_BUFFER_LENGTH];//used to store the adc0 values(current measurment)
baxterja 73:b059b5bdc664 198 //float output_print_buffer2[PRINT_BUFFER_LENGTH];//used to store the adc0 values(voltage measurment)
baxterja 70:8cd7a8a2c153 199 int print_buffer_count = 0;
baxterja 70:8cd7a8a2c153 200
bmazzeo 66:72c5c24e532c 201 pdb_init(); // Initalize PDB
bmazzeo 66:72c5c24e532c 202 dac_init(); // initializes DAC
timmey9 45:d591d138cdeb 203 adc_init(); // always initialize adc before dma
bmazzeo 53:83a90a47c1fd 204 pc.printf("ADC Initialized\r\n");
baxterja 74:ebc9f09fda11 205 dma_init2(); // initializes DMAs
bmazzeo 55:2526b3317bc8 206 dma_reset(); // This clears any DMA triggers that may have gotten things into a different state
bmazzeo 67:ec0c58490ce6 207 pc.printf("Buffer Size: %i\r\n", len);
bmazzeo 54:1697dc574b96 208
timmey9 46:a015ebf4663b 209 led_green = 1;
baxterja 79:5f24cfd685d8 210 pc.printf("High: %x Mid: %x Low: %x",SIM->UIDH,SIM->UIDML,SIM->UIDL);
timmey9 40:bd6d8c35e822 211 pc.printf("\r\n\r\n\r\n");
timmey9 37:8bdc71f3e874 212
bmazzeo 64:bb4a4bd57681 213 pdb_start();
baxterja 84:5b4466dd2326 214 bool is_actively_printing = false;
baxterja 84:5b4466dd2326 215 char read_in_character = 'a';
baxterja 86:b3520f4453cf 216 //while(print_buffer_count<PRINT_BUFFER_LENGTH)
baxterja 86:b3520f4453cf 217 timer.start();
baxterja 73:b059b5bdc664 218 while(!GATHER_STATISTICS||print_buffer_count<PRINT_BUFFER_LENGTH)
baxterja 73:b059b5bdc664 219 {
baxterja 79:5f24cfd685d8 220 if(pc.readable())//if the python code has sent any characters recently
baxterja 79:5f24cfd685d8 221 {
baxterja 85:f1be018aacac 222 while(pc.readable())//if there is a Keyboard input
baxterja 84:5b4466dd2326 223 {
baxterja 87:80c9169acb85 224 read_in_character = pc.getc();
baxterja 85:f1be018aacac 225 if (read_in_character == 'R')//start printing
baxterja 86:b3520f4453cf 226 {
baxterja 86:b3520f4453cf 227 timer.reset();
baxterja 86:b3520f4453cf 228 //timer.start();
baxterja 84:5b4466dd2326 229 is_actively_printing = true;
baxterja 86:b3520f4453cf 230 }
baxterja 85:f1be018aacac 231 else if (read_in_character == 'S')//Stop printing
baxterja 86:b3520f4453cf 232 {
baxterja 86:b3520f4453cf 233 //timer.stop();
baxterja 84:5b4466dd2326 234 is_actively_printing = false;
baxterja 86:b3520f4453cf 235 }
baxterja 85:f1be018aacac 236 else if (read_in_character == 'P')//return probe number
baxterja 84:5b4466dd2326 237 pc.printf("p%d",probe_number);
baxterja 86:b3520f4453cf 238 else if (read_in_character == 'I')//return probe number
baxterja 86:b3520f4453cf 239 pc.printf("IMPEDANCE");
baxterja 84:5b4466dd2326 240 }
baxterja 84:5b4466dd2326 241
baxterja 79:5f24cfd685d8 242 }
baxterja 79:5f24cfd685d8 243
baxterja 85:f1be018aacac 244 while(sampling_status == 0)//wait until the ADCs read a new values
baxterja 73:b059b5bdc664 245 {
baxterja 85:f1be018aacac 246 status_0 = 1; //used for debugging allows to see utilization
baxterja 85:f1be018aacac 247 print_filter_data(&pc,is_actively_printing);//while waiting for new samples print as much data as possible
baxterja 75:8bb94685c80b 248 //Thread::wait(.0001);
baxterja 73:b059b5bdc664 249 }
baxterja 73:b059b5bdc664 250 sampling_status = 0;//sets sampling status to 0. DMA sets it to one once ADCs sample
baxterja 73:b059b5bdc664 251 status_0 = 0;//Tied to D0. use an O-scope to measure how much free time there is to play with.
baxterja 73:b059b5bdc664 252 status_1 = 1;//Tied to D1. use an O-scope to measure how much time the processing takes
baxterja 73:b059b5bdc664 253
baxterja 77:1ee17a9e9f8b 254 for(int i = 0; i < len; i++)
bmazzeo 61:a56cca07d4a6 255 {
baxterja 73:b059b5bdc664 256 static_output_array0[i] = out_val_pre[DAC_COUNTER];//DAC output
baxterja 73:b059b5bdc664 257 DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table
baxterja 73:b059b5bdc664 258 if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter
baxterja 77:1ee17a9e9f8b 259 //static_output_array0[i+1] = out_val_pre[DAC_COUNTER];//DAC output
baxterja 77:1ee17a9e9f8b 260 //DAC_COUNTER++;//Counter to kepp track of where we are in our precomputed table
baxterja 77:1ee17a9e9f8b 261 //if (DAC_COUNTER>=pre_compute_length) {DAC_COUNTER = 0;}//wrap around the counter
baxterja 73:b059b5bdc664 262
baxterja 85:f1be018aacac 263 //Note currently we are sampling 2 adc at 100 KHz. If desired you can also sample 4 ADCs at 50 KHz
baxterja 77:1ee17a9e9f8b 264 input_50k[1][i]=float(static_input_array1[i]);
baxterja 77:1ee17a9e9f8b 265 // input_50k[3][i/2]=static_input_array1[i+1];
baxterja 77:1ee17a9e9f8b 266 input_50k[0][i]=float(static_input_array0[i]);
baxterja 77:1ee17a9e9f8b 267 //input_50k[2][i/2]=static_input_array0[i+1];
baxterja 73:b059b5bdc664 268
baxterja 73:b059b5bdc664 269 }//End of for loop going throught the buffer of adc samples
baxterja 85:f1be018aacac 270
baxterja 85:f1be018aacac 271
baxterja 78:10b2916b8f5c 272 f1.input(input_50k,64,DEMOD_200HZ);
baxterja 84:5b4466dd2326 273 //f1_b.input(input_50k,64,DEMOD_1000HZ);
baxterja 77:1ee17a9e9f8b 274 //status_3 = 1;
baxterja 77:1ee17a9e9f8b 275 //pc.printf("Y");
baxterja 77:1ee17a9e9f8b 276 //status_3 = 0;
baxterja 73:b059b5bdc664 277
baxterja 70:8cd7a8a2c153 278
baxterja 73:b059b5bdc664 279 status_1 = 0;//turn off D1 used in deterimining how long processing is taking
baxterja 85:f1be018aacac 280
baxterja 73:b059b5bdc664 281 }//end of while loop
baxterja 73:b059b5bdc664 282
baxterja 85:f1be018aacac 283 //prints ADC buffer if desired
baxterja 85:f1be018aacac 284
baxterja 73:b059b5bdc664 285 //for(int i = 0; i<PRINT_BUFFER_LENGTH; i++)//print all the adc values measured
baxterja 73:b059b5bdc664 286 //{
baxterja 73:b059b5bdc664 287 // printf("%.1f %.1f\n\r",output_print_buffer[i],output_print_buffer2[i]);
baxterja 73:b059b5bdc664 288 //}
baxterja 73:b059b5bdc664 289
baxterja 70:8cd7a8a2c153 290
bmazzeo 61:a56cca07d4a6 291 }
bmazzeo 63:7903a33e2fd4 292