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.
Fork of ADC_spikes by
Diff: main.cpp
- Revision:
- 1:bdd134268a52
- Parent:
- 0:62a007afbcbf
- Child:
- 2:4c61969f2207
--- a/main.cpp Thu Dec 01 16:37:07 2011 +0000
+++ b/main.cpp Fri Dec 02 10:04:15 2011 +0000
@@ -1,14 +1,13 @@
#include "mbed.h"
-#define NUM_SAMPLES 500000
+#define NUM_SAMPLES 500000 // size of sample series
-DigitalOut myled(LED1);
Serial pc(USBTX,USBRX);
AnalogIn ain(p17);
-Timer t;
-
+// initialise error counters
+int num_4;
int num_8;
int num_16;
int num_32;
@@ -19,13 +18,13 @@
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;
@@ -36,95 +35,67 @@
num_1024 = 0;
num_2048 = 0;
-/*
- while (1) {
- pc.printf("%f\n",ain.read());
-// wait(0.1);
- }
-*/
-
-
-
-// do a calibration
-
+// Take the average over 500,000 samples
+// This is because teh bias to 1.65v has a tolerance
float AVERAGE = 0.5;
- // taking an average
pc.printf("Taking an average over %d samples\n",NUM_SAMPLES);
while (num_samples < NUM_SAMPLES) {
- float r = ain.read();
-
- if ((r > 0.49) && (r < 0.51)) {
- AVERAGE = (AVERAGE + r) /2.0;
- num_samples++;
- }
-
+ float r = ain.read();
+
+ if ((r > 0.49) && (r < 0.51)) {
+ AVERAGE = (AVERAGE + r) /2.0;
+ 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) {
- pc.printf("Reading floats\n");
- num_samples = 0;
- t.reset();
- t.start();
-
- while (num_samples < NUM_SAMPLES) {
- float a = ain.read();
-
+ 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
-
- num_samples++;
+ 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);
}
-
- t.stop();
- pc.printf("Time taken for %d samples was %fs\n",NUM_SAMPLES,t.read());
- pc.printf("over 8 lsb = %d\n",num_8);
- pc.printf("over 16 lsb = %d\n",num_16);
- pc.printf("over 32 lsb = %d\n",num_32);
- pc.printf("over 64 lsb = %d\n",num_64);
- pc.printf("over 128 lsb = %d\n",num_128);
- pc.printf("over 256 lsb = %d\n",num_256);
- pc.printf("over 512 lsb = %d\n",num_512);
- pc.printf("over 1024 lsb = %d\n",num_1024);
- pc.printf("over 2048 lsb = %d\n",num_2048);
-
-
-
-
-
-/*
- pc.printf("Reading _u16\n");
- num_samples = 0;
- t.reset();
- t.start();
- while (num_samples < NUM_SAMPLES) {
- int a = ain.read_u16();
- num_samples++;
- }
- t.stop();
- pc.printf("Time taken for %d samples was %fs\n",NUM_SAMPLES,t.read());
-
-*/
-
-
}
