In this article we will program 8051 microcontroller for interface with 16x2 character LCD. Here are the basics behind LCD pins.
Connections:
Microcontroller used is P89V51RD2 in keil and 80C51 in proteus.
Program explaination:
- Include header file depending upon your microcontroller.
- Define RS and EN pins
- Declare required functions
- write main.
- Write function's code.
Main function:
First we have to initialize LCD. After initialization, write string to LCD.
Delay function:
Delay function is required for waiting between to instructions on LCD or writing character on LCD. It can be used to adjust width of EN pulse. Delay between 2 instructions is required because if execution of previous instruction is not done yet then LCD can misbehave.
LCD Initialization function:
Initializing RS and EN with zero value is not compulsory here. First give some delay because we have to wait for some time after turning on power of microcontroller. Then select mode of LCD.
- 0x38 selects 8 bit mode with 2 lines on LCD and each character representation is done by 5x7 dot matrix.
- 0x0E s used to turn on display and cursor.
- 0x01 for clearing display if in case any garbage value is previously present on LCD then it will be cleared.
- 0x06 is used for starting entries for LCD.
- 0x80 is used for moving cursor to first character space in line 1.
wrcmd function:
It is used for write command function. It has 2 arguments first argument is for 8 bit signal to be given to LCD for instruction or character. Second argument is for selecting mode between instruction and character. If we want to pass instruction then send 0 from wrcmd's second argument as used in init_lcd function. If we want to pass character then send 1 from wrcmd's second argument as used in write_to_lcd (i.e. write string) function. give received argument to RS and post connected to LCD in function. At the end provide one pulse on EN pin so that at the falling edge instruction will be executed or character will be written on LCD.
write_to_LCD function:
Here we receive pointer to character array as argument and each character is sent for printing on LCD one by one until Null character is reached.
//header file
#include<reg51.h>
//RS and EN pins are connected to P3.0 and P3.1 respectively
sbit RS = P3^0;
sbit EN = P3^1;
//required functions
void delay(unsigned char);
void init_LCD();
void wrcmd(unsigned char, unsigned char);
void write_to_LCD(unsigned char *);
int main(void)
{
//initalise LCD
init_LCD();
//write string to LCD
write_to_LCD("Hello World!");
while(1)
{
}
}
//Delay function
void delay(unsigned char i)
{
unsigned char j,k;
for(j=0;j<i;j++)
{
for(k=0;k<255;k++);
}
}
void init_LCD()
{
RS=0;
EN=0;
//delay for power on
delay(255);
//defines 2 lines
//8 bit mode
//5x7 dot representation
wrcmd(0x38,0);
//for 4 bit mode use wrcmd(0x28,0);
delay(5);
//display on
//cursor on
wrcmd(0x0E,0);
delay(5);
//clear display
wrcmd(0x01,0);
delay(5);
//go into entry mode
wrcmd(0x06,0);
delay(5);
//initialise cursor position to line 1 first place
wrcmd(0x80,0);
}
//LCD write command
void wrcmd(unsigned char n, unsigned char m)
{
//select mode between instrucrtion and character
//m=0 for instruction
//m=1 for character
RS=m;
//write instruction/character on port connected to LCD
P2=n;
//Enable pulse
EN=1;
delay(1);
EN=0;
}
//write string to LCD
void write_to_LCD(unsigned char *a)
{
//condition for end of string
while(*a!='\0')
{
//write character
wrcmd(*a,1);
//increase pointer
a++;
delay(5);
}
}
LCD interfacing with 8051 microcontroller |
If you have any doubts about this code then comment below, I will try my level best to solve them. Click on this link to download the program file and simulation file.
Note: If you are connecting RS and EN to port 0 then be aware then port 0 is drain port and here you have to use pull up register otherwise circuit won't work.
No comments:
Post a Comment