INTERFACING 16X2 LCD WITH 8051 MICROCONTROLLER | Gadgetronicx

Follow Us on Social sites and Stay Connected!!!

INTERFACING 16X2 LCD WITH 8051 MICROCONTROLLER

Interfcaing-16x2-LCD-8051-Microcontroller-tutorial
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.

16x2-LCD-pin-description
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
Commands-table-16x2-LCD-module
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.
  • 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:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<reg51.h>
  4. #include<string.h>
  5. void cmdwrite(void); 
  6. void datawrite(void);
  7. void delay(void);
  8. sbit en=P2^0;    //Enable was assigned to Posrt 2.0        
  9. sbit rs=P2^1;    //Register select was assigned to Port 2.1
  10. sbit rw=P2^2;    //Read/write was assigned to Port 2.2 
  11. code unsigned char a[6]={0x38,0x0f,0x06,0x0c,0x01,0x02}; //Array of commands to LCD
  12. code unsigned char b[12]="GADGETRONICX";      //Characters to be displayed 
  13. unsigned int i,j,k,l;        //Assigning Intergers
  14. void main(void)           //Main program
  15. {
  16. for(i=0;i<=8;i++)     //For loop to input arrays
  17. {
  18. P1=a[i];              //Sending the Command array to Port1 
  19. cmdwrite();        //Calling Cmdwrite function
  20. delay();              //Calling delay function
  21. }
  22. P1=0x80;             //Sending 80H that is starting address values to port1
  23. cmdwrite();
  24. delay();
  25. for(j=0;j<=12;j++)
  26. {
  27. P1=b[j];             //Sending the characters to Port1
  28. datawrite();       //Calling out datawrite function
  29. }
  30. while(1);            //Infinite loop
  31. }
  32. void cmdwrite()      //Subroutine program
  33. {
  34. rs=0;                //Setting Reset pin low
  35. rw=0;                //Setting Read/Write Pin as low
  36. en=0;                //Setting Enable low
  37. en=1;                //Setting Enable pin high
  38. delay();             //Calling out delay
  39. }
  40. void datawrite()
  41. {
  42. rs=1;                //Setting reset Pin high
  43. rw=0; 
  44. en=0;
  45. en=1;
  46. delay();
  47. }
  48. void delay()         //Delay Subroutine
  49. {
  50. for(k=1;k<500;k++);
  51. {
  52. for(l=1;l<=300;l++);
  53. }
  54. }
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.
   

1 comment:

Justin said...

well explained but i feel the diagram is not very clear have to magnify the design bit straining