User manual
time.sleep(mSec)
GPIO.output(LCD_E, GPIO.LOW) #return E low
time.sleep(mSec) #wait before doing anything else
Each waiting period is specified in the HD44780 datasheet. You can minimize wait times
by following the specifications exactly. I chose a lazier method. I empirically picked a delay
that is longer than necessary, but short enough to minimize visual distractions: one
millisecond. It works. Even half a millisecond is OK on my displays. Go much shorter,
however, and you’ll need to account for all of the timing requirements listed in the spec. It is
up to you.
Bytes can be send to the controller as either data byte (characters) or as commands. The
controller uses the input line RS to distinguish the two: anything sent when RS is low is a
command, and anything sent when RS is high is a character. We’ll modify our SendByte
routine to account for this requirement.
def SendByte(data,charMode=False):
GPIO.output(LCD_RS,charMode) #set mode: command vs. char
SendNibble(data) #send upper bits first
…etc…
By using a default parameter, any call to SendByte will default to a command. Let’s create
a second routine for sending characters:
def SendChar(ch):
SendByte(ord(ch),True)
Now we have routines for sending commands and characters to the display. It would be
nice to test them right away, but we can’t: we have to initialize the display first. All
HD44780-based displays require certain startup commands to specify things like data-
length, cursor-mode, etc. Without going into a lot of detail here, our display requires the
following “magic bytes” to initialize it: 0x33, 0x32, 0x28, 0x0C, 0x06, 0x01. These
command bytes will set the data-length to 4 bits, turn the cursor off, enable sequential
addressing, and clear the display.
def InitLCD():
SendByte(0x33) #initialize
SendByte(0x32) #set to 4-bit mode
SendByte(0x28) #2 line, 5x7 matrix
…etc…
It’s time to write something on the LCD display. Write a routine to display a string, like
‘Hello, World’. All we need to do is write each character, one at a time. A simple for-loop
will do the trick:
def ShowMessage(string):
for character in string:
SendChar(character)