-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlcd.py
130 lines (111 loc) · 4 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# HD44780 class for micropython board (http://micropython.org)
# Written by Will Pimblett, based on http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/
# http://github.com/wjdp/micropython-lcd
# http://wjdp.co.uk
import pyb
class HD44780(object):
# Pinout, change within or outside class for your use case
PINS = ['Y1','Y2','Y3','Y4','Y5','Y6']
# Pin names, don't change
PIN_NAMES = ['RS','E','D4','D5','D6','D7']
# Dict of pins
pins = {}
# Pin mode, push-pull control
PIN_MODE = pyb.Pin.OUT_PP
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
# Designation of T/F for character and command modes
LCD_CHR = True
LCD_CMD = False
LINES = {
0: 0x80, # LCD RAM address for the 1st line
1: 0xC0, # LCD RAM address for the 2nd line
# Add more if desired
}
# Timing constants
E_PULSE = 50
E_DELAY = 50
def init(self):
# Initialise pins
for pin, pin_name in zip(self.PINS, self.PIN_NAMES):
# setattr(self, 'LCD_'+pin_name, # Unsupported
# pyb.Pin(pin, self.PIN_MODE))
self.pins['LCD_'+pin_name] = pyb.Pin(pin, self.PIN_MODE)
# Initialise display
self.lcd_byte(0x33,self.LCD_CMD)
self.lcd_byte(0x32,self.LCD_CMD)
self.lcd_byte(0x28,self.LCD_CMD)
self.lcd_byte(0x0C,self.LCD_CMD)
self.lcd_byte(0x06,self.LCD_CMD)
self.lcd_byte(0x01,self.LCD_CMD)
def clear(self):
# Clear the display
self.lcd_byte(0x01,self.LCD_CMD)
def set_line(self, line):
# Set the line that we're going to print to
self.lcd_byte(self.LINES[line], self.LCD_CMD)
def set_string(self, message):
# Pad string out to LCD_WIDTH
# message = message.ljust(LCD_WIDTH," ")
m_length = len(message)
if m_length < self.LCD_WIDTH:
short = self.LCD_WIDTH - m_length
blanks=str()
for i in range(short):
blanks+=' '
message+=blanks
for i in range(self.LCD_WIDTH):
self.lcd_byte(ord(message[i]), self.LCD_CHR)
def lcd_byte(self, bits, mode):
# Send byte to data pins
# bits = data
# mode = True for character
# False for command
self.pin_action('LCD_RS', mode) # RS
# High bits
self.pin_action('LCD_D4', False)
self.pin_action('LCD_D5', False)
self.pin_action('LCD_D6', False)
self.pin_action('LCD_D7', False)
if bits&0x10==0x10:
self.pin_action('LCD_D4', True)
if bits&0x20==0x20:
self.pin_action('LCD_D5', True)
if bits&0x40==0x40:
self.pin_action('LCD_D6', True)
if bits&0x80==0x80:
self.pin_action('LCD_D7', True)
# Toggle 'Enable' pin
self.udelay(self.E_DELAY)
self.pin_action('LCD_E', True)
self.udelay(self.E_PULSE)
self.pin_action('LCD_E', False)
self.udelay(self.E_DELAY)
# Low bits
self.pin_action('LCD_D4', False)
self.pin_action('LCD_D5', False)
self.pin_action('LCD_D6', False)
self.pin_action('LCD_D7', False)
if bits&0x01==0x01:
self.pin_action('LCD_D4', True)
if bits&0x02==0x02:
self.pin_action('LCD_D5', True)
if bits&0x04==0x04:
self.pin_action('LCD_D6', True)
if bits&0x08==0x08:
self.pin_action('LCD_D7', True)
# Toggle 'Enable' pin
self.udelay(self.E_DELAY)
self.pin_action('LCD_E', True)
self.udelay(self.E_PULSE)
self.pin_action('LCD_E', False)
self.udelay(self.E_DELAY)
def udelay(self, us):
# Delay by us microseconds, set as function for portability
pyb.udelay(us)
def pin_action(self, pin, high):
# Pin high/low functions, set as function for portability
if high:
self.pins[pin].high()
else:
self.pins[pin].low()