Below mentioned code is 4*4 alphanumeric keypad,
my code is working absolutely fine, is there any optimizing techniques to reduce code size and complications can any one please suggest me...
KEYPAD Specification:
M 1 2 3
U 4 5 6
D 7 8 9
E S 0 C
O/P(col) = 0
I/P(row) = 1
- Code: Select all
[code]
#include<P30f5011.h>
int main()
{
uart_init();
while(1)
{
KeyPadScan();
delay(100);
}
}
////////////////////// KP headers /////////////////////////////
const unsigned char keys[16] = {0x11,0x12,0x14,0x18,
0x21,0x22,0x24,0x28,
0x41,0x42,0x44,0x48,
0x81,0x82,0x84,0x88};
//[R][C]
const unsigned char key_char[16][4]= {{'M','M','M','M'}, //[1][0]
{'1',',','.','@'}, //[1][1]
{'2','A','B','C'}, //[1][2]
{'3','D','E','F'}, //[1][3]
{'U','U','U','U'}, //[2][0]
{'4','G','H','I'}, //[2][1]
{'5','J','K','L'}, //[2][2]
{'6','M','N','O'}, //[2][3]
{'D','D','D','D'}, //[3][0]
{'7','P','Q','R'}, //[3][1]
{'8','S','T','U'}, //[3][2]
{'9','U','V','W'}, //[3][3]
{'E','E','E','E'}, //[4][0]
{'*','X','Y','Z'}, //[4][1]
{'0','0','0','0'}, //[4][2]
{'C','C','C','C'}}; //[4][3]
unsigned char key_pos1=0,n=0;
void KP_INIT(void)
{
ADCON1bits.ADON = 0; //ADC DISABLED
ADPCFG = 0xFFFF; //Digital port active
TRISB = 0x00F0; //CONFIGURE directions of TRISB(COL as INPUT ROW as OUTPUT)
LATB = 0x0000; //ALL LATB PINS LOW
printf("Key pad init done...\n");
}
void KeyPadScan( void )
{
unsigned char j=0,i=0;
PORTB=0x0F; //initializing portb again and again(COL as LOW and Rows As HIGH)
if(PORTB != 0x0F ) //if pressed Cols switch wil high,and enters into loop
{
for(i=0;i<4;i++)
{
PORTB = 1<<i; //ROW i-th value high remaining zeros (i=0,1,2,3),and pressed value of Col switch get high
for(j=0;j<16;j++) //in pressed key row and col get high remaining all pins zero...
{
if((PORTB == keys[j]))
{
if(key_pos1 != j)
n=0;
key_pos1 = j;
if(n==0 || n==1 || n==2 || n==3)
{
printf("%c\n",key_char[j][n]);
n++;
if(n==4)
n=0;
break;
}
}
}
}
}
}
[/code]