Wednesday 6 March 2013

HOW TO INTERFACE 16X2 LCD WITH 8051 MICROCONTROLLER FOR DISPLAYING A CHARACTER



Interface 16x2 LCD with microcontrollers AT89C51,AT89S51 , AT89S52, AT89C52, AT89C2051 with C program



This topic explains how to display a single character on 16x2 LCD by interfacing it to 8051 microcontroller (AT89C51) with busy flag checking. A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers data and command. A single character can be displayed on LCD by properly configuring its data and command registers. 

Connection diagram
1. Command/Instruction Register- stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing, clearing the screen, setting the cursor position, controlling display etc.

 2. Data Register- stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

To Program the LCD:

1. Data pin8 (DB7) of the LCD is busy flag and is read when R/W = 1 & RS = 0. When busy flag=1, it means that LCD is not ready to accept data since it is busy with the internal operations. Therefore before passing any data to LCD, its command register should be read and busy flag should be checked.

2. To send data on the LCD, data is first written to the data pins with R/W = 0 (to specify the write operation) and RS = 1 (to select the data register). A high to low pulse is given at EN pin when data is sent. Each write operation is performed on the positive edge of the Enable signal.

3. To send a command on the LCD, a particular command is first specified to the data pins with R/W = 0 (to specify the write operation) and RS = 0 (to select the command register). A high to low pulse is given at EN pin when data is sent.

Here P1 port of the microcontroller is used as output port which sends the data byte to data pins of the LCD. The control pins (pin 4, 5 & 6) are connected to pins 0, 1 & 6, respectively, of P2 port of the microcontroller. Pin 3 of LCD is connected to a preset of 10k to adjust the contrast on LCD screen. Here busy flag of data pin 7 is checked before sending any command or data to LCD in the function lcdready().

Program for interfacing LCD with microconroller8051
#include<reg51.h>
#define cmdport  P2
#define dataport  P1            //P2=LCD data pins
sbit rs=cmdport^0;
sbit rw=cmdport^1;
sbit en=cmdport^2;
sbit busy=dataport^7;
void delay(unsigned int value)
{
unsigned int i,j;
for(i=0;i<value;i++)
for(j=0;j<1275;j++);
}
void lcdready()    // busy flag checking function
{
busy=1;         // make busy pin as input
rs=0;
rw=1;
while(busy==1)
{
en=0;
delay(1);
en=1;
return;
}
void  lcdcmd(unsigned  char  command)
{
lcdready();      // check the busy flag
dataport=command;
rs = 0;                                      // rs  =0 for command
rw = 0;           // r/w =0 for write operation
en = 1;
delay(1);
en = 0;
return;
}
void  lcddata(unsigned  char  display)

{
lcdready();
dataport=display;
rs= 1;                           // rs  =1 for data
rw=0;           // r/w =0 for write operation
en=1;
delay(1);
en=0;
return;
}

void main()
{
lcdcmd(0x38);            // init LCD 2 lines,5x7 matrix
lcdcmd(0x0E);         // LCD on,cursor blinking
lcdcmd(0x01);         // clear LCD screen
lcdcmd(0x06);         // shift cursor right
lcdcmd(0x86);         //cursor line 1,position 6
lcddata('A');
}


Related Projects
Automatic Night Lamp with Morning Alarm
Embedded & Microcontroller Project Topics

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...