Êíèãà: Advanced PIC Microcontroller Projects in C

4.3.2 LCD Library

4.3.2 LCD Library

One thing all microcontrollers lack is some kind of video display. A video display would make a microcontroller much more user-friendly, enabling text messages, graphics, and numeric values to be output in a more versatile manner than with 7-segment displays, LEDs, or alphanumeric displays. Standard video displays require complex interfaces and their cost is relatively high. LCDs are alphanumeric (or graphic) displays which are frequently used in microcontroller-based applications. These display devices come in different shapes and sizes. Some LCDs have forty or more character lengths with the capability to display several lines. Others can be programmed to display graphic images. Some modules offer color displays, while others incorporate backlighting so they can be viewed in dimly lit conditions.

There are basically two types of LCDs as far as the interfacing technique is concerned: parallel and serial. Parallel LCDs (e.g., the Hitachi HD44780 series) are connected to the microcontroller circuitry such that data is transferred to the LCD using more than one line, usually four or eight data lines. Serial LCDs are connected to a microcontroller using one data line only, and data is transferred using the RS232 asynchronous data communications protocol. Serial LCDs are generally much easier to work with but more costly than parallel ones. In this book only parallel LCDs are discussed, as they are used more often in microcontroller-based projects.

Low-level programming of a parallel LCD is usually a complex task and requires a good understanding of the internal operation of the LCD, including the timing diagrams. Fortunately, mikroC language provides functions for both text-based and graphic LCDs, simplifying the use of LCDs in PIC-microcontroller-based projects. The HD44780 controller is a common choice in LCD-based microcontroller applications. A brief description of this controller and information on some commercially available LCD modules follows.

The HD44780 LCD Controller

The HD44780 is one of the most popular LCD controllers, being used both in industrial and commercial applications and also by hobbyists. The module is monochrome and comes in different shapes and sizes. Modules with 8, 16, 20, 24, 32, and 40 characters are available. Depending on the model, the display provides a 14-pin or 16-pin connector for interfacing. Table 4.3 shows the pin configuration and pin functions of a typical 14-pin LCD.

Table 4.3: Pin configuration of the HD44780 LCD module

Pin no. Name Function
1 VSS Ground
2 VDD +ve supply
3 VEE Contrast
4 RS Register select
5 R/W Read/write
6 EN Enable
7 D0 Data bit 0
8 D1 Data bit 1
9 D2 Data bit 2
10 D3 Data bit 3
11 D4 Data bit 4
12 D5 Data bit 5
13 D6 Data bit 6
14 D7 Data bit 7

VSS is the 0V supply or ground. The VDD pin should be connected to the positive supply. Although the manufacturers specify a 5V DC supply, the modules usually work with as low as 3V or as high as 6V.

Pin 3 is named as VEE and is the contrast control pin. It is used to adjust the contrast of the display and should be connected to a DC supply. A potentiometer is usually connected to the power supply with its wiper arm connected to this pin and the other leg of the potentiometer connected to the ground. This way the voltage at the VEE pin, and hence the contrast of the display, can be adjusted as desired.

Pin 4 is the register select (RS) and when this pin is LOW, data transferred to the LCD is treated as commands. When RS is HIGH, character data can be transferred to and from the module.

Pin 5 is the read/write (R/W) pin. This pin is pulled LOW in order to write commands or character data to the LCD module. When this pin is HIGH, character data or status information can be read from the module.

Pin 6 is the enable (EN) pin, which is used to initiate the transfer of commands or data between the module and the microcontroller. When writing to the display, data is transferred only on the HIGH to LOW transition of this pin. When reading from the display, data becomes available after the LOW to HIGH transition of the enable pin, and this data remains valid as long as the enable pin is at logic HIGH.

Pins 7 to 14 are the eight data bus lines (D0 to D7). Data can be transferred between the microcontroller and the LCD module using either a single 8-bit byte or two 4-bit nibbles. In the latter case, only the upper four data lines (D4 to D7) are used. The 4-bit mode has the advantage of requiring fewer I/O lines to communicate with the LCD.

The mikroC LCD library provides a large number of functions to control text-based LCDs with 4-bit and 8-bit data interfaces, and for graphics LCDs. The most common are the 4-bit-interface text-based LCDs. This section describes the available mikroC functions for these LCDs. Further information on other text-or graphics-based LCD functions are available in the mikroC manual.

The following are the LCD functions available for 4-bit-interface text-based LCDs:

• Lcd_Config

• Lcd_Init

• Lcd_Out

• Lcd_Out_Cp

• Lcd_Chr

• Lcd_Chr_Cp

• Lcd_Cmd

Lcd_Config  The Lcd_Config function is used to configure the LCD interface. The default connection between the LCD and the microcontroller is:

LCD Microcontroller port pin
RS           2
EN           3
D4           4
D5           5
D6           6
D7           7

The R/W pin of the LCD is not used and should be connected to the ground. This function should be used to change the default connection. It should be called with the parameters in the following order:

port name, RS pin, EN pin, R/W pin, D7 pin, D6 pin, D5 pin, D4 pin

The port name should be specified by passing its address. For example, if the RS pin is connected to RB0, EN pin to RB1, D7 pin to RB2, D6 pin to RB3, D5 pin to RB4, and the D4 pin to RB5, then the function should be called as follows:

Lcd_Config(&PORTB, 0, 1, 2, 3, 4, 5);

Lcd_Init  The Lcd_Init function is called to configure the interface between the microcontroller and the LCD when the default connections are made as just illustrated. The port name should be specified by passing its address. For example, assuming that the LCD is connected to PORTB and the preceding default connections are used, the function should be called as:

Lcd_Init(&PORTB);

Lcd_Out  The Lcd_Out function displays text at the specified row and column position of the LCD. The function should be called with the parameters in the following order:

row, column, text

For example, to display text “Computer” at row 1 and column 2 of the LCD we should call the function as:

Lcd_Out(1, 2, "Computer");

Lcd_Out_Cp  The Lcd_Out_Cp function displays text at the current cursor position. For example, to display text “Computer” at the current cursor position the function should be called as:

Lcd_Out_Cp("Computer");

Lcd_Chr  The Lcd_Chr function displays a character at the specified row and column position of the cursor. The function should be called with the parameters in the following order:

row, column, character

For example, to display character “K” at row 2 and column 4 of the LCD we should call the function as:

LCD_Chr(2, 4, 'K');

Lcd_Chr_Cp  The Lcd_Chr_Cp function displays a character at the current cursor position. For example, to display character “M” at the current cursor position the function should be called as:

Lcd_Chr_Cp('M');

Lcd_Cmd  The Lcd_Cmd function is used to send a command to the LCD. With the commands we can move the cursor to any required row, clear the LCD, blink the cursor, shift display, etc. A list of the most commonly used LCD commands is given in Table 4.4. For example, to clear the LCD we should call the function as:

Lcd_Cmd(Lcd_Clear);

Table 4.4: LCD commands

LCD command Description
LCD_CLEAR Clear display
LCD_RETURN_HOME Return cursor to home position
LCD_FIRST_ROW Move cursor to first row
LCD_SECOND_ROW Move cursor to second row
LCD_THIRD_ROW Move cursor to third row
LCD_FOURTH_ROW Move cursor to fourth row
LCD_BLINK_CURSOR_ON Blink cursor
LCD_TURN_ON Turn display on
LCD_TURN_OFF Turn display off
LCD_MOVE_CURSOR_LEFT Move cursor left
LCD_MOVE_CURSOR_RIGHT Move cursor right
LCD_SHIFT_LEFT Shift display left
LCD_SHIFT_RIGHT Shift display right

An example illustrates initialization and use of the LCD.

Example 4.12

A text-based LCD is connected to a PIC18F452 microcontroller in the default mode as shown in Figure 4.19. Write a program to send the text “My Computer” to row 1, column 4 of the LCD.


Figure 4.19: Connecting an LCD to a PIC microcontroller

Solution 4.12

The required program listing is given in Figure 4.20 (program LCD.C). At the beginning of the program PORTB is configured as output with the TRISB = 0 statement. The LCD is then initialized, the display is cleared, and the text message “My Computer” is displayed on the LCD.

/*********************************************************************
                     WRITING TEXT TO AN LCD
                     ======================
A text based LCD is connected to a PIC microcontroller in the default mode.
This program displays the text "My Computer" on the LCD.
Programmer: Dogan Ibrahim
File:       LCD.C
Date:       May, 2007
***********************************************************************/
void main() {
 TRISB = 0;                    // Configure PORTB as output
 Lcd_Init(PORTB);              // Initialize the LCD
 Lcd_Cmd(LCD_CLEAR);           // Clear the LCD
 Lcd_Out(1, 4, "My Computer"); // Display text on LCD
}


Figure 4.20: LCD program listing

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 1.141. Çàïðîñîâ Ê ÁÄ/Cache: 3 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç