Lcd character panel
Context
● You are developing an embedded application using one or more members of the 8051 family of microcontrollers.
● The application has a time-triggered architecture, constructed using a scheduler.
● You are creating the user interface for your application.
Problem
How do you connect an LCD-based ‘character panel’ display to your embedded processor and control it efficiently using a high-level programming language?
Background
As outlined at the start of this chapter, we are concerned here with LCD character panels based on the HD44780 microcontroller.
Key HD44780 components
The HD44780 contains its own reset circuitry, memory and so forth: it contains sev- eral important components (Figure 22.1), which may be accessed and controlled from an attached microcontroller or microprocessor. We will discuss some of the key com- ponents in the sections that follow.
As shown in Figure 22.1, the interface between the microcontroller and the HD44780 consists of five sets of signals. A summary of each of these signals is given in Table 22.1.
DD RAM
At the heart of the HD44780 is the DD RAM: the ‘Display Data’ RAM. This stores dis- play data represented in 8-bit character codes. Its capacity is 80 characters; as a result the maximum display sizes are 20 charactersx4 lines or 40 charactersx2 lines.
Use of the HD44780 involves transferring data (via the 4-bit or 8-bit data bus) into the DD RAM. The HD44780 will then refresh the display as required using these data.
Characters that can be stored and displayed via DD RAM include most of the core (displayable) characters from the ASCII table, with only minor changes (Table 22.2): this means that, in general, you can simply send character data to the display and obtain the expected outputs.
Note that none of these characters uses the bottom line of the display (the cursor line): as a result, low-case characters with ‘tails’ (g, j, p, q, y) will appear higher than normal on the display.
CG RAM
The CG RAM is the ‘Character Generator’ RAM: this can be used to allow the creation and display of user-defined characters. This facility is helpful if you wish to display characters outside the ‘standard’ range, such as a ‘£’ sign.
We demonstrate how to use CG RAM in an example that follows.
Registers
The HD44780 has two 8-bit registers, an instruction register (IR) and a data register (DR):
● The IR stores instruction codes, such as display clear and cursor shift and address information for DDRAM and character generator CGRAM.
● The DR temporarily stores data to be written into DDRAM or CGRAM. Data written into the DR from the 8051 are automatically written into DDRAM or CGRAM by an internal operation.
Busy flag (BF)
When the busy flag is 1, the HD44780 is performing an internal operation and fur- ther instructions or data will not be accepted. When RS = 0 and R/W = 1 (Table 22.4), the busy flag is output to DB7.
Cursor/blink control circuit
As the name suggests, the cursor/blink control circuit generates the cursor or character blinking. The cursor or the blinking will appear with the digit located at the display data RAM (DDRAM) address set in the address counter (AC). For example, when the address counter is 0x08, the cursor position is displayed at DDRAM address 0x08.
Address counter (AC)
The address counter (AC) assigns addresses to both DDRAM and CGRAM. When an address of an instruction is written into the IR, the address information is sent from the IR to the AC.
Selection of either DDRAM or CGRAM is also determined concurrently by the instruction.
After writing to DDRAM or CGRAM, the AC is automatically incremented by 1. The AC contents are then output to DB0 to DB6 when RS = 0 and R/W = 1 (Table 22.4).
Solution
As discussed in ‘Background’, we will consider only LCD devices based on the Hitachi HD44780 controller.
Hardware
The HD44780 can send data in either two 4-bit operations or one 8-bit operation. This feature was probably originally intended to allow connection to both 4-bit and 8-bit microcontrollers. However, even with 8-bit microcontrollers (such as the 8051) the 4-bit interface is the most commonly applied, since:
1 The 4-bit interface is sufficiently fast for most applications (a single character is transferred in under 0.1 ms).
2 It saves four port pins.
We will use a 4-bit interface in our examples.
Despite its name the ‘4-bit’ interface actually requires six port pins: four for the data bus and (usually) two additional port pins for the control lines (Figure 22.2).
In addition to the ground and power connections, you also need to connect the contrast adjustment pin (Vo). We have successfully used many LCD display by pulling this pin to ground. However, the recommended contrast adjustment takes the form of a potentiometer, connected as shown in Figure 22.3.
Back lighting
If the module has a backlight, this will greatly increase the power consumption: check your data sheet for details. Before assuming that you require a backlight, try some of the modern displays now available: recent devices have greatly improved contrast and visibility.
The data bus
The data bus (4-bit) needs to be connected to four port pins with internal pull-up resistors. If using a port (e.g. Port 0) without internal pull-ups, add external 10K pull- ups (to Vcc).
Software
Complete software libraries are given in the examples that follow.
Note that in these examples – as in the RS-232 library – we have implemented the library as a MUL TI – ST AGE T ASK : each character is written to a buffer and updates of the LCD display itself are carried out with a scheduled ‘update’ function.
Note also that these examples illustrate the use of user-defined characters.
Hardware resource implications
Uses a number of port pins, plus one timer.
Reliability and safety issues
LCD displays are reliable and have a long life.
Portability
This pattern can be applied to any microcontroller family.
Overall strengths and weaknesses
LCDs provide the basis of a professional and flexible user interface.
The panels are not cheap.
Even a ‘4-bit’ interface requires six pins.
Related patterns and alternative solutions
● See MX LED DISPLA Y [page 450]
● See PC LINK ( RS -232) [page 362]