Interfacing of LCD plays a significant role in wide range of digital display applications such as Voltmeter, Ammeter, Locker etc.But many times we find it difficult to interface and program the LCD properly to make it display our desired characters.Through this post i am going to explain how to interface a 16x2 LCD with a 8051 Microcontroller and programming it using Embedded C.
16x2 LCD:
This type of LCD module is very common and used widely in many types of display applications.It consists of 16 rows and 2 columns of 5x7 dot marices.The LCD display was a 16 pin package with back light,Contrast adjustment and 5x7 dot resolution.It consists of two built in registers known as data and Command register each has a specific function to perform with the display.The Data register is for writing the data to be displayed and Command register is to place the commands.The pin description of 16x2 LCD was given below.
The datas and the Commands both are given to the LCD through the data pins DB0 to DB7 but the logic state in the RS pin decides the data or command was given to the LCD.As stated in the above table for sending commands the RS pins should be in Logic 0 and for the datas the RS Pin should hold the Logic 1 or high state.
COMMANDS FOR THE LCD:
The commands to the LCD was given in order to initialize the LCD and to give the starting address or position of the characters to be written in the LCD.The various commands to the LCD was given below
The above commands perform the function specified to them and should be given to the LCD with the Register select (RS) pin in logic 0.
INITIALIZING THE LCD:
The first step in coding the LCD is initializing the LCD connected by giving the commands as input through the data line DB0-DB7.For initializing the LCD following commands should be given to the LCD module
16x2 LCD:
This type of LCD module is very common and used widely in many types of display applications.It consists of 16 rows and 2 columns of 5x7 dot marices.The LCD display was a 16 pin package with back light,Contrast adjustment and 5x7 dot resolution.It consists of two built in registers known as data and Command register each has a specific function to perform with the display.The Data register is for writing the data to be displayed and Command register is to place the commands.The pin description of 16x2 LCD was given below.
The datas and the Commands both are given to the LCD through the data pins DB0 to DB7 but the logic state in the RS pin decides the data or command was given to the LCD.As stated in the above table for sending commands the RS pins should be in Logic 0 and for the datas the RS Pin should hold the Logic 1 or high state.
COMMANDS FOR THE LCD:
The commands to the LCD was given in order to initialize the LCD and to give the starting address or position of the characters to be written in the LCD.The various commands to the LCD was given below
The above commands perform the function specified to them and should be given to the LCD with the Register select (RS) pin in logic 0.
INITIALIZING THE LCD:
The first step in coding the LCD is initializing the LCD connected by giving the commands as input through the data line DB0-DB7.For initializing the LCD following commands should be given to the LCD module
- First send 38H command to tell LCD to use 2 lines 5x7 matrix
- Send 0FH for making the LCD,cursor and Cursor blinking ON.
- Send 06H for incrementing cursor position.
- Finally 01 and 02 for clearing screen and returning home.
DATA TRANSMISSION TO THE LCD:
The data transmission to a LCD must be performed by means of assigning logic states to three pins RS,R/W and E.The data is send to the module by following these steps.
The data transmission to a LCD must be performed by means of assigning logic states to three pins RS,R/W and E.The data is send to the module by following these steps.
- Give logic 0 to R/W pin
- Register select RS pin should be high logic 1 for a data transmission
- Place the data to send in the data register.
- Pulse En pin from low to high and certain time dalay must be given for a data transmission since the LCD needs En pin in high state to take the data inputs.
Now lets move into the Programming of the LCD using this AT89C52 microcontroller and this programming was done by using IDE 8051 program software.
PROGRAM:
- #include<stdio.h>
- #include<stdlib.h>
- #include<reg51.h>
- #include<string.h>
- void cmdwrite(void);
- void datawrite(void);
- void delay(void);
- sbit en=P2^0; //Enable was assigned to Posrt 2.0
- sbit rs=P2^1; //Register select was assigned to Port 2.1
- sbit rw=P2^2; //Read/write was assigned to Port 2.2
- code unsigned char a[6]={0x38,0x0f,0x06,0x0c,0x01,0x02}; //Array of commands to LCD
- code unsigned char b[12]="GADGETRONICX"; //Characters to be displayed
- unsigned int i,j,k,l; //Assigning Intergers
- void main(void) //Main program
- {
- for(i=0;i<=8;i++) //For loop to input arrays
- {
- P1=a[i]; //Sending the Command array to Port1
- cmdwrite(); //Calling Cmdwrite function
- delay(); //Calling delay function
- }
- P1=0x80; //Sending 80H that is starting address values to port1
- cmdwrite();
- delay();
- for(j=0;j<=12;j++)
- {
- P1=b[j]; //Sending the characters to Port1
- datawrite(); //Calling out datawrite function
- }
- while(1); //Infinite loop
- }
- void cmdwrite() //Subroutine program
- {
- rs=0; //Setting Reset pin low
- rw=0; //Setting Read/Write Pin as low
- en=0; //Setting Enable low
- en=1; //Setting Enable pin high
- delay(); //Calling out delay
- }
- void datawrite()
- {
- rs=1; //Setting reset Pin high
- rw=0;
- en=0;
- en=1;
- delay();
- }
- void delay() //Delay Subroutine
- {
- for(k=1;k<500;k++);
- {
- for(l=1;l<=300;l++);
- }
- }
This program comprises of one main program and three subroutine programs for data writing "datawrite" and for command writing "cmdwrite" function was used.And this subroutine programs was called in the main "void main" programs to perform the desired function.
Hope this post will make you clear about the LCD interface with the Microcontroller.Share this post with others through social networks if you like it.Post you queries and comments about this post to us and we are happy to help you.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.![]()
Image may be NSFW.Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.