forked from no3z/ScratchTJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lcd.py
More file actions
73 lines (62 loc) · 1.9 KB
/
test_lcd.py
File metadata and controls
73 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import RPi.GPIO as GPIO
from time import sleep
# LCD pin setup (using BCM numbering)
LCD_RS = 17 # Register Select
LCD_E = 27 # Enable
LCD_D4 = 25
LCD_D5 = 22
LCD_D6 = 16
LCD_D7 = 24
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True # Character mode
LCD_CMD = False # Command mode
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(LCD_E, GPIO.OUT)
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)
def lcd_byte(bits, mode):
GPIO.output(LCD_RS, mode)
# High nibble
GPIO.output(LCD_D4, bits & 0x10 == 0x10)
GPIO.output(LCD_D5, bits & 0x20 == 0x20)
GPIO.output(LCD_D6, bits & 0x40 == 0x40)
GPIO.output(LCD_D7, bits & 0x80 == 0x80)
lcd_toggle_enable()
# Low nibble
GPIO.output(LCD_D4, bits & 0x01 == 0x01)
GPIO.output(LCD_D5, bits & 0x02 == 0x02)
GPIO.output(LCD_D6, bits & 0x04 == 0x04)
GPIO.output(LCD_D7, bits & 0x08 == 0x08)
lcd_toggle_enable()
def lcd_toggle_enable():
sleep(E_DELAY)
GPIO.output(LCD_E, True)
sleep(E_PULSE)
GPIO.output(LCD_E, False)
sleep(E_DELAY)
def lcd_init():
# LCD Initialization sequence
lcd_byte(0x33, LCD_CMD) # Initialize
lcd_byte(0x32, LCD_CMD) # Set to 4-bit mode
lcd_byte(0x28, LCD_CMD) # 2 line, 5x7 matrix
lcd_byte(0x0C, LCD_CMD) # Display on, Cursor off, Blink off
lcd_byte(0x06, LCD_CMD) # Increment cursor
lcd_byte(0x01, LCD_CMD) # Clear display (additional delay needed here)
sleep(0.003) # Clear display needs a longer delay
def lcd_string(message):
message = message.ljust(LCD_WIDTH, " ") # Pad to ensure 16 characters
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]), LCD_CHR)
try:
lcd_init()
lcd_string("Hello, World!")
sleep(3)
finally:
GPIO.cleanup()