LCD interfacing 8051 Microcontrollers 4-bit
Interfacing Standard Alphanumeric LCD using 4 bit mode in 8051 MCU
In this article i will explain how to interface a standard 16×2 or 20×4 alphanumeric LCD to 8051 microcontrollers using 4 bit data mode. “LCD.h” file is available for free download from here.
LCD is the most common display device used by the designer for testing purpose and for displaying real time or stored data. It is very easy to drive a LCD module using a microcontroller.
Table below shows the pin details of LCD (16 pins)
Circuit Diagram shows Connection of LCD to any PORT of Microcontroller.
While using LCD in 4 bit data mode it saves 4 bits of our total GPIO lines, that’s why it is most commonly used. MSB of any data or commnad is sent first over 4 bits and then 4 LSB sent by shifting the data byte 4 times left.
Below is the code which is compiled usiong Keil uVision3 Compiler.
/**************************************************************** File name : LCD.H for driving LCD in 4bit mode Auther: Anupam Dubey (dubey.anupam@gmail.com) *****************************************************************/#define LCDPORT P2 #define RS P2_0 #define RW P2_1 #define E P2_2 bit status=0 #define lcd_delay 60 void delay(unsigned int j) { unsigned int i,k; for(i=0;i<j;i++) { for(k=0;k<100;k++); } } void _lcd_init_write(unsigned char a) { RS = 0; RW = 0; LCDPORT=a; E=1; delay(lcd_delay); E=0; } void lcd_com(unsigned char a){ unsigned char temp; if(status){ status=0; goto a; } RS=0; a: RW=0; temp=a; temp&=0xF0; LCDPORT&=0x0F; LCDPORT|=temp; E=1; delay(lcd_delay); E=0; temp=a<<4; temp&=0xF0; LCDPORT&=0x0F; LCDPORT|=temp; E=1; delay(lcd_delay); E=0; } void lcd_data(unsigned char a){ status=1; RS=1; lcd_com(a); } void lcd_init(void){ delay(lcd_delay); _lcd_init_write(0x30); delay(lcd_delay); _lcd_init_write(0x30); delay(lcd_delay); _lcd_init_write(0x30); delay(lcd_delay); _lcd_init_write(0x20); delay(lcd_delay); lcd_com(0x28); delay(lcd_delay); lcd_com(4); delay(lcd_delay); lcd_com(0x85); delay(lcd_delay); lcd_com(6); delay(lcd_delay); lcd_com(1); delay(lcd_delay); } void lcd_puts(char *aaa) { unsigned int i=0; for(;aaa[i]!=0;i++)lcd_data(aaa[i]); }
#define LCDPORT P2#define RS P2_0#define RW P2_1#define E P2_2bit status=0;#define lcd_delay 60void delay(unsigned int j){unsigned int i,k;for(i=0;i<j;i++){for(k=0;k<100;k++);}}void _lcd_init_write(unsigned char a){RS = 0;RW = 0;LCDPORT=a;E=1;delay(lcd_delay);E=0;}void lcd_com(unsigned char a){unsigned char temp;if(status){status=0;goto a;}RS=0;a:RW=0;temp=a;temp&=0xF0;LCDPORT&=0x0F;LCDPORT|=temp;E=1;delay(lcd_delay);E=0;temp=a<<4;temp&=0xF0;LCDPORT&=0x0F;LCDPORT|=temp;E=1;delay(lcd_delay);E=0;}void lcd_data(unsigned char a){status=1;RS=1;lcd_com(a);}void lcd_init(void){delay(lcd_delay);_lcd_init_write(0x30);delay(lcd_delay);_lcd_init_write(0x30);delay(lcd_delay);_lcd_init_write(0x30);delay(lcd_delay);_lcd_init_write(0x20);delay(lcd_delay);lcd_com(0x28);delay(lcd_delay);lcd_com(4);delay(lcd_delay);lcd_com(0x85);delay(lcd_delay);lcd_com(6);delay(lcd_delay);lcd_com(1);delay(lcd_delay);}void lcd_puts(char *aaa){unsigned int i=0;for(;aaa[i]!=0;i++)lcd_data(aaa[i]);}
LCD Interfacing WinAVR Routines
Circuit Diagram For LCD interface
LCD.H
#define lcd_port PORTB //LCD Registers addresses #define LCD_RS 0×01 #define LCD_RW 0×02 #define LCD_EN 0×04 void lcd_reset(void) { lcd_port = 0xFF; _delay_ms(20); lcd_port = 0×30+LCD_EN; lcd_port = 0×30; _delay_ms(10); lcd_port = 0×30+LCD_EN; lcd_port = 0×30; _delay_ms(1); lcd_port = 0×30+LCD_EN; lcd_port = 0×30; _delay_ms(1); lcd_port = 0×20+LCD_EN; lcd_port = 0×20; _delay_ms(1); } void lcd_cmd (char cmd) { lcd_port = (cmd & 0xF0)|LCD_EN; lcd_port = (cmd & 0xF0); lcd_port = ((cmd << 4) & 0xF0)|LCD_EN; lcd_port = ((cmd << 4) & 0xF0); _delay_ms(2); _delay_ms(2); } void lcd_init (void) { lcd_reset(); // Call LCD reset lcd_cmd(0×28); // 4-bit mode – 2 line – 5×7 font. lcd_cmd(0×0C); // Display no cursor – no blink. lcd_cmd(0×06); // Automatic Increment – No Display shift. lcd_cmd(0×80); // Address DDRAM with 0 offset 80h. } void lcd_data (unsigned char dat) { lcd_port = ((dat & 0xF0)|LCD_EN|LCD_RS); lcd_port = ((dat & 0xF0)|LCD_RS); lcd_port = (((dat << 4) & 0xF0)|LCD_EN|LCD_RS); lcd_port = (((dat << 4) & 0xF0)|LCD_RS); _delay_ms(2); _delay_ms(2); }
void lcd_puts(char *aaa) { unsigned int i; for(i=0;aaa[i]!=0;i++) lcd_data(aaa[i]); }
MAIN.C
#include<avr/io.h> //HEADER FILE FOR AVR INPUT OUTPUT #include<compat/deprecated.h>//HEADER FILE FOR FUNCTIONS LIKE SBI #include<util/delay.h> #include<lcd.h> /*—————————————————————- —————–MAIN PROGRAM——-q—————————— —————————————————————–*/ int main(void) { //SET 1 for OUTPUT PORT DDRB=0xFF; //SET 0 FOR INPUT PORT PORTB=0×00; lcd_init(); lcd_puts(”Devesh LCD demo”); lcd_cmd(0xc0); lcd_puts(”electroons.com”); while(1) {; } return 0; }
Contact-
Devesh Samaiya
devesh@electroons.com






