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.
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
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