Demo for button and backlight use with Arduino LCD Keypad Shield (HD44780) and similar 16x2 hardware clones. Press 'Up' or 'Down' button to ON or OFF backlight. Build and test with STM32 Nucleo F411RE.
main.cpp@0:8f6ff5bb5a2d, 2015-12-23 (annotated)
- Committer:
- werwolf_lg
- Date:
- Wed Dec 23 16:58:19 2015 +0000
- Revision:
- 0:8f6ff5bb5a2d
First version demo for Arduino LCD Keypad Shield and STM32 Nucleo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
werwolf_lg | 0:8f6ff5bb5a2d | 1 | #include "mbed.h" |
werwolf_lg | 0:8f6ff5bb5a2d | 2 | #include "TextLCD.h" |
werwolf_lg | 0:8f6ff5bb5a2d | 3 | |
werwolf_lg | 0:8f6ff5bb5a2d | 4 | Serial pc(SERIAL_TX, SERIAL_RX); |
werwolf_lg | 0:8f6ff5bb5a2d | 5 | AnalogIn button(A0); // Init button (SELECT, LEFT, UP, DOWN, RIGHT) |
werwolf_lg | 0:8f6ff5bb5a2d | 6 | // LCD (RS, E, D4, D5, D6, D7); |
werwolf_lg | 0:8f6ff5bb5a2d | 7 | TextLCD lcd(D8, D9, D4, D5, D6, D7); |
werwolf_lg | 0:8f6ff5bb5a2d | 8 | PwmOut backlight(D10); // Backlight LCD |
werwolf_lg | 0:8f6ff5bb5a2d | 9 | DigitalOut led(LED1); |
werwolf_lg | 0:8f6ff5bb5a2d | 10 | |
werwolf_lg | 0:8f6ff5bb5a2d | 11 | int main() { |
werwolf_lg | 0:8f6ff5bb5a2d | 12 | |
werwolf_lg | 0:8f6ff5bb5a2d | 13 | // Set backlight period and duty cycle |
werwolf_lg | 0:8f6ff5bb5a2d | 14 | backlight.period(0.002); |
werwolf_lg | 0:8f6ff5bb5a2d | 15 | backlight = 1; |
werwolf_lg | 0:8f6ff5bb5a2d | 16 | |
werwolf_lg | 0:8f6ff5bb5a2d | 17 | |
werwolf_lg | 0:8f6ff5bb5a2d | 18 | lcd.cls(); // Clear LCD |
werwolf_lg | 0:8f6ff5bb5a2d | 19 | lcd.locate(1,0); // Set locate (1 row, 2 column) |
werwolf_lg | 0:8f6ff5bb5a2d | 20 | lcd.printf("LCD Key Shield"); |
werwolf_lg | 0:8f6ff5bb5a2d | 21 | wait(1); |
werwolf_lg | 0:8f6ff5bb5a2d | 22 | |
werwolf_lg | 0:8f6ff5bb5a2d | 23 | int meas; |
werwolf_lg | 0:8f6ff5bb5a2d | 24 | |
werwolf_lg | 0:8f6ff5bb5a2d | 25 | while(1) { |
werwolf_lg | 0:8f6ff5bb5a2d | 26 | led = (led == 1) ? 0 : 1; |
werwolf_lg | 0:8f6ff5bb5a2d | 27 | |
werwolf_lg | 0:8f6ff5bb5a2d | 28 | meas = button.read() * 1000; // Read the analog input value (value from 0.0 to 1.0) and convert to int value (from 0 to 1000) |
werwolf_lg | 0:8f6ff5bb5a2d | 29 | |
werwolf_lg | 0:8f6ff5bb5a2d | 30 | lcd.cls(); |
werwolf_lg | 0:8f6ff5bb5a2d | 31 | lcd.locate(0,0); |
werwolf_lg | 0:8f6ff5bb5a2d | 32 | lcd.printf("Press button"); |
werwolf_lg | 0:8f6ff5bb5a2d | 33 | lcd.locate(0,1); |
werwolf_lg | 0:8f6ff5bb5a2d | 34 | |
werwolf_lg | 0:8f6ff5bb5a2d | 35 | if (meas < 50) { |
werwolf_lg | 0:8f6ff5bb5a2d | 36 | lcd.printf("BUTTON: Right "); |
werwolf_lg | 0:8f6ff5bb5a2d | 37 | backlight = 0.5; // Set 50% backlight |
werwolf_lg | 0:8f6ff5bb5a2d | 38 | } |
werwolf_lg | 0:8f6ff5bb5a2d | 39 | else if (meas < 210) { |
werwolf_lg | 0:8f6ff5bb5a2d | 40 | lcd.printf("BUTTON: Up"); |
werwolf_lg | 0:8f6ff5bb5a2d | 41 | backlight = 1; // Power ON backlight |
werwolf_lg | 0:8f6ff5bb5a2d | 42 | } |
werwolf_lg | 0:8f6ff5bb5a2d | 43 | else if (meas < 460){ |
werwolf_lg | 0:8f6ff5bb5a2d | 44 | lcd.printf("BUTTON: Down"); |
werwolf_lg | 0:8f6ff5bb5a2d | 45 | backlight = 0; // Power OFF backlight |
werwolf_lg | 0:8f6ff5bb5a2d | 46 | } |
werwolf_lg | 0:8f6ff5bb5a2d | 47 | else if (meas < 720){ |
werwolf_lg | 0:8f6ff5bb5a2d | 48 | lcd.printf("BUTTON: Left"); |
werwolf_lg | 0:8f6ff5bb5a2d | 49 | } |
werwolf_lg | 0:8f6ff5bb5a2d | 50 | else if (meas > 950){ |
werwolf_lg | 0:8f6ff5bb5a2d | 51 | lcd.printf("BUTTON: Select"); |
werwolf_lg | 0:8f6ff5bb5a2d | 52 | } |
werwolf_lg | 0:8f6ff5bb5a2d | 53 | pc.printf("BUTTON: %4d\n", meas); |
werwolf_lg | 0:8f6ff5bb5a2d | 54 | |
werwolf_lg | 0:8f6ff5bb5a2d | 55 | wait(0.1); |
werwolf_lg | 0:8f6ff5bb5a2d | 56 | } |
werwolf_lg | 0:8f6ff5bb5a2d | 57 | } |