Clean up timing, remove debug

This commit is contained in:
James Bowman 2019-03-04 09:35:30 -08:00
parent 5d85445b4c
commit d89332cc69
1 changed files with 41 additions and 60 deletions

View File

@ -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.
Note that the modules require a 5V VCC; they don't function using the
3.3V VCC of I2CDriver.
"""
import sys
@ -15,12 +19,7 @@ class HD44780:
self.i2 = i2
self.a = a
# Command (0) / Data (1) (aka RS) (D0)
# R/W (D1)
# Enable/CLK (D2)
# Backlight control (D3)
self.nybble(3)
self.nybble(3) # Enter 4-bit mode
self.nybble(3)
self.nybble(3)
self.nybble(2)
@ -30,42 +29,38 @@ class HD44780:
self.cmd(0x06) # inc cursor to right when writing and don't scroll
self.cmd(0x80) # set cursor to row 1, column 1
time.sleep(.2)
self.data(0x42)
while 0:
self.port(0)
time.sleep(.1)
self.port(8)
time.sleep(.1)
# self.clear()
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 | 3) # $0C 8-bit mode
self.cmd(0x06) # $06 8-bit mode
self.cmd(0x01) # $01 8-bit mode
""" Clear the screen """
self.cmd(0x01)
time.sleep(.003)
def port(self, *bb):
# Write byte to port
if 0:
self.i2.start(self.a, 0)
self.i2.write(bb)
self.i2.stop()
time.sleep(.000500)
time.sleep(.050)
else:
for b in bb:
self.i2.start(self.a, 0)
self.i2.write([b])
self.i2.stop()
time.sleep(.005000)
def show(self, line, text):
""" Send string to LCD. Newline wraps to second line"""
self.cmd({0:0x80, 1:0xc0}[line])
for c in text:
self.data(ord(c))
def cmd(self, b):
self.nybble(b >> 4)
self.nybble(b & 0xf)
time.sleep(.000053)
def data(self, b):
self.nybble(b >> 4, 1)
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):
print(" nyb %x" % n)
bl = 8 | rs
self.port(
bl | (n << 4),
@ -73,29 +68,15 @@ class HD44780:
bl | (n << 4)
)
def cmd(self, b):
print("cmd %02x" % b)
self.nybble(b >> 4)
self.nybble(b & 0xf)
time.sleep(.004100)
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))
def port(self, *bb):
# Write bytes to port, setting the PCF8574 outputs
self.i2.start(self.a, 0)
self.i2.write(bb)
self.i2.stop()
if __name__ == '__main__':
i2 = I2CDriver(sys.argv[1])
i2.scan()
d = HD44780(i2)
d.message("HELLO WORLD")
d.show(0, "HELLO WORLD")
time.sleep(.5)
d.show(1, "0123456789012345")