User manual
4) FASTER, FASTER
The functions in Part 2 included low-level routines for sending data to the LCD. One of
those routines included timing delays required by the LCD. The routines worked, and I was
happy. But now I wonder: can we shorten any of the delays? And if so, will it speed up our
data transfer to the LCD? We now have a tool, TimeTest, that will measure time and give
us a visual indicator of success.
Let’s look at the routine with the most delays: PulseEnableLine. Recall that each byte of
our data is sent to the controller as two 4-bit nibbles, and we clock each in a brief pulse on
the enable (LCD_E) line:
def PulseEnableLine():
mSec = 0.001
time.sleep(mSec) #DELAY (A): our first 1 mS wait
GPIO.output(LCD_E, GPIO.HIGH)
time.sleep(mSec) #DELAY (B): our second 1 mS wait
GPIO.output(LCD_E, GPIO.LOW)
time.sleep(mSec) #DELAY (C): our third 1 mS wait
This routine contains 3 mS of waiting time, and it is called twice per byte. In Part 2 I said we
could halve those waiting times, reducing our waiting from 6 mS to 3 mS per byte. It works.
But when I try to reduce those times to 0.1 mS (100 microseconds), my display falters.
When in doubt, RTFM (read the manual). On page 49, the HD44780 datasheet gives the
timing for the LCD_E line as 450 nanoseconds. The controller needs less than half a
microsecond of pulse! So why would our display falter at 100 microseconds? OK, RTFM
again. Some commands require much more execution time than others. In particular, the
Clear Screen and Return Home instructions each require a whopping 1.52 mS to execute –
40 times longer than the other commands. We call the Clear Screen command in our
initialization sequence. Modify that command, adding in a delay:
def ClearDisplay():
#This command requires 1.5mS processing time, so delay is needed
SendByte(CLEARDISPLAY)
time.sleep(0.0015) #delay for 1.5mS
Now we can go back to PulseEnableLine and reduce the delays. Here is a table of
TimeTest results when I reduce the A, B, and C delays:
Wow! By removing the delays we
have improved performance 35-
fold. Why don’t we need the
delays? Because Python is slow,
and each Python statement takes
longer than the required pulse
time. Try it and see.
Condition
TimeTest Result
All delays = 1 mS
4.50 seconds
All delays = 0.5 mS
2.54
All delays = 0.1 mS
1.00
Remove A&C, B=0.1 mS
0.45
Remove A&C; B=0.001 mS
0.35
Remove A, B, & C
0.13