Skip to content

Commit 5c4a342

Browse files
committed
add lcd display
1 parent 324d44e commit 5c4a342

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ stepper.py : use 28BYJ-48 5V DC stepper
3535
sonic.py : use HC-SR04 to measure distance
3636
Pin:D5, D6
3737

38+
lcd.py : show message on 2 line LCD Display
39+
Pin:D1, D2, D5, D6, D7, D8
3840

3941
```

lcd.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from pyb import Pin
2+
from time import sleep_ms, sleep_us, ticks_us
3+
from machine import Timer
4+
5+
RS = Pin(5, Pin.OUT)
6+
E = Pin(4, Pin.OUT)
7+
D4 = Pin(14, Pin.OUT)
8+
D5 = Pin(12, Pin.OUT)
9+
D6 = Pin(13, Pin.OUT)
10+
D7 = Pin(15, Pin.OUT)
11+
12+
# Define some device constants
13+
WIDTH = 16 # Maximum characters per line
14+
CHR = True
15+
CMD = False
16+
LINE1 = 0x80 # LCD RAM address for the 1st line
17+
LINE2 = 0xC0 # LCD RAM address for the 2nd line
18+
DELAY = 15 #ms
19+
20+
def lcd_enable():
21+
E.value(1)
22+
sleep_ms(DELAY)
23+
E.value(0)
24+
sleep_ms(DELAY)
25+
26+
def lcd_byte(bits, Mode=True): #CMD:False, CHR=True
27+
E.value(0)
28+
RS.value(1 if Mode else 0)
29+
D7.value(1 if (0x80 & bits) else 0)
30+
D6.value(1 if (0x40 & bits) else 0)
31+
D5.value(1 if (0x20 & bits) else 0)
32+
D4.value(1 if (0x10 & bits) else 0)
33+
# print("%s %02x %d %d %d %d" % (str(chr), bits,D4.value(), D5.value(), D6.value(), D7.value()))
34+
lcd_enable()
35+
#
36+
E.value(0)
37+
RS.value(1 if Mode else 0)
38+
D7.value(1 if (0x08 & bits) else 0)
39+
D6.value(1 if (0x04 & bits) else 0)
40+
D5.value(1 if (0x02 & bits) else 0)
41+
D4.value(1 if (0x01 & bits) else 0)
42+
# print("%d %d %d %d" % (D4.value(), D5.value(), D6.value(), D7.value()))
43+
lcd_enable()
44+
45+
def lcd_init():
46+
sleep_ms(DELAY)
47+
lcd_byte(0x33, CMD)
48+
lcd_byte(0x32, CMD)
49+
lcd_byte(0x06, CMD)
50+
lcd_byte(0x0C, CMD)
51+
lcd_byte(0x28, CMD)
52+
lcd_clear()
53+
54+
def lcd_clear():
55+
lcd_byte(0x01, CMD)
56+
lcd_byte(LINE1, CMD)
57+
58+
def lcd_str(msg, line=LINE1):
59+
lcd_byte(line, CMD)
60+
if len(msg) > WIDTH:
61+
print("over length")
62+
return
63+
for i in range(len(msg)):
64+
lcd_byte(ord(msg[i]), CHR)
65+
66+
if __name__ == '__main__':
67+
lcd_init()
68+
lcd_str("Hello World!", LINE1)
69+
lcd_str("2nd line.", LINE2)
70+
71+
#

0 commit comments

Comments
 (0)