Program to benchmark AnalogIn performance

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
chris
Date:
Fri Dec 02 13:51:06 2011 +0000
Parent:
2:4c61969f2207
Commit message:

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 4c61969f2207 -r 4c45267b653d main.cpp
--- a/main.cpp	Fri Dec 02 11:42:51 2011 +0000
+++ b/main.cpp	Fri Dec 02 13:51:06 2011 +0000
@@ -1,104 +1,115 @@
-#include "mbed.h"
-
-#define NUM_SAMPLES 500000 // size of sample series
-
-Serial pc(USBTX,USBRX);
-
-AnalogIn ain(p17);
-
-// initialise error counters
-int num_4;
-int num_8;
-int num_16;
-int num_32;
-int num_64;
-int num_128;
-int num_256;
-int num_512;
-int num_1024;
-int num_2048;
-
-
-int main() {
-
-    pc.baud(115200);
-
-    int num_samples = 0;
-
-    num_4 = 0;
-    num_8 = 0;
-    num_16 = 0;
-    num_32 = 0;
-    num_64 = 0;
-    num_128 = 0;
-    num_256 = 0;
-    num_512 = 0;
-    num_1024 = 0;
-    num_2048 = 0;
-
-// Take the average over 500,000 samples
-// This is because the bias to 1.65v has a tolerance
-    float AVERAGE = 0.5;
-
-    pc.printf("Taking an average over %d samples\n",NUM_SAMPLES);
-    while (num_samples < NUM_SAMPLES) {
-        float r = ain.read();
-
-        if ((r > 0.45) && (r < 0.55)) {
-            AVERAGE += r;
-            num_samples++;
-        }
-    }
-    
-    AVERAGE /= NUM_SAMPLES;
-    
-    num_samples = 0;
-    pc.printf("Average = %f\n",AVERAGE);
-
-
-// Now start sampling series of 500,000
-// acculumating the errors seen in each range
-    while (1) {
-
-        for (int i = 0; i < NUM_SAMPLES ; i++) {
-            float a = ain.read();
-
-            if (a > (AVERAGE + 0.5000)) {
-                num_2048++;    // > 2048 lsb
-            } else if (a > (AVERAGE + 0.2500)) {
-                num_1024++;    // > 1024 lsb
-            } else if (a > (AVERAGE + 0.0625)) {
-                num_512++;    // > 512 lsb
-            } else if (a > (AVERAGE + 0.0312)) {
-                num_256++;    // > 256 lsb
-            } else if (a > (AVERAGE + 0.0312)) {
-                num_128++;    // > 128 lsb
-            } else if (a > (AVERAGE + 0.0156)) {
-                num_64++;    // > 64 lsb
-            } else if (a > (AVERAGE + 0.0078)) {
-                num_32++;    // > 32 lsb
-            } else if (a > (AVERAGE + 0.0039)) {
-                num_16++;    // > 16 lsb
-            } else if (a > (AVERAGE + 0.0019)) {
-                num_8++;    // > 8 lsb
-            } else if (a > (AVERAGE + 0.0009)) {
-                num_4++;    // > 8 lsb
-            }
-
-            num_samples++;
-        }
-
-        // Every 500,000 print the results
-        pc.printf("%d\t",num_samples);
-        pc.printf("%d\t",num_4);
-        pc.printf("%d\t",num_8);
-        pc.printf("%d\t",num_16);
-        pc.printf("%d\t",num_32);
-        pc.printf("%d\t",num_64);
-        pc.printf("%d\t",num_128);
-        pc.printf("%d\t",num_256);
-        pc.printf("%d\t",num_512);
-        pc.printf("%d\t",num_1024);
-        pc.printf("%d\n",num_2048);
-    }
-}
+#include "mbed.h"
+
+#define NUM_SAMPLES 500000 // size of sample series
+#define SAMPLE_BLOCKS 5
+
+Timer t;
+
+Serial pc(USBTX,USBRX);
+
+AnalogIn ain(p20);
+
+// initialise error counters
+int num_4;
+int num_8;
+int num_16;
+int num_32;
+int num_64;
+int num_128;
+int num_256;
+int num_512;
+int num_1024;
+int num_2048;
+
+
+int main() {
+
+    pc.baud(115200);
+
+    int num_samples = 0;
+
+    num_4 = 0;
+    num_8 = 0;
+    num_16 = 0;
+    num_32 = 0;
+    num_64 = 0;
+    num_128 = 0;
+    num_256 = 0;
+    num_512 = 0;
+    num_1024 = 0;
+    num_2048 = 0;
+
+// Take the average over 500,000 samples
+// This is because the bias to 1.65v has a tolerance
+    float AVERAGE = 0.5;
+
+    pc.printf("Taking an average over %d samples\n",NUM_SAMPLES);
+    while (num_samples < NUM_SAMPLES) {
+        float r = ain.read();
+
+        if ((r > 0.45) && (r < 0.55)) {
+            AVERAGE += r;
+            num_samples++;
+        }
+    }
+    
+    AVERAGE /= NUM_SAMPLES;
+    
+    num_samples = 0;
+    pc.printf("Average = %f\n",AVERAGE);
+
+
+// Now start sampling series of 500,000
+// acculumating the errors seen in each range
+    pc.printf("Profiling %d samples\n",SAMPLE_BLOCKS*NUM_SAMPLES);
+    for (int j=0; j < SAMPLE_BLOCKS ; j++) {
+
+        t.reset();
+        t.start();
+        
+        for (int i = 0; i < NUM_SAMPLES ; i++) {
+            float a = ain.read();
+
+            if (a > (AVERAGE + 0.5000)) {
+                num_2048++;    // > 2048 lsb
+            } else if (a > (AVERAGE + 0.2500)) {
+                num_1024++;    // > 1024 lsb
+            } else if (a > (AVERAGE + 0.0625)) {
+                num_512++;    // > 512 lsb
+            } else if (a > (AVERAGE + 0.0312)) {
+                num_256++;    // > 256 lsb
+            } else if (a > (AVERAGE + 0.0312)) {
+                num_128++;    // > 128 lsb
+            } else if (a > (AVERAGE + 0.0156)) {
+                num_64++;    // > 64 lsb
+            } else if (a > (AVERAGE + 0.0078)) {
+                num_32++;    // > 32 lsb
+            } else if (a > (AVERAGE + 0.0039)) {
+                num_16++;    // > 16 lsb
+            } else if (a > (AVERAGE + 0.0019)) {
+                num_8++;    // > 8 lsb
+            } else if (a > (AVERAGE + 0.0009)) {
+                num_4++;    // > 8 lsb
+            }
+
+            num_samples++;
+        }
+        t.stop();
+        
+        // Every 500,000 print the results
+        pc.printf("%d\t",num_samples);
+        pc.printf("%d\t",num_4);
+        pc.printf("%d\t",num_8);
+        pc.printf("%d\t",num_16);
+        pc.printf("%d\t",num_32);
+        pc.printf("%d\t",num_64);
+        pc.printf("%d\t",num_128);
+        pc.printf("%d\t",num_256);
+        pc.printf("%d\t",num_512);
+        pc.printf("%d\t",num_1024);
+        pc.printf("%d\t",num_2048);
+        pc.printf("%f\n",t.read());
+    }
+    
+    pc.printf("==== Test Complete ====\n");
+}