There are a lot of LCD modules that use the HD44780 controller. This is a chip that accepts parallel data (8 bits) to control an LCD character display. In my recent twitter2LCD project I needed to determine some of the control instructions such as how to make text scroll and how to control the position of the cursor. There are quite a few places where the complete instruction set is posted, but they are geared more toward seasoned digital electronics users. The main insight I can offer is that if you are using a serial-enabled LCD (as I was), you have to convert the 8 control bits into a byte and send that byte after a specific control character. For the Sparkfun LCD-09393 and related SerLCD modules, the control byte is “0xFE.” This byte grabs the attention of the onboard controller and tells it that the following byte is an instruction and not a character.The basic instructions can be determined from the following table.
The various bit values have specific meanings outlined in the following figure.
The trick is then to read the DB0 throught DB7 bits as a byte where the LSB is DB0. For example, to clear the display I would send binary “00000001” or hex: 0x01. This is the documented clear byte in the Sparkfun SerLCD datasheet. Some other examples are included in that datasheet, but the following is a table of all HD44780 control combinations.
|Return cursor to home, and un-shift display||0x02|
|Entry mode: The following control how the cursor behaves after each character is entered|
|move cursor right, don’t shift display||0x04|
|move cursor right, do shift display (left)||0x05|
|move cursor right, don’t shift display (this is the most common)||0x06|
|move cursor right, do shift display (left)||0x07|
|Display control: The following control display properties|
|turn display off||0x08
|display on, cursor off,||0x0C|
|display on, cursor on, steady cursor||0x0E|
|display on, cursor on, blinking cursor||0x0F|
|The following commands move the cursor and shift the display|
|Shift cursor left||0x10|
|Shift cursor right||0x14|
|Shift display left||0x18|
|Shift display right||0x1C|
|Function set: the following commands set functions of the controller.|
|These are more advanced and with a serial controller, you won’t need to mess with them. If you do have to change these, follow the recipe of creating a byte from the bits shown in the table above.|
|Cursor position: see following discussion for more detail.||0x80 + position|
The cursor position can seem like black magic but it makes much more sense once you know that the HD44780 is designed to control a 40 character 4-line display. So if you have a 16×2 then you will only see the first 16 characters of the top two lines. Simple enough once you get used to taking these character positions into account. For example, in a 16×2 display, the first line is position 0-15. So 0x80 is the first position, 0x80 + 12 = 0x8C is the 13th (remember, they are zero indexed). The second line is a little tricky since it shows positions 64-79. Just add the position number (in decimal) to 0x80 (in hex) to get the hex address of the cursor position. The address is the command to move the cursor to that location. So, to move to the 13th position of the top line in a 16×2 Sparkfun Display, I would send “0xFE 0x8C”. The first byte warns the onboard microcontroller that a command is coming, and the second byte is the command.