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 7:af255a90505e, committed 2015-12-19
- Comitter:
- DeWayneDennis
- Date:
- Sat Dec 19 21:47:52 2015 +0000
- Parent:
- 6:63de50ac29be
- Commit message:
- Final Code
Changed in this revision
--- a/Sample/adc.cpp Fri Nov 06 20:50:30 2015 +0000
+++ b/Sample/adc.cpp Sat Dec 19 21:47:52 2015 +0000
@@ -19,27 +19,6 @@
* 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;
@@ -54,9 +33,10 @@
SIM_SOPT7 = SIM_SOPT7_ADC1TRGSEL(0); // Select triggering by PDB and select pre-trigger A
// calibrate the ADC
- __disable_irq();
- if(adc_cal()) {red = 0; green = 0;} // if calibration fails, display yellow
- __enable_irq();
+ //__disable_irq();
+ //if(adc_cal()) {red = 0; green = 0;} // if calibration fails, display yellow
+ //if(adc_cal1()) {red = 0; green = 0;} // if calibration fails, display yellow
+ //__enable_irq();
// Setup Configuration Register 1
@@ -98,12 +78,15 @@
ADC1_SC3 = 0; // Hardware Average set to 4 samples averaged
// Hardware Average Disabled
// select single conversion mode
-
+ // Setup Status and Control Register 3 now that calibration is complete
+ //ADC0_SC3 = ADC_SC3_AVGS(0) | ADC_SC3_AVGE_MASK; // Hardware Average set to 16 samples averaged
+ // select single conversion mode
+ //ADC1_SC3 = ADC_SC3_AVGS(0) | ADC_SC3_AVGE_MASK; // Hardware Average set to 16 samples averaged
// Setup Status and Control Register 1A
ADC0_SC1B = 0; // clear register
ADC0_SC1B &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
ADC0_SC1B |= ADC_SC1_AIEN_MASK; // enable interrupt (for debugging)
- ADC0_SC1B |= ADC_SC1_ADCH(13); // select channel 13
+ ADC0_SC1B |= ADC_SC1_ADCH(12); // select channel 13
ADC1_SC1A = 0; // clear register
ADC1_SC1A &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
ADC1_SC1A |= ADC_SC1_AIEN_MASK; // enable interrupt (for debugging)
@@ -154,6 +137,33 @@
return 0;
}
+int adc_cal1(void)
+{
+ ADC1_CFG1 |= (ADC_CFG1_MODE(3) | // 16 bits mode
+ ADC_CFG1_ADICLK(1)| // Input Bus Clock divided by 2 (20-25 MHz out of reset (FEI mode) / 2)
+ ADC_CFG1_ADIV(2)) ; // Clock divide by 4 (2.5-3 MHz)
+
+ ADC1_SC3 |= ADC_SC3_AVGE_MASK | // Enable HW average
+ ADC_SC3_AVGS(3) | // Set HW average of 32 samples
+ ADC_SC3_CAL_MASK; // Start calibration process
+
+ while(ADC1_SC3 & ADC_SC3_CAL_MASK); // Wait for calibration to end
+
+ if(ADC1_SC3 & ADC_SC3_CALF_MASK) return 1; // Check for successful calibration
+
+ uint16_t calib = 0; // calibration variable
+ calib += ADC1->CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0;
+ calib /= 2;
+ calib |= 0x8000; // Set MSB
+ ADC1_PG = calib;
+ calib = 0;
+ calib += ADC1_CLMS + ADC1_CLM4 + ADC1_CLM3 + ADC1_CLM2 + ADC1_CLM1 + ADC1_CLM0;
+ calib /= 2;
+ calib |= 0x8000; // Set MSB
+ ADC1_MG = calib;
+
+ return 0;
+}
@@ -239,4 +249,4 @@
debug.printf("ADC0_RB: %08x\r\n",ADC0_RB); //(0x00000000)
debug.printf("ADC0_SC2: %08x\r\n",ADC0_SC2); //(0x00000044)
debug.printf("ADC0_SC3: %08x\r\n\n",ADC0_SC3); //(0x00000000)
-}
\ No newline at end of file
+}
--- a/Sample/adc.h Fri Nov 06 20:50:30 2015 +0000 +++ b/Sample/adc.h Sat Dec 19 21:47:52 2015 +0000 @@ -1,18 +1,19 @@ #ifndef ADC_H_ #define ADC_H_ - + #include "mbed.h" #include "dma.h" - - + + /*** * Initializes the ADC to 16-bit single-ended, hardware triggered * with DMA enabled. **/ void adc_init(); int adc_cal(void); +int adc_cal1(void); //void adc_single_sample(); //void adc_start(); //void adc_stop(); - + #endif /* ADC_H_ */ \ No newline at end of file
--- a/Sample/dma.cpp Fri Nov 06 20:50:30 2015 +0000
+++ b/Sample/dma.cpp Sat Dec 19 21:47:52 2015 +0000
@@ -13,14 +13,14 @@
int len = TOTAL_SAMPLES;
uint16_t sample_array0[TOTAL_SAMPLES];
uint16_t sample_array1[TOTAL_SAMPLES];
-//uint16_t out_val_pre[TOTAL_SAMPLES];//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 CARRIERFREQUENCY 1000
#define twopi 3.14159265359 * 2
/* DMA0 and DMA1 are triggered by ADC0 and ADC1 (which are triggered
@@ -28,9 +28,9 @@
* 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);
-// }
+ for(int precompute_counter = 0; precompute_counter < TOTAL_SAMPLES; precompute_counter++){
+ out_val_pre[precompute_counter] = (int) (cos(twopi * CARRIERFREQUENCY * DMA_PERIOD * precompute_counter) * 150.0 + 2755.0);
+ }
toggle_dma0 = 1;
toggle_dma1 = 1;
@@ -68,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) &out_val_pre[0];//&FTM2_CNT;
- //DMA_TCD2_DADDR = (uint32_t) &DAC0_DAT0L;
+ 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
@@ -78,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 = 0x02; // Source address offset of 2 bits per transaction
- //DMA_TCD2_DOFF = 0x00; // 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)
@@ -108,8 +108,8 @@
DMA_TCD1_DLASTSGA = -len*2; // Destination address adjustment
//Change source and destination
- //DMA_TCD2_SLAST = -len*2; // Source address adjustment
- //DMA_TCD2_DLASTSGA = 0; // 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;
--- a/Sample/pdb.cpp Fri Nov 06 20:50:30 2015 +0000
+++ b/Sample/pdb.cpp Sat Dec 19 21:47:52 2015 +0000
@@ -132,4 +132,4 @@
debug2.printf("PDB0_PO0DLY: %08x\r\n",PDB0_PO0DLY);
debug2.printf("PDB0_PO1DLY: %08x\r\n",PDB0_PO1DLY);
debug2.printf("PDB0_PO2DLY: %08x\r\n\n",PDB0_PO2DLY);
-}
\ No newline at end of file
+}
--- a/SignalProcessing.cpp Fri Nov 06 20:50:30 2015 +0000
+++ b/SignalProcessing.cpp Sat Dec 19 21:47:52 2015 +0000
@@ -1,17 +1,17 @@
#include "mbed.h"
#include "SignalProcessing.h"
+#include "Sensor.h"
#define pre_compute_length 2000
#define DMA_PERIOD .00001
#define DMA_FREQUENCY 100000
-#define CARRIERFREQUENCY 10000
+#define CARRIERFREQUENCY 1000
uint16_t phase_counter = 0;
float i_mod_pre[pre_compute_length];
float q_mod_pre[pre_compute_length];
//uint16_t out_val_pre[pre_compute_length];
-float filteredLong, filteredLongRef;
#define twopi 3.14159265359 * 2
@@ -28,7 +28,7 @@
-/*
+
#define FIR_33_LENGTH 128
float FIR33_Sample1_i[FIR_33_LENGTH];
float FIR33_Sample1_q[FIR_33_LENGTH];
@@ -160,7 +160,7 @@
0.000976800083269572,
-0.00572883284452795
};
-#define NUMSAMPLESAVERAGE 33
+#define NUMSAMPLESAVERAGE 100
#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)
@@ -237,7 +237,7 @@
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);
+ //printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag1/mag2);
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;
@@ -251,11 +251,11 @@
}
}
-*/
-/*
+
+
#define FIR_100_LENGTH 64
float FIR100_Sample1_i[FIR_100_LENGTH];
float FIR100_Sample1_q[FIR_100_LENGTH];
@@ -329,8 +329,9 @@
0.000293288299851251,
-0.0127764520494401
};
-#define NUMSAMPLESAVERAGE 100
-#define DecimationFactor_10K 10
+
+//#define NUMSAMPLESAVERAGE 100
+#define DecimationFactor_1K 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");
@@ -351,7 +352,7 @@
FIR100_Sample2_i_DecimatedSum += FIR100_Sample2_i_input;
FIR100_Sample2_q_DecimatedSum += FIR100_Sample2_q_input;
decimationCounter++;
- if (decimationCounter >= DecimationFactor_10K)//once 10 samples have com
+ if (decimationCounter >= DecimationFactor_1K)//once 10 samples have com
{
decimationCounter = 0;//reset decimation counter
//add sample to 10K filter
@@ -403,9 +404,11 @@
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);
+ 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));
+ setFiltered(mag1);
+ setFilteredRef(mag2);
+ printf("V1: %f\tV2: %f\tRatio: %f\n\r",mag1,mag2,mag1/mag2);
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;
@@ -419,12 +422,12 @@
}
}
-*/
-#define NUMSAMPLESAVERAGE 100
+
+
#define FIR_1K_LENGTH 32
float FIR1K_Sample1_i[FIR_1K_LENGTH];
float FIR1K_Sample1_q[FIR_1K_LENGTH];
@@ -470,15 +473,15 @@
#define DecimationFactor_10K 10
void filter1K(float FIR1K_Sample1_i_input, float FIR1K_Sample1_q_input, float FIR1K_Sample2_i_input, float FIR1K_Sample2_q_input)
{
- 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;
-
+// 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;
+// static int finalAverageCounter = 0;
+//
static uint8_t decimationCounter = 0;//used to keep track of how many samples you have currently decimated
- static uint16_t finalAverageCounter = 0; //used to keep track of how many elements to average across
static float FIR1K_Sample1_i_DecimatedSum=0;
static float FIR1K_Sample1_q_DecimatedSum=0;//when decimating sum up all 10 samples at a time have that be your output value
static float FIR1K_Sample2_i_DecimatedSum=0;
@@ -527,29 +530,28 @@
FIR1K_Sample2_i_Output += FIR1K_Sample2_i[fir_index] * lp_1K_coeff[fir_counter];
FIR1K_Sample2_q_Output += FIR1K_Sample2_q[fir_index] * lp_1K_coeff[fir_counter];
}
- Final_Average1_i+=FIR1K_Sample1_i_Output;
- Final_Average1_q+=FIR1K_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value
- Final_Average2_i+=FIR1K_Sample2_i_Output;
- Final_Average2_q+=FIR1K_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);
- //store filtered values for later reading
- filteredLong = mag1;
- filteredLongRef = mag2;
-
- 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;
- }
+
+ //Final_Average1_i+=FIR1K_Sample1_i_Output;
+// Final_Average1_q+=FIR1K_Sample1_q_Output;//when decimating sum up all 10 samples at a time have that be your output value
+// Final_Average2_i+=FIR1K_Sample2_i_Output;
+// Final_Average2_q+=FIR1K_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(FIR1K_Sample1_i_Output*FIR1K_Sample1_i_Output+FIR1K_Sample1_q_Output*FIR1K_Sample1_q_Output);
//float mag2 = sqrt(FIR1K_Sample2_i_Output*FIR1K_Sample2_i_Output+FIR1K_Sample2_q_Output*FIR1K_Sample2_q_Output);
//printf("V1: %f\tV2: %f\n\r",mag1,mag2);
- //filter100(FIR1K_Sample1_i_Output, FIR1K_Sample1_q_Output, FIR1K_Sample2_i_Output, FIR1K_Sample2_q_Output);
+ filter100(FIR1K_Sample1_i_Output, FIR1K_Sample1_q_Output, FIR1K_Sample2_i_Output, FIR1K_Sample2_q_Output);
}
}
@@ -717,11 +719,4 @@
//float mag2 = sqrt(FIR100K_Sample2_i_Output*FIR100K_Sample2_i_Output+FIR100K_Sample2_q_Output*FIR100K_Sample2_q_Output);
//printf("V1: %f\tV2: %f\n\r",mag1,mag2);
filter10K(FIR100K_Sample1_i_Output, FIR100K_Sample1_q_Output, FIR100K_Sample2_i_Output, FIR100K_Sample2_q_Output);
-}
-
-float getFiltered(){
- return filteredLong;
-}
-float getFilteredRef(){
- return filteredLongRef;
-}
+}
\ No newline at end of file
--- a/SignalProcessing.h Fri Nov 06 20:50:30 2015 +0000 +++ b/SignalProcessing.h Sat Dec 19 21:47:52 2015 +0000 @@ -3,6 +3,4 @@ void pre_compute_tables(); void filter100K(int sample1, int sample2); -float getFiltered(); -float getFilteredRef(); #endif \ No newline at end of file
--- a/pause.cpp Fri Nov 06 20:50:30 2015 +0000
+++ b/pause.cpp Sat Dec 19 21:47:52 2015 +0000
@@ -5,13 +5,13 @@
inline void pause(uint32_t seconds) {
for(seconds; seconds > 0; seconds--) for(uint32_t i = 0x1ffffff; i > 0; i--) asm("nop");
}
-
+
inline void pause_ms(uint32_t milliseconds) {
for(milliseconds; milliseconds > 0; milliseconds--) for(uint32_t i = 0x8312; i > 0; i--) asm("nop");
}
-
+
inline void pause_us(uint32_t microseconds) {
for(microseconds; microseconds > 0; microseconds--) for(uint32_t i = 0x21; i > 0; i--) asm("nop");
}
-
+
#endif
\ No newline at end of file
