To test the analog input noise using potential divider rule. {{/media/uploads/yoonghm/analog_noise.jpg}}
main.cpp@0:93964709b7a6, 2011-12-06 (annotated)
- Committer:
- yoonghm
- Date:
- Tue Dec 06 16:23:00 2011 +0000
- Revision:
- 0:93964709b7a6
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yoonghm | 0:93964709b7a6 | 1 | /* |
yoonghm | 0:93964709b7a6 | 2 | * The program try to test the phenomenon as mentioned in |
yoonghm | 0:93964709b7a6 | 3 | * http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/ |
yoonghm | 0:93964709b7a6 | 4 | * |
yoonghm | 0:93964709b7a6 | 5 | * a) Unused ADC pins are either tied to ground, or declared as DigitalOut |
yoonghm | 0:93964709b7a6 | 6 | * b) Quality of signal source, including low noise design techniques such |
yoonghm | 0:93964709b7a6 | 7 | * as filtering. |
yoonghm | 0:93964709b7a6 | 8 | * c) Reduce debugging communication via USB |
yoonghm | 0:93964709b7a6 | 9 | */ |
yoonghm | 0:93964709b7a6 | 10 | |
yoonghm | 0:93964709b7a6 | 11 | #include "mbed.h" |
yoonghm | 0:93964709b7a6 | 12 | |
yoonghm | 0:93964709b7a6 | 13 | #define SSIZE 100 |
yoonghm | 0:93964709b7a6 | 14 | |
yoonghm | 0:93964709b7a6 | 15 | AnalogIn ain(p20); |
yoonghm | 0:93964709b7a6 | 16 | BusOut unused(p15,p16,p17,p18,p19); // Make unused analog pin to DigitalOut |
yoonghm | 0:93964709b7a6 | 17 | |
yoonghm | 0:93964709b7a6 | 18 | float value[SSIZE]; |
yoonghm | 0:93964709b7a6 | 19 | float max; |
yoonghm | 0:93964709b7a6 | 20 | float min; |
yoonghm | 0:93964709b7a6 | 21 | double itg; |
yoonghm | 0:93964709b7a6 | 22 | |
yoonghm | 0:93964709b7a6 | 23 | void printNow() |
yoonghm | 0:93964709b7a6 | 24 | { |
yoonghm | 0:93964709b7a6 | 25 | time_t s; |
yoonghm | 0:93964709b7a6 | 26 | char buffer[20]; |
yoonghm | 0:93964709b7a6 | 27 | |
yoonghm | 0:93964709b7a6 | 28 | s = time(NULL); |
yoonghm | 0:93964709b7a6 | 29 | strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&s)); |
yoonghm | 0:93964709b7a6 | 30 | printf("%s\n", buffer); |
yoonghm | 0:93964709b7a6 | 31 | } |
yoonghm | 0:93964709b7a6 | 32 | |
yoonghm | 0:93964709b7a6 | 33 | void printSecNow() |
yoonghm | 0:93964709b7a6 | 34 | { |
yoonghm | 0:93964709b7a6 | 35 | time_t s; |
yoonghm | 0:93964709b7a6 | 36 | s = time(NULL); |
yoonghm | 0:93964709b7a6 | 37 | printf("%d\n", s); |
yoonghm | 0:93964709b7a6 | 38 | } |
yoonghm | 0:93964709b7a6 | 39 | |
yoonghm | 0:93964709b7a6 | 40 | int main() |
yoonghm | 0:93964709b7a6 | 41 | { |
yoonghm | 0:93964709b7a6 | 42 | set_time(1323180910); |
yoonghm | 0:93964709b7a6 | 43 | |
yoonghm | 0:93964709b7a6 | 44 | while (1) |
yoonghm | 0:93964709b7a6 | 45 | { |
yoonghm | 0:93964709b7a6 | 46 | //printNow(); |
yoonghm | 0:93964709b7a6 | 47 | printf("\n-----------------\n"); |
yoonghm | 0:93964709b7a6 | 48 | printSecNow(); |
yoonghm | 0:93964709b7a6 | 49 | max = itg = 0.0; |
yoonghm | 0:93964709b7a6 | 50 | min = 3.3; |
yoonghm | 0:93964709b7a6 | 51 | // Sample before printing to avoid debug communucation |
yoonghm | 0:93964709b7a6 | 52 | for (int i=0; i<SSIZE; i++) |
yoonghm | 0:93964709b7a6 | 53 | { |
yoonghm | 0:93964709b7a6 | 54 | value[i] = ain; |
yoonghm | 0:93964709b7a6 | 55 | if (value[i] > max) max = value[i]; |
yoonghm | 0:93964709b7a6 | 56 | if (value[i] < min) min = value[i]; |
yoonghm | 0:93964709b7a6 | 57 | itg += value[i]; |
yoonghm | 0:93964709b7a6 | 58 | } |
yoonghm | 0:93964709b7a6 | 59 | printSecNow(); |
yoonghm | 0:93964709b7a6 | 60 | |
yoonghm | 0:93964709b7a6 | 61 | // Now print out |
yoonghm | 0:93964709b7a6 | 62 | for (int i=0; i<SSIZE; i++) |
yoonghm | 0:93964709b7a6 | 63 | { |
yoonghm | 0:93964709b7a6 | 64 | printf("%0.3f ", value[i]*3.3); |
yoonghm | 0:93964709b7a6 | 65 | } |
yoonghm | 0:93964709b7a6 | 66 | printf("\n"); |
yoonghm | 0:93964709b7a6 | 67 | printf("max=%0.3f, min=%0.3f, avg=%0.3f\n", |
yoonghm | 0:93964709b7a6 | 68 | max*3.3, min*3.3, 3.3*itg/SSIZE); |
yoonghm | 0:93964709b7a6 | 69 | wait(10); |
yoonghm | 0:93964709b7a6 | 70 | } |
yoonghm | 0:93964709b7a6 | 71 | } |