By SC Lim, RH2T Vol.4, Mar10
- LCD: Interfacing with PIC Microcontrollers (Part 2)
- LCD: Interfacing with PIC Microcontrollers (Part 3)
- LCD: Interfacing with PIC Microcontrollers (Part 4)
Besides LED and 7-segment display, LCD is another useful device to show program’s current state, to give instructions, for debugging and so on.
LCD stands for Liquid Crystal Display. An LCD is a passive device. It does not produce any light and simply alters the light travelling through it. With a voltage applied to it, the liquid crystal polarizes transmitted light in a different direction to when no voltage is applied. A polarizing filter in front of the display then blocks one of the two polarizations (i.e. the areas in which a voltage was applied or the ones where no voltage was applied) and therefore in some areas of the screen appear ‘black’, this effect is used to draw the characters and graphics displayed by an LCD.
Reflective twisted nematic liquid crystal display
1. Polarizing filter film with a vertical axis to polarize light as it enters.
2. Glass substrate with ITO electrodes. The shapes of these electrodes will determine the shapes that will appear when the LCD is turned ON. Vertical ridges etched on the surface are smooth.
3. Twisted nematic liquid crystal.
4. Glass substrate with common electrode film (ITO) with horizontal ridges to line up with the horizontal filter.
5. Polarizing filter film with a horizontal axis to block/pass light.
6. Reflective surface to send light back to viewer. (In a backlit LCD, this layer is replaced with a light source.)
Almost all LCDs have a strong light source built in behind a glass panel (which contains the liquid crystal), this ensures that the areas of light and dark on the screen (dictated by the areas on the liquid crystal panel across which a voltage is applied) have good contrast. Displays with no backlights that rely solely on the light incident on the LCD panel cannot be used in low light conditions.
Most commonly used character based LCDs are based on Hitachi’s HD44780 controller or other which are compatible with HD44580. In this tutorial, we will discuss about character based LCDs, their interfacing with PIC microcontrollers, various interfaces (8-bit/4-bit), programming, special stuff and tricks you can do with these simple looking LCDs which can give a new look to your application.
2.0 Pin Description
The most commonly used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have only 1 controller and support at most of 80 characters, whereas LCDs supporting more than 80 characters make use of 2 HD44780 controllers.
Most LCDs with one controller have 14 pins or 16 pins (two extra pins are for back-light LED connections) while LCDs with two controllers have two more pins to enable the additional controller. We will focus on LCDs with one controller in this article since it is one of the commonly used models in the market. Likewise, the operations are applicable for two controllers LCD as well. Pin description is shown in the table below.
Character LCD pins with one controller
The HD44780 standard requires 3 control lines as well as either 4 or 8 I/O lines for the data bus. The user may select whether the LCD is to operate with a 4-bit data bus or an 8-bit data bus. If a 4-bit data bus is used the LCD will require a total of 7 data lines (3 control lines plus the 4 lines for the data bus). If an 8-bit data bus is used the LCD will require a total of 11 data lines (3 control lines plus the 8 lines for the data bus).
The three control lines are referred to as EN, RS, and RW.
The EN line is called “Enable.” This control line is used to tell the LCD that you are sending in data. To send data to the LCD, your program should make sure this line is low (0) and then set the other two control lines and/or put data on the data bus. When the other lines are completely ready, bring EN high (1) and wait for the minimum amount of time required by the LCD datasheet (this varies from LCD to LCD), and end by bringing it low (0) again.
The RS line is the “Register Select” line. When RS is low (0), the data is to be treated as a command or special instruction (such as clear screen, position cursor, etc.). When RS is high (1), the data being sent is text data which sould be displayed on the screen. For example, to display the letter “T” on the screen you would set RS high.
The RW line is the “Read/Write” control line. When RW is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively querying (or reading) the LCD. Only one instruction (“Get LCD status”) is a read command. All others are write commands–so RW will almost always be low.
The data bus consists of 4 or 8 lines (depending on the mode of operation selected by the user). In the case of an 8-bit data bus, the lines are referred to as DB0, DB1, DB2, DB3, DB4, DB5, DB6, and DB7.
Finally, the power supply pins for the backlight – LED+ and LED-. Some LCD modules come without the backlight. In that case, these pins are not found or are left disconnected. The recommended voltage for LED+ is 4.2V and LED- should be connected to ground (GND). Vary the value of the resistor connected to LED+ will change the brightness of the backlight. Normally, 220 Ohm or 330 Ohm resistor will be used. For advanced user, you may connect the pin to PWM output and change the brightness in your software by altering the PWM duty cycle. We will further discuss this in upcoming issues.
3.0 Hardware Connection
A typical LCD hardware connection to PIC microcontroller with backlight turned on permanently is shown in figure below. To turn off the backlight, disconnect the supplies to pin 15 and 16. RB4, RB5 and RB6 of PIC16F877A are used for the control signals while PORTD of the microcontroller is the data bus.
A typical LCD to PIC microcontroller hardware connection
4.0 DDRAM – Display Data RAM
Display data RAM (DDRAM) stores display data represented in 8-bit character codes. Its extended capacity is 80 X 8 bits, or 80 characters. The area in display data RAM (DDRAM) that is not used for display can be used as general data RAM. So whatever you send on the DDRAM is actually displayed on the LCD. For LCDs like 1×16, only 16 characters are visible, so whatever you write after 16 characters is written in DDRAM but is not visible to the user. Figures below will show you the DDRAM addresses of 1 line and 2 lines LCDs.
Now you might be thinking that when you send an ASCII value to DDRAM, how the character is displayed on LCD? So the answer is in CGROM. The Character Generator ROM (CGROM) generates 5×8 dots or 5×10 dots character patterns from 8-bit character codes (see figures below for more details). It can generate 208 5×8 dot character patterns and 32 5×10 dot character patterns. User-defined character patterns are also available by mask-programmed ROM. Here we will only discuss 5×8 dots character patterns LCD module which is commonly used.
As you can see in both the code maps, the character code from 0x00 to 0x07 is occupied by the CGRAM characters or the user defined characters. If user wants to display the fourth custom character then the code to display it is 0x03 i.e. when user sends 0x03 code to the LCD DDRAM, the fourth user created character or pattern will be displayed on the LCD.
6.0 CGRAM – Character Generator RAM
As indicated by its name, CGRAM area is used to create custom characters in LCD. In the character generator RAM, user can rewrite character patterns by program. For 5 x 8 dots, eight character patterns can be written, and for 5 x 10 dots, four character patterns can be written. We will not cover how to use CGRAM area to make custom characters in this tutorial. Please stay tune for the future issue.
7.0 BF – Busy Flag
Busy Flag is a status indicator flag for LCD. When we send a command or data to the LCD for processing, this flag is set (i.e BF =1) and as soon as the instruction is executed successfully this flag is cleared (BF = 0). This is helpful in determining the exact amount of delay for the LCD process.
To read Busy Flag, the condition RS = 0 and R/W = 1 must be met and the MSB of the LCD data bus (D7) acts as busy flag. When BF = 1 means LCD is busy and will not accept next command or data and BF = 0 means LCD is ready for the next command or data to process.
8.0 Instruction Register (IR) and Data Register (DR)
There are two 8-bit registers in HD44780 controller Instruction and Data register. Instruction register corresponds to the register where you send commands to LCD e.g LCD shift command, LCD clear, LCD address etc. and Data register is used for storing data which is to be displayed on LCD. When send the enable signal of the LCD is asserted, the data on the pins is latched in to the data register and data is then moved automatically to the DDRAM and hence is displayed on the LCD. Data Register is not only used for sending data to DDRAM but also for CGRAM, the address where you want to send the data, is decided by the instruction you send to LCD. We will discuss more on LCD instruction set further in this tutorial.
9.0 LCD Commands and Instruction Set
Only the Instruction Register (IR) and the Data Register (DR) of the LCD can be controlled by the MCU. Before starting the internal operation of the LCD, control information is temporarily stored into these registers to allow interfacing with various MCUs, which operate at different speeds, or various peripheral control devices. The internal operation of the LCD is determined by signals sent from the MCU. These signals, which include register selection signal (RS), read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD instructions. There are four categories of instructions:
- Designate LCD functions, such as display format, data length, etc.
- Set internal RAM addresses
- Perform data transfer with internal RAM
- Perform miscellaneous functions
Commands and Instructions set for LCD type HD44780
Although looking at the table you can make your own commands and test them. Below is a brief list of useful commands which are frequently used while working on the LCD.
The table above will help you while writing programs for LCD. But after you are done testing with it, I recommend you to use the previous table (Commands and Instructions set for LCD type HD44780) to get more grip on working with LCD and trying your own commands.
That’s all for this issue. In the next part of the tutorial we will continue with the software programming for PIC microcontrollers to interface with the LCD module. Please continue to follow this topic and let your application interacts with you through LCD!
1. Hitachi HD66710 Datasheet