Rob Dobson / Mbed 2 deprecated TestAnalogWithSnippet

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // analog input p20 (ADC0[5], pin 3) test with stats
00002 
00003 #include "mbed.h"
00004 
00005 // Code from Simon Ford 2010-01-04
00006 void mbedAdcInit()
00007 {
00008     // power on, clk divider /4
00009     LPC_SC->PCONP |= (1 << 12);          
00010     LPC_SC->PCLKSEL0 &= ~(0x3 << 24);    
00011 
00012     // software-controlled ADC settings
00013     LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected
00014               | (200 << 8)    // CLKDIV: PCLK max ~= 25MHz, /25 to give safe 1MHz
00015               | (0 << 16)    // BURST: 0 = software control 
00016               | (0 << 17)    // CLKS: not applicable 
00017               | (1 << 21)    // PDN: 1 = operational
00018               | (0 << 24)    // START: 0 = no start
00019               | (0 << 27);   // EDGE: not applicable
00020 
00021     // setup P1_31 as sel 3 (ADC), mode 2 (no pull)    
00022     LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 30);
00023     LPC_PINCON->PINSEL3 |= (unsigned int)0x3 << 30;
00024     
00025     LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 30);
00026     LPC_PINCON->PINMODE3 |= (unsigned int)0x2 << 30;
00027 
00028 }
00029 
00030 unsigned int mbedGetVal()
00031 {
00032     // Select channel and start conversion
00033     LPC_ADC->ADCR &= ~0xFF;
00034     LPC_ADC->ADCR |= 1 << 5; // ADC0[5]
00035     LPC_ADC->ADCR |= 1 << 24;
00036 
00037     // Repeatedly get the sample data until DONE bit
00038     unsigned int data;
00039     do {
00040        data = LPC_ADC->ADGDR;
00041     } while ((data & ((unsigned int)1 << 31)) == 0);
00042        data = LPC_ADC->ADGDR;
00043 
00044     // Stop conversion    
00045     LPC_ADC->ADCR &= ~(1 << 24);
00046 
00047     return data;
00048 }
00049 
00050 // Code gleaned from MBED forum
00051 void DispInfo()
00052 {
00053     printf("SystemCoreClock = %d Hz\n", SystemCoreClock);
00054  
00055     int Fin = 12000000; // 12MHz XTAL
00056     
00057     printf("PLL Registers:\n");
00058     printf(" - PLL0CFG = 0x%08X\n", LPC_SC->PLL0CFG);
00059     printf(" - CLKCFG  = 0x%08X\n", LPC_SC->CCLKCFG);
00060     
00061     int M = (LPC_SC->PLL0CFG & 0xFFFF) + 1;
00062     int N = (LPC_SC->PLL0CFG >> 16) + 1;
00063     int CCLKDIV = LPC_SC->CCLKCFG + 1;
00064 
00065     printf("Clock Variables:\n");
00066     printf(" - Fin = %d\n", Fin);
00067     printf(" - M   = %d\n", M);
00068     printf(" - N   = %d\n", N);
00069     printf(" - CCLKDIV = %d\n", CCLKDIV);
00070 
00071     int Fcco = (2 * M * 12000000) / N;
00072     int CCLK = Fcco / CCLKDIV;
00073 
00074     printf("Clock Results:\n");    
00075     printf(" - Fcco = %d\n", Fcco);
00076     printf(" - CCLK = %d\n", CCLK);    
00077 
00078     printf("PCLKSEL0 = 0x%08x\n",  LPC_SC->PCLKSEL0);    
00079     printf("PCLKSEL1 = 0x%08x\n",  LPC_SC->PCLKSEL1);    
00080     
00081 }
00082 
00083 int main() {
00084 
00085     const int NUM_LOOPS = 100;
00086     const int NUM_SAMPS = 1024;
00087     
00088     mbedAdcInit();
00089     wait_ms(100);
00090 
00091     DispInfo();
00092 
00093     // Table of deviation counts
00094     const int DEV_HIST_SIZE = 10;
00095     int devTable[DEV_HIST_SIZE+1];
00096     for (int i = 0; i < DEV_HIST_SIZE+1; i++)
00097         devTable[i] = 0;
00098 
00099     // Run many times
00100     double mean;
00101     double lowestStdDev = 1024;
00102     double highestStdDev = 0;
00103     double sumForMean = 0;
00104     for (int j = 0; j < NUM_LOOPS; j++)
00105     {
00106         // Get ADC readings
00107         unsigned int adgdrVals[NUM_SAMPS];
00108         unsigned int samples[NUM_SAMPS];
00109         for(int i=0; i<NUM_SAMPS; i++) {
00110             adgdrVals[i] = mbedGetVal();
00111             samples[i] = (adgdrVals[i] >> 5) & 0x3ff;
00112             wait_ms(1);
00113         }
00114     
00115         // Calculate mean
00116         double localSum = 0;
00117         for(int i=0; i<NUM_SAMPS; i++)
00118             localSum += samples[i];
00119         double localMean = localSum / NUM_SAMPS;
00120         sumForMean += localSum;
00121         
00122         // Calculate std-dev sum
00123         double sumSqrDiffs = 0;
00124         for(int i=0; i<NUM_SAMPS; i++)
00125             sumSqrDiffs += (samples[i] - localMean) * (samples[i] - localMean);
00126             
00127         // Calculate stdDev and see if low/high
00128         double stdDev = sqrt(sumSqrDiffs / NUM_SAMPS);
00129         if (lowestStdDev > stdDev)
00130             lowestStdDev = stdDev;
00131         if (highestStdDev < stdDev)
00132             highestStdDev = stdDev;
00133         
00134         // Histogram of values
00135         for (int i = 0; i < NUM_SAMPS; i++)
00136         {
00137             int outOfBoundsDist = int(fabs(samples[i] - localMean));
00138             if (outOfBoundsDist >= DEV_HIST_SIZE)
00139                 devTable[DEV_HIST_SIZE]++;
00140             else
00141                 devTable[outOfBoundsDist]++;
00142         }
00143         
00144         // Show results that are out of bounds
00145         printf("Results out of bounds for pass %d:\n", j);
00146         for(int i=0; i<NUM_SAMPS; i++) {
00147             if (fabs(samples[i] - localMean) > 5)
00148                 printf("%5d, 0x%04X, %d\n", i, adgdrVals[i], samples[i]);
00149         }
00150     }
00151     
00152     // Show overall stats
00153     mean = sumForMean / NUM_SAMPS / NUM_LOOPS;
00154     printf("Mean: %.2f\n", mean);
00155     printf("Std-dev (highest, lowest): %.2f, %.2f\n", highestStdDev, lowestStdDev);
00156     printf("Deviation Table\n");
00157     printf("Dist.\tCount\n");
00158     for (int i = 0; i < DEV_HIST_SIZE+1; i++)
00159         printf("%s%d\t%d\n", i == DEV_HIST_SIZE ? "> " : "", i, devTable[i]);
00160 }