-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcd.py
75 lines (54 loc) · 2.15 KB
/
lcd.py
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
74
75
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
from time import sleep
import sys
class HD44780:
def __init__(self, pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 18]):
self.pin_rs=pin_rs
self.pin_e=pin_e
self.pins_db=pins_db
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin_e, GPIO.OUT)
GPIO.setup(self.pin_rs, GPIO.OUT)
for pin in self.pins_db:
GPIO.setup(pin, GPIO.OUT)
self.clear()
def clear(self):
""" Blank / Reset LCD """
self.cmd(0x33) # $33 8-bit mode
self.cmd(0x32) # $32 8-bit mode
self.cmd(0x28) # $28 8-bit mode
self.cmd(0x0C) # $0C 8-bit mode
self.cmd(0x06) # $06 8-bit mode
self.cmd(0x01) # $01 8-bit mode
def cmd(self, bits, char_mode=False):
""" Send command to LCD """
sleep(0.001)
bits=bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs, char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i], True)
GPIO.output(self.pin_e, True)
GPIO.output(self.pin_e, False)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4,8):
if bits[i] == "1":
GPIO.output(self.pins_db[::-1][i-4], True)
GPIO.output(self.pin_e, True)
GPIO.output(self.pin_e, False)
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
for char in text:
if char == '\n':
self.cmd(0xC0) # next line
else:
self.cmd(ord(char),True)
if __name__ == '__main__':
lcd =HD44780()
lcd.message("Hi Vikas")
time.sleep(1)