ad space

Program Code for Interfacing AT89S52 with LCD

Here a program code for AT89S52 microcontroller to display text on 16x2 LCD is provided. AT89S52 is a 8051 microcontroller architecture based microcontroller manufactured by ATMEL,now Microchip. Although this microcontroller is old it is still valuable as a learning point for computer and electronics engineers. This microcontroller is very basic and therefore used for teaching purpose to graduates in electronics engineering colleges and universities. In the previous AT89S52 microcontroller tutorial, Programming & Interfacing LED and Switch with AT89S52, we showed how to interface led and switch with AT89S52 microcontroller. Here we will illustrate how to interface and work with LCD. 

The following shows the circuit diagram of interfacing LCD with AT89S52 microcontroller.

LCD AT89S52

Here the port 1 and port 0 of the microcontroller are used to connect 8 data lines and 3 control signal lines of the 16x2 LCD. The circuit was simulated in Proteus Professional electronics and PCB design software.

The following is the program code for AT89S52 with LCD.


#include <reg52.h>
#define LCDData P1

sbit En=P2^0;  
sbit RW=P2^1;  
sbit RS=P2^2;							

void LCDinit(void);
void cmdsend(unsigned char);
void MSDelay(unsigned char);
unsigned char strlen(unsigned char);

     
void main(void){
	unsigned char message[]="www.ee-diary.com";
	unsigned char Len;
	unsigned char z;

Len = strlen(message);
  
LCDinit();

for (z=0;z<=Len;z++){
	if(z == 16) 
		cmdsend(0xC0);
	if(z == 32){
		cmdsend(0x01); 
		cmdsend(0x80);
	} 

	LCDData=message[z];
	RS=1;
	RW=0;
	En=1; 
	MSDelay(2);
	En=0; 
	MSDelay(20);
	}
}

unsigned char strlen(char s[]){
	int i;
	i = 0;
	while (s[i] != '\0')
	++i;
	return i;
}

void LCDinit(){
      cmdsend(0x38);                  //function set: 8 bit, 2 lines, 5x7 dot matirx
      cmdsend(0x0E);                  //display on and cursor on
      cmdsend(0x01);                  // clear display
      cmdsend(0x06);                  // entry mode: move cursor right, don’t shift display
      cmdsend(0x80);                  //cursor position to first position of first line
      }
      
void cmdsend(unsigned char cmdword){ 
      P1 = cmdword;
      RS=0;          //select the instruction register
      RW=0;          // 0 for write operation
      En =1;         //start data write
      MSDelay(2);
      En =0;         // end data write
      }
      
void MSDelay(unsigned char t){
	unsigned int i,j;
	for (i=0;i<t;i++)
	for (j=0;j<1275;j++);
   }

The above program code was compiled using Keil C compiler. In the above program, the LCD is used using 8 lines but we can also use just 4 lines. This is set by sending suitable command to the LCD. This is done in the LCDinit() function. In this function we set up the LCD such as 8 or 4 lines, configure whether to use and display cursor, shifting etc. The cmdsend() is command to send the command to the LCD. The MSDelay() function is used for creating delay in millisecond. The crystal frequency used is 11.0596MHz.

Post a Comment

Previous Post Next Post