LCD 16×2
LCDs (Liquid Crystal Displays) are commonly used in embedded systems to display status information or parameters.
The LCD 16×2 is a 16-pin device, consisting of 8 data pins (D0-D7) and 3 control pins (RS, RW, EN). The remaining 5 pins are for power supply and backlighting.
The control pins allow us to configure the LCD in command or data mode, as well as in read or write mode, specifying when to perform these actions.
The LCD 16×2 can operate in either 4-bit or 8-bit mode, depending on the application’s requirements. To use the display, we first send commands in command mode to configure the LCD, after which we can send data in data mode to display the required information.
For more information about the LCD 16×2 and how to use it, refer to the “LCD 16×2 Display Module” section in Sensors and Modules.
LCD16x2 Pinout
16×2 LCD Display
LCD 16×2 4-bit Mode
- In 4-bit mode, data/command is sent in a 4-bit (nibble) format.
- To do this 1st send a Higher 4-bit and then send a lower 4-bit of data/command.
- Only 4 data (D4 – D7) pins of 16×2 of LCD are connected to the microcontroller and other control pins RS (Register select), RW (Read/write), and E (Enable) is connected to other GPIO Pins of the controller.
- Therefore, due to such connections, we can save four GPIO pins which can be used for another application.
LCD16x2 4-bit mode connection with 8051
LCD 16×2 4 bit mode connection with 8051
Programming LCD16x2 4-bit mode
Initialization
- Wait for 15ms to allow the LCD16x2 power-on initialization time.
- Send the
0x02
command to initialize the LCD in 4-bit mode. - Send the
0x28
command to configure the LCD for 2-line display, 4-bit mode, and 5×8 dot character matrix. - Send a Display ON command (
0x0E
or0x0C
). - Send the
0x06
command to set the cursor to increment automatically.
void LCD_Init (void) /* LCD Initialize function */
{
delay(20); /* LCD Power ON Initialization time >15ms */
LCD_Command (0x02); /* 4bit mode */
LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit 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 */
}
Now that the LCD is successfully initialized and ready to accept data in 4-bit mode, we can send command/data to the LCD by transmitting the higher nibble followed by the lower nibble. Since the 16×2 LCD’s data pins (D4-D7) are used, the lower nibble needs to be shifted right by 4 before transmission.
LCD 16×2 4-bit Mode Command Write Function:
The LCD is now successfully initialized and ready to accept data in 4-bit mode for display.
To send command/data to the 16×2 LCD, we need to first send the higher nibble followed by the lower nibble. Since the LCD’s data pins (D4-D7) are used, the lower nibble must be shifted right by 4 bits before transmission.
LCD 16×2 4-bit Mode Command Write Function:
- First, send the higher nibble of the command (the first 4 bits).
- Set the RS pin low (RS=0) to select the command register.
- Set the RW pin low (RW=0) to initiate a write operation (or connect it to ground).
- Apply a high-to-low pulse on the Enable (E) pin.
- Next, send the lower nibble of the command (the last 4 bits).
- Apply another high-to-low pulse on the Enable (E) pin.
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0);/* Send upper nibble */
rs=0; /* Command reg. */
rw=0; /* Write operation */
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
Data Write Function for LCD 16×2 in 4-bit Mode:
- Send the higher nibble of the data:
- Set the RS pin high (RS = 1) to select the data register.
- Set the RW pin low (RW = 0) for write operation (or connect RW to ground).
- Provide a high-to-low pulse on the Enable (E) pin.
- Send the lower nibble of the data:
- Shift the lower nibble to the right by 4 bits.
- Provide another high-to-low pulse on the Enable (E) pin.
void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0);/* Send upper nibble */
rs=1; /* Data reg.*/
rw=0; /* Write operation*/
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (char_data << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
Code 8051 LCD16x2 4 bit mode
/*
LCD16x2 4 bit 8051 interface
http://www.electronicwings.com
*/
#include<reg51.h>
sfr LCD_Port=0x90; /* P1 port as data port */
sbit rs=P1^0; /* Register select pin */
sbit rw=P1^1; /* Read/Write pin */
sbit en=P1^2; /* Enable pin */
/* Function to provide delay Approx 1ms with 11.0592 Mhz crystal*/
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<112;j++);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0);/* Send upper nibble */
rs=0; /* Command reg. */
rw=0; /* Write operation */
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0);/* Send upper nibble */
rs=1; /*Data reg.*/
rw=0; /*Write operation*/
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (char_data << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
void LCD_String (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 (0x02); /* 4bit mode */
LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit 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); /* Go to 2nd line*/
LCD_String_xy(1,0,"Hello World"); /*write string on 2nd line*/
while(1); /* Infinite loop. */
}
LCD 16×2 4bit mode output with 8051
LCD 16×2 4bit mode output with 8051