|
|
View previous topic :: View next topic |
Author |
Message |
sareehy
Joined: 03 May 2016 Posts: 20
|
|
Posted: Mon Aug 14, 2017 2:51 pm |
|
|
Thank you guys
I tested the keypad using scope using only pull-up resistors tied to 5V
the signal was as in the following:
Would it be better to use external pull-up resistors or internal ?
Sorry for bothering you .
Your effort is highly appreciated |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Mon Aug 14, 2017 3:00 pm |
|
|
Looks good to me, nice clean square edges, which is what you want.
The proof is pressing all the keys and confirm it works in the 'Real World'.
I use external 10K resistors only because some I/O pins on some PICS do NOT have internal pullups available. While it take a very small space on the PCB, it means the hardware is aways the same from product to product and I can substitute the PIC if I have to, without worrying if it'll work or have to change code.
Jay |
|
|
sareehy
Joined: 03 May 2016 Posts: 20
|
|
Posted: Mon Aug 14, 2017 3:45 pm |
|
|
temtronic wrote: | Looks good to me, nice clean square edges, which is what you want.
The proof is pressing all the keys and confirm it works in the 'Real World'.
I use external 10K resistors only because some I/O pins on some PICS do NOT have internal pullups available. While it take a very small space on the PCB, it means the hardware is aways the same from product to product and I can substitute the PIC if I have to, without worrying if it'll work or have to change code.
Jay |
so just connect the keypad with pull-up resistor ^^
thank you temtronic |
|
|
sareehy
Joined: 03 May 2016 Posts: 20
|
|
Posted: Fri Aug 18, 2017 8:27 am |
|
|
Hi all ^_^
I tried to use pins rb4-rb7 as output.
I modified the kbd_1row as the following:
Code: |
#if defined use_portb_kbd
#byte kbd = getenv("SFR:PORTB")
#else
#byte kbd = getenv("SFR:PORTD")
#endif
#define ROW0 (1 << 0) // Pin B0
#define ROW1 (1 << 1) // Pin B1
#define ROW2 (1 << 2) // Pin B2
#define ROW3 (1 << 3) // pin B3
#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
#define ALL_PINS (ALL_ROWS)
// Keypad layout:
char const KEYS[4] = {'1','2','3','4'};
void kbd_init()
{
//port_b_pullups(0x0F);
set_tris_b(0x0f);
}
char kbd_getc(void)
{
static int1 kbd_down = FALSE;
static char last_key = '\0';
BYTE kchar;
BYTE row;
static unsigned int8 temp=0;
kchar='\0';
kbd = input_b()&0x0f;
if(kbd_down)
{
if((kbd & (ALL_ROWS))==(ALL_ROWS))
{
kbd_down=FALSE;
kchar=last_key;
last_key='\0';
}
}
else
{
if((kbd & (ALL_ROWS)) != (ALL_ROWS))
{
if((kbd & ROW0)==0)
row=0;
else if((kbd & ROW1)==0)
row=1;
else if((kbd & ROW2)==0)
row=2;
else if((kbd & ROW3)==0)
row=3;
last_key =KEYS[row];
kbd_down = TRUE;
}
}
delay_ms(10); // Debounce delay
return(kchar);
}
char kbd_pressed(void){
char keypress;
keypress=kbd_getc();
while(keypress=='\0'){
keypress=kbd_getc();
}
return (keypress);
}
|
Test code:
Code: |
#include <16F877A.h>
//#rom 0X2100= {11,14,0,16,4,27,0,0,0,0,0}
//#rom 0X2100= {0,0,0,0,0,0,0,0,0}
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,PUT, NOLVP
#use rs232(baud=9600, UART, ERRORS)
#use fast_io(b)
#define use_portb_kbd
#include "kbd_1row.c"
//======================================
void main()
{
char k;
printf("Ready\n\r");
//set_tris_b(0x0f);
kbd_init();
while(TRUE)
{
k=kbd_pressed();
printf("key:%c\n\r",k);
output_toggle(pin_b7);
}
}
|
Pin B7 should toggle every key pressed but it remains low all the time.
I think the problem is here:
Code: |
#if defined use_portb_kbd
#byte kbd = getenv("SFR:PORTB")
#else
#byte kbd = getenv("SFR:PORTD")
#endif
|
Code: |
kbd = input_b()&0x0f;
|
Regards |
|
|
sareehy
Joined: 03 May 2016 Posts: 20
|
|
Posted: Fri Aug 18, 2017 12:41 pm |
|
|
hi,
i modified kbd_getc as the following
Code: |
char kbd_getc(void)
{
static int1 kbd_down = FALSE;
static char last_key = '\0';
BYTE kchar;
BYTE row;
byte RDio=0;
static unsigned int8 temp=0;
kchar='\0';
//kbd = input_b()&0x0f;
RDio= input_b()&0x0f;
if(kbd_down)
{
if((kbd & (ALL_ROWS))==(ALL_ROWS))
{
kbd_down=FALSE;
kchar=last_key;
last_key='\0';
}
}
else
{
if((kbd & (ALL_ROWS)) != (ALL_ROWS))
{
if((kbd & ROW0)==0)
row=0;
else if((kbd & ROW1)==0)
row=1;
else if((kbd & ROW2)==0)
row=2;
else if((kbd & ROW3)==0)
row=3;
last_key =KEYS[row];
kbd_down = TRUE;
}
}
delay_ms(10); // Debounce delay
return(kchar);
}
|
pins rb4-rb7 now working fine as o/p ^^
any comments on my modification ?
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Fri Aug 18, 2017 12:48 pm |
|
|
You have fiddled with the code without understanding it.
kbd is already defined as a variable to _input_ from the keyboard. That is what the #byte is doing.
In this line:
kbd = input_b()&0x0f;
You are reading portB, then writing the value back out to portB. No wonder it doesn't work....
The rest of the code correctly uses the kbd as input. |
|
|
sareehy
Joined: 03 May 2016 Posts: 20
|
|
Posted: Fri Aug 18, 2017 4:13 pm |
|
|
Ttelmah wrote: | You have fiddled with the code without understanding it.
kbd is already defined as a variable to _input_ from the keyboard. That is what the #byte is doing.
In this line:
kbd = input_b()&0x0f;
You are reading portB, then writing the value back out to portB. No wonder it doesn't work....
The rest of the code correctly uses the kbd as input. |
thanks a lot
so no need to use this line ?
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Fri Aug 18, 2017 10:51 pm |
|
|
Exactly.
However it is important how your TRIS is being used. The code here relies on the keyboard bits being set as inputs. They will be provided you don't use a port wide output. You talk about outputting on the high four bits of the port. If you are using the standard output_high, or output_low on these, the low four bits will happily stay as they are. However if you did 'output_b' (port wide output), this would set the kbd bits as outputs and stop things working. |
|
|
sareehy
Joined: 03 May 2016 Posts: 20
|
|
Posted: Sat Aug 19, 2017 12:58 am |
|
|
Ttelmah wrote: | Exactly.
However it is important how your TRIS is being used. The code here relies on the keyboard bits being set as inputs. They will be provided you don't use a port wide output. You talk about outputting on the high four bits of the port. If you are using the standard output_high, or output_low on these, the low four bits will happily stay as they are. However if you did 'output_b' (port wide output), this would set the kbd bits as outputs and stop things working. |
sorry for bothering you :(
Code: |
#if defined use_portb_kbd
#byte kbd = getenv("SFR:PORTB")
#else
#byte kbd = getenv("SFR:PORTD")
#endif
#define ROW0 (1 << 0) // Pin B0
#define ROW1 (1 << 1) // Pin B1
#define ROW2 (1 << 2) // Pin B2
#define ROW3 (1 << 3) // pin B3
#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
#define ALL_PINS (ALL_ROWS)
// Keypad layout:
char const KEYS[4] = {'1','2','3','4'};
void kbd_init()
{
//port_b_pullups(0x0F);
set_tris_b(0x0f);
}
char kbd_getc(void)
{
static int1 kbd_down = FALSE;
static char last_key = '\0';
BYTE kchar;
BYTE row;
static unsigned int8 temp=0;
kchar='\0';
//kbd = input_b();
if(kbd_down)
{
if((kbd & (ALL_ROWS))==(ALL_ROWS))
{
kbd_down=FALSE;
kchar=last_key;
last_key='\0';
}
}
else
{
if((kbd & (ALL_ROWS)) != (ALL_ROWS))
{
if((kbd & ROW0)==0)
row=0;
else if((kbd & ROW1)==0)
row=1;
else if((kbd & ROW2)==0)
row=2;
else if((kbd & ROW3)==0)
row=3;
last_key =KEYS[row];
kbd_down = TRUE;
}
}
delay_ms(20); // Debounce delay
return(kchar);
}
char kbd_pressed(void){
char keypress;
keypress=kbd_getc();
while(keypress=='\0'){
keypress=kbd_getc();
}
return (keypress);
}
|
test code
Code: |
#include <16F877A.h>
//#rom 0X2100= {11,14,0,16,4,27,0,0,0,0,0}
//#rom 0X2100= {0,0,0,0,0,0,0,0,0}
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,PUT, NOLVP
#use rs232(baud=9600, UART, ERRORS)
#use fast_io(b)
#define use_portb_kbd
#include "kbd_1row.c"
//======================================
void main()
{
char k;
printf("Ready\n\r");
//set_tris_b(0x0f);
kbd_init();
while(TRUE)
{
k=kbd_pressed();
printf("key:%c\n\r",k);
output_toggle(pin_b7);
}
}
|
i modified kbd_1row and it worked fine..
thank you for your appreciated effort |
|
|
|
|
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
|