Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Chemical_Sensor_DMA by
Revision 4:9fd291254686, committed 2015-11-06
- Comitter:
- baxterja
- Date:
- Fri Nov 06 03:33:09 2015 +0000
- Parent:
- 3:a85b742be262
- Child:
- 5:1b2dc43e8947
- Commit message:
- This is version 1 of the working filter
Changed in this revision
--- a/Sample/adc.cpp Sat Oct 31 20:06:37 2015 +0000 +++ b/Sample/adc.cpp Fri Nov 06 03:33:09 2015 +0000 @@ -19,6 +19,27 @@ * time by the PDB. */ void adc_init() { + + //DAC stuff + //DAC0 clock enabled + + + + /* + * The DAC selects DACREF_1 as the reference voltage. + * The DAC hardware trigger is selected. + * The DAC soft trigger is not valid. + * High-Power mode + * The DAC buffer read pointer top flag interrupt is disabled. + * The DAC buffer read pointer bottom flag interrupt is disabled. + * + * DAC0_OUT is PTB1 by defualt + */ + + + + + // red, indicating not ready red = 0; green = 1;
--- a/Sample/dma.cpp Sat Oct 31 20:06:37 2015 +0000 +++ b/Sample/dma.cpp Fri Nov 06 03:33:09 2015 +0000 @@ -13,25 +13,39 @@ int len = TOTAL_SAMPLES; uint16_t sample_array0[TOTAL_SAMPLES]; uint16_t sample_array1[TOTAL_SAMPLES]; -uint16_t angle_array[TOTAL_SAMPLES+FILENAME_SIZE];//Change this to DAC Values +//uint16_t out_val_pre[TOTAL_SAMPLES];//Change this to DAC Values bool dma_done = false; bool dma_half_done = false; - +#define pre_compute_length 2000 +#define DMA_PERIOD .00001 +#define DMA_FREQUENCY 100000 +#define CARRIERFREQUENCY 10000 +#define twopi 3.14159265359 * 2 /* DMA0 and DMA1 are triggered by ADC0 and ADC1 (which are triggered * by the PDB). However, DMA2 is triggered directly by the PDB. This * is becuase DMA2 is reading FTM2, which cannot trigger the DMA. */ void dma_init() { +// for(int precompute_counter = 0; precompute_counter < TOTAL_SAMPLES; precompute_counter++){ +// out_val_pre[precompute_counter] = (int) (cos(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter) * 4965.0 + 49650.0); +// } + toggle_dma0 = 1; toggle_dma1 = 1; toggle_dma2 = 1; // Enable clock for DMAMUX and DMA + //SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; + //SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK; + + SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; SIM_SCGC6 |= SIM_SCGC6_FTM2_MASK; // make sure clock is enabled for FTM2 - + + + // Enable DMA channels and select MUX to the correct source (see page 95 of user manual DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(40); // ADC0 DMAMUX_CHCFG1 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(41); // ADC1 @@ -54,8 +68,8 @@ DMA_TCD0_DADDR = (uint32_t) sample_array0; DMA_TCD1_SADDR = (uint32_t) &ADC1_RA; DMA_TCD1_DADDR = (uint32_t) sample_array1; - DMA_TCD2_SADDR = (uint32_t) &FTM2_CNT; - DMA_TCD2_DADDR = (uint32_t) angle_array; + //DMA_TCD2_SADDR = (uint32_t) &out_val_pre[0];//&FTM2_CNT; + //DMA_TCD2_DADDR = (uint32_t) &DAC0_DAT0L; // Set an offset for source and destination address DMA_TCD0_SOFF = 0x00; // Source address offset of 2 bits per transaction @@ -64,26 +78,26 @@ DMA_TCD1_DOFF = 0x02; // Destination address offset of 1 bit per transaction //DAC DMA Chang soff to 2, and DOFF to 0 - DMA_TCD2_SOFF = 0x00; // Source address offset of 2 bits per transaction - DMA_TCD2_DOFF = 0x02; // Destination address offset of 1 bit per transaction + //DMA_TCD2_SOFF = 0x02; // Source address offset of 2 bits per transaction + //DMA_TCD2_DOFF = 0x00; // Destination address offset of 1 bit per transaction // Set source and destination data transfer size DMA_TCD0_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); DMA_TCD1_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); - DMA_TCD2_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); + //DMA_TCD2_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); // Number of bytes to be transfered in each service request of the channel DMA_TCD0_NBYTES_MLNO = 0x02; DMA_TCD1_NBYTES_MLNO = 0x02; - DMA_TCD2_NBYTES_MLNO = 0x02; + //DMA_TCD2_NBYTES_MLNO = 0x02; // Current major iteration count DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); DMA_TCD1_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); DMA_TCD1_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); - DMA_TCD2_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); - DMA_TCD2_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); + // DMA_TCD2_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); + //DMA_TCD2_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); // Adjustment value used to restore the source and destiny address to the initial value // After reading 'len' number of times, the DMA goes back to the beginning by subtracting len*2 from the address (going back to the original address) @@ -94,13 +108,13 @@ DMA_TCD1_DLASTSGA = -len*2; // Destination address adjustment //Change source and destination - DMA_TCD2_SLAST = 0; // Source address adjustment - DMA_TCD2_DLASTSGA = -len*2; // Destination address adjustment + //DMA_TCD2_SLAST = -len*2; // Source address adjustment + //DMA_TCD2_DLASTSGA = 0; // Destination address adjustment // Setup control and status register DMA_TCD0_CSR = 0; DMA_TCD1_CSR = 0; - DMA_TCD2_CSR = 0; + // DMA_TCD2_CSR = 0; // enable interrupt call at end of major loop DMA_TCD0_CSR |= DMA_CSR_INTMAJOR_MASK | DMA_CSR_INTHALF_MASK;
--- a/Sample/dma.h Sat Oct 31 20:06:37 2015 +0000 +++ b/Sample/dma.h Fri Nov 06 03:33:09 2015 +0000 @@ -4,10 +4,10 @@ #include "mbed.h" #define FILENAME_SIZE 26 -#define TOTAL_SAMPLES 18980 // a multiple of 1460 (tcp payload length within a packet) +#define TOTAL_SAMPLES 2000//18980 // a multiple of 1460 (tcp payload length within a packet) extern uint16_t sample_array0[TOTAL_SAMPLES]; extern uint16_t sample_array1[TOTAL_SAMPLES]; -extern uint16_t angle_array[TOTAL_SAMPLES+FILENAME_SIZE]; +extern uint16_t out_val_pre[TOTAL_SAMPLES]; extern bool dma_done; extern bool dma_half_done;
--- a/Sample/pdb.cpp Sat Oct 31 20:06:37 2015 +0000 +++ b/Sample/pdb.cpp Fri Nov 06 03:33:09 2015 +0000 @@ -44,6 +44,7 @@ #else PDB0_IDLY = 0; // need to trigger interrupt every counter reset which happens when modulus reached PDB0_MOD = 0x257; // period of timer set to 10us + PDB0_DACINT0 |= (0x257); PDB0_CH0DLY0 = 0; PDB0_CH0DLY1 = 0; PDB0_CH1DLY0 = 0; @@ -61,8 +62,10 @@ | PDB_SC_MULT(0) // Multiplication factor | PDB_SC_CONT_MASK // Contintuous, rather than one-shot, mode | PDB_SC_LDOK_MASK; // Need to ok the loading or it will not load certain regsiters! + //PDB0_DACINT0 |= (0x257); // we won't subdivide the clock... + PDB0_DACINTC0 |= 0x01; // enable the DAC interval trigger #endif - + //PDB0_DACINT0 |= (0x257); } void pdb_start() {
--- a/SignalProcessing.cpp Sat Oct 31 20:06:37 2015 +0000 +++ b/SignalProcessing.cpp Fri Nov 06 03:33:09 2015 +0000 @@ -10,15 +10,15 @@ float i_mod_pre[pre_compute_length]; float q_mod_pre[pre_compute_length]; -int out_val_pre[pre_compute_length]; +//uint16_t out_val_pre[pre_compute_length]; -float twopi = 3.14159265359 * 2; +#define 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 * DMA_PERIOD * precompute_counter) * 4965.0 + 49650.0); + //out_val_pre[precompute_counter] = (int) (cos(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter) * 4965.0 + 49650.0); i_mod_pre[precompute_counter] = (cos(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter)); q_mod_pre[precompute_counter] = (-sin(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter)); @@ -27,6 +27,234 @@ +/* +#define FIR_33_LENGTH 128 +float FIR33_Sample1_i[FIR_33_LENGTH]; +float FIR33_Sample1_q[FIR_33_LENGTH]; +float FIR33_Sample2_i[FIR_33_LENGTH]; +float FIR33_Sample2_q[FIR_33_LENGTH]; +uint8_t FIR33_Position = 0; +//Fs = 33, Order = 63, Fpass = 1, Fstop = 5 +static float lp_33_coeff[FIR_33_LENGTH] = { +0.00112064914891618, +0.00122265940667415, +0.00134320266606308, +0.00148248497505270, +0.00163752208873233, +0.00180917436717814, +0.00199487440500326, +0.00219591677377202, +0.00240942435359224, +0.00263715016478107, +0.00287620469595143, +0.00312896642110311, +0.00339115146671554, +0.00366703367280862, +0.00395013103364271, +0.00424959421024652, +0.00454749937640992, +0.00486587065943205, +0.00519015600428925, +0.00551644394778870, +0.00585208754960567, +0.00619545680874284, +0.00654599064376495, +0.00690046871786865, +0.00725861046415282, +0.00761931288503866, +0.00798229719629724, +0.00834687323478277, +0.00871239637379653, +0.00907848882392502, +0.00944349817409369, +0.00980681579778287, +0.0101666025128362, +0.0105235376400116, +0.0108740956379341, +0.0112200337619850, +0.0115576132398033, +0.0118892792678013, +0.0122101053096016, +0.0125220143316710, +0.0128250887436567, +0.0131145746752160, +0.0133925589239844, +0.0136577821847874, +0.0139097056313474, +0.0141461938484364, +0.0143672111207676, +0.0145723586994284, +0.0147612534972782, +0.0149330287990815, +0.0150868196634048, +0.0152228115376150, +0.0153400576626228, +0.0154384553803622, +0.0155165567546008, +0.0155766985839343, +0.0156161625679816, +0.0156360898266519, +0.0156360898266519, +0.0156161625679816, +0.0155766985839343, +0.0155165567546008, +0.0154384553803622, +0.0153400576626228, +0.0152228115376150, +0.0150868196634048, +0.0149330287990815, +0.0147612534972782, +0.0145723586994284, +0.0143672111207676, +0.0141461938484364, +0.0139097056313474, +0.0136577821847874, +0.0133925589239844, +0.0131145746752160, +0.0128250887436567, +0.0125220143316710, +0.0122101053096016, +0.0118892792678013, +0.0115576132398033, +0.0112200337619850, +0.0108740956379341, +0.0105235376400116, +0.0101666025128362, +0.00980681579778287, +0.00944349817409369, +0.00907848882392502, +0.00871239637379653, +0.00834687323478277, +0.00798229719629724, +0.00761931288503866, +0.00725861046415282, +0.00690046871786865, +0.00654599064376495, +0.00619545680874284, +0.00585208754960567, +0.00551644394778870, +0.00519015600428925, +0.00486587065943205, +0.00454749937640992, +0.00424959421024652, +0.00395013103364271, +0.00366703367280862, +0.00339115146671554, +0.00312896642110311, +0.00287620469595143, +0.00263715016478107, +0.00240942435359224, +0.00219591677377202, +0.00199487440500326, +0.00180917436717814, +0.00163752208873233, +0.00148248497505270, +0.00134320266606308, +0.00122265940667415, +0.00112064914891618, +0.00104032814636243, +0.000982028128151230, +0.000950473399969370, +0.000946711675637481, +0.000976800083269572, +-0.00572883284452795 +}; +#define NUMSAMPLESAVERAGE 33 +#define DecimationFactor_33 3 + +void filter33(float FIR33_Sample1_i_input, float FIR33_Sample1_q_input, float FIR33_Sample2_i_input, float FIR33_Sample2_q_input) +{ + //printf("f"); + static uint8_t finalAverageCounter = 0; + static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated + static float FIR33_Sample1_i_DecimatedSum=0; + static float FIR33_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value + static float FIR33_Sample2_i_DecimatedSum=0; + static float FIR33_Sample2_q_DecimatedSum=0; + + static float Final_Average1_i=0; + static float Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value + static float Final_Average2_i=0; + static float Final_Average2_q=0; + + FIR33_Sample1_i_DecimatedSum += FIR33_Sample1_i_input;//add sample to the sum of previous sample + FIR33_Sample1_q_DecimatedSum += FIR33_Sample1_q_input; + FIR33_Sample2_i_DecimatedSum += FIR33_Sample2_i_input; + FIR33_Sample2_q_DecimatedSum += FIR33_Sample2_q_input; + decimationCounter++; + if (decimationCounter >= DecimationFactor_33)//once 10 samples have com + { + decimationCounter = 0;//reset decimation counter + //add sample to 10K filter + FIR33_Sample1_i[FIR33_Position] = FIR33_Sample1_i_DecimatedSum; + FIR33_Sample1_q[FIR33_Position] = FIR33_Sample1_q_DecimatedSum; + FIR33_Sample2_i[FIR33_Position] = FIR33_Sample2_i_DecimatedSum; + FIR33_Sample2_q[FIR33_Position] = FIR33_Sample2_q_DecimatedSum; + + FIR33_Sample1_i_DecimatedSum = 0;//reset decimated sum to 0 for next sample + FIR33_Sample1_q_DecimatedSum = 0; + FIR33_Sample2_i_DecimatedSum = 0; + FIR33_Sample2_q_DecimatedSum = 0; + + FIR33_Position++; //increment circular buffer + if (FIR33_Position >= FIR_33_LENGTH) //wrap around + { + FIR33_Position = 0; + } + + // Low pass filter of demodulated signal + float FIR33_Sample1_i_Output = FIR33_Sample1_i[FIR33_Position] * lp_33_coeff[0];//first multiply of convolution + float FIR33_Sample1_q_Output = FIR33_Sample1_q[FIR33_Position] * lp_33_coeff[0]; + float FIR33_Sample2_i_Output = FIR33_Sample2_i[FIR33_Position] * lp_33_coeff[0]; + float FIR33_Sample2_q_Output = FIR33_Sample2_q[FIR33_Position] * lp_33_coeff[0]; + int fir_index; + for(int fir_counter = 1; fir_counter < FIR_33_LENGTH; fir_counter++)//the rest of the convolution + { + fir_index = FIR33_Position + fir_counter; + if (fir_index >= FIR_33_LENGTH) + { + fir_index -= FIR_33_LENGTH; + } + FIR33_Sample1_i_Output += FIR33_Sample1_i[fir_index] * lp_33_coeff[fir_counter];//convolving + FIR33_Sample1_q_Output += FIR33_Sample1_q[fir_index] * lp_33_coeff[fir_counter]; + FIR33_Sample2_i_Output += FIR33_Sample2_i[fir_index] * lp_33_coeff[fir_counter]; + FIR33_Sample2_q_Output += FIR33_Sample2_q[fir_index] * lp_33_coeff[fir_counter]; + } + + //float mag1 = sqrt(FIR33_Sample1_i_Output*FIR33_Sample1_i_Output+FIR33_Sample1_q_Output*FIR33_Sample1_q_Output); + //float mag2 = sqrt(FIR33_Sample2_i_Output*FIR33_Sample2_i_Output+FIR33_Sample2_q_Output*FIR33_Sample2_q_Output); + //printf("V1: %f\tV2: %f\n\r",mag1,mag2); + + + Final_Average1_i+=FIR33_Sample1_i_Output; + Final_Average1_q+=FIR33_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value + Final_Average2_i+=FIR33_Sample2_i_Output; + Final_Average2_q+=FIR33_Sample2_q_Output; + finalAverageCounter++; + if (finalAverageCounter>=NUMSAMPLESAVERAGE) + { + finalAverageCounter=0; + float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q); + float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q); + printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag2/mag1); + Final_Average1_i=0; + Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value + Final_Average2_i=0; + Final_Average2_q=0; + } + //float mag1 = sqrt(FIR33_Sample1_i_Output*FIR33_Sample1_i_Output+FIR33_Sample1_q_Output*FIR33_Sample1_q_Output); + //float mag2 = sqrt(FIR33_Sample2_i_Output*FIR33_Sample2_i_Output+FIR33_Sample2_q_Output*FIR33_Sample2_q_Output); + //printf("V1: %f\tV2: %f\n\r",mag1,mag2); + //filter33(FIR33_Sample1_i_Output, FIR33_Sample1_q_Output, FIR33_Sample2_i_Output, FIR33_Sample2_q_Output); + + } +} + +*/ + + + + #define FIR_100_LENGTH 64 float FIR100_Sample1_i[FIR_100_LENGTH]; float FIR100_Sample1_q[FIR_100_LENGTH]; @@ -35,75 +263,76 @@ uint8_t FIR100_Position = 0; //Fs = 100, Order = 63, Fpass = 1, Fstop = 5 static float lp_100_coeff[FIR_100_LENGTH] = { --0.00122570885798289, --0.00188999637047550, --0.00153249020825747, --0.00274386935197498, --0.00291939338925560, --0.00386012904484287, --0.00422028369122005, --0.00487674304247136, --0.00511534666379814, --0.00534548809850574, --0.00517201272363819, --0.00477057905668482, --0.00391668442164137, --0.00268115305854952, --0.000941210196010094, -0.00126714884107596, -0.00399477044758532, -0.00719945087788976, -0.0108724813712926, -0.0149475081564352, -0.0193653158514034, -0.0240295387918664, -0.0288389643323217, -0.0336754471496198, -0.0384141305817155, -0.0429241300038060, -0.0470794715470055, -0.0507566277450335, -0.0538451475324508, -0.0562499862639974, -0.0578974160014292, -0.0587336099426047, -0.0587336099426047, -0.0578974160014292, -0.0562499862639974, -0.0538451475324508, -0.0507566277450335, -0.0470794715470055, -0.0429241300038060, -0.0384141305817155, -0.0336754471496198, -0.0288389643323217, -0.0240295387918664, -0.0193653158514034, -0.0149475081564352, -0.0108724813712926, -0.00719945087788976, -0.00399477044758532, -0.00126714884107596, --0.000941210196010094, --0.00268115305854952, --0.00391668442164137, --0.00477057905668482, --0.00517201272363819, --0.00534548809850574, --0.00511534666379814, --0.00487674304247136, --0.00422028369122005, --0.00386012904484287, --0.00291939338925560, --0.00274386935197498, --0.00153249020825747, --0.00188999637047550, --0.00122570885798289 +-0.0127764520494401, +0.000293288299851251, +0.000495191328182707, +0.000838157407497840, +0.00131525363039988, +0.00192793120776209, +0.00267432434442746, +0.00355353493845821, +0.00456036887752939, +0.00569272050003285, +0.00694090445590618, +0.00830083540850880, +0.00975882121858377, +0.0113079414406056, +0.0129307225563250, +0.0146171626466673, +0.0163450416131430, +0.0181032221508212, +0.0198618118533429, +0.0216165682984801, +0.0233125759526720, +0.0249849393161916, +0.0265739031713570, +0.0280649931338029, +0.0294518285092336, +0.0307008224951110, +0.0318087914746566, +0.0327529952249549, +0.0335263522468559, +0.0341150932603310, +0.0345128562450896, +0.0347130017511486, +0.0347130017511486, +0.0345128562450896, +0.0341150932603310, +0.0335263522468559, +0.0327529952249549, +0.0318087914746566, +0.0307008224951110, +0.0294518285092336, +0.0280649931338029, +0.0265739031713570, +0.0249849393161916, +0.0233125759526720, +0.0216165682984801, +0.0198618118533429, +0.0181032221508212, +0.0163450416131430, +0.0146171626466673, +0.0129307225563250, +0.0113079414406056, +0.00975882121858377, +0.00830083540850880, +0.00694090445590618, +0.00569272050003285, +0.00456036887752939, +0.00355353493845821, +0.00267432434442746, +0.00192793120776209, +0.00131525363039988, +0.000838157407497840, +0.000495191328182707, +0.000293288299851251, +-0.0127764520494401 }; #define NUMSAMPLESAVERAGE 100 #define DecimationFactor_10K 10 void filter100(float FIR100_Sample1_i_input, float FIR100_Sample1_q_input, float FIR100_Sample2_i_input, float FIR100_Sample2_q_input) { + //printf("f"); static uint8_t finalAverageCounter = 0; static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated static float FIR100_Sample1_i_DecimatedSum=0; @@ -175,16 +404,17 @@ finalAverageCounter=0; float mag1 = sqrt(Final_Average1_i*Final_Average1_i+Final_Average1_q*Final_Average1_q); float mag2 = sqrt(Final_Average2_i*Final_Average2_i+Final_Average2_q*Final_Average2_q); - printf("V1: %f\tV2: %f\n\r",mag1,mag2); + printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag2/mag1); Final_Average1_i=0; Final_Average1_q=0;//when decimating sum up all 10 samples at a time have that be your output value Final_Average2_i=0; Final_Average2_q=0; } + //float mag1 = sqrt(FIR100_Sample1_i_Output*FIR100_Sample1_i_Output+FIR100_Sample1_q_Output*FIR100_Sample1_q_Output); //float mag2 = sqrt(FIR100_Sample2_i_Output*FIR100_Sample2_i_Output+FIR100_Sample2_q_Output*FIR100_Sample2_q_Output); //printf("V1: %f\tV2: %f\n\r",mag1,mag2); - //filter100(FIR100_Sample1_i_Output, FIR100_Sample1_q_Output, FIR100_Sample2_i_Output, FIR100_Sample2_q_Output); + //filter33(FIR100_Sample1_i_Output, FIR100_Sample1_q_Output, FIR100_Sample2_i_Output, FIR100_Sample2_q_Output); } } @@ -396,22 +626,22 @@ uint8_t FIR100K_Position = 0; //Fs = 100000, Order = 15, Fpass = 100, Fstop = 9000 static float lp_100K_coeff[FIR_100K_LENGTH] = { -0.0242947345184044, -0.0283599756767857, -0.0416004471421328, -0.0557840684332377, -0.0695867614174704, -0.0816182734401924, -0.0904748943926879, -0.0951919735506062, -0.0951919735506062, -0.0904748943926879, -0.0816182734401924, -0.0695867614174704, -0.0557840684332377, -0.0416004471421328, -0.0283599756767857, -0.0242947345184044 +0.0102922869776514, +0.0196357484226367, +0.0346068275004222, +0.0528964828542786, +0.0725363140207878, +0.0908817555678698, +0.105122693390187, +0.112904816559370, +0.112904816559370, +0.105122693390187, +0.0908817555678698, +0.0725363140207878, +0.0528964828542786, +0.0346068275004222, +0.0196357484226367, +0.0102922869776514 }; void filter100K(int sample1, int sample2)
--- a/main.cpp Sat Oct 31 20:06:37 2015 +0000 +++ b/main.cpp Fri Nov 06 03:33:09 2015 +0000 @@ -1,92 +1,155 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * - * Pinout for FRDM-k64f - * J2 - * X X - * X X - * X X - * J3 X X GND - * X X X X SCLK - * X X X X MISO - * X X X X MOSI - * X X X X CS - * X X GND X X - * GND X X X X - * GND X X - * 5Vin X X J1 - * X X - * J4 X X motorA - * X X X X motorB - * mic1 X X X X - * mic2 X X X X - * X X X X - * X X quadA X X - * X X quadB X X - * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "mbed.h" #include "pause.cpp" - -// Sampling #include "Sample/adc.h" #include "Sample/pdb.h" -#include "Sample/quad.h" - -#include "AngleEncoder.h" -#include "MotorControl.h" - #include "SignalProcessing.h" // for debug purposes Serial pc(USBTX, USBRX); DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); -//DigitalIn switch1(PTA4); -//DigitalIn switch2(PTC6); -//DigitalOut TotalInd(PTC4); -//DigitalOut SampleInd(PTC5); - -MotorControl motor(PTC2, PTA2, 2000, 25); // cw, ccw, period_us, safetyPeriod_us -AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz -// defined in dma.cpp -extern int len; -extern uint16_t sample_array0[]; -extern uint16_t sample_array1[]; -extern uint16_t angle_array[]; -extern bool dma_done; -extern bool dma_half_done; -int motorVal = 10; - -// Declaration of functions void output_data(); Timer t1; using namespace std; + +// uint16_t out_val_pre[]; +// +// +// uint16_t phase_counter2 = 0; +// #define pre_compute_length 2000 +// +// void ISR_repeat() { +// WaveOut.write_u16(out_val_pre[phase_counter2]); //creates a wave that bounces between 0 & 3.3 V +// +// phase_counter2++; +// if (phase_counter2 >= pre_compute_length) phase_counter2 = 0; +//} //ISR_repeat +// +//Ticker timer0; +// + + + +#define PDB_DACINTC0_TOE 0x01 // 0x01 -> PDB DAC interal trigger enabled + +#define DAC0_DAT0 (uint16_t *)0x400CC000 // DAC word buffer base address + +uint16_t *p1; +void setUpDac() +{ + SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; // turn on clock to the DAC + SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK; // turn on clock to the DAC + //DAC0_C0 = 0; + DAC0_C0 |= DAC_C0_DACEN_MASK ; // enable the DAC; must do before any of the following + //DAC0_C0 |= DAC_C0_REFSEL(0) + //DAC0_C0 &= 0x9f; + //DAC0_C0 |= DAC_C0_DACRFS_MASK; // 3.3V VDDA is DACREF_2 + //DAC0_C2 =9; + //DAC0_C2 |= 0x9; + //DAC0_C2 |= DAC_C2_DACBFUP(9); + DAC0_C2 =9; + //DAC0_C2 |= DAC_C2_DACBFUP(9); // resets to 15 but setting it anyway... + DAC0_C1 = 1; + //DAC0_C1 |= (0x01); // 0x01 enables the DAC buffer! See note above + + // prefill the DAC buffer with first 16 values from the lut + + p1 = DAC0_DAT0; + for (int i = 0; i < 16; i++) + { + *p1++ = (uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 460.0 + 2870.0); // 3351.0 + //printf("Pointer: %d\tValue: %d\n\r", (uint32_t)p1,(int) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 800.0 + 3103.0)); + } + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT0L,DAC0_DAT0H,DAC0_DAT0L|DAC0_DAT0H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT1L,DAC0_DAT1H,DAC0_DAT1L|DAC0_DAT1H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT2L,DAC0_DAT2H,DAC0_DAT2L|DAC0_DAT2H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT3L,DAC0_DAT3H,DAC0_DAT3L|DAC0_DAT3H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT4L,DAC0_DAT4H,DAC0_DAT4L|DAC0_DAT4H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT5L,DAC0_DAT5H,DAC0_DAT5L|DAC0_DAT5H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT6L,DAC0_DAT6H,DAC0_DAT6L|DAC0_DAT6H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT7L,DAC0_DAT7H,DAC0_DAT7L|DAC0_DAT7H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT8L,DAC0_DAT8H,DAC0_DAT8L|DAC0_DAT8H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT9L,DAC0_DAT9H,DAC0_DAT9L|DAC0_DAT9H<<8); + //printf ("data High: %d\n\r",DAC0_DAT0H); + + //printf ("data Low: %d\n\r",DAC0_DAT1L); + //printf ("data High: %d\n\r",DAC0_DAT1H); + /* + p1 = DAC0_DAT0; + for (int i = 0; i < 16; i++) + { + *p1++; + printf("Pointer: %d\n\r", (uint32_t)*p1); + } + + DAC0_DAT0L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 0) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT1L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 1) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT2L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 2) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT3L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 3) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT4L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 4) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT5L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 5) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT6L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 6) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT7L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 7) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT8L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 8) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT9L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 9) * 4965.0 + 49650.0))&0xFF); + + DAC0_DAT0H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 0) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT1H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 1) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT2H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 2) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT3H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 3) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT4H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 4) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT5H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 5) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT6H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 6) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT7H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 7) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT8H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 8) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT9H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 9) * 4965.0 + 49650.0))>>8)&0x0F); + */ + /* + DAC0_SR = 0x00; + SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; + SIM_SCGC6 |= SIM_SCGC2_DAC0_MASK; + DAC0_C0 = 0; + //DAC0_C0 |= DAC_C0_DACTRGSEL_MASK; + DAC0_C0 |= DAC_C0_DACEN_MASK ; //The DAC system is enabled. + + DAC0_C1 = 0; + //DAC0_C0 |= DAC_C0_DACTRGSEL_MASK; + DAC0_C0 |= 0x80;//DAC_C0_DMAEN_MASK ; + DAC0_DAT0L = 0xFF; + DAC0_DAT0H = 0x00; + */ +} + int main() { + //DAC0_C2 =0; + led_blue = 1; led_green = 1; led_red = 1; pre_compute_tables(); - printf("STARTING\n\r"); - t1.reset(); - for (int t = 0; t<500000; t++) - { - int input1 = 5000*cos(3.14159265359 * 2*10000*t*.00001)+2500; - int input2 = 2500*sin(3.14159265359 * 2*10000*t*.00001)+2500; - t1.start(); - filter100K(input1, input2); - t1.stop(); - } - printf("FINAL TIME: %f\n\r",t1.read()); - /* - pc.baud(230400); +// t1.reset(); +// for (int t = 0; t<500000; t++) +// { +// int input1 = 5000*cos(3.14159265359 * 2*10000*t*.00001)+2500; +// int input2 = 2500*sin(3.14159265359 * 2*10000*t*.00001)+2500; +// t1.start(); +// filter100K(input1, input2); +// t1.stop(); +// } +// printf("FINAL TIME: %f\n\r",t1.read()); + + + +// pc.baud(230400); pc.printf("Starting...\r\n"); + for(int i = 0; i < 86; i++) { @@ -110,6 +173,11 @@ //quad_init(); // initialize FTM2 quadrature decoder //quad_invert(); // invert the direction of counting adc_init(); // initialize ADCs (always initialize adc before dma) + setUpDac(); + // DAC0_C1 |= 0x80; + // DAC0_DAT0L = 0xFF; + // DAC0_DAT0H = 0xFF; + // printf("Dac should be 3.3V"); dma_init(); // initializes DMAs pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 @@ -121,215 +189,39 @@ led_green = 1; pause_ms(200); led_green = 0; - pause_ms(500); + pause_ms(500); led_green = 1; pdb_start(); - t1.start(); - t1.reset(); + + //timer0.attach_us(&ISR_repeat, 10); int startAddress = (int)&sample_array0[0]; + //timer0.attach_us(&ISR_repeat, 100); + int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2; + int currentIndex; + /* + printf("Dac Control0 Register: %X\n\r",DAC0_C0); + printf("Dac Control2 Register: %X\n\r",DAC0_C2); + printf("Dac Control1 Register: %X\n\r",DAC0_C1); + printf("Dac Control2 Register: %X\n\r\n\r",DAC0_C2); + //PDB0_DACINT0 = 0x257; + printf("Dac intc Register: %X\n\r",PDB0_DACINTC0); + printf("Dac int Register: %X\n\r",PDB0_DACINT0); + */ while (1) { - if (t1.read()>2) + + + destinationIndex = (DMA_TCD0_DADDR-startAddress)/2; + while (currentIndex!=destinationIndex) { - //pdb_start(); - pc.printf("%i\t%i\t%i\r\n", sample_array0[1], sample_array1[1],(DMA_TCD0_DADDR-startAddress)/2); - t1.reset(); + filter100K(sample_array0[currentIndex], sample_array1[currentIndex]); + currentIndex++; + if (currentIndex>=2000) + currentIndex = 0; + } - } -*/ - // while(1) { -// if(pc.readable() > 0) { -// char temp = pc.getc(); -// -// switch(temp) { -// case '1': // run motor and sample -// { -// // wait for angle to set to zero -// led_blue = 0; -// while(!angle_encoder.set_zero()) {pc.printf("AngleNotSet");} -// quad_reset(); -// led_blue = 1; -// -// // release mallet -// motor.releaseMallet(); -// -// // wait for mallet to drop to certin point before beginning to sample -// /* -// led_red = 0; -// int angleVal; -// do { -// angleVal = angle_encoder.absolute_angle(); -// } -// while(!(angleVal > 150 && angleVal < 400)); -// led_red = 1; -// */ -// -// // begin sampling -// pdb_start(); -// //pause_us(TOTAL_SAMPLES*10); -// //while(!dma_done) {led_green = 0;} -// led_green = 1; -// -// // reset mallet -// motor.reset(); -// output_data(); -// -// -// led_red = 1; -// led_blue = 1; -// led_green = 1; -// } -// break; -// -// case '2': -// { -// motor.releaseMallet(); -// pause(1); -// -// while(!angle_encoder.set_zero()) {} -// quad_reset(); -// -// pdb_start(); -// motor.clockwise(1); -// pause(1); -// motor.off(); -// output_data(); -// -// } -// break; -// -// case '3': -// { -// /* -// while(angleVar) -// { -// counter++; -// angleVar = angle_encoder.absolute_angle(); -// angleVar -= motorVal; -// if(angleVar == 0x00ff0000) {} // do nothing -// //else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed -// else if(angleVar < 43) { -// angleVar = 0; // exit loop -// //motorB.pulsewidth_us(0);} // min speed -// //else motorB.pulsewidth_us(angleVar*800/34); -// } -// motor.off(); -// */ -// } -// break; -// -// -// /******************************************************************** -// * The code below is for testing and troubleshooting. Don't delete. * -// ********************************************************************/ -// case 'B': -// case 'b': -// led_blue = !led_blue; -// pc.printf("Blue LED\r\n"); -// break; -// case 'R': -// case 'r': -// led_red = !led_red; -// pc.printf("Red LED\r\n"); -// break; -// -// // test sampling -// case 'Q': -// case 'q': -// quad_reset(); -// pdb_start(); -// -// while(!dma_half_done) { -// pc.printf("first half\r\n"); -// pause_ms(1); -// } -// -// while(!dma_done) { -// pc.printf("second half\r\n"); -// pause_ms(1); -// } -// for(int i = 0; i < len; i++) pc.printf("%i\t %i\t %i\r\n", sample_array0[i],sample_array1[i], angle_array[i]); -// pc.printf("\r\n"); -// -// led_red = 1; -// led_green = 1; -// led_blue = 1; -// -// -// /* -// while(!angle_encoder.set_zero()) {} -// quad_reset(); -// -// -// pdb_start(); -// pause_us(TOTAL_SAMPLES*10); -// output_data();*/ -// -// break; -// case 'W': -// case 'w': // reads 0 unless the counter is running -// pc.printf("PDB: %i\r\n",PDB0_CNT); -// break; -// -// // test angle encoder -// case 'A': -// case 'a': // set zero -// if(angle_encoder.set_zero()) { -// pc.printf("Zero set\r\n"); -// } -// else { -// pc.printf("Zero NOT set\r\n"); -// } -// break; -// case 'S': -// case 's': // perform "no operation", which returns 0xa5 -// { -// pc.printf("NOP: 0x%02x\r\n", angle_encoder.nop()); -// break; -// } -// case 'D': -// case 'd': // read absolute and relative angles -// { -// pc.printf("Angle: %i %i\r\n", angle_encoder.absolute_angle(), quad_read()); -// break; -// } -// case 'F': -// case 'f': -// pc.printf("Quad Cnt: %i\r\n", quad_read()); -// break; -// -// // test motor -// case 'Z': -// case 'z': // release the mallet -// motor.releaseMallet(); -// break; -// case 'X': -// case 'x': -// motor.hardReset(motorVal); -// break; -// case '+': -// case '=': -// motorVal++; -// if(motorVal > 8000) motorVal = 8000; -// pc.printf("MotorVal: %i\r\n", motorVal); -// break; -// case '-': -// { -// motorVal--; -// if(motorVal < 0) motorVal = 0; -// pc.printf("MotorVal: %i\r\n", motorVal); -// break; -// } -// } -// } -// } - -} - -void output_data() -{ - for (int n = 0; n < len; n++) { - pc.printf("%i\t%i\r\n", sample_array0[n], sample_array1[n]); } -} \ No newline at end of file + + +}