My motivation in explaining this simple kind of project is that I’ve looked for but have been unable to find any source code that quickly explains how an LCD can be interfaced in 4-bit mode to an 8-bit microcontroller. I know it isn’t rocket science and that any pertinent data can be derived from datasheets, but that can be kind of confusing to someone who has never programmed in a low level language like C before and is especially difficult to those who have never programmed at all. Almost every line is commented, and there is even support for using the standard printf function to write directly to the LCD with the printf command.
The source provided below is written in C and is compiled using the free GNU AVR-GCC compiler. Testing was conducted on two of Atmel’s AVR line of 8-bit microcontrollers: the ATmega8, and ATmega128. With a little work it can be ported for use with PIC series microcontrollers from Microchip or even any other microcontroller on the market.
The LCD controller is HD44780 (standard Hitachi interface) compatible and has 4 lines of 20 characters each — see the picture below. Such LCD screens are easy to find either through a surplus store or an eBay vendor. Try not to spend more than $20 (including shipping) on these non-graphical LCDs because otherwise you are spending way too much.
The lines on the screen can be a little misleading; both lines 1 and 3, and both lines 2 and 4 are treated as though they are one line each and that the display were really 2 lines of 40 characters each. As a result, after 20 characters have been written on each line the display wraps unexpectedly. The first character after line 1 will end up on line 3 instead of line 2, and the first character after line 2 will end up on line 4 instead of line 3. If a character is written after the end of line 3 it will end up at the beginning of line 2. The next write after line 4, though, writes to line 1 as anticipated. Luckily, this is accounted for in software as the microcontroller keeps track of the current position of the cursor. The reason I bring this up is if you planned on using a 1 or 2 line display you would not suffer from this phenomena and thus could comment out any unneeded lines of code.
To interface the display, at least 6 I/O lines are required to run in 4-bit mode (7 if there is a need to read any data from the LCD), and at least 10 I/O lines in 8-bit mode (11 if reading data). Don’t have or don’t feel like using at least 6 I/O lines? The code fits into an implementation built around I2C — with a little hardware modification and design. For more information on that please check out the I2C LCD (I haven’t completed this page as of yet).
In both 4 and 8-bit modes it can be awkward to connect an LCD to a development board without having wires going everywhere if there is not a 16-pin LCD header readily available. Furthermore, if a 3.3V microcontroller is used to drive the output data pins then an additional 5.0V step-down voltage regulator and capacitors will be needed (assuming an input of greater than 6.5V is used). While some of the Hitachi LCDs can run with an input source voltage between 2.7V and 5.5V (HD44780U), it turns out that the screen I own (HD44780S) only has the ability to handle a 5V input. To solve this issue, I designed an LCD Header PCB. Look at that project page if that is the design route you wish to take for it has pictures of the finished design and also a schematic for possible PCB production or wire-wrapping/cabling.
Source File Packages:
Individual Source Files:
Datasheets and Miscellaneous PDFs:
HD44780S/U Datasheet – [399kB]
- Data sheet for the controller. This document painfully details everything needed to start writing to and reading from any HD44780-compatible LCD screen.
Common LCD Commands – [10kB]
- A list of the most commonly used LCD commands and their respective execution times.
Defining Custom LCD Characters – [65kB]
- A fairly extensive guide on how to create your own custom characters.
- Quick reference guide to figuring out which memory address corresponds to which character on a variety of different size LCD screens.
Hantronix Specification Sheet – [165kB]
- A quick reference guide to everything needed to get up and running with an LCD.