Mihail Stoyanov
/
ADC_spikes
Program to benchmark AnalogIn performance
Fork of ADC_spikes by
main.cpp@2:4c61969f2207, 2011-12-02 (annotated)
- Committer:
- chris
- Date:
- Fri Dec 02 11:42:51 2011 +0000
- Revision:
- 2:4c61969f2207
- Parent:
- 1:bdd134268a52
- Child:
- 3:4c45267b653d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:62a007afbcbf | 1 | #include "mbed.h" |
chris | 0:62a007afbcbf | 2 | |
chris | 1:bdd134268a52 | 3 | #define NUM_SAMPLES 500000 // size of sample series |
chris | 0:62a007afbcbf | 4 | |
chris | 0:62a007afbcbf | 5 | Serial pc(USBTX,USBRX); |
chris | 0:62a007afbcbf | 6 | |
chris | 0:62a007afbcbf | 7 | AnalogIn ain(p17); |
chris | 0:62a007afbcbf | 8 | |
chris | 1:bdd134268a52 | 9 | // initialise error counters |
chris | 1:bdd134268a52 | 10 | int num_4; |
chris | 0:62a007afbcbf | 11 | int num_8; |
chris | 0:62a007afbcbf | 12 | int num_16; |
chris | 0:62a007afbcbf | 13 | int num_32; |
chris | 0:62a007afbcbf | 14 | int num_64; |
chris | 0:62a007afbcbf | 15 | int num_128; |
chris | 0:62a007afbcbf | 16 | int num_256; |
chris | 0:62a007afbcbf | 17 | int num_512; |
chris | 0:62a007afbcbf | 18 | int num_1024; |
chris | 0:62a007afbcbf | 19 | int num_2048; |
chris | 0:62a007afbcbf | 20 | |
chris | 2:4c61969f2207 | 21 | |
chris | 0:62a007afbcbf | 22 | int main() { |
chris | 0:62a007afbcbf | 23 | |
chris | 0:62a007afbcbf | 24 | pc.baud(115200); |
chris | 1:bdd134268a52 | 25 | |
chris | 0:62a007afbcbf | 26 | int num_samples = 0; |
chris | 0:62a007afbcbf | 27 | |
chris | 1:bdd134268a52 | 28 | num_4 = 0; |
chris | 0:62a007afbcbf | 29 | num_8 = 0; |
chris | 0:62a007afbcbf | 30 | num_16 = 0; |
chris | 0:62a007afbcbf | 31 | num_32 = 0; |
chris | 0:62a007afbcbf | 32 | num_64 = 0; |
chris | 0:62a007afbcbf | 33 | num_128 = 0; |
chris | 0:62a007afbcbf | 34 | num_256 = 0; |
chris | 0:62a007afbcbf | 35 | num_512 = 0; |
chris | 0:62a007afbcbf | 36 | num_1024 = 0; |
chris | 0:62a007afbcbf | 37 | num_2048 = 0; |
chris | 0:62a007afbcbf | 38 | |
chris | 1:bdd134268a52 | 39 | // Take the average over 500,000 samples |
chris | 2:4c61969f2207 | 40 | // This is because the bias to 1.65v has a tolerance |
chris | 0:62a007afbcbf | 41 | float AVERAGE = 0.5; |
chris | 0:62a007afbcbf | 42 | |
chris | 0:62a007afbcbf | 43 | pc.printf("Taking an average over %d samples\n",NUM_SAMPLES); |
chris | 0:62a007afbcbf | 44 | while (num_samples < NUM_SAMPLES) { |
chris | 1:bdd134268a52 | 45 | float r = ain.read(); |
chris | 1:bdd134268a52 | 46 | |
chris | 2:4c61969f2207 | 47 | if ((r > 0.45) && (r < 0.55)) { |
chris | 2:4c61969f2207 | 48 | AVERAGE += r; |
chris | 1:bdd134268a52 | 49 | num_samples++; |
chris | 1:bdd134268a52 | 50 | } |
chris | 0:62a007afbcbf | 51 | } |
chris | 2:4c61969f2207 | 52 | |
chris | 2:4c61969f2207 | 53 | AVERAGE /= NUM_SAMPLES; |
chris | 2:4c61969f2207 | 54 | |
chris | 1:bdd134268a52 | 55 | num_samples = 0; |
chris | 0:62a007afbcbf | 56 | pc.printf("Average = %f\n",AVERAGE); |
chris | 0:62a007afbcbf | 57 | |
chris | 2:4c61969f2207 | 58 | |
chris | 1:bdd134268a52 | 59 | // Now start sampling series of 500,000 |
chris | 2:4c61969f2207 | 60 | // acculumating the errors seen in each range |
chris | 1:bdd134268a52 | 61 | while (1) { |
chris | 0:62a007afbcbf | 62 | |
chris | 1:bdd134268a52 | 63 | for (int i = 0; i < NUM_SAMPLES ; i++) { |
chris | 1:bdd134268a52 | 64 | float a = ain.read(); |
chris | 0:62a007afbcbf | 65 | |
chris | 1:bdd134268a52 | 66 | if (a > (AVERAGE + 0.5000)) { |
chris | 1:bdd134268a52 | 67 | num_2048++; // > 2048 lsb |
chris | 1:bdd134268a52 | 68 | } else if (a > (AVERAGE + 0.2500)) { |
chris | 1:bdd134268a52 | 69 | num_1024++; // > 1024 lsb |
chris | 1:bdd134268a52 | 70 | } else if (a > (AVERAGE + 0.0625)) { |
chris | 1:bdd134268a52 | 71 | num_512++; // > 512 lsb |
chris | 1:bdd134268a52 | 72 | } else if (a > (AVERAGE + 0.0312)) { |
chris | 1:bdd134268a52 | 73 | num_256++; // > 256 lsb |
chris | 1:bdd134268a52 | 74 | } else if (a > (AVERAGE + 0.0312)) { |
chris | 1:bdd134268a52 | 75 | num_128++; // > 128 lsb |
chris | 1:bdd134268a52 | 76 | } else if (a > (AVERAGE + 0.0156)) { |
chris | 1:bdd134268a52 | 77 | num_64++; // > 64 lsb |
chris | 1:bdd134268a52 | 78 | } else if (a > (AVERAGE + 0.0078)) { |
chris | 1:bdd134268a52 | 79 | num_32++; // > 32 lsb |
chris | 1:bdd134268a52 | 80 | } else if (a > (AVERAGE + 0.0039)) { |
chris | 1:bdd134268a52 | 81 | num_16++; // > 16 lsb |
chris | 1:bdd134268a52 | 82 | } else if (a > (AVERAGE + 0.0019)) { |
chris | 1:bdd134268a52 | 83 | num_8++; // > 8 lsb |
chris | 1:bdd134268a52 | 84 | } else if (a > (AVERAGE + 0.0009)) { |
chris | 1:bdd134268a52 | 85 | num_4++; // > 8 lsb |
chris | 1:bdd134268a52 | 86 | } |
chris | 1:bdd134268a52 | 87 | |
chris | 1:bdd134268a52 | 88 | num_samples++; |
chris | 1:bdd134268a52 | 89 | } |
chris | 1:bdd134268a52 | 90 | |
chris | 1:bdd134268a52 | 91 | // Every 500,000 print the results |
chris | 1:bdd134268a52 | 92 | pc.printf("%d\t",num_samples); |
chris | 1:bdd134268a52 | 93 | pc.printf("%d\t",num_4); |
chris | 1:bdd134268a52 | 94 | pc.printf("%d\t",num_8); |
chris | 1:bdd134268a52 | 95 | pc.printf("%d\t",num_16); |
chris | 1:bdd134268a52 | 96 | pc.printf("%d\t",num_32); |
chris | 1:bdd134268a52 | 97 | pc.printf("%d\t",num_64); |
chris | 1:bdd134268a52 | 98 | pc.printf("%d\t",num_128); |
chris | 1:bdd134268a52 | 99 | pc.printf("%d\t",num_256); |
chris | 1:bdd134268a52 | 100 | pc.printf("%d\t",num_512); |
chris | 1:bdd134268a52 | 101 | pc.printf("%d\t",num_1024); |
chris | 1:bdd134268a52 | 102 | pc.printf("%d\n",num_2048); |
chris | 0:62a007afbcbf | 103 | } |
chris | 0:62a007afbcbf | 104 | } |