Skip to content

Commit 5f719e2

Browse files
committed
Added support for 16x2 LCD scren for output messages
1 parent 4fa8b4e commit 5f719e2

1 file changed

Lines changed: 206 additions & 0 deletions

File tree

lcd_16x2.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#!/usr/bin/python
2+
#--------------------------------------
3+
# ___ ___ _ ____
4+
# / _ \/ _ \(_) __/__ __ __
5+
# / , _/ ___/ /\ \/ _ \/ // /
6+
# /_/|_/_/ /_/___/ .__/\_, /
7+
# /_/ /___/
8+
#
9+
# lcd_16x2.py
10+
# 16x2 LCD Test Script
11+
#
12+
# Author : Matt Hawkins
13+
# Date : 06/04/2015
14+
#
15+
# http://www.raspberrypi-spy.co.uk/
16+
#
17+
# Copyright 2015 Matt Hawkins
18+
#
19+
# This program is free software: you can redistribute it and/or modify
20+
# it under the terms of the GNU General Public License as published by
21+
# the Free Software Foundation, either version 3 of the License, or
22+
# (at your option) any later version.
23+
#
24+
# This program is distributed in the hope that it will be useful,
25+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
# GNU General Public License for more details.
28+
#
29+
# You should have received a copy of the GNU General Public License
30+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
31+
#
32+
#--------------------------------------
33+
34+
# The wiring for the LCD is as follows:
35+
# 1 : GND
36+
# 2 : 5V
37+
# 3 : Contrast (0-5V)*
38+
# 4 : RS (Register Select)
39+
# 5 : R/W (Read Write) - GROUND THIS PIN
40+
# 6 : Enable or Strobe
41+
# 7 : Data Bit 0 - NOT USED
42+
# 8 : Data Bit 1 - NOT USED
43+
# 9 : Data Bit 2 - NOT USED
44+
# 10: Data Bit 3 - NOT USED
45+
# 11: Data Bit 4
46+
# 12: Data Bit 5
47+
# 13: Data Bit 6
48+
# 14: Data Bit 7
49+
# 15: LCD Backlight +5V**
50+
# 16: LCD Backlight GND
51+
52+
#import
53+
import RPi.GPIO as GPIO
54+
import time
55+
56+
# Define GPIO to LCD mapping
57+
LCD_RS = 25
58+
LCD_E = 19
59+
LCD_D4 = 13
60+
LCD_D5 = 6
61+
LCD_D6 = 5
62+
LCD_D7 = 12
63+
64+
65+
# Define some device constants
66+
LCD_WIDTH = 16 # Maximum characters per line
67+
LCD_CHR = True
68+
LCD_CMD = False
69+
70+
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
71+
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
72+
73+
# Timing constants
74+
E_PULSE = 0.0005
75+
E_DELAY = 0.0005
76+
77+
def main():
78+
# Main program block
79+
80+
# Initialise display
81+
lcd_init()
82+
83+
while True:
84+
85+
# Send some test
86+
lcd_string("Rasbperry Pi",LCD_LINE_1)
87+
lcd_string("16x2 LCD Test",LCD_LINE_2)
88+
89+
time.sleep(3) # 3 second delay
90+
91+
# Send some text
92+
lcd_string("1234567890123456",LCD_LINE_1)
93+
lcd_string("abcdefghijklmnop",LCD_LINE_2)
94+
95+
96+
97+
time.sleep(3) # 3 second delay
98+
99+
# Send some text
100+
lcd_string("RaspberryPi-spy",LCD_LINE_1)
101+
lcd_string(".co.uk",LCD_LINE_2)
102+
103+
time.sleep(3)
104+
105+
# Send some text
106+
lcd_string("Follow me on",LCD_LINE_1)
107+
lcd_string("Twitter @RPiSpy",LCD_LINE_2)
108+
109+
time.sleep(3)
110+
111+
def lcd_init():
112+
113+
114+
GPIO.setwarnings(False)
115+
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
116+
GPIO.setup(LCD_E, GPIO.OUT) # E
117+
GPIO.setup(LCD_RS, GPIO.OUT) # RS
118+
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
119+
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
120+
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
121+
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
122+
123+
# Initialise display
124+
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
125+
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
126+
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
127+
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
128+
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
129+
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
130+
time.sleep(E_DELAY)
131+
132+
def lcd_byte(bits, mode):
133+
# Send byte to data pins
134+
# bits = data
135+
# mode = True for character
136+
# False for command
137+
138+
GPIO.output(LCD_RS, mode) # RS
139+
140+
# High bits
141+
GPIO.output(LCD_D4, False)
142+
GPIO.output(LCD_D5, False)
143+
GPIO.output(LCD_D6, False)
144+
GPIO.output(LCD_D7, False)
145+
if bits&0x10==0x10:
146+
GPIO.output(LCD_D4, True)
147+
if bits&0x20==0x20:
148+
GPIO.output(LCD_D5, True)
149+
if bits&0x40==0x40:
150+
GPIO.output(LCD_D6, True)
151+
if bits&0x80==0x80:
152+
GPIO.output(LCD_D7, True)
153+
154+
# Toggle 'Enable' pin
155+
lcd_toggle_enable()
156+
157+
# Low bits
158+
GPIO.output(LCD_D4, False)
159+
GPIO.output(LCD_D5, False)
160+
GPIO.output(LCD_D6, False)
161+
GPIO.output(LCD_D7, False)
162+
if bits&0x01==0x01:
163+
GPIO.output(LCD_D4, True)
164+
if bits&0x02==0x02:
165+
GPIO.output(LCD_D5, True)
166+
if bits&0x04==0x04:
167+
GPIO.output(LCD_D6, True)
168+
if bits&0x08==0x08:
169+
GPIO.output(LCD_D7, True)
170+
171+
# Toggle 'Enable' pin
172+
lcd_toggle_enable()
173+
174+
def lcd_toggle_enable():
175+
# Toggle enable
176+
time.sleep(E_DELAY)
177+
GPIO.output(LCD_E, True)
178+
time.sleep(E_PULSE)
179+
GPIO.output(LCD_E, False)
180+
time.sleep(E_DELAY)
181+
182+
def lcd_string(message,line,just="l"):
183+
"""Sends a string to the display. Default justification is left."""
184+
185+
if just=="r":
186+
message = message.rjust(LCD_WIDTH," ")
187+
elif just=="c":
188+
message = message.center(LCD_WIDTH," ")
189+
else:
190+
message = message.ljust(LCD_WIDTH," ")
191+
192+
lcd_byte(line, LCD_CMD)
193+
194+
for i in range(LCD_WIDTH):
195+
lcd_byte(ord(message[i]),LCD_CHR)
196+
197+
if __name__ == '__main__':
198+
199+
try:
200+
main()
201+
except KeyboardInterrupt:
202+
pass
203+
finally:
204+
lcd_byte(0x01, LCD_CMD)
205+
lcd_string("Goodbye!",LCD_LINE_1)
206+
GPIO.cleanup()

0 commit comments

Comments
 (0)