|
|
View previous topic :: View next topic |
Author |
Message |
bofin Guest
|
kbd.c revisited |
Posted: Fri Jul 25, 2003 5:57 pm |
|
|
<font face="Courier New" size=-1>hi, I have implemented the code from <a href="http://www.pic-c.com/forum/general/posts/12750.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/12750.html</a> and it works a treat.
... up to a point. The routine seems to 'fall through'. ie it doesn't wait for a key press before returning.
I am using a 4x4 keypad on port B and an LCD unit on port D on a F877. compiler version 3.083. compiler v3.125 made no difference.
here is a code snippet (part of main()):
void main(void)
{
int number;
char k;
lcd_init();
kbd_init();
while(TRUE){
lcd_putc("\fEnter Command.\n");
k=kbd_getc();
while(k!=0) {
if(k='A')
AllUnits();
else if('B')
GroupUnits();
else if('C')
OneUnit() ;
}
k='\0';
}
one sample routine for calling GroupUnit() {OneUnit() is very similar}
void AllUnits() {
lcd_putc("\fSelect ALL units\nMake anouncement.");
printf("A");
output_low(LED);
}
// select Group
void GroupUnits() {
char k;
int number;
lcd_putc("\fSelect GROUP \nEnter GROUP number");
number=kbd_num();
lcd_putc("\fMake anouncement.");
lcd_putc("\nPress D to finish");
printf("G\%c",number);
output_low(LED);
k=kbd_getc();
while(k!=0) {
if((k=='D')||(k=='F'))
output_high(LED);
}
}
int kbd_num( ){
char i,j,k;
int m;
i=kbd_getc(); lcd_putc(i);
j=kbd_getc(); lcd_putc(j);
k=kbd_getc(); lcd_putc(k);
return(m = i*100 + j*10 + k);
}
Now, I expect the kbd_getc() routine within kbd_num() to wait for the user to push keys and hence dsiplay the numbers entered, but it just cycles through the "Enter Command" part of main().
If I press 'A' for the AllUnits option, the PIC cycles within that routine. The only way out is a reset button on MCLR.
PS I'd like to express my thanks to PCM Programmer for all the hassle he puts up with from us users of the forum.
Thanks for your help with my problem
John</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516380 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: kbd.c revisited |
Posted: Fri Jul 25, 2003 6:51 pm |
|
|
:=The routine seems to 'fall through'. ie it doesn't wait for a key press before returning.
--------------------------------------------------------
That's the CCS sample code and it expects to be polled
constantly. Here's a quick routine that I whipped out that
will wait for a key to be pressed. I've also shown how to
call it from your main() routine. It will sit there at the
line "k = wait_for_key();" until it gets a key.
When it gets a key, the value will be returned and put in "k".
Then you can test the value of k, and do whatever you want
with it.
<PRE>
This new routine will wait for a key to be pressed.
<BR>
char wait_for_key(void)
{
char c;
<BR>
while(1)
{
c = kbd_getc();
<BR>
if(c)
return(c);
<BR>
delay_us(100);
}
<BR>
}
<BR>
The delay value of 100 us is set to work with the CCS value
of 33, for the keyboard debounce factor. It works OK with
my crystal frequency of 8 MHz. It should probably work OK
with whatever frequency you're using too.
<BR>
//----------------------
main()
{
char k;
<BR>
lcd_init();
kbd_init();
<BR>
lcd_putc("\fReady!\n");
<BR>
while(TRUE)
{
// Comment out these two lines.
// k=kbd_getc();
// if(k!=0)
<BR>
k = wait_for_key(); // <--- Add this line.
if(k=='*')
lcd_putc('\f');
else
lcd_putc(k);
}
}
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516382 |
|
|
bofin Guest
|
Re: kbd.c revisited |
Posted: Mon Jul 28, 2003 4:37 am |
|
|
I really want to express my thanks to PCM Programmer for helping me with this. It was a case of being too close to the problem. You know how it gets ....
Thanks
John
:=:=The routine seems to 'fall through'. ie it doesn't wait for a key press before returning.
:=--------------------------------------------------------
:=
:=That's the CCS sample code and it expects to be polled
:=constantly. Here's a quick routine that I whipped out that
:=will wait for a key to be pressed. I've also shown how to
:=call it from your main() routine. It will sit there at the
:=line "k = wait_for_key();" until it gets a key.
:=When it gets a key, the value will be returned and put in "k".
:=Then you can test the value of k, and do whatever you want
:=with it.
:=
:=<PRE>
:=This new routine will wait for a key to be pressed.
:=<BR>
:=char wait_for_key(void)
:={
:=char c;
:=<BR>
:=while(1)
:= {
:= c = kbd_getc();
:=<BR>
:= if(c)
:= return(c);
:=<BR>
:= delay_us(100);
:= }
:=<BR>
:=}
:=<BR>
:=
:=The delay value of 100 us is set to work with the CCS value
:=of 33, for the keyboard debounce factor. It works OK with
:=my crystal frequency of 8 MHz. It should probably work OK
:=with whatever frequency you're using too.
:=
:=<BR>
:=//----------------------
:=main()
:={
:=char k;
:=<BR>
:=lcd_init();
:=kbd_init();
:=<BR>
:=lcd_putc("\fReady!\n");
:=<BR>
:=while(TRUE)
:= {
:= // Comment out these two lines.
:= // k=kbd_getc();
:= // if(k!=0)
:=<BR>
:= k = wait_for_key(); // <--- Add this line.
:=
:= if(k=='*')
:= lcd_putc('\f');
:= else
:= lcd_putc(k);
:= }
:=}
:=</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516422 |
|
|
|
|
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
|