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:
- 0:62a007afbcbf
- Child:
- 1:bdd134268a52
diff -r 000000000000 -r 62a007afbcbf main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Dec 01 16:37:07 2011 +0000
@@ -0,0 +1,130 @@
+#include "mbed.h"
+
+#define NUM_SAMPLES 500000
+
+DigitalOut myled(LED1);
+Serial pc(USBTX,USBRX);
+
+AnalogIn ain(p17);
+
+Timer t;
+
+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_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;
+
+/*
+ while (1) {
+ pc.printf("%f\n",ain.read());
+// wait(0.1);
+ }
+*/
+
+
+
+
+// do a calibration
+
+ 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++;
+ }
+
+ }
+ pc.printf("Average = %f\n",AVERAGE);
+
+
+
+ pc.printf("Reading floats\n");
+ num_samples = 0;
+ t.reset();
+ t.start();
+
+ while (num_samples < NUM_SAMPLES) {
+ 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++;
+ }
+
+ 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());
+
+*/
+
+
+}
