|
|
View previous topic :: View next topic |
Author |
Message |
project2010
Joined: 14 Mar 2010 Posts: 21
|
4x4 keypad error |
Posted: Thu May 27, 2010 12:16 pm |
|
|
Code: |
#include<18F4620.h> //include 18F4620 ports
#device adc=10
#fuses HS,NOWDT,PUT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20000000) //use 20MHz crystal
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <LCD20x4.c>
#define row0 PIN_D4
#define row1 PIN_D5
#define row2 PIN_D6
#define row3 PIN_D7
#define col0 PIN_D0
#define col1 PIN_D1
#define col2 PIN_D2
#define col3 PIN_D3
// 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()
{
//set_tris_b(0xF0);
//output_b(0xF0);
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();
printf(lcd_putc,"\r\Starting ...");
while(TRUE)
{
k=kbd_getc();
if(k!=0)
{
if(k=='*')
printf(lcd_putc,"%c", '*');
else
printf(lcd_putc,"%c", k);
}
}
} |
I'm using the flex 4x4 driver
but it only can display 1,5,9,D.
What is the problem? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 27, 2010 12:18 pm |
|
|
I noticed you're using PortD. Do you have pull-up resistors on the Row
pins ? You need them. You can use 10K resistors. |
|
|
project2010
Joined: 14 Mar 2010 Posts: 21
|
|
Posted: Thu May 27, 2010 12:22 pm |
|
|
PCM programmer wrote: | I noticed you're using PortD. Do you have pull-up resistors on the Row
pins ? You need them. You can use 10K resistors. |
Dear sir
i added 10k to D4,D5,D6,D7
connect to 5V -----10k--------row |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 27, 2010 12:32 pm |
|
|
Post the Manufacturer and Part Number of your keypad.
Or post a link to the data sheet for it. |
|
|
project2010
Joined: 14 Mar 2010 Posts: 21
|
|
Posted: Thu May 27, 2010 12:43 pm |
|
|
last time i use the left one....but it broke
i replace by black one....but it does not work....
i checked the black one
pin 1,5 will connect when col 1 is press
2,6will connect when col 1 is press
3,7will connect when col 1 is press
4,8 will connect when col 1 is press
i can't find the datasheet.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 27, 2010 1:42 pm |
|
|
Quote: | pin 1,5 will connect when col 1 is press
2,6will connect when col 1 is press
3,7will connect when col 1 is press
4,8 will connect when col 1 is press |
I don't understand this description.
Press the "1" key. Then post the two pins that are connected.
Also do it for the "2" and "3" and "A" keys. Post the pins that are
connected for each key. (Only press one key for each test).
Also do the test for the "4", "7" and "*" keys. |
|
|
project2010
Joined: 14 Mar 2010 Posts: 21
|
|
Posted: Thu May 27, 2010 2:18 pm |
|
|
if i push 1/4/7/* key pin1,5 will connected
if i push 2/5/8/0 key pin2,6 will connected
if i push 3/6/9/# key pin3,7 will connected
if i push A/B/C/D key pin4,8 will connected |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 27, 2010 2:36 pm |
|
|
I think your keypad is defective. There should be one pair of pins
connected for each key that is pressed. For example, I have a 4x3
keypad. It gives the following connections:
Code: |
Key Pins
Pressed Connected
1 1 to 5
4 2 to 5
7 3 to 5
* 4 to 5
2 1 to 6
5 2 to 6
8 3 to 6
0 4 to 6
3 1 to 7
6 2 to 7
9 3 to 7
# 4 to 7
|
From this you can see that pins 5, 6 and 7 are the column pins.
That's because any time you press a key in the 1st column, pin 5 is used.
For the 2nd column, pin 6 is the common pin. For the 3rd column, pin 7
is the common pin. Then pins 1-4 are the row pins.
This type of result is what I hoped to get from you. But now, maybe
your keypad is defective. |
|
|
|
|
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
|