User manual

4) INPUT & OUTPUT
Let’s combine our string-writing ability with the switch input routine from part 1. In Part 1 we
read the status of each switch and displayed it on the console. Now we will display switch
status on the LCD screen instead. First, write something meaningful on the display:
WriteMessage(‘Press a switch...’)
And put the switch status on the second display line. Wait a sec, how do we put stuff on
the second line? Send a carriage return? Sorry, that doesn’t work. How about sending a
full line of 16 characters to the display? Surely the next character will go on the next line.
No, sorry again.
Characters sent to the LCD controller are placed at the current cursor position. In
sequential mode, the cursor is advanced with each character sent. But unfortunately,
second-line addresses do not immediately follow the first line. The cursor address for Line1
is 0x00. The cursor address for Line 2 is 0x40. Strange, but true. If you have a 20x4
display, the arrangement is even more confusing:
20x4 Display
Address
Line 1
0x00
Line 2
0x40
Line 3
0x14
Line 4
0x54
To set the cursor position, send a byte equal to the set cursor command (0x80) + the
desired cursor address. Now we can put together our demo. Four true/false results do not
all fit on a 16 character line. Using the string format of “%d %d %d %d”, the boolean results
are converted to more compact (d for decimal) ones and zeros.
while (True):
GotoLine(1)
switchValues = CheckSwitches()
decimalResult = " %d %d %d %d" % switchValues
ShowMessage(decimalResult)
time.sleep(0.2)
That’s it for part 2. We can now read the status of each switch and display short messages
on the LCD display. In part 3 we will add useful display routines for cursor control, text
positioning, and scrolling.
16x2 Display
Line 1
Line 2