
Test the PWM functionality in conjunction with an analog input - drive the brightness of a LCD backlight with the ambient light level. Serves as testcase of a PWM problem, see code comment.
main.cpp@0:708a1d284406, 2011-01-19 (annotated)
- Committer:
- hlipka
- Date:
- Wed Jan 19 19:46:05 2011 +0000
- Revision:
- 0:708a1d284406
initial test version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:708a1d284406 | 1 | #include "mbed.h" |
hlipka | 0:708a1d284406 | 2 | |
hlipka | 0:708a1d284406 | 3 | #include "math.h" |
hlipka | 0:708a1d284406 | 4 | |
hlipka | 0:708a1d284406 | 5 | /* |
hlipka | 0:708a1d284406 | 6 | read the light value from a photo transistor connected to P20 and |
hlipka | 0:708a1d284406 | 7 | drive a LCDs LED backlight connected to P25. |
hlipka | 0:708a1d284406 | 8 | The backlight should dimm in the dark and brighten up when the lights go on. |
hlipka | 0:708a1d284406 | 9 | |
hlipka | 0:708a1d284406 | 10 | NOTE: the version below doesn't work, remove the printf in line 35! |
hlipka | 0:708a1d284406 | 11 | */ |
hlipka | 0:708a1d284406 | 12 | int main() { |
hlipka | 0:708a1d284406 | 13 | DigitalOut led1(LED1); |
hlipka | 0:708a1d284406 | 14 | float f=0.0; |
hlipka | 0:708a1d284406 | 15 | PwmOut display(p25); |
hlipka | 0:708a1d284406 | 16 | display.period_ms(1); |
hlipka | 0:708a1d284406 | 17 | display=f; |
hlipka | 0:708a1d284406 | 18 | |
hlipka | 0:708a1d284406 | 19 | AnalogIn light(p20); |
hlipka | 0:708a1d284406 | 20 | |
hlipka | 0:708a1d284406 | 21 | for (int i=0;i<100;i++) |
hlipka | 0:708a1d284406 | 22 | { |
hlipka | 0:708a1d284406 | 23 | f=f+0.01; |
hlipka | 0:708a1d284406 | 24 | display=f; |
hlipka | 0:708a1d284406 | 25 | wait (0.03); |
hlipka | 0:708a1d284406 | 26 | } |
hlipka | 0:708a1d284406 | 27 | printf("%f\n",display.read()); |
hlipka | 0:708a1d284406 | 28 | led1=1; |
hlipka | 0:708a1d284406 | 29 | |
hlipka | 0:708a1d284406 | 30 | while (true) |
hlipka | 0:708a1d284406 | 31 | { |
hlipka | 0:708a1d284406 | 32 | double d=light; |
hlipka | 0:708a1d284406 | 33 | double sq=pow(d,0.6); |
hlipka | 0:708a1d284406 | 34 | display.write(sq*3); |
hlipka | 0:708a1d284406 | 35 | printf("%f / %f\n",d,sq); |
hlipka | 0:708a1d284406 | 36 | wait(1); |
hlipka | 0:708a1d284406 | 37 | } |
hlipka | 0:708a1d284406 | 38 | } |