|
|
View previous topic :: View next topic |
Author |
Message |
Gusest Guest
|
Help me in scan key |
Posted: Tue Jan 29, 2008 2:16 am |
|
|
Pls help me this code below can not display key:
#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//Keypad connection:
#define col0 PIN_B4
#define col1 PIN_B5
#define col2 PIN_B6
#define col3 PIN_B7
#define row0 PIN_B0
#define row1 PIN_B1
#define row2 PIN_B2
#define row3 PIN_B3
// Keypad layout:
char const KEYS[4][4] = {{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second
void kbd_init() {
}
short int ALL_ROWS (void)
{
if (input (row0) & input (row1) & input (row2) & input (row3))
return (0);
else
return (1);
}
char kbd_getc( ) {
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;
byte kchar;
byte row;
kchar='\0';
if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
switch (col) {
case 0 :
output_low(col0);
output_high(col1);
output_high(col2);
output_high(col3);
break;
case 1 :
output_high(col0);
output_low(col1);
output_high(col2);
output_high(col3);
break;
case 2 :
output_high(col0);
output_high(col1);
output_low(col2);
output_high(col3);
break;
case 3 :
output_high(col0);
output_high(col1);
output_high(col2);
output_low(col3);
break;
}
if(kbd_down) {
if(!ALL_ROWS()) {
kbd_down=false;
kchar=last_key;
last_key='\0';
}
} else {
if(ALL_ROWS()) {
if(!input (row0))
row=0;
else if(!input (row1))
row=1;
else if(!input (row2))
row=2;
else if(!input (row3))
row=3;
last_key =KEYS[row][col];
kbd_down = true;
} else {
++col;
if(col==3)
col=0;
}
}
kbd_call_count=0;
}
return(kchar);
}
void main()
{
kbd_init();
while(TRUE)
{
printf(" Keypad is press is:\n");
getc();
printf("%c,%d",kbd_getc(),kbd_getc());
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 29, 2008 2:55 am |
|
|
Quote: |
void kbd_init()
{
}
|
You need to add this line of code to the routine above:
Code: | port_b_pullups(TRUE); |
Quote: | void main()
{
kbd_init();
while(TRUE)
{
printf(" Keypad is press is:\n");
getc();
printf("%c,%d",kbd_getc(),kbd_getc()); }
}
|
The code shown in bold is wrong. Look in this file to see how to poll
the keypad:
Quote: | c:\Program Files\picc\Examples\Ex_lcdkb.c |
|
|
|
virtual33
Joined: 29 Jan 2008 Posts: 1
|
Thank you your help! |
Posted: Tue Jan 29, 2008 9:32 pm |
|
|
Now It running ok. Because I use Pic18F thus no need use kbd_init(); function
// This code is editted from CCS C forum by virtual33
#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//Keypad connection:
#define col0 PIN_B4
#define col1 PIN_B5
#define col2 PIN_B6
#define col3 PIN_B7
#define row0 PIN_B3
#define row1 PIN_B2
#define row2 PIN_B1
#define row3 PIN_B0
// Keypad layout:
char const KEYS[4][4] = {{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second
//void kbd_init() {
//port_b_pullups(TRUE);
//}
short int ALL_ROWS (void)
{
if (input (row0) & input (row1) & input (row2) & input (row3))
return (0);
else
return (1);
}
char kbd_getc( ) {
static byte kbd_call_count;
static short int kbd_down;
static char last_key;
static byte col;
byte kchar;
byte row;
kchar='\0';
if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
switch (col) {
case 0 :
output_low(col0);
output_high(col1);
output_high(col2);
output_high(col3);
break;
case 1 :
output_high(col0);
output_low(col1);
output_high(col2);
output_high(col3);
break;
case 2 :
output_high(col0);
output_high(col1);
output_low(col2);
output_high(col3);
break;
case 3 :
output_high(col0);
output_high(col1);
output_high(col2);
output_low(col3);
break;
}
if(kbd_down) {
if(!ALL_ROWS()) {
kbd_down=false;
kchar=last_key;
last_key='\0';
}
} else {
if(ALL_ROWS()) {
if(!input (row0))
row=0;
else if(!input (row1))
row=1;
else if(!input (row2))
row=2;
else if(!input (row3))
row=3;
last_key =KEYS[row][col];
kbd_down = true;
} else {
++col;
if(col==4)
col=0;
}
}
kbd_call_count=0;
}
return(kchar);
}
void main()
{
char k;
// lcd_init();
// kbd_init();
// lcd_putc("\fReady...\n");
while (TRUE) {
k=kbd_getc();
if(k!=0)
printf("Key %c is pressed \n\r", k);
//if(k=='*')
// lcd_putc('\f');
//else
// lcd_putc(k);
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 29, 2008 10:15 pm |
|
|
You still need pull-ups on the Row pins. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|