Pantech.AI

How to Interface LCD16x2 in 8-bit with 8051

LCD 16×2 

LCDs (Liquid Crystal Displays) are commonly used in embedded systems to display status information or parameters.

The LCD 16×2 module has 16 pins, including 8 data pins (D0-D7) and 3 control pins (RS, RW, EN). The remaining 5 pins provide power and control the backlight.

Control pins configure the LCD in either command or data mode, as well as in read or write mode, determining when to read or write data.

The LCD 16×2 can operate in either 4-bit or 8-bit mode, depending on the application’s needs. To use it, specific commands must be sent in command mode for initial configuration. Once set up, data can be sent in data mode to display the desired content.

For more details on the LCD 16×2 and its usage, refer to the “LCD 16×2 Display Module” section in Sensors and Modules.

LCD 16×2 Pinout  

LCD 16×2 Pin Diagram

LCD16x2 with 8051 Interfacing Diagram

LCD 16×2 Interface with 8051

LCD16x2 with 8051 Pin Connection

LCD 162 Pins89c51 Pins
Data Pins    D0-D7PORT1
RSPORT2.0
RWPORT2.1
EPORT2.2

Programming LCD16x2

Initialize LCD16x2 

Initializing the LCD is straightforward:

  1. Power on the LCD.
  2. Wait 15ms for the LCD16x2 power-on initialization.
  3. Send the command 0x38 to set 2-line display, 5×8 character matrix, and 8-bit mode.
  4. Send a Display ON command (either 0x0E or 0x0C).
  5. Send the 0x06 command to enable cursor increment.
void LCD_Init (void)		/* LCD Initialize function */
{
	delay(20);		/* LCD Power ON Initialization time >15ms */
	LCD_Command (0x38);	/* Initialization of 16X2 LCD in 8bit mode */
	LCD_Command (0x0C);	/* Display ON Cursor OFF */
	LCD_Command (0x06);	/* Auto Increment cursor */
	LCD_Command (0x01);	/* Clear display */
	LCD_Command (0x80);	/* Cursor at home position */
}

The LCD is now successfully initialized and ready to receive data for display.

LCD16x2 Command Write Function:

  1. Send the command to the data port.
  2. Set the RS pin low (RS=0) to select the command register.
  3. Set the RW pin low (RW=0) to initiate a write operation.
  4. Apply a high-to-low pulse on the Enable (E) pin for at least 450ns.
  5. During this Enable pulse, the LCD latches the data on D0-D7 and executes the command, as RS is set to the command register.
void LCD_Command (unsigned char cmd)	/* LCD16x2 command funtion */
{
	lcd_data_port= cmd;
	rs=0;				/* command reg. */
	rw=0;				/* Write operation */
	en=1; 
	delay(1);
	en=0;
	delay(5);
}

LCD16x2 Data Write Function

  1. Send the data to the data port.
  2. Set the RS pin high (RS=1) to select the data register.
  3. Set the RW pin low (RW=0) to initiate a write operation.
  4. Provide a high-to-low pulse on the Enable (E) pin, lasting at least 450 ns.
  5. When the Enable pulse is given, the LCD latches the data from D0 to D7 and displays it on the 5×8 matrix, as RS is set to the data register.
void LCD_Char (unsigned char char_data)	/* LCD data write function */
{
	lcd_data_port=char_data;
	rs=1;				/*Data reg.*/
	rw=0;				/* Write operation*/
	en=1;   				
	delay(1);
	en=0;
	delay(5);
}

Note

Upon powering on, the LCD16x2 requires a self-initialization time of 15ms before it can accept any commands. When programming, ensure a delay of over 15ms after power-on before sending any commands to the LCD.

Most commands execute within microseconds, but the 0x01 command (Clear Display) takes 1.64ms to complete. Therefore, after issuing this command, a delay of at least 1.63ms is necessary to ensure proper execution.

LCD16x2 8051 Code 

/*  
   LCD16x2 8 bit 8051 interface
   http://www.electronicwings.com
*/


#include<reg51.h>

sfr lcd_data_port=0x90;		/* P1 port as data port */
sbit rs=P2^0;			/* Register select pin */
sbit rw=P2^1;			/* Read/Write pin */
sbit en=P2^2;			/* Enable pin */


void delay(unsigned int count)  /* Function to provide delay Approx 1ms */
{
	int i,j;
	for(i=0;i<count;i++)
	for(j=0;j<112;j++);
}

void LCD_Command (unsigned char cmd)  /* LCD16x2 command funtion */
{
	lcd_data_port= cmd;
	rs=0;			/* command reg. */
	rw=0;			/* Write operation */
	en=1; 
	delay(1);
	en=0;
	delay(5);
}

void LCD_Char (unsigned char char_data)  /* LCD data write function */
{
	lcd_data_port=char_data;
	rs=1;			/* Data reg.*/
	rw=0;			/* Write operation*/
	en=1;   				
	delay(1);
	en=0;
	delay(5);
}

void LCD_String (unsigned char *str) /* Send string to LCD function */
{
	int i;
	for(i=0;str[i]!=0;i++)  /* Send each char of string till the NULL */
	{
		LCD_Char (str[i]);  /* Call LCD data write */
	}
}

void LCD_String_xy (char row, char pos, char *str)  /* Send string to LCD function */
{
	if (row == 0)
	LCD_Command((pos & 0x0F)|0x80);
	else if (row == 1)
	LCD_Command((pos & 0x0F)|0xC0);
	LCD_String(str);	/* Call LCD string function */
}

void LCD_Init (void)		/* LCD Initialize function */
{	
	delay(20);		/* LCD Power ON Initialization time >15ms */
	LCD_Command (0x38);	/* Initialization of 16X2 LCD in 8bit mode */
	LCD_Command (0x0C);	/* Display ON Cursor OFF */
	LCD_Command (0x06);	/* Auto Increment cursor */
	LCD_Command (0x01);	/* clear display */
	LCD_Command (0x80);	/* cursor at home position */
}

void main()
{

	LCD_Init();		/* initialization of LCD*/

	LCD_String("ElectronicWINGS");  /* write string on 1st line of LCD*/
	LCD_Command(0xC0);
	LCD_String("Hello World");  /*write string on 2nd line*/

	while(1);		/* Infinite loop. */
	
}

LCD 16×2 Code Output

LCD16x2 string output

LCD16x2 Rolling Display

To scroll a string or character on the LCD, the following commands need to be used:

Commandmeaning
0x1cShift entire display right
0x18Shift entire display left

To roll the display, use the following steps in a loop:

  1. Display the string on the LCD.
  2. Roll it to the right by using the 0x1C command.
  3. Roll it to the left by using the 0x18 command.

Main function code for LCD16x2 Scrolling Display

void main()
{
  unsigned char i,shift;
	delay(5);
	LCD_Init();			/* initilize LCD */

	LCD_String("ElectronicWings");  /* display string */
	delay(1000);

	shift=15;			/* number of time shifts count=15 */
	
while(1)
{
	for(i=0;i<shift;i++)				
  {
		LCD_Command(0x1c);  	/* shift display right */
		delay(300);
  }
	
	shift=30;			/* number of time shifts 30 */
	
  for(i=0;i<30;i++)
  {
		LCD_Command(0x18);  	/* shift display left */
		delay(300);
  }
 }	

Leave a Comment

Your email address will not be published. Required fields are marked *