Keypad scanning and interfacing with PIC16f877 microcontroller

Here I give you very important and simple project on Keypad / keyboard scanning and interfacing with PIC16f877microcontroller . To start big project you need to know small and necessary one first. So we start form basic . In my previous post I write on

Scrolling Text on LCD by PIC Microcontroller

Displaying text on LCD by interfaced with PIC16F877 microcontroller in 4 bit mode

Simple Project on LED blinking by PIC 16 Microcontroller

Step by step Project guide on PIC Microcontroller

Keypad scanning and interfacing with PIC16f877 microcontroller

 

 So today I start with Keypad scanning and interfacing with PIC16f877 microcontroller. As like my other projects I used 1 ) Proteus 7 Professional and 2) Mikcro C Pro for pic to make my project.

In this project we will see some new command for interfacing 4*4 matrix keypad / keyboard.

 char  keypadPort at PORTC : is use for defining port of microcontroller with keypad. Here we defining port C for that.

Keypad_Init()  :  is use for initializing.

Keypad_Key_Click() : is use for wait for key to be pressed and released.


 Now see the complete Embedded C Programing of our project Keypad scanning and interfacing with PIC16f877 microcontroller

Embedded C Code
// Name : Scrolling Text on LCD
// Purpose : Main file for using LCD with PIC16F877 in 4bit mode.
// Author : Subham Dutta
// Date : 14-09-13
// Website : www.nbcafe.in
char keypadPort at PORTC;
// Start LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
void main() {
unsigned short kp, cnt, oldstate = 0;
char txt[6];
cnt = 0;                                                                                                        // Reset counter
Keypad_Init();                                                                                    // Initialize Keypad
ANSEL = 0;                                                                                          // Configure AN pins as digital I/O
ANSELH = 0;
do {
kp = 0;                                                                                                     // Reset key code variable
// Wait for key to be pressed and released
do
kp = Keypad_Key_Click();                                                   // Store key code in kp variable
while (!kp);
// Prepare value for output, transform key to it’s ASCII value
switch (kp) {
case 1: kp = 55; break; // 7                   // Uncomment this block for keypad4x4
case 2: kp = 56; break; // 8
case 3: kp = 57; break; // 9
case 4: kp = 47; break; // /
case 5: kp = 52; break; // 4
case 6: kp = 53; break; // 5
case 7: kp = 54; break; // 6
case 8: kp = 42; break; // *
case 9: kp = 49; break; // 1
case 10: kp = 50; break; // 2
case 11: kp = 51; break; // 3
case 12: kp = 45; break; // –
case 13: kp = 133; break; // —
case 14: kp = 48; break; // 0
case 15: kp = 61; break; // =
case 16: kp = 43; break; // +}
if (kp == 133)
{
Lcd_Init();                                                                 // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);                                  // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);                   // Cursor off
Lcd_Out(1, 1, “Key :”);                                        // Write message text on LCD
Lcd_Out(2, 1, “Answer = “);
}
Lcd_Chr(1, 10, kp);                                            // Print key ASCII value on LCD} while (1);
}

Simmulation : Now we have to now the how the PIC16F877 IC , LCD and Keypad are interface and work. For that I give you the video you must see that and you will find the connection and how it simulates in Proteus.