fbpx

Interface Keypad With PIC

Description

Keypad is an input device.Keypad is essential in all embedded devices where the user needs to interact with devices. From Calculator to Computer, the keypad is essential where a number of input switches are required.

  • Keypad is made by arranging push button switches in row and columns in a matrix format.
  • In 4×3 matrix keypad, 12 input switch is required to connect with a microcontroller.
  • The status of the input switch can be determined by scanning row and column of the keypad.
  • In 4×3 matrix keypad, 4 row and 3 columns will be available.
  • A logic 0 is given to Row1 and logic 1 is given to Row2&Row4.
  • The column is scanned. If any switch belongs to 1st row is pressed the corresponding column will be logic 0 and we can detect the pressed key.
  • This process is repeated for all rows.

Hardware Required

  • 4×3 Keypad.
  • Pic16f877a MicroController.

Circuit

Virtual Circuit

Code

 unsigned short key, cnt, oldstate = 0;
 char txt[6];
 char keypadPort at PORTA;
 sbit LCD_RS at RB1_bit;
 sbit LCD_EN at RB0_bit;
 sbit LCD_D4 at RB4_bit;
 sbit LCD_D5 at RB5_bit;
 sbit LCD_D6 at RB6_bit;
 sbit LCD_D7 at RB7_bit;
 sbit LCD_RS_Direction at TRISB1_bit;
 sbit LCD_EN_Direction at TRISB0_bit;
 sbit LCD_D4_Direction at TRISB4_bit;
 sbit LCD_D5_Direction at TRISB5_bit;
 sbit LCD_D6_Direction at TRISB6_bit;
 sbit LCD_D7_Direction at TRISB7_bit;
 void main()
 {
 cnt = 0;
 Keypad_Init();
 Lcd_Init();
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Out(1, 1, "1");
 Lcd_Out(1, 1, "Key :");
 Lcd_Out(2, 1, "Times:");
 do
 {
 key = 0;
 do
 key = Keypad_Key_Click();
 while (!key);
 switch (key)
 {
 case 1: key = 49; break;
 case 2: key = 50; break;
 case 3: key = 51; break;
 //case 4: kp = 65; break; // A
 case 5: key = 52; break;
 case 6: key = 53; break;
 case 7: key = 54; break;
 //case 8: kp = 66; break; // B
 case 9: key = 55; break;
 case 10: key = 56; break;
 case 11: key = 57; break;
 //case 12: kp = 67; break;
 case 13: key = 42; break;
 case 14: key = 48; break;
 case 15: key = 35; break;
 //case 16: kp = 68; break; // D
 }
 if (key != oldstate)
 {
 cnt = 1;
 oldstate = key;
 }
 else
 {
 cnt++;
 }
 Lcd_Chr(1, 10, key);
 if (cnt == 255)
 {
 cnt = 0;
 Lcd_Out(2, 10, " ");
 }
 WordToStr(cnt, txt);
 Lcd_Out(2, 10, txt);
 }
 while (1);
 } 

Leave a Reply

Your email address will not be published. Required fields are marked *