View previous topic :: View next topic |
Author |
Message |
Ed_Banses
Joined: 15 Sep 2005 Posts: 15
|
Custom KeyPad Problem |
Posted: Thu Feb 08, 2007 8:44 am |
|
|
Hi all,
I am working now in a project where I have to interface a custom made KeyPad with a PIC.
KeyPad has each button connected straight to a PIC pin in one side and GND in the other side. There are 7 buttons.
I adapted the flexible keypad code to my project but I am getting problems.
The keys response is not good as some times I need to press more than once to get signal or keep the key pressed to read it.
Please find below my code:
Code: |
#include <16F877A.h>
#device ICD=TRUE
#device *=16
#fuses HS, PUT, NOWDT, NOBROWNOUT, NOLVP//,NODEBUG, NOCPD, PROTECT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#define DEBOUNCE 33
#define KEY_F1 PIN_B1
#define KEY_F2 PIN_B4
#define KEY_F3 PIN_B5
#define KEY_ENTER PIN_B2
#define KEY_UP PIN_B6
#define KEY_DOWN PIN_B3
int8 key;
//Functions to drive the KeyPad
void kbd_init()
{
port_b_pullups(true);
}
short int ALL_ROWS (void)
{
if (input (KEY_F1) & input (KEY_F2) & input (KEY_F3) & input (KEY_ENTER))
return (0);
else
return (1);
}
int read_keypad()
{
static BYTE kbd_call_count;
static short int kbd_down;
static char last_key;
BYTE kchar;
kchar = 0;
if(kbd_call_count++ > DEBOUNCE)
{
if(kbd_down) {
if(!ALL_ROWS()) {
kbd_down=FALSE;
kchar=last_key;
last_key = 0;
}
}
else
{
if(!input (KEY_F1))
last_key = 1;
else if(!input (KEY_F2))
last_key = 2;
else if(!input (KEY_F3))
last_key = 3;
else if(!input (KEY_ENTER))
last_key = 4;
kbd_down = TRUE;
}
}
return(kchar);
}
void main()
{
kbd_init();
printf("\n\rTry KeyKad");
while(TRUE)
{
key = 0;
while(key == 0){key = read_keypad();}
printf("\n\rKey pressed was: %u",key);
}
}
|
I will appreciate any help from you.
Thanks a lot in advance.
Kind Regards,
Eduardo |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 09, 2007 1:14 am |
|
|
The problem is that when you modified the code, you cut out some
lines that were needed. I compared your code to the original code
and fixed it. The lines that need to be added back in are marked
with comments below:
Code: |
int read_keypad()
{
static BYTE kbd_call_count;
static short int kbd_down;
static char last_key;
BYTE kchar;
kchar = 0;
if(kbd_call_count++ > DEBOUNCE)
{
if(kbd_down) {
if(!ALL_ROWS()) {
kbd_down=FALSE;
kchar=last_key;
last_key = 0;
}
}
else
{
if(ALL_ROWS()) { // Add this line
if(!input (KEY_F1))
last_key = 1;
else if(!input (KEY_F2))
last_key = 2;
else if(!input (KEY_F3))
last_key = 3;
else if(!input (KEY_ENTER))
last_key = 4;
kbd_down = TRUE;
} // Add this line
}
kbd_call_count = 0; // Add this line
}
return(kchar);
} |
|
|
|
Ed_Banses
Joined: 15 Sep 2005 Posts: 15
|
|
Posted: Fri Feb 09, 2007 3:48 am |
|
|
Thanks a lot PCM programmer!!!!
It works!!!!
You have save me again.
If you come to manchester at any time I will buy you a couple of pits ;)
Thanks again.
Kind Regards,
Eduardo |
|
|
|