|
|
View previous topic :: View next topic |
Author |
Message |
emyeuKH
Joined: 22 Sep 2015 Posts: 4
|
4x4 keypad driver |
Posted: Tue Sep 22, 2015 10:45 pm |
|
|
Hi all
I have some problems about keyboard. I use driver which is modified by PCM Programmer and set name: keyboard.c.
http://www.ccsinfo.com/forum/viewtopic.php?t=28022&start=14
Rows have external pull-up resistor (4k7). But don't have anything appear on screen LCD. Please explain to me how to show it. I use pic16F877A, portD for LCD, want to use portC for keyboard. The other port for the other task. Thank you very much for helping.
This is driver keyboard.c
Code: |
//Keypad connection:
#define row0 PIN_C4
#define row1 PIN_C5
#define row2 PIN_C6
#define row3 PIN_C7
#define col0 PIN_C0
#define col1 PIN_C1
#define col2 PIN_C2
#define col3 PIN_C3
// Keypad layout:
char const KEYS[4][4] =
{{'1','2','3','U'},
{'4','5','6','D'},
{'7','8','9','C'},
{'0','.','F','S'}};
#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_c(0xF0);
//output_c(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);
} |
This is my code:
Code: |
#include <16F877A.h>
#include <define_877A.h>
#FUSES HS, NOWDT, NOBROWNOUT, NOLVP, NOPROTECT
#device *=16 ADC=8
#use delay(crystal=12000000)
#define LED PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_ENABLE_PIN PIN_D3
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>
#include <keyboard.c>
void main()
{ char k;
kbd_init();
lcd_init();
delay_ms(100);
lcd_cursor_on(false);
while(true)
{ k=kbd_getc();
delay_ms(100);
printf(lcd_putc,"\f %c",k);
output_toggle(LED);
delay_ms(200);
}
}
// 1 2 3 Up
// 4 5 6 Down
// 7 8 9 Clear
// 0 . Defaut Set |
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 23, 2015 1:26 pm |
|
|
Quote: | while(true)
{
k=kbd_getc();
delay_ms(100);
printf(lcd_putc,"\f %c",k);
output_toggle(LED);
delay_ms(200);
}
|
Your polling loop is wrong. Most of the time, kbd_getc() will return 0.
Look at the code in main() in the link to the keypad driver. It only does
something if k is non-zero. It tests k with an if() statement to see if
it is non-zero. Modify your loop to do it the same way. |
|
|
|
|
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
|