16x2 LCD interfacing and programming in 4 bit and 8 bit mode using 8051 Microcontroller - Gadgetronicx

16x2 LCD interfacing and programming in 4 bit and 8 bit mode using 8051 Microcontroller

16x2-lcd-display-pin-diagram
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 in 4 bit and 8 bit mode 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 D0 to D7 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 datasthe 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.


MODE OF OPERATION:

  1. 8 Bit mode.
  2. 4 Bit mode.

8 BIT MODE:


In 8 bit mode LCD requires uses 8 pins for data/command transmission from the microcontroller. The data bytes will be sent through the pins (D0-D7) along with EN,RS and RW(optional) of the LCD. So we need to dedicate a total of 11 pins from the Microcontroller to the LCD.  


DESIGN:


Interfcaing-16x2-LCD-8051-Microcontroller-tutorial

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 D0-D7. For initializing the LCD following commands should be given to the LCD module
  • RS pin should be low to tell the LCD that the incoming byte is a command, so make it low.
  • Place the byte in D0-D7 pins of LCD and set the EN pin to high and then make it low with time delay of 10ms between them.
  • Send 38H command to use 2 lines 5x7 matrix in 8 bit mode. 
  • 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/COMMAND TRANSMISSION TO THE LCD:

The data transmission to a LCD must be performed by means of assigning logic states to three pins RS and E. R/W pin is not needed so we can ground it as shown in the schematic diagram. The data is send to the module by following these steps.
  • RS pin should be high for a data transmission and low for a command transmission.
  • Place the data/command in the data lines D0-D7 of the LCD.
  • Pulse En pin from high to low with certain time delay for transmission to complete.
Now lets move into the Programming of the LCD using this AT89S52 microcontroller and this programming was done by using Keil Uvision Software.

CODE:

  1. #include<reg51.h> 
  2. #define LCD P2
  3. sbit RS=P3^0; 
  4. sbit EN=P3^1; 
  5. unsigned char txt[12]="GADGETRONICX";       //Data to display
  6. void delay()  
  7.  {   
  8.    int k;   
  9.    for(k=0;k<=1000;k++); 
  10.   } 
  11. void lcd(char a,short int b)       //Subroutine for command and data transmission 
  12.   {   
  13.    LCD=a;                             //Placing byte in D0-D7 lines of LCD
  14.    RS=b;                              //0 for command and 1 for data
  15.    EN=1;   
  16.    delay();   
  17.    EN=0; 
  18.   }
  19. void main()
  20. {
  21.    int i=0;
  22.    lcd(0x38,0);                      //Initializing LCD
  23.    lcd(0x0f,0);   
  24.    lcd(0x01,0);  
  25.    lcd(0x02,0); 
  26.    lcd(0x80,0);   
  27.    for(i=0;i<=11;i++)           //Transmitting Data
  28.     {
  29.       lcd(txt[i],1);
  30.     }
  31.    while(1);
  32.  }


4 BIT MODE:

In 8 bit mode LCD requires uses 4 pins for data/command transmission from the microcontroller. The data bytes will be sent through the pins (D4-D7) higher bits along with EN,RS and RW(optional) of the LCD. So we need to dedicate a total of 7 pins from the Microcontroller to the LCD.  

The main difference between the 4 and 8 bit mode is usage of pins from the microcontroller. .Also 8 bit mode is faster than the four bit mode which sends the data in the form of nibbles (4 bits).


DESIGN:


4-bit-interfacing-design-diagram-8051-microcontroller


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 D4-D7 in the fom of Nibbles. For initializing the LCD following a specific reset sequence should be given and then initialized to follow the 4 bit mode.
  • Place the byte in D4-D7 pins of LCD and set the EN pin to high and then make it low with time delay of 10ms between them.
  • Send 28H command to use 2 lines 5x7 matrix in 4 bit mode. 
  • 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/COMMAND TRANSMISSION TO THE LCD:

The data transmission to a LCD must be performed by means of assigning logic states to three pins RS and E. R/W pin is not needed so we can ground it as shown in the schematic diagram. The data is send to the module by following these steps.
  • RS pin should be high to convey LCD a data transmission is going to take place.
  • Place the Upper nibble in the lower four bits of Port 2 by means of bit shifting and mask the upper four bits.
  • Pulse En pin from high to low with certain time delay for transmission to complete.
  • Now place the lower nibble and mask the rest of the bits, then repeat step 3. 

CODE:

  1. #include<reg51.h>
  2. #define LCD     P2
  3. #define ENABLE      0x10
  4. #define REG_SL      0x20

  5. void delay()
  6.  {
  7.    int i,j;
  8.    for(i=0;i<=500;i++);
  9.    for(j=0;j<=500;j++);
  10.  }

  11. void lcd_reset()                   //Reset sequence as stated in the LCD datasheet
  12.  {
  13. LCD = 0xFF;
  14. delay();
  15. LCD = 0x03|ENABLE;
  16. LCD = 0x03;
  17. delay();
  18. LCD = 0x03|ENABLE;
  19. LCD = 0x03;
  20. delay();
  21. LCD = 0x03|ENABLE;
  22. LCD = 0x03;
  23. delay();
  24. LCD = 0x02|ENABLE;
  25. LCD = 0x02;
  26. delay();
  27.  }

  28. void lcd_cmd(char cmd)               //Subroutine to send command
  29.  { 
  30. LCD = ((cmd>>4)&0x0F)|ENABLE;     //Sending higher nibble by shifting with Pulsing Enable high
  31. delay();
  32. LCD = ((cmd>>4)&0x0F);
  33.   delay();
  34. LCD = (cmd & 0x0F)|ENABLE;          //Sending lower nibble Enable low
  35. LCD = (cmd & 0x0F);
  36.  }

  37. void lcd_data (char *dat)          //Subroutine to send data
  38.  { 
  39.   while(*dat!='\0')
  40. {
  41. LCD = (((*dat>>4)&0x0F)|ENABLE|REG_SL); //Sending data by making RS=1
  42. delay();
  43. LCD = (((*dat>>4)&0x0F)|REG_SL);
  44. delay();
  45. LCD = ((*dat & 0x0F)|ENABLE|REG_SL);
  46. delay();
  47. LCD = ((*dat & 0x0F)|REG_SL);
  48. delay();
  49. dat++;
  50. }
  51.  }

  52. void lcd_init ()
  53.  {
  54. lcd_cmd(0x28);       // 4-bit mode - 2 line - 5x7 font. 
  55. lcd_cmd(0x0F);       // Display cursor with blink.
  56. lcd_cmd(0x06);       // Automatic Increment - No Display shift.
  57.    lcd_cmd(0x01);
  58.    lcd_cmd(0x02);       //Clear screen and return home    
  59. lcd_cmd(0x80);       // First row first column
  60.   }

  61.  void main()
  62.   {
  63.     lcd_reset();
  64.     lcd_init();
  65.     lcd_data("GADGETRONICX");
  66.     while(1);
  67.   }

NOTE:

  • Sometimes you might also want to use reset sequence for 8 bit mode to make the LCD work properly, in that case transmit the command "0x30" with enable pulse from high to low three times with certain delay in between each command.
  • Time delay for processing command/data may vary depending on the LCD , so in case you face any problem with command/data transmission try increasing the delay between high to low enable transition.

2 comments:

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

    ReplyDelete

We are working hard as we can to provide you precise information with working Circuit Designs and Programs. However if you find any content inappropriate or misleading kindly suggest modification or improvement through Contact Us form. For queries and opinion regarding the article kindly post a comment here, we will assist you.