Solutions for the Analog Input experiments for LPC812 MAX
Dependencies: lpc812_exp_lib_PCF8591 mbed
Revision 0:24709c18bbe0, committed 2013-11-22
- Comitter:
- embeddedartists
- Date:
- Fri Nov 22 10:31:23 2013 +0000
- Commit message:
- First version
Changed in this revision
diff -r 000000000000 -r 24709c18bbe0 lpc812_exp_lib_PCF8591.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lpc812_exp_lib_PCF8591.lib Fri Nov 22 10:31:23 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/embeddedartists/code/lpc812_exp_lib_PCF8591/#53bf66c0e0f6
diff -r 000000000000 -r 24709c18bbe0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Nov 22 10:31:23 2013 +0000 @@ -0,0 +1,107 @@ +#include "mbed.h" +#include "PCF8591.h" + +Serial pc(USBTX, USBRX); // tx, rx + +PCF8591 adc; + +float getTrimpotValue() +{ + // read a value + int v = adc.read(PCF8591::A0); + + // convert it from 0..255 to 0-3.3V + float b = v; + return (b / 256) * 3.3; +} + +static void experiment1() +{ + pc.printf("Analog input tests\n"); + while(1) { + pc.printf("%f V\n", getTrimpotValue()); + wait(0.25); + } +} + +int getDigit() +{ + // read a value + int v = adc.read(PCF8591::A0); + + // convert it from 0..255 to 0-9 + return (10 * v) / 256; +} + +static void experiment2() +{ + pc.printf("Digit input tests\n"); + while(1) { + pc.printf("%d\n", getDigit()); + wait(0.25); + } +} + +int getLightSensorValue(bool filter) +{ + static int lastValue = -1; + + // read a value + int v = adc.read(PCF8591::A1); + + if (lastValue == -1) { + lastValue = v; + } + + // apply filter + if (filter) { + lastValue = ((7*lastValue) + v) >> 3; + } else { + lastValue = v; + } + + return lastValue; +} + +static void experiment3_alt1() +{ + pc.printf("Light sensor tests - unfiltered\n"); + while(1) { + pc.printf("%d\n", getLightSensorValue(false)); + wait(0.25); + } +} + +static void experiment3_alt2() +{ + pc.printf("Light sensor tests - filtered\n"); + while(1) { + pc.printf("%d\n", getLightSensorValue(true)); + wait(0.25); + } +} + +static void experiment4() +{ + int lastValue = -1000; + + pc.printf("Trimpot tests with threshold\n"); + while(1) { + // read a value + int value = adc.read(PCF8591::A0); + if (abs(value - lastValue) >= 5) { + pc.printf("%d\n", value); + lastValue = value; + } + wait(0.1); + } +} + +int main() +{ + //experiment1(); // read 0-3.3V + //experiment2(); // read digits + //experiment3_alt1(); // read light sensor data + //experiment3_alt2(); // read filtered light sensor data + experiment4(); // read trimpot data, print only big changes +} \ No newline at end of file
diff -r 000000000000 -r 24709c18bbe0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Nov 22 10:31:23 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file