sachin.kolkar
New Member
hi,
i m facing problem with 4X4 keypad matrix interfacing with PIC16F72 uc
When i run the program with simulator it works fine... one port is i am using for the key value using LED display .
when i connect the led & key board matrix , LED's are toggles continuesly in random manner
here is my code
PLZ help me guys......
i m facing problem with 4X4 keypad matrix interfacing with PIC16F72 uc
When i run the program with simulator it works fine... one port is i am using for the key value using LED display .
when i connect the led & key board matrix , LED's are toggles continuesly in random manner
here is my code
Code:
#define keyport PORTB //Keypad Port
#define keyportddr TRISB //Data Direction Register
static char keytab[17]={0,1,2,3,10,4,5,6,11,7,8,9,12,13,0,14,15};
unsigned char keyval; //A variable
void key_init();
unsigned char get_key();
unsigned char i,key=1,j;
void compare();
int main()
{
DDRD=0x00;
PORTD=0x00;
DDRA=0xC0;
PORTA=0xFF;
while(1)
{
key_init();
get_key();
}
}
/*
+---------------------------------------+
| Prototype: void key_init(void); |
| Return Type: void |
| Arguments: None |
| Description: Initialize ports and |
| Keypad. |
+---------------------------------------+
*/
void key_init(){
keyportddr = 0xF0;
keyport = 0xFF;
}
/*
+-----------------------------------------------+
| Prototype: unsigned char get_key(void); |
| Return Type: unsigned char |
| Arguments: None |
| Description: To read key from the keypad |
+-----------------------------------------------+
*/
unsigned char get_key(){
key=1;
for(i=0;i<4;i++){ //Loop for 4 rows
keyport &=~(0x01<<i); //Make rows low one by one
if(PORTB.F4==0){
//check COL1
while(!(keyportpin & (1<<col1)));
//wait for release
PORTD=keytab[key];
return key;
//return pressed key value
}
if((PORTB.F5==0))){
//Check COL2
key += 1;
//Second key pressed
while(!(keyportpin & (1<<col2)));
//wait for release
PORTD=keytab[key];
return key;
//return key value
}
if(PORTB.F6==0){
//Check COL3
key += 2;
//Third key pressed
while(!(keyportpin & (1<<col3)));
//Wait for release
PORTD=keytab[key];
return key;
//Return value
}
if(PORTB.F7==0){
//check COL4
key += 3;
//Fourth key pressed
while(!(keyportpin & (1<<col4)));
//Wait for release
PORTD=keytab[key];
return key;
//Return key value
}
key +=4; //Next row
keyport |= 0x01<<i;
//make read row high again
}
return 0; //return false if no key pressed
}
PLZ help me guys......