LCD Interfacing with 8051 Microcontroller

LCD Interfacing with 8051 Microcontroller





Hex CodeCommand to LCD Instruction Register
0FLCD ON, cursor ON
01Clear display screen
02Return home
04Decrement cursor (shift cursor to left)
06Increment cursor (shift cursor to right)
05Shift display right
07Shift display left
0EDisplay ON, cursor blinking
80Force cursor to the beginning of the first line
C0Force cursor to the beginning of the second line
382 lines and 5×7 matrix
83Cursor line 1 position 3
3CActivate the second line
08Display OFF, cursor OFF
C1Jump to the second line, position 1
OCDisplay ON, cursor OFF
C1Jump to the second line, position 1
C2Jump to a second line, position 2


Circuit Diagram :


Source Code:

// Program for LCD Interfacing with 8051 Microcontroller (P89V51RD2) 

#include<reg51.h>
#include <string.h>

#define display_port P2      //Data pins connected to port 2 on microcontroller
sbit rs = P0^0;  //Register Select pin connected to pin 0 of port 0
sbit rw = P0^1;  // Read/Write pin connected to pin 0 of port 1
sbit e =  P0^2;  //Enable pin connected to pin 0 of port 2

void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i<time;i++)    
    for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port = command;
    rs= 0;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}

void lcd_data(unsigned char disp_data)  //Function to send display _data to LCD
{
    display_port = disp_data;
    rs= 1;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}

 void lcd_init()    //Function to prepare the LCD  and get it ready
{
    lcd_cmd(0x38);  // for _using 2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0F);  // turn display ON, cursor blinking
    msdelay(10);
    lcd_cmd(0x01);  //clear screen
    msdelay(10);
    lcd_cmd(0x81);  // bring cursor to position 1 of line 1
    msdelay(10);
}
void main()
{
    unsigned char a[15]="Surajkumar";    //string of 14 characters with a null terminator.
    int l=0;
		int len = strlen(a);
	
    lcd_init();
	
		for(l;l<len;l++)
		{
			lcd_data(a[l]);
			msdelay(50);
			
		} 
		
	   // OR
	
		
    //while(a[l] != '\0') // searching the null terminator in the sentence
    //{
    //    lcd_data(a[l]);
    //    l++;
    //    msdelay(50);
    //}
 
  while(1);

}

Comments

Post a Comment

Popular posts from this blog

Serial Interrupt Programming in 8051 Microcontroller

Interfacing LED with 8051 Microcontroller in Assembly