You are on page 1of 7

This tutorial will show you how to read a 4x4 keypad input and write to an alphanumeric LCD interfaced

to your PIC micro, it's pretty simple and straight forward.

First create a new project and add a c main file like in the steps for Blinking a LED using MPLAB X , PIC16F877A Components 1.PIC16F877A micro controller 2. 4x4 matrix keypad initial setup supply logic 0 (0V or GND) to all 4 keypad row wires or set the row pins as output Supply logic 1 (high voltage) to all 4 keypad coloum wires or set coloums as input Steps to read a key from the keypad 1. Find the Row first, by selecting each row 2. scan the colum which pressed key is 3 Find the key pressed ,the function findKey() is

for this 4. If we have at least one key pressed, then return from keypadread() will be non zero.

PROGRAM ############################################## ######################### /* * File: main.c * Author: Ebin Ephrem */

#include<htc.h> #if defined(WDTE_OFF) __CONFIG(WDTE_OFF & LVP_OFF); #elif defined (WDTDIS) __CONFIG(WDTDIS & LVPDIS); #endif char findKey(unsigned int a, unsigned int b); char readKeyboard(); void main() { TRISB = 0xF0; unsigned int re; while(1) { re=0; re= readKeyboard(); } } char readKeyboard() { unsigned int i = 0; for(i=0;i<4;i++) { if(i == 0) PORTB = 1; else if(i == 1)

PORTB = 2; else if(i == 2) PORTB = 4; else if(i == 3) PORTB = 8; if(RB4) return findKey(i,0); if(RB5) return findKey(i,1); if(RB6) return findKey(i,2); if(RB7) return findKey(i,3); } return ' '; } char findKey(unsigned int a, unsigned int b) //generating key character { if(b == 0) { if(a == 3) return '3'; else if(a == 2) return '2'; else if(a == 1) return '1';

else if(a == 0) return '0'; } else if(b == 1) { if(a == 3) return '8'; else if(a == 2) return '7'; else if(a == 1) return '6'; else if(a == 0) return '5'; } else if(b == 2) { if(a == 3) return 'b'; else if(a == 2) return '8'; else if(a == 1) return 'A'; else if(a == 0) return '-'; } else if(b == 3) { if(a == 3) return 'C';

else if(a == 2) return 'U'; else if(a == 1) return 'E'; else if(a == 0) return 'F'; } }

////////////////
Code:
#include "Includes.h" // Function name: InitKeypad void InitKeypad(void) { Keypad_PORT = 0x00; // Set Keypad port pin values zero Keypad_PORT_Dir = 0xF0; // Last 4 pins input, First 4 pins output // Enable weak internal pull up on input pins OPTION_REG &= 0x7F; }

// Function name: READ_SWITCHES // Scan all the keypad keys to detect any pressed key. char READ_SWITCHES(void) { RowA = 0; RowB = 1; RowC = 1; RowD = 1; //Test Row A if if if if (C1 (C2 (C3 (C4 == == == == 0) 0) 0) 0) { { { { __delay_ms(250); __delay_ms(250); __delay_ms(250); __delay_ms(250); while while while while (C1==0); (C2==0); (C3==0); (C4==0); return return return return '7'; '8'; '9'; '/'; } } } }

RowA = 1; RowB = 0; RowC = 1; RowD = 1;

//Test Row B

if (C1 == 0) { __delay_ms(250); while (C1==0); return '4'; } if (C2 == 0) { __delay_ms(250); while (C2==0); return '5'; } if (C3 == 0) { __delay_ms(250); while (C3==0); return '6'; }

if (C4 == 0) { __delay_ms(250); while (C4==0); return 'x'; } RowA = 1; RowB = 1; RowC = 0; RowD = 1; if if if if (C1 (C2 (C3 (C4 == == == == 0) 0) 0) 0) { { { { __delay_ms(250); __delay_ms(250); __delay_ms(250); __delay_ms(250); while while while while //Test Row C return return return return '1'; '2'; '3'; '-'; } } } }

(C1==0); (C2==0); (C3==0); (C4==0);

RowA = 1; RowB = 1; RowC = 1; RowD = 0; if if if if } (C1 (C2 (C3 (C4 == == == == 0) 0) 0) 0) { { { { __delay_ms(250); __delay_ms(250); __delay_ms(250); __delay_ms(250); while while while while

//Test Row D return return return return 'C'; '0'; '='; '+'; } } } }

(C1==0); (C2==0); (C3==0); (C4==0);

return 'n';

// Means no key has been pressed

// Function name: GetKey // Read pressed key value from keypad and return its value char GetKey(void) // Get key from user { char key = 'n'; // Assume no key pressed while(key=='n') // Wait untill a key is pressed key = READ_SWITCHES(); // Scan the keys again and again return key; } //when key pressed then return its value

You might also like