Solutions for the Analog Input experiments for LPC812 MAX
Dependencies: lpc812_exp_lib_PCF8591 mbed
main.cpp@0:24709c18bbe0, 2013-11-22 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Nov 22 10:31:23 2013 +0000
- Revision:
- 0:24709c18bbe0
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:24709c18bbe0 | 1 | #include "mbed.h" |
embeddedartists | 0:24709c18bbe0 | 2 | #include "PCF8591.h" |
embeddedartists | 0:24709c18bbe0 | 3 | |
embeddedartists | 0:24709c18bbe0 | 4 | Serial pc(USBTX, USBRX); // tx, rx |
embeddedartists | 0:24709c18bbe0 | 5 | |
embeddedartists | 0:24709c18bbe0 | 6 | PCF8591 adc; |
embeddedartists | 0:24709c18bbe0 | 7 | |
embeddedartists | 0:24709c18bbe0 | 8 | float getTrimpotValue() |
embeddedartists | 0:24709c18bbe0 | 9 | { |
embeddedartists | 0:24709c18bbe0 | 10 | // read a value |
embeddedartists | 0:24709c18bbe0 | 11 | int v = adc.read(PCF8591::A0); |
embeddedartists | 0:24709c18bbe0 | 12 | |
embeddedartists | 0:24709c18bbe0 | 13 | // convert it from 0..255 to 0-3.3V |
embeddedartists | 0:24709c18bbe0 | 14 | float b = v; |
embeddedartists | 0:24709c18bbe0 | 15 | return (b / 256) * 3.3; |
embeddedartists | 0:24709c18bbe0 | 16 | } |
embeddedartists | 0:24709c18bbe0 | 17 | |
embeddedartists | 0:24709c18bbe0 | 18 | static void experiment1() |
embeddedartists | 0:24709c18bbe0 | 19 | { |
embeddedartists | 0:24709c18bbe0 | 20 | pc.printf("Analog input tests\n"); |
embeddedartists | 0:24709c18bbe0 | 21 | while(1) { |
embeddedartists | 0:24709c18bbe0 | 22 | pc.printf("%f V\n", getTrimpotValue()); |
embeddedartists | 0:24709c18bbe0 | 23 | wait(0.25); |
embeddedartists | 0:24709c18bbe0 | 24 | } |
embeddedartists | 0:24709c18bbe0 | 25 | } |
embeddedartists | 0:24709c18bbe0 | 26 | |
embeddedartists | 0:24709c18bbe0 | 27 | int getDigit() |
embeddedartists | 0:24709c18bbe0 | 28 | { |
embeddedartists | 0:24709c18bbe0 | 29 | // read a value |
embeddedartists | 0:24709c18bbe0 | 30 | int v = adc.read(PCF8591::A0); |
embeddedartists | 0:24709c18bbe0 | 31 | |
embeddedartists | 0:24709c18bbe0 | 32 | // convert it from 0..255 to 0-9 |
embeddedartists | 0:24709c18bbe0 | 33 | return (10 * v) / 256; |
embeddedartists | 0:24709c18bbe0 | 34 | } |
embeddedartists | 0:24709c18bbe0 | 35 | |
embeddedartists | 0:24709c18bbe0 | 36 | static void experiment2() |
embeddedartists | 0:24709c18bbe0 | 37 | { |
embeddedartists | 0:24709c18bbe0 | 38 | pc.printf("Digit input tests\n"); |
embeddedartists | 0:24709c18bbe0 | 39 | while(1) { |
embeddedartists | 0:24709c18bbe0 | 40 | pc.printf("%d\n", getDigit()); |
embeddedartists | 0:24709c18bbe0 | 41 | wait(0.25); |
embeddedartists | 0:24709c18bbe0 | 42 | } |
embeddedartists | 0:24709c18bbe0 | 43 | } |
embeddedartists | 0:24709c18bbe0 | 44 | |
embeddedartists | 0:24709c18bbe0 | 45 | int getLightSensorValue(bool filter) |
embeddedartists | 0:24709c18bbe0 | 46 | { |
embeddedartists | 0:24709c18bbe0 | 47 | static int lastValue = -1; |
embeddedartists | 0:24709c18bbe0 | 48 | |
embeddedartists | 0:24709c18bbe0 | 49 | // read a value |
embeddedartists | 0:24709c18bbe0 | 50 | int v = adc.read(PCF8591::A1); |
embeddedartists | 0:24709c18bbe0 | 51 | |
embeddedartists | 0:24709c18bbe0 | 52 | if (lastValue == -1) { |
embeddedartists | 0:24709c18bbe0 | 53 | lastValue = v; |
embeddedartists | 0:24709c18bbe0 | 54 | } |
embeddedartists | 0:24709c18bbe0 | 55 | |
embeddedartists | 0:24709c18bbe0 | 56 | // apply filter |
embeddedartists | 0:24709c18bbe0 | 57 | if (filter) { |
embeddedartists | 0:24709c18bbe0 | 58 | lastValue = ((7*lastValue) + v) >> 3; |
embeddedartists | 0:24709c18bbe0 | 59 | } else { |
embeddedartists | 0:24709c18bbe0 | 60 | lastValue = v; |
embeddedartists | 0:24709c18bbe0 | 61 | } |
embeddedartists | 0:24709c18bbe0 | 62 | |
embeddedartists | 0:24709c18bbe0 | 63 | return lastValue; |
embeddedartists | 0:24709c18bbe0 | 64 | } |
embeddedartists | 0:24709c18bbe0 | 65 | |
embeddedartists | 0:24709c18bbe0 | 66 | static void experiment3_alt1() |
embeddedartists | 0:24709c18bbe0 | 67 | { |
embeddedartists | 0:24709c18bbe0 | 68 | pc.printf("Light sensor tests - unfiltered\n"); |
embeddedartists | 0:24709c18bbe0 | 69 | while(1) { |
embeddedartists | 0:24709c18bbe0 | 70 | pc.printf("%d\n", getLightSensorValue(false)); |
embeddedartists | 0:24709c18bbe0 | 71 | wait(0.25); |
embeddedartists | 0:24709c18bbe0 | 72 | } |
embeddedartists | 0:24709c18bbe0 | 73 | } |
embeddedartists | 0:24709c18bbe0 | 74 | |
embeddedartists | 0:24709c18bbe0 | 75 | static void experiment3_alt2() |
embeddedartists | 0:24709c18bbe0 | 76 | { |
embeddedartists | 0:24709c18bbe0 | 77 | pc.printf("Light sensor tests - filtered\n"); |
embeddedartists | 0:24709c18bbe0 | 78 | while(1) { |
embeddedartists | 0:24709c18bbe0 | 79 | pc.printf("%d\n", getLightSensorValue(true)); |
embeddedartists | 0:24709c18bbe0 | 80 | wait(0.25); |
embeddedartists | 0:24709c18bbe0 | 81 | } |
embeddedartists | 0:24709c18bbe0 | 82 | } |
embeddedartists | 0:24709c18bbe0 | 83 | |
embeddedartists | 0:24709c18bbe0 | 84 | static void experiment4() |
embeddedartists | 0:24709c18bbe0 | 85 | { |
embeddedartists | 0:24709c18bbe0 | 86 | int lastValue = -1000; |
embeddedartists | 0:24709c18bbe0 | 87 | |
embeddedartists | 0:24709c18bbe0 | 88 | pc.printf("Trimpot tests with threshold\n"); |
embeddedartists | 0:24709c18bbe0 | 89 | while(1) { |
embeddedartists | 0:24709c18bbe0 | 90 | // read a value |
embeddedartists | 0:24709c18bbe0 | 91 | int value = adc.read(PCF8591::A0); |
embeddedartists | 0:24709c18bbe0 | 92 | if (abs(value - lastValue) >= 5) { |
embeddedartists | 0:24709c18bbe0 | 93 | pc.printf("%d\n", value); |
embeddedartists | 0:24709c18bbe0 | 94 | lastValue = value; |
embeddedartists | 0:24709c18bbe0 | 95 | } |
embeddedartists | 0:24709c18bbe0 | 96 | wait(0.1); |
embeddedartists | 0:24709c18bbe0 | 97 | } |
embeddedartists | 0:24709c18bbe0 | 98 | } |
embeddedartists | 0:24709c18bbe0 | 99 | |
embeddedartists | 0:24709c18bbe0 | 100 | int main() |
embeddedartists | 0:24709c18bbe0 | 101 | { |
embeddedartists | 0:24709c18bbe0 | 102 | //experiment1(); // read 0-3.3V |
embeddedartists | 0:24709c18bbe0 | 103 | //experiment2(); // read digits |
embeddedartists | 0:24709c18bbe0 | 104 | //experiment3_alt1(); // read light sensor data |
embeddedartists | 0:24709c18bbe0 | 105 | //experiment3_alt2(); // read filtered light sensor data |
embeddedartists | 0:24709c18bbe0 | 106 | experiment4(); // read trimpot data, print only big changes |
embeddedartists | 0:24709c18bbe0 | 107 | } |