
- •Table of Contents
- •Preface
- •Additional Material
- •Basic Electronics
- •1.0 The Atom
- •1.1 Isotopes and Ions
- •1.2 Static Electricity
- •1.3 Electrical Charge
- •1.4 Electrical Circuits
- •1.5 Circuit Elements
- •1.6 Semiconductors
- •Number Systems
- •2.0 Counting
- •2.1 The Origins of the Decimal System
- •2.2 Types of Numbers
- •2.3 Radix Representations
- •2.4 Number System Conversions
- •Data Types and Data Storage
- •3.0 Electronic-Digital Machines
- •3.1 Character Representations
- •3.2 Storage and Encoding of Integers
- •3.3 Encoding of Fractional Numbers
- •3.4 Binary-Coded Decimals (BCD)
- •Digital Logic, Arithmetic, and Conversions
- •4.0 Microcontroller Logic and Arithmetic
- •4.1 Logical Instructions
- •4.2 Microcontroller Arithmetic
- •4.3 Bit Manipulations and Auxiliary Operations
- •4.4 Unsigned Binary Arithmetic
- •4.5 Signed Binary Arithmetic
- •4.6 Data Format Conversions
- •Circuits and Logic Gates
- •5.0 Digital Circuits
- •5.1 The Diode Revisited
- •5.2 The Transistor
- •5.3 Logic Gates
- •5.4 Transistor-Transistor Logic
- •5.5 Other TTL Logic Families
- •5.6 CMOS Logic Gates
- •Circuit Components
- •6.0 Power Supplies
- •6.1 Clocked Logic and Flip-flops
- •6.2 Clocks
- •6.3 Frequency Dividers and Counters
- •6.4 Multiplexers and Demultiplexers
- •6.5 Input Devices
- •The Microchip PIC
- •7.0 The PICMicro Microcontroller
- •7.1 PIC Architecture
- •Mid-range PIC Architecture
- •8.0 Processor Architecture and Design
- •8.1 The Mid-range Core Features
- •8.2 Mid-Range CPU and Instruction Set
- •8.3 EEPROM Data Storage
- •8.4 Data Memory Organization
- •8.5 Mid-range I/O and Peripheral Modules
- •PIC Programming: Tools and Techniques
- •9.0 Microchip’s MPLAB
- •9.1 Integrated Development Environment
- •9.2 Simulators and Debuggers
- •9.3 Programmers
- •9.4 Engineering PIC Software
- •9.5 Pseudo Instructions
- •Programming Essentials: Input and Output
- •10.0 16F84A Programming Template
- •10.1 Introducing the 16F84A
- •10.2 Simple Circuits and Programs
- •10.3 Programming the Seven-segment LED
- •10.4 A Demonstration Board
- •Interrupts
- •11.0 Interrupts on the 16F84
- •11.1 Interrupt Sources
- •11.2 Interrupt Handlers
- •11.3 Interrupt Programming
- •11.4 Sample Programs
- •Timers and Counters
- •12.0 The 16F84 Timer0 Module
- •12.1 Delays Using Timer0
- •12.2 Timer0 as a Counter
- •12.3 Timer0 Programming
- •12.4 The Watchdog Timer
- •12.5 Sample Programs
- •LCD Interfacing and Programming
- •13.0 LCD Features and Architecture
- •13.1 Interfacing with the HD44780
- •13.2 HD44780 Instruction Set
- •13.3 LCD Programming
- •13.4 Sample Programs
- •Communications
- •14.0 PIC Communications Overview
- •14.1 Serial Data Transmission
- •14.2 Parallel Data Transmission
- •14.4 PIC Protocol-based Serial Programming
- •14.5 Sample Programs
- •Data EEPROM Programming
- •15.0 PIC Internal EEPROM Memory
- •15.1 EEPROM Devices and Interfaces
- •15.2 Sample Programs
- •Analog to Digital and Realtime Clocks
- •16.0 A/D Converters
- •16.1 A/D Integrated Circuits
- •16.2 PIC On-Board A/D Hardware
- •16.3 Realtime Clocks
- •16.4 Sample Programs
- •Index

LCD Interfacing and Programming |
279 |
In LCDs with a backlight option sometimes the connectors have two extra pins, usually numbered 15 and 16. Pin number 15 is connected to a 5V source for the backlight and pin number 16 to ground. Typical LCD wiring is shown in Table 13.1.
Table 13.1
Hitachi HD44780 LCD Controller Pin-Out (80 characters or less)
PIN NUMBER |
SYMBOL |
DESCRIPTION |
|
|
|
1 |
Vss |
Ground |
2 |
Vcc |
Vcc (Power supply +5V) |
3 |
Vee |
Contrast control |
4 |
RS |
Set/reset |
|
|
0 = instruction input |
|
|
1 = data input |
5 |
R/W |
R/W (read/write select) |
|
|
0 = write to LCD |
|
|
1 = read LCD data |
6 |
E |
Enable. Clock signal to |
|
|
initiate data transfer |
7 |
DB0 |
Data bus line 0 |
8 |
DB1 |
Data bus line 1 |
9 |
DB2 |
Data bus line 2 |
10 |
DB3 |
Data bus line 3 |
11 |
DB4 |
Data bus line 4 |
12 |
DB5 |
Data bus line 5 |
13 |
DB6 |
Data bus line 6 |
14 |
DB7 |
Data bus line 7 |
The pin-out in Table 13.1 refers to controllers that address no more than 80 characters. In addition, some LCDs with LED backlighting contain two additional pins, usually numbered 15 and 16. In these cases, pin number 15 is a +5 VDC source for the backlight and pin 16 is the backlight ground.
From the pin-out in Table 13.1, it is evident that the interface to the LCD uses eight parallel lines (lines 7 to 14). However, it is also possible to drive the LCD using just four lines, saving connections on limited circuits.
The reader should beware that LCDs are often furnished in custom boards that may or may not have other auxiliary components. These boards are often wired differently from the examples shown in Figure 13-2. In all cases, the device’s documentation and the corresponding data sheets should provide the appropriate wiring information.
13.1 Interfacing with the HD44780
The Hitachi 44780 controller allows parallel interfacing using 4- or 8-bit data paths. In the 4-bit mode, each data byte must be divided into a high-order and a low-order nibble and are transmitted sequentially, the high-nibble first. In the 8-bit parallel mode, each data byte is transmitted from the PIC to the controller as a unit. The advantage of using the 4-bit mode is greater economy of I/O lines on the PIC side. The disadvantages are slightly more complicated programming and minimally slower execution speed. Our first example and circuit uses the 8-bit data mode so as to avoid complications. Once
280 |
Chapter 13 |
the main processing routines are developed, make the necessary modifications so as to make possible the 4-bit data mode.
In addition to the data transmission mode, there are other circuit options to be considered. Two control lines between the microcontroller and the HD44780-driven LCD are necessary in all cases: one to the RS line to select between data and instruction input modes, and another one to the E line to provide the pulse that initiates the data transfer. The R/W control line, which selects between the read and the write mode of the LCD controller, can be connected or grounded. If the R/W line is not connected to a microcontroller port, then the HD44780 operates only in the write data mode and all read operations are unavailable.
13.1.1 Busy Flag or Timed Delay Options
Since many applications do not read text data from controller memory, the write-only mode is often an attractive option, especially considering that microcontroller I/O ports are often in short supply and that this option saves one port for other duties. However, there is a less apparent drawback to not being able to read LCD data, which is that the application is not able to monitor the busy flag. This flag, which indicates that the controller has concluded its operation, is mapped to bit 7. Since testing the BF requires reading this bit, not connecting the R/W line has the effect that applications cannot use the busy flag and must rely on timing routines to ensure that each operation completes before the next one begins. The timing requirements for each instruction are listed in the rightmost column in Table 13.3. The subjects of timing and delay routines are discussed in detail later in this chapter.
For the circuit designer, to read or not to read controller data is a decision with several tradeoffs. Using time delay routines to ensure that each controller operation has concluded is a viable option that saves one interface line. On the other hand, code that relies on timing routines is externally dependent on the clocks and timer hardware. If code that relies on timing routines is ported to another circuit with a different microcontroller, clocks, or timer hardware, the delays may change and the routines could fail. Furthermore, the use of delay routines often is not efficient, since controller operations can terminate before the timed delay has expired.
On the other hand, code that reads the busy flag to determine the termination of a controller operation is not without dangers. If the controller or the circuit fails, then the program can hang up in an endless loop, waiting for the busy flag to clear. To be absolutely safe, the code would have to contain an external wait loop when testing the busy flag, so that if the external loop expires, then the processing can assume that there is a hardware problem and break out of the flag test loop. The programmer must decide whether this safety mechanism for reading the busy flag is necessary since its implementation requires a somewhat complicated exception response.
In the code samples developed in this chapter, we implement both ways of ensuring operation completion. The code also furnishes a software switch that allows selecting the preferred option.

LCD Interfacing and Programming |
281 |
13.1.2 Contrast Control
In addition to the control lines that require processor interface, the HD44780 contains other control lines. One such line is used for the LCD contrast. The contrast control line (usually labeled Vee) is connected to pin number 3 (see Table 13.1). The actual implementation of the contrast control function varies according to the manufacturer.
In general, for an LCD with a normal temperature range, the contrast control line is wired as shown in Figure 13-3.
HD44780
1
14
+5 V
10K Ohm
Figure 13-3 Typical Contrast Adjustment Circuit
13.1.3 Display Backlight
Some LCDs are equipped with a LED backlight so as to make the displayed characters more visible. In different LCDs, backlight is implemented in different ways. Some manufacturers wire the backlight directly to the LCD power supply, while others provide additional pins that allow turning the backlight on or off independently of the LCD display. Backlit displays with 14 pins belong to the first type, while those with 16 pins have independent backlight control. If the backlight pins are adjacent to the other display pins, then they are numbered 15 and 16. In this case pin number 15 is wired, through a current limiting resistor, to the +5V source and pin 16 to ground. Sometimes the current-limiting resistor is built into the display. This information is available in the device’s data sheet.
Note that some 4-line displays use pins 15 and 16 for other purposes. In these systems, backlight control, if available, is provided by separate pins.
13.1.4 Display Memory Mapping
The Hitachi HD44780 is a memory-mapped system in which characters are displayed by storing their ASCII codes in the corresponding memory address associated with each digit-display area. The area of controller RAM mapped to character-display memory has a capacity of 80 characters. This area is known as display data RAM or
DDRAM.

282 |
Chapter 13 |
In order to save circuitry, the common lines of the controller outputs to the liquid crystal display hardware are multiplexed. In this context, the duty ratio of a system is the number of multiplexed common lines. The most common duty ratio is 1/16, although 1/8 and 1/11 are found in some systems. Since the duty ratio measures the number of multiplexed lines, it also determines the display mapping. For example, in a single-line-by-16 character display with a 1/16 duty ratio the first eight characters are mapped to one set of consecutive memory addresses and the second eight characters to another set of addresses. The reason is that in every display line, sixteen common access lines are multiplexed, instead of eight. By the same token, a two-line-by-sixteen character display with a 1/16 duty ratio requires 16 common lines. In this case, the address of the second lines is not a continuation of the address of the first line, but is in another address set not contiguous to the first one.
For example, in a typical two-line-by-sixteen character display, the addresses of the 16 characters in the first line are from 0x00 to 0x0F, while the addresses of the characters in the second line are from 0x40 to 0x4F. Since there are 80 memory locations in the controller’s DDRAM, each line contains storage for a total of 40 characters. The range of the entire first line is from 0x00 to 0x27 (40 characters total) but of these, only 16 are actually displayed. The same applies to the second line of 16 characters. In this case, the storage area is in the range 0x28 to 0x4f, but only 16 characters are displayed. In the single-line-by-sixteen character display mentioned first the addresses of the first eight characters would be a set from 0x00 to 0x07 and the addresses of the second eight characters in the line are from 0x40 to 0x47. Table 13.2 lists the memory address mapping of some common LCD configurations.
Table 13.2
7-bit DDRAM Address Mapping for Common LCDs
CHARACTERS/ |
LINE |
CHARACTER |
FIRST IN |
NEXT IN |
LAST IN |
ROW |
NUMBER |
NUMBER |
GROUP |
GROUP |
GROUP |
|
|
|
|
|
|
8/1 |
1 |
1 |
0x00 |
0x01 |
0x07 |
8/2 |
1 |
1 |
0x00 |
0x01 |
0x07 |
|
2 |
1 |
0x40 |
0x41 |
0x47 |
16/1 |
1 |
1 |
0x00 |
0x01 |
0x07 |
|
1 |
9 |
0x40 |
0x41 |
0x47 |
16/2 |
1 |
1 |
0x00 |
0x01 |
0x0f |
|
2 |
1 |
0x40 |
0x41 |
0x4f |
20/2 |
1 |
1 |
0x00 |
0x01 |
0x13 |
|
2 |
1 |
0x40 |
0x41 |
0x53 |
24/2 |
1 |
1 |
0x00 |
0x01 |
0x17 |
|
2 |
1 |
0x40 |
0x41 |
0x57 |
16/4 |
1 |
1 |
0x00 |
0X01 |
0x0f |
|
2 |
1 |
0x40 |
0x41 |
0x4f |
|
3 |
1 |
0x10 |
0x11 |
0x1f |
|
4 |
1 |
0x50 |
0x51 |
0x5f |
20/4 |
1 |
1 |
0x00 |
0x01 |
0x13 |
|
2 |
1 |
0x40 |
0x41 |
0x53 |
|
3 |
1 |
0x14 |
0x15 |
0x27 |
|
4 |
1 |
0x54 |
0x55 |
0x67 |