LCD Interfacing with 8051 microcontroller

AT89S51 is an electronic device falling under the category of the microcomputer. It is basically an 8-bit CMOS device capable of performing high at low-power and comes with flash programmable and erasable read-only memory (PEROM) of 4K byte size. AT89S51 is compatible with the industry-standard MCS-51 instruction set as well as pinout and the device is manufactured using Atmel’s high-density nonvolatile memory technology. Being a highly powerful microcomputer, AT89S51 provides effective solutions to numerous embedded control applications. In this chapter, the process of LCD interfacing with 8051(AT89S51 )is being discussed.

Character LCD (Liquid Crystal Display)

A 16×2 character LCD module is the most basic electronic display device. 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. 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.

Character LCD pinout for  interfacing lcd with 8051

Features

The features of a character LCD module make it more suitable for 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-purpose I/O pins which is a major criterion for an embedded system designer. There is no limitation in characters that can be displayed using the module. The contrast of the module can be adjusted using the VEE pin of the module and LED backlight which makes the display more bright can be enabled with LED+ and LED- pin.

Registers

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, the command register is selected and when RS is high, the 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, a 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. Display on, cursor off – 0x0C
3. Increment cursor – 0x04

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

Example Firmware for Interfacing LCD with 8051

The firmware should include the general header file for 8051 microcontrollers which is reg51.h. The header file also contains a delay library.

#include <reg51.h>                     
#include "delay.h"                          //header file for delay library

Sending the Commands

The register select pin of the LCD module should be connected to a general-purpose I/O pin and the corresponding pin should be made low. R/W pin should be grounded to select the write operation. The command register will not be accessed. Enabling the LCD would latch in the value of the data port into the command register of the module. Enabling the module involves applying a high to low pulse to the Enable pin of LCD.

void LCD_cmd(unsigned char command)
{
LCD_command_mode();                    //access in command mode
DATA_PORT=command;                     //command in data port
    
/* LCD enabled with high to low transition in enable     pin
     latches the command data in to the command register of LCD*/
ENABLE_PIN=1;
Delay_us(5000);    
ENABLE_PIN=0;    
}

RS, R/W, Data_PORT, and Enable_pin should be macro defined in the firmware.

Example:

/* access the LCD in data write mode and 
command write mode with RS=1,RW=0 & RS=0,RW=0 respectively */
#define LCD_command_mode(); P3_1=0;P3_2=0;
#define LCD_datawrite_mode(); P3_1=0;P3_2=1; 

/* defines the dataport and control port(Enable pin) */
#define DATA_PORT P2
#define ENABLE_PIN P3_0

Sending Data

Data write operation involves similar steps as that of command write operation except data register should be selected by setting the RS pin and grounding the R/W pin. Enabling the module would then latch in the value in the data port to the data register of the module and the corresponding character will be displayed on the LCD module.

LCD data sending

Example firmware:

void LCD_write_char(unsigned char character)
{
  LCD_datawrite_mode();               //access in data write mode
    DATA_PORT=character;                //data in the data port
    
/* LCD enabled with high to low transition in enable     pin
     latches the data into the data register of LCD*/
    
    ENABLE_PIN=1;
  Delay_us(5000);    
  ENABLE_PIN=0;    
}

RS, R/W, Data_PORT, Enable_pin should be macro defined in the firmware.

Initializing the LCD

Prior to writing data into the character LCD, it needs to be initialized. Initialization is done to configure the module for the specific use. It involves writing some initialization commands into the command register. Some of the initialization commands include a command to turn the display on and cursor off, the command to set the cursor at the preferred position and the command to set the option for cursor increment or decrement and so on.

Ex:

void LCD_init()
{
 LCD_cmd(0x38);                                  /* LCD command - 5x7 matrix                                 */
 LCD_cmd(0x83);                                  /* LCD command - Force cursor to the begining of first line */
 LCD_cmd(0x0C);                                  /* LCD command - Display ON, cursor OFF                     */
 LCD_cmd(0x01);                                  /* LCD command - Clear display                              */    
 Delay_us(10000);
}