Quantcast
Viewing all articles
Browse latest Browse all 215

16x2 LCD and Keypad Interface with PIC16F877A Microcontroller

Image may be NSFW.
Clik here to view.
PIC-microcontroller-tutorial-keypad-16x2-lcd-interface
Design of LCD and Keypad Interface to PIC Mircocontroller
Interfacing of LCD and Keypad are one of the important interfacing concepts of PIC microcontroller since both the input and output element can form a complete embedded system design. This tutorial is about teaching you how to get input input values from keypad by polling method and display the input into a 16x2 LCD.

16x2 LCD:

This type of LCD is widely used to display the status of the system and to display the obtain output. This LCD consists of 16 columns and 2 rows, therefore named as 16x2 LCD. Usually a LCD consists of two built in registers known as Data and Command register. Command reg is meant for giving commands such as blink on, cursor on/off etc while Data reg is to display the input character.

You have to follow these steps to write a command or data into the LCD.

  • Place the Data/Command in the pins D0-D7 of the LCD.
  • If you are intended to write command you gotta make the RS(register select) pin of the LCD low, that is RS=0
  • Whereas for writing data you have to make this RS high, that is RS=1
  • Then the EN of the LCD must undergo a high to low logic transition with some delay in between them, that is EN=1 to EN=0 with specific delay.
  • The R/W pin should remain in the Logic 0.

Usually LCD's have some preassigned commands that would do specific tasks in functioning of the LCD. The command table of a 16x2 LCD is given below.

Image may be NSFW.
Clik here to view.
LCD-command-list-table-hex-values-data
Command Table for 16x2 LCD
Still not clear about the working of LCD? I suggest you to go through this Tutorial on LCD interface

4x3 Keypad:

We have used a 4x3 keypad which means it has four rows and three columns in it. The keypad was scanned for any key inputs and this was done by means of a method called polling. The scanning takes place by keeping a specific Row low at a time and read the status of the column pins as input at that instant. This chain goes on through all the rows and by this way input is read by Microcontroller.

Read how to constuct keypad and how the scanning works  in this Tutorial of Keypad Interface and scanning.

CODE:

The below code was built by using CCS C Compiler for PIC Microcontrollers.

  1. #include <keypad.h>
  2. #bit C1=0x08.0     //Assigning Columns of Keypad to PORT D
  3. #bit C2=0x08.1
  4. #bit C3=0x08.2
  5. #bit R1=0x08.3    //Assigning the rows of Keypad to PORT D
  6. #bit R2=0x08.4
  7. #bit R3=0x08.5
  8. #bit R4=0x08.6
  9. #bit TRIS_C1=0x88.0                //Setting the TRIS  pins of PORT D
  10. #bit TRIS_C2=0x88.1
  11. #bit TRIS_C3=0x88.2
  12. #bit TRIS_R1=0x88.3
  13. #bit TRIS_R2=0x88.4
  14. #bit TRIS_R3=0x88.5
  15. #bit TRIS_R4=0x88.6
  16. #byte lcd=0x06                        //Assigning LCD to the PORT B
  17. #byte TRIS_lcd=0x86
  18. #bit rs=0x07.0
  19. #bit en=0x07.1
  20. #bit TRIS_rs=0x87.0             //Assigning RS pin to PORT C.0
  21. #bit TRIS_en=0x87.1            //Assigning EN pin to PORT C.1
  22. void lcd_msg(char b,int c);
  23. char keypad();
  24. char a;
  25. void main()
  26. {
  27. TRIS_R1=TRIS_R2=TRIS_R3=TRIS_R4=0;  //Directions to row and col
  28. TRIS_C1=TRIS_C2=TRIS_C3=1;
  29. TRIS_lcd=TRIS_rs=TRIS_en=0;
  30. lcd_msg(0x38,0);                                     //lcd_msg function calling for data/command to lcd
  31. lcd_msg(0x80,0);
  32. lcd_msg(0x0f,0);
  33.    while(TRUE)
  34.    {
  35.      a=keypad();                                      //Taking keypad input to a char variable
  36.      lcd_msg(a,1);
  37.    }

  38. }
  39. char keypad()
  40. {
  41. while(TRUE)
  42. {
  43.      R1=1;                                           //Scanning Process
  44.      R2=R3=R4=0;
  45.      if(C1==1)
  46.      {
  47.      while(C1==1);
  48.      return '1';
  49.      }
  50.      if(C2==1)
  51.      {
  52.      while(C2==1);
  53.      return '2';
  54.      }
  55.      if(C3==1)
  56.      {
  57.      while(C3==1);
  58.      return '3';
  59.      }
  60.      R2=1;
  61.      R1=R3=R4=0;
  62.      if(C1==1)
  63.      {
  64.      while(C1==1);
  65.      return '4';
  66.      }
  67.      if(C2==1)
  68.      {
  69.      while(C2==1);
  70.      return '5';
  71.      }
  72.      if(C3==1)
  73.      {
  74.      while(C3==1);
  75.      return '6';
  76.      }
  77.      R3=1;
  78.      R1=R2=R4=0;
  79.      if(C1==1)
  80.      {
  81.      while(C1==1);
  82.      return '7';
  83.      }
  84.      if(C2==1)
  85.      {
  86.      while(C2==1);
  87.      return '8';
  88.      }
  89.      if(C3==1)
  90.      {
  91.      while(C3==1);
  92.      return '9';
  93.      }
  94.      R4=1;
  95.      R1=R2=R3=0;
  96.      if(C1==1)
  97.      {
  98.      while(C1==1);
  99.      return '*';
  100.      }
  101.      if(C2==1)
  102.      {
  103.      while(C2==1);
  104.      return '0';
  105.      }
  106.      if(C3==1)
  107.      {
  108.      while(C3==1);
  109.      return '#';
  110.      }
  111. }
  112. }
  113. void lcd_msg(char b,int c)                  //LCD command/data sending function
  114. {
  115. lcd=b;
  116. rs=c;
  117. en=1;
  118. delay_ms(20);                                 //calling out delay of 20msec
  119. en=0;
  120. delay_ms(20);
  121. }


The code comprises of Two subroutines - "lcd_msg()" is for writing data/command to the LCD where keypad() is to scan the keys of the keypad. In the "lcd_msg()" function the command/data value is directly passed to the function while calling by means of "char b" and RS value by using "int c" , 0 implies for command and 1 for data.

Was this tutorial helpful? Share this with others through social sites and don't forget to follow us there for more of these PIC microcontroller tutorials. Enjoy Coding.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 215

Trending Articles