16x2 LCD Interface with 8051 (8-bit Mode)
This tutorial explains how to interface a 16x2 LCD display with the 8051 microcontroller using 8-bit mode.
📌 Overview
A 16x2 LCD can display:
- 16 characters per line
- 2 lines
We will connect:
- Data Pins (D0–D7) → Port 2
- Control Pins (RS, RW, EN) → Port 0
🔌 Pin Configuration
LCD Pin Description
| Pin | Name | Description |
|---|---|---|
| 1 | VSS | Ground |
| 2 | VDD | +5V |
| 3 | VEE | Contrast control |
| 4 | RS | Register Select |
| 5 | RW | Read/Write |
| 6 | EN | Enable |
| 7-14 | D0-D7 | Data Pins |
| 15 | LED+ | Backlight + |
| 16 | LED- | Backlight - |
🔗 Circuit Connections
Data Lines (8-bit Mode)
- D0 → P2.0
- D1 → P2.1
- D2 → P2.2
- D3 → P2.3
- D4 → P2.4
- D5 → P2.5
- D6 → P2.6
- D7 → P2.7
Control Lines
- RS → P0.0
- RW → P0.1
- EN → P0.2
⚙️ Control Signal Function
| Pin | Function |
|---|---|
| RS = 0 | Command mode |
| RS = 1 | Data mode |
| RW = 0 | Write |
| RW = 1 | Read |
| EN | High-to-low pulse to latch data |
🧠 LCD Commands
| Command | Function |
|---|---|
| 0x38 | 8-bit mode, 2 lines |
| 0x0C | Display ON, cursor OFF |
| 0x01 | Clear display |
| 0x06 | Entry mode |
💻 8051 C Program
#include <reg51.h>
#define LCD_DATA P2
sbit RS = P0^0;
sbit RW = P0^1;
sbit EN = P0^2;
// Delay function
void delay(unsigned int time) {
unsigned int i, j;
for(i = 0; i < time; i++)
for(j = 0; j < 1275; j++);
}
// Command function
void lcd_cmd(unsigned char cmd) {
LCD_DATA = cmd;
RS = 0;
RW = 0;
EN = 1;
delay(1);
EN = 0;
}
// Data function
void lcd_data(unsigned char dat) {
LCD_DATA = dat;
RS = 1;
RW = 0;
EN = 1;
delay(1);
EN = 0;
}
// LCD Initialization
void lcd_init() {
lcd_cmd(0x38); // 8-bit, 2 line
lcd_cmd(0x0C); // Display ON
lcd_cmd(0x01); // Clear screen
lcd_cmd(0x06); // Entry mode
}
// Print string
void lcd_string(char *str) {
while(*str) {
lcd_data(*str++);
}
}
// Main function
void main() {
lcd_init();
lcd_string("Hello 8051!");
while(1);
}