Dependencies:   mbed-dsp mbed

Fork of DSP_200kHz by Mazzeo Research Group

Revision:
54:1697dc574b96
Parent:
53:83a90a47c1fd
Child:
55:2526b3317bc8
--- a/DMA_sampling/adc.cpp	Tue Feb 09 15:11:08 2016 +0000
+++ b/DMA_sampling/adc.cpp	Tue Feb 16 18:33:44 2016 +0000
@@ -20,7 +20,7 @@
 DigitalOut green(LED_GREEN);
 DigitalOut red(LED_RED);
 
-//Serial debug(USBTX, USBRX);
+Serial debug(USBTX, USBRX);
 
 
 void adc_init()
@@ -31,8 +31,8 @@
     SIM_SCGC3 |= SIM_SCGC3_ADC1_MASK;
     
     // Set ADC hardware trigger to PDB0
-    SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0); // Select triggering by PDB and select pre-trigger A
-    SIM_SOPT7 = SIM_SOPT7_ADC1TRGSEL(0); // Select triggering by PDB and select pre-trigger A
+    //SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0); // Select triggering by PDB and select pre-trigger A
+    //SIM_SOPT7 = SIM_SOPT7_ADC1TRGSEL(0); // Select triggering by PDB and select pre-trigger A
        
     // turn on the clock to the PDB
 //    SIM->SCGC6 |= SIM_SCGC6_PDB_MASK;
@@ -71,14 +71,18 @@
 
     ADC0_CFG1 = 0x0C; //ADLPC = 0, ADIV = 0, ADLSMP = 0, MODE = 0b11, ADICLK = 00 (16-bit single-ended)
     ADC1_CFG1 = 0x0C; //ADLPC = 0, ADIV = 0, ADLSMP = 0, MODE = 0b11, ADICLK = 00 (16-bit single-ended)
+
+//    ADC0_CFG1 = 0x00; //ADLPC = 0, ADIV = 0, ADLSMP = 0, MODE = 0b00, ADICLK = 00 (16-bit single-ended)
+//    ADC1_CFG1 = 0x00; //ADLPC = 0, ADIV = 0, ADLSMP = 0, MODE = 0b00, ADICLK = 00 (16-bit single-ended)
+
     
     // It is necessary not to have hardware trigger for calbiration
     ADC0_CFG2 = 0x00; // MUXSEL = 0, ADACKEN = 0, ADHSC = 0, ADLSTS = 0 (does not matter - short sample time)
     ADC1_CFG2 = 0x00; // MUXSEL = 0, ADACKEN = 0, ADHSC = 0, ADLSTS = 0 (does not matter - short sample time)
 
     // Use averaging to make calibration better
-    ADC0_SC3  = 0x0F; // CAL = 0, CALF = 0, res[5:4], ADCO = 1 (continuous conversion), AVGE = 1, AVGS = 11
-    ADC1_SC3  = 0x0F; // CAL = 0, CALF = 0, res[5:4], ADCO = 1 (continuous conversion), AVGE = 1, AVGS = 11         
+    ADC0_SC3  = 0x07; // CAL = 0, CALF = 0, res[5:4], ADCO = 0 (continuous conversion), AVGE = 1, AVGS = 11
+    ADC1_SC3  = 0x07; // CAL = 0, CALF = 0, res[5:4], ADCO = 0 (continuous conversion), AVGE = 1, AVGS = 11         
     
     
     // calibrate the ADC (following Joey's code example)
@@ -86,6 +90,12 @@
     while(ADC0_SC3&ADC_SC3_CALF_MASK) {} // wait for calibration to complete
     ADC1_SC3 |= ADC_SC3_CAL_MASK; // start calibration
     while(ADC1_SC3&ADC_SC3_CALF_MASK) {} // wait for calibration to complete
+
+    debug.printf("ADC0_CLP0:%d\r\n", ADC0_CLP0);
+    debug.printf("ADC0_CLP1:%d\r\n", ADC0_CLP1);
+    debug.printf("ADC0_CLP2:%d\r\n", ADC0_CLP2);
+    debug.printf("ADC0_CLP3:%d\r\n", ADC0_CLP3);
+    debug.printf("ADC0_CLPS:%d\r\n", ADC0_CLPS);
        
     // calculate the gains (see user manual page 864)
     int16_t gain = (ADC0_CLP0+ADC0_CLP1+ADC0_CLP2+ADC0_CLP3+ADC0_CLP4+ADC0_CLPS);
@@ -108,15 +118,51 @@
     gain |= 0x8000;  // set the MSB
     ADC1_MG = gain;
     
+//    ADC0_SC3 &= ~ADC_SC3_CAL_MASK; // stop calibration
+//    ADC1_SC3 &= ~ADC_SC3_CAL_MASK; // stop calibration
+    
 
-    // Now set up for use in the rest of the program.
-    ADC0_SC2  = 0x44; // ADACT = 0, ADTRG = 1 (HW trigger), ACFE = 0, ACFGT = 0, ACREN = 0, DMAEN = 1, REFSEL = 00 (DMAEN needs to be asserted)
-    ADC1_SC2  = 0x44; // ADACT = 0, ADTRG = 1 (HW trigger), ACFE = 0, ACFGT = 0, ACREN = 0, DMAEN = 1, REFSEL = 00 (DMAEN needs to be asserted)    
-    
+    // Now set up for use in the rest of the program - software trigger
+    ADC0_SC2  = 0x04; // ADACT = 0, ADTRG = 0 (HW trigger), ACFE = 0, ACFGT = 0, ACREN = 0, DMAEN = 1, REFSEL = 00 (DMAEN needs to be asserted)
+    ADC1_SC2  = 0x04; // ADACT = 0, ADTRG = 0 (HW trigger), ACFE = 0, ACFGT = 0, ACREN = 0, DMAEN = 1, REFSEL = 00 (DMAEN needs to be asserted)    
+   
+   
     ADC0_SC3  = 0x08; // CAL = 0, CALF = 0, res[5:4], ADCO = 1 (continuous conversion), AVGE = 0, AVGS = 00
     ADC1_SC3  = 0x08; // CAL = 0, CALF = 0, res[5:4], ADCO = 1 (continuous conversion), AVGE = 0, AVGS = 00      
+
+//    ADC0_SC3  = 0x00; // CAL = 0, CALF = 0, res[5:4], ADCO = 0 (continuous conversion), AVGE = 0, AVGS = 00
+//    ADC1_SC3  = 0x00; // CAL = 0, CALF = 0, res[5:4], ADCO = 0 (continuous conversion), AVGE = 0, AVGS = 00      
+
+
     
-    // debug.printf("Calbiration complete. Hardware continuous conversion running.\r\n");
+    // A write to SC1A is necessary as a software trigger
+    ADC0_SC1A = 0x0C; //AIEN = 0, DIFF = 0, Channel = AD12 (PTB2)
+    ADC1_SC1A = 0x0E; //AIEN = 0, DIFF = 0, Channel = AD14 (PTB10)  
+
+    while( (ADC1_SC1A&ADC_SC1_COCO_MASK)) {}
+    //gain = ADC0_RA; // read the register to clear SC1A[COCO]
+    debug.printf("ADC0_RA:%i\r\n", ADC0_RA);
+    debug.printf("ADC0_RB:%i\r\n", ADC0_RB);
+    debug.printf("ADC1_RA:%i\r\n", ADC1_RA);
+    debug.printf("ADC1_RB:%i\r\n", ADC1_RB);
+
+/*
+
+    ADC0_SC1A = 0x0C; //AIEN = 0, DIFF = 0, Channel = AD12 (PTB2)
+    ADC1_SC1A = 0x0E; //AIEN = 0, DIFF = 0, Channel = AD14 (PTB10)  
+
+    while( (ADC1_SC1A&ADC_SC1_COCO_MASK)) {}
+    debug.printf("ADC1_RA:%i\r\n", ADC1_RA);
+
+    ADC0_SC1A = 0x0C; //AIEN = 0, DIFF = 0, Channel = AD12 (PTB2)
+    ADC1_SC1A = 0x0E; //AIEN = 0, DIFF = 0, Channel = AD14 (PTB10)  
+
+
+    while( (ADC1_SC1A&ADC_SC1_COCO_MASK)) {}
+    debug.printf("ADC1_RA:%i\r\n", ADC1_RA);
+ */
+    
+    // adebug.printf("Calbiration complete. Hardware continuous conversion running.\r\n");
     // The ADCs are now just continuously converting and generating DMA signals after each conversion
 }