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.

Dependencies:   mbed TextLCD

Committer:
jack1930
Date:
Wed Oct 27 16:37:51 2021 +0000
Revision:
1:1a6fd44ce241
Parent:
0:8f6ff5bb5a2d
STM32L152

Who changed what in which revision?

UserRevisionLine numberNew 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);
jack1930 1:1a6fd44ce241 8 //PwmOut backlight(D10); // Backlight LCD
werwolf_lg 0:8f6ff5bb5a2d 9 DigitalOut led(LED1);
werwolf_lg 0:8f6ff5bb5a2d 10
jack1930 1:1a6fd44ce241 11
werwolf_lg 0:8f6ff5bb5a2d 12 int main() {
werwolf_lg 0:8f6ff5bb5a2d 13
werwolf_lg 0:8f6ff5bb5a2d 14 // Set backlight period and duty cycle
jack1930 1:1a6fd44ce241 15 //backlight.period(0.002);
jack1930 1:1a6fd44ce241 16 //backlight = 1;
jack1930 1:1a6fd44ce241 17
werwolf_lg 0:8f6ff5bb5a2d 18
werwolf_lg 0:8f6ff5bb5a2d 19 lcd.cls(); // Clear LCD
werwolf_lg 0:8f6ff5bb5a2d 20 lcd.locate(1,0); // Set locate (1 row, 2 column)
werwolf_lg 0:8f6ff5bb5a2d 21 lcd.printf("LCD Key Shield");
werwolf_lg 0:8f6ff5bb5a2d 22 wait(1);
werwolf_lg 0:8f6ff5bb5a2d 23
werwolf_lg 0:8f6ff5bb5a2d 24 int meas;
werwolf_lg 0:8f6ff5bb5a2d 25
werwolf_lg 0:8f6ff5bb5a2d 26 while(1) {
werwolf_lg 0:8f6ff5bb5a2d 27 led = (led == 1) ? 0 : 1;
werwolf_lg 0:8f6ff5bb5a2d 28
jack1930 1:1a6fd44ce241 29 meas = button * 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 30
werwolf_lg 0:8f6ff5bb5a2d 31 lcd.cls();
werwolf_lg 0:8f6ff5bb5a2d 32 lcd.locate(0,0);
werwolf_lg 0:8f6ff5bb5a2d 33 lcd.printf("Press button");
werwolf_lg 0:8f6ff5bb5a2d 34 lcd.locate(0,1);
werwolf_lg 0:8f6ff5bb5a2d 35
werwolf_lg 0:8f6ff5bb5a2d 36 if (meas < 50) {
werwolf_lg 0:8f6ff5bb5a2d 37 lcd.printf("BUTTON: Right ");
jack1930 1:1a6fd44ce241 38 //backlight = 0.5; // Set 50% backlight
werwolf_lg 0:8f6ff5bb5a2d 39 }
jack1930 1:1a6fd44ce241 40 else if (meas < 250) {
werwolf_lg 0:8f6ff5bb5a2d 41 lcd.printf("BUTTON: Up");
jack1930 1:1a6fd44ce241 42 //backlight = 1; // Power ON backlight
werwolf_lg 0:8f6ff5bb5a2d 43 }
jack1930 1:1a6fd44ce241 44 else if (meas < 530){
werwolf_lg 0:8f6ff5bb5a2d 45 lcd.printf("BUTTON: Down");
jack1930 1:1a6fd44ce241 46 //backlight = 0; // Power OFF backlight
werwolf_lg 0:8f6ff5bb5a2d 47 }
jack1930 1:1a6fd44ce241 48 else if (meas < 780&&meas>735){
werwolf_lg 0:8f6ff5bb5a2d 49 lcd.printf("BUTTON: Left");
werwolf_lg 0:8f6ff5bb5a2d 50 }
werwolf_lg 0:8f6ff5bb5a2d 51 else if (meas > 950){
werwolf_lg 0:8f6ff5bb5a2d 52 lcd.printf("BUTTON: Select");
werwolf_lg 0:8f6ff5bb5a2d 53 }
werwolf_lg 0:8f6ff5bb5a2d 54 pc.printf("BUTTON: %4d\n", meas);
werwolf_lg 0:8f6ff5bb5a2d 55
werwolf_lg 0:8f6ff5bb5a2d 56 wait(0.1);
werwolf_lg 0:8f6ff5bb5a2d 57 }
werwolf_lg 0:8f6ff5bb5a2d 58 }