Clean up timing, remove debug
This commit is contained in:
parent
5d85445b4c
commit
d89332cc69
|
@ -1,6 +1,10 @@
|
||||||
"""
|
"""
|
||||||
Example for LCD1602, in which a PCF8574 I/O expander drives a HD44780
|
Example for LCD1602, in which a PCF8574 I/O expander drives a HD44780.
|
||||||
Available from various vendors.
|
Available from various vendors.
|
||||||
|
|
||||||
|
Note that the modules require a 5V VCC; they don't function using the
|
||||||
|
3.3V VCC of I2CDriver.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
@ -15,12 +19,7 @@ class HD44780:
|
||||||
self.i2 = i2
|
self.i2 = i2
|
||||||
self.a = a
|
self.a = a
|
||||||
|
|
||||||
# Command (0) / Data (1) (aka RS) (D0)
|
self.nybble(3) # Enter 4-bit mode
|
||||||
# R/W (D1)
|
|
||||||
# Enable/CLK (D2)
|
|
||||||
# Backlight control (D3)
|
|
||||||
|
|
||||||
self.nybble(3)
|
|
||||||
self.nybble(3)
|
self.nybble(3)
|
||||||
self.nybble(3)
|
self.nybble(3)
|
||||||
self.nybble(2)
|
self.nybble(2)
|
||||||
|
@ -30,42 +29,38 @@ class HD44780:
|
||||||
self.cmd(0x06) # inc cursor to right when writing and don't scroll
|
self.cmd(0x06) # inc cursor to right when writing and don't scroll
|
||||||
self.cmd(0x80) # set cursor to row 1, column 1
|
self.cmd(0x80) # set cursor to row 1, column 1
|
||||||
|
|
||||||
time.sleep(.2)
|
self.clear()
|
||||||
self.data(0x42)
|
|
||||||
|
|
||||||
while 0:
|
|
||||||
self.port(0)
|
|
||||||
time.sleep(.1)
|
|
||||||
self.port(8)
|
|
||||||
time.sleep(.1)
|
|
||||||
# self.clear()
|
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
""" Blank / Reset LCD """
|
""" Clear the screen """
|
||||||
self.cmd(0x33) # $33 8-bit mode
|
self.cmd(0x01)
|
||||||
self.cmd(0x32) # $32 8-bit mode
|
time.sleep(.003)
|
||||||
self.cmd(0x28) # $28 8-bit mode
|
|
||||||
self.cmd(0x0C | 3) # $0C 8-bit mode
|
|
||||||
self.cmd(0x06) # $06 8-bit mode
|
|
||||||
self.cmd(0x01) # $01 8-bit mode
|
|
||||||
|
|
||||||
def port(self, *bb):
|
def show(self, line, text):
|
||||||
# Write byte to port
|
""" Send string to LCD. Newline wraps to second line"""
|
||||||
if 0:
|
self.cmd({0:0x80, 1:0xc0}[line])
|
||||||
self.i2.start(self.a, 0)
|
for c in text:
|
||||||
self.i2.write(bb)
|
self.data(ord(c))
|
||||||
self.i2.stop()
|
|
||||||
time.sleep(.000500)
|
def cmd(self, b):
|
||||||
time.sleep(.050)
|
self.nybble(b >> 4)
|
||||||
else:
|
self.nybble(b & 0xf)
|
||||||
for b in bb:
|
time.sleep(.000053)
|
||||||
self.i2.start(self.a, 0)
|
|
||||||
self.i2.write([b])
|
def data(self, b):
|
||||||
self.i2.stop()
|
self.nybble(b >> 4, 1)
|
||||||
time.sleep(.005000)
|
self.nybble(b & 0xf, 1)
|
||||||
|
|
||||||
|
# The PCF8574 outputs are connected to the HD44780
|
||||||
|
# pins as follows:
|
||||||
|
|
||||||
|
# P0 RS (0: command, 1: data)
|
||||||
|
# P1 R/W (0: write, 1: read)
|
||||||
|
# P2 Enable/CLK
|
||||||
|
# P3 Backlight control
|
||||||
|
# P4-7 D4-D7
|
||||||
|
|
||||||
def nybble(self, n, rs = 0):
|
def nybble(self, n, rs = 0):
|
||||||
print(" nyb %x" % n)
|
|
||||||
bl = 8 | rs
|
bl = 8 | rs
|
||||||
self.port(
|
self.port(
|
||||||
bl | (n << 4),
|
bl | (n << 4),
|
||||||
|
@ -73,29 +68,15 @@ class HD44780:
|
||||||
bl | (n << 4)
|
bl | (n << 4)
|
||||||
)
|
)
|
||||||
|
|
||||||
def cmd(self, b):
|
def port(self, *bb):
|
||||||
print("cmd %02x" % b)
|
# Write bytes to port, setting the PCF8574 outputs
|
||||||
self.nybble(b >> 4)
|
self.i2.start(self.a, 0)
|
||||||
self.nybble(b & 0xf)
|
self.i2.write(bb)
|
||||||
time.sleep(.004100)
|
self.i2.stop()
|
||||||
|
|
||||||
def data(self, b):
|
|
||||||
print("DAT %02x" % b)
|
|
||||||
self.nybble(b >> 4, 1)
|
|
||||||
self.nybble(b & 0xf, 1)
|
|
||||||
time.sleep(.004100)
|
|
||||||
|
|
||||||
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.data(ord(char))
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
i2 = I2CDriver(sys.argv[1])
|
i2 = I2CDriver(sys.argv[1])
|
||||||
i2.scan()
|
|
||||||
d = HD44780(i2)
|
d = HD44780(i2)
|
||||||
d.message("HELLO WORLD")
|
d.show(0, "HELLO WORLD")
|
||||||
|
time.sleep(.5)
|
||||||
|
d.show(1, "0123456789012345")
|
||||||
|
|
Loading…
Reference in New Issue