Program to benchmark AnalogIn performance

Dependencies:   mbed

Fork of ADC_spikes by Chris Styles

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define NUM_SAMPLES 500000 // size of sample series
00004 #define SAMPLE_BLOCKS 5
00005 
00006 Timer t;
00007 
00008 Serial pc(USBTX,USBRX);
00009 
00010 AnalogIn ain(p20);
00011 
00012 // initialise error counters
00013 int num_4;
00014 int num_8;
00015 int num_16;
00016 int num_32;
00017 int num_64;
00018 int num_128;
00019 int num_256;
00020 int num_512;
00021 int num_1024;
00022 int num_2048;
00023 
00024 
00025 int main() {
00026 
00027     pc.baud(115200);
00028 
00029     int num_samples = 0;
00030 
00031     num_4 = 0;
00032     num_8 = 0;
00033     num_16 = 0;
00034     num_32 = 0;
00035     num_64 = 0;
00036     num_128 = 0;
00037     num_256 = 0;
00038     num_512 = 0;
00039     num_1024 = 0;
00040     num_2048 = 0;
00041 
00042 // Take the average over 500,000 samples
00043 // This is because the bias to 1.65v has a tolerance
00044     float AVERAGE = 0.5;
00045 
00046     pc.printf("Taking an average over %d samples\n",NUM_SAMPLES);
00047     while (num_samples < NUM_SAMPLES) {
00048         float r = ain.read();
00049 
00050         if ((r > 0.45) && (r < 0.55)) {
00051             AVERAGE += r;
00052             num_samples++;
00053         }
00054     }
00055     
00056     AVERAGE /= NUM_SAMPLES;
00057     
00058     num_samples = 0;
00059     pc.printf("Average = %f\n",AVERAGE);
00060 
00061 
00062 // Now start sampling series of 500,000
00063 // acculumating the errors seen in each range
00064     pc.printf("Profiling %d samples\n",SAMPLE_BLOCKS*NUM_SAMPLES);
00065     for (int j=0; j < SAMPLE_BLOCKS ; j++) {
00066 
00067         t.reset();
00068         t.start();
00069         
00070         for (int i = 0; i < NUM_SAMPLES ; i++) {
00071             float a = ain.read();
00072 
00073             if (a > (AVERAGE + 0.5000)) {
00074                 num_2048++;    // > 2048 lsb
00075             } else if (a > (AVERAGE + 0.2500)) {
00076                 num_1024++;    // > 1024 lsb
00077             } else if (a > (AVERAGE + 0.0625)) {
00078                 num_512++;    // > 512 lsb
00079             } else if (a > (AVERAGE + 0.0312)) {
00080                 num_256++;    // > 256 lsb
00081             } else if (a > (AVERAGE + 0.0312)) {
00082                 num_128++;    // > 128 lsb
00083             } else if (a > (AVERAGE + 0.0156)) {
00084                 num_64++;    // > 64 lsb
00085             } else if (a > (AVERAGE + 0.0078)) {
00086                 num_32++;    // > 32 lsb
00087             } else if (a > (AVERAGE + 0.0039)) {
00088                 num_16++;    // > 16 lsb
00089             } else if (a > (AVERAGE + 0.0019)) {
00090                 num_8++;    // > 8 lsb
00091             } else if (a > (AVERAGE + 0.0009)) {
00092                 num_4++;    // > 8 lsb
00093             }
00094 
00095             num_samples++;
00096         }
00097         t.stop();
00098         
00099         // Every 500,000 print the results
00100         pc.printf("%d\t",num_samples);
00101         pc.printf("%d\t",num_4);
00102         pc.printf("%d\t",num_8);
00103         pc.printf("%d\t",num_16);
00104         pc.printf("%d\t",num_32);
00105         pc.printf("%d\t",num_64);
00106         pc.printf("%d\t",num_128);
00107         pc.printf("%d\t",num_256);
00108         pc.printf("%d\t",num_512);
00109         pc.printf("%d\t",num_1024);
00110         pc.printf("%d\t",num_2048);
00111         pc.printf("%f\n",t.read());
00112     }
00113     
00114     pc.printf("==== Test Complete ====\n");
00115 }