4 bit LCD Interfacing with PIC Microcontroller

In the previous chapter, we discussed how a character LCD is interfaced with a PIC microcontroller in 8-bit mode. A character LCD can be configured in 8 bit or 4-bit mode in which 8 data pins and 4 data pins are used respectively this feature allows efficient use of the digital I/0 pins of the microcontroller. This article explains 4 bit LCD interfacing with PIC Microcontroller.

Character LCD

A 16×2 character liquid crystal display (LCD) module is the most basic electronic display module which is widely used. The module will consist of 2 rows each with 16 columns which can display 16 characters. Several other LCD modules are also available like 20×4 dimension LCD which can display 20 characters per line and 4 such lines would be available. The choice for the module depends on the requirement.

The main advantage of using a character LCD instead of a seven segment display and other multi-segment LEDs is that there is no limitation in displaying special & custom character animations and so on. All character LCDs will have 16 pins among which 8 are data pins through which data or commands are passed into the LCD registers.

Charactor-lcd

Features

The features of a character LCD module make it more suitable as an electronic display than 7 segment displays and other multi-segment display modules. Most importantly the module can be interfaced much easily unlike other modules with no complexity in both hardware and software.The 4-bit mode interfacing of the LCD module enables an efficient method of saving the number of general purposes I/O pins which is a major criterion for an embedded system designer. There is no limitation in characters which can be displayed using the module. The contrast of the module can be adjusted using the VEE pin of the module and LED backlight can be enabled with LED+ and LED- which makes the display more bright.

Register

A Character LCD consists of two registers

  1. Command register
  2. Data register

The RS(Register Select) pin of the LCD module is used to select the specific register. When RS is low, command register is selected and when RS is high, data register is selected. State of R/W pin determines the operation to be performed whether to read or write data.

All instructions to be executed by the LCD are latched into the command register of the LCD. LCD commands include a clear display, the cursor on/off, display shift and so on.

Character to be displayed on the LCD is stored in the data register. The data is stored temporarily in the data register.

Commands

Commands are instructions given to the LCD module to perform a predefined task. The task to be performed is defined by the manufacturer. Some of the LCD commands are listed below.

  1. Clear display – 0x01
  2. The display on, cursor off – 0x0C
  3. Increment cursor – 0x04

The rest of the commands can be obtained from the device datasheet.

Sending commands

Hardware setup for interfacing the character LCD in 4-bit mode is by connecting the 4 data lines to the GP I/O pins of the controller. Sending a command or data into an LCD configured in 4-bit mode from 8-bit mode such that data need to be sent a nibble after the another. MSB of the data is sent first followed by LSB.

The LCD can be configured in 4-bit mode by sending appropriate instruction which is called ‘Function set’ to it. The function set is the hexadecimal instruction for the LCD MPU to select the working model of LCD. According to the table in the datasheet of the LCD module, the value of function set to initialize the LCD module in 4-bit mode can be calculated as 0x20.

void lcd_init ()
{

    RS=0;RW=0;
    Data_PORT =0x20;
    EN=1;
    delay_ms(5);
    EN=0;

    lcd_cmd(0x28);       // 4-bit mode - 2 line - 5x7 font.
    lcd_cmd(0x0C);       // Display no cursor - no blink.
    lcd_cmd(0x06);       // Automatic Increment - No Display shift.
    lcd_cmd(0x80);       // Address DDRAM with 0 offset 80h.
 }

RS, RW and Enable pins should be macro defined in the firmware for example.

#define RS                  LATBbits.LATB2                        /* Register select pin definition                         */
#define RW                  LATBbits.LATB1                        /* Read/Write pin definition                              */
#define EN                  LATBbits.LATB0                        /* Enable  pin definition                                 */
#define Data_PORT           PORTD                                 /* DATA PORT definition                                   */
#define CTRL_PORT_DIR       TRISB
#define DATA_PORT_DIR       TRISD

Since we know that only for data lines will be connected in hardware with the microcontroller port. The data needs to be sent in 4 bits. MSB nibble can be sent in first followed by LSB nibble. For example:

void lcd_cmd (char cmd)
{
    RS=0;RW=0;
    Data_PORT = cmd;
    EN=1;
    delay_ms(5);
    EN=0;
    Data_PORT =((cmd<<4) & 0xF0);
    EN=1;
    delay_ms(5);
    EN=0;
}

It is not mandatory to follow the above-given logic. The control port is selected different from data port to reduce the complexity of the firmware.

Sending data

Sending data to a 4 bit LCD follows the same method as done with the command except that the data register needs to be selected instead of the command register by driving the RS line high.

lcd-wave-form-read

void lcd_data(char data)
{
    RS=1;RW=0;
    Data_PORT =data;
    EN=1;
    delay_ms(5);
    EN=0;
    Data_PORT=((data<<4)&0xF0);
    EN=1;
    delay_ms(5);
    EN=0;
}

4 bit LCD interfacing with PIC Microcontroller – Circuit diagram

4 bit LCD interfacing with PIC Microcontroller
Character LCD 4-bit interfacing circuit