keypad using pic16f877a
I did this one for an 8052 using Ke*l compiler. This one is for 3x4, but gives the general idea. Should be easily adaptable for a PIC.
//Read Keypad and returns Digit Value
unsigned char GetKey(void) // Get Keypress
{
sbit C3 = 0xA4; //P2.4
sbit C2 = 0xA5; //P2.5
sbit C1 = 0xA6; //P2.6
sfr P2 = 0xA0; //Port 2 Lower nibble connection
unsigned char Digit, KeyRead;
C1 = 0; C2 = 1; C3 = 1; // Column 1 On
KeyRead = (P2 & 0x0F);
if (KeyRead == 14) Digit = 1; // Read Rows and Translate
if (KeyRead == 13) Digit = 4;
if (KeyRead == 11) Digit = 7;
if (KeyRead == 7) Digit = 10; // Cancel Key
C1 = 1; C2 = 0; C3 = 1; // Column 2 On
KeyRead = (P2 & 0x0F);
if (KeyRead == 14) Digit = 2;
if (KeyRead == 13) Digit = 5;
if (KeyRead == 11) Digit = 8;
if (KeyRead == 7) Digit = 0;
C1 = 1; C2 = 1; C3 = 0; // Column 3 On
KeyRead = (P2 & 0x0F);
if (KeyRead == 14) Digit = 3;
if (KeyRead == 13) Digit = 6;
if (KeyRead == 11) Digit = 9;
if (KeyRead == 7) Digit = 11; // Call Key
C1 = 0 ; C2 = 0; C3 = 0;
while ((P2 & 0x0F) != 0x0F) {} // Waits for Key Release
Sounder = 0;
return Digit;
}