Position: Index > Unclassified >

PIC16F877a : Interfacing a Keypad and an LCD

2017-11-26 06:27  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.


 This tutorial will show you how to read a 3x4 keypad input and write to an alphanumeric LCD interfaced to your PIC micro, it's pretty simple and straight forward. The Keypad:A keypad is a matrix of push-buttons aligned in rows and columns to provide user input, a 3x4 keypad is basically formed like the following schematic:PIC16F877Ait consists of 3 columns and 4 rows, hence the name, whenever the user taps one button, the column and the row corresponding to that button are shorted, for example, if a user chooses to tap the "5" button, col2 and row2 are shorted, if a users picks "0" button, col2 and row4 are shorted, and so on. So how do you read a keypad using a Microcontroller?the method used to read such input is called Scanning, you connect the rows and the columns to the microcontroller, you set the columns as outputs, rows as inputs (or vice versa) and you start feeding a pattern of inputs to the columns while checking the readings of the rows. For example, if you tied all rows through pull-up resistors to Vcc, all row inputs are typically 1s (1111), now if you start feeding columns with the following pattern, 011, 101, 110, you can check which row conducts the 0 signal to the controller, thus, you'll detect the shorted row and column.example: if a user presses button "6", when I feed the columns by 011, the rows output would be 1111, when I feed it by 101, the rows output would be also 1111, but when I feed it by 110, button "6" will conduct the 0 signal, thus my rows output would be 1011, Knowing that I used col3 to conduct the 0 signal, and I got the 0 signal at row2, I'll know the pressed button is "6". Using PIC-C to Read a Keypad:PIC-C provides a common library for reading keypads named kbd.c, all you have to do is to include the kbd.c library in your code, and use the function kbd_getc() to scan the keypad.kbd.c library uses either port D or port B to interface the keypad, you'll find the schematic for the interface further below, but if you need to use other ports to interface your keypad, you'll have to use a user defined library, you can find those on ccsinfo.com forum, or you can write your own library (which is pretty simple). Note: the kbd_getc(); function returns the ASCII code of the pressed button (returns a character).  The Alphanumeric LCD:An alphanumeric LCD is a type of LCDs that only displays English letters, numbers and some ASCII characters, those LCDs contains a controller unit that processes the input and forms the displayed character on the LCD, most alphanumeric LCDs have a parallel interface, some other LCDs have a serial interface to minimize the construction complicity.Alphanumeric LCDs comes in different sizes, the size describes the number of characters an LCD can display, common sizes are 16x2, 20x2, 16x4, 20x4 ..etc. a 16x2 has 2 rows and can display 16 characters on each row.PIC16F877AParallel LCD Pinout:Parallel LCDs typically have 16 pins as following:- Power Supply Pins:Vss    : Ground reference (0V)Vcc    : +5V supply voltageVee    : Contrast pin, typically connected to GND via a resistor (0Ω - 5kΩ), change the value to change the contrast.- Control Bus Pins:RS     : Register Select, used to set the LCD either to command mode or to data mode.R/W   : Read/Write select, used either to read characters from LCD or to write new characters.E        : Active low Enable signal.- Data Bus Pins:D0-D7 : used to pass 8Bit character data, you have the choice either to use only 4Bit bus (D4-D7) or to use the whole data bus.- Backlight Voltage Pins:A    : Anode (+5V)K    : Cathode (0V)used to turn ON/OFF the backlight LED of your LCD. Using PIC-C to Write to LCD:To write to an LCD you need a software library, you can either go with the original lcd.c library that comes with your compiler's software bundle (which I don't recommend), or you can use flex_lcd.c libraries written by PCM programmer, here:The flex_lcd.c library gives you the opportunity to select PIC pins to be connected to the LCD by changing the definers at the top of the library. After including the flex_lcd.c library in your code, you should initialize the LCD at the beginning of your main code using the function lcd_init();Then you can use the function lcd_putc("Your String"); to display your strings. The Schematic:PIC16F877AClick on the schematic to zoom in,I've selected the LCD pinouts in flex_lcd as following: #define LCD_DB4   PIN_B4#define LCD_DB5   PIN_B5#define LCD_DB6   PIN_B6#define LCD_DB7   PIN_B7 #define LCD_E     PIN_B3#define LCD_RS    PIN_B1#define LCD_RW    PIN_B2 and commented out// #define USE_LCD_RW   1 The PIC-C Code:/* PIC16F877A interface to 3x4 Keypad and 16x2 LCD  Badr Ghatasheh 079@live.it */  #include <16f877a.h> #fuses XT,NOLVP,NOWDT,NOPROTECT #use delay(clock=4000000) #include "flex_lcd.c" #include <kbd.c>  void main() {    char k=0;                     // Character k to store keypad input   delay_ms(1);    /* Peripherals Configurations */    lcd_init();                   // Turn LCD ON, along with other initialization commands   kbd_init();                   // Initialize Keypad        lcd_gotoxy(1,1);              // point LCD cursor to col1 row1   lcd_putc("Hello World !!");  // print on LCD    lcd_gotoxy(1,2);              // point LCD cursor to col1 row2    while(1) // infinite loop    {       k = kbd_getc();            // read keypad input (if exists)       if(k!=0)                   // if key captured       {          lcd_putc(k);            // print captured key to lcd         k=0;                    // reset k for another loop round         delay_ms(250);          // delay between key presses      }       delay_ms(1);               // delay_between read trials   } }