View previous topic :: View next topic |
Author |
Message |
Chris007
Joined: 25 Mar 2010 Posts: 11
|
Small Code Problem |
Posted: Tue Mar 30, 2010 5:08 pm |
|
|
Hi,
I'm having a bit of trouble with this code, basically I want the user to be able to select an option from that given on the screen. The lcd displays the options but its not accepting the input, the hardware is 100% and working I am able to pick up inputs using a different program:
Below is the program:
Code: | #include<16f877a.h>
#fuses xt,noprotect,nowdt
#use delay(clock=4000000)
#include<lcd420.c>
#include<kbd44.c>
void main()
{
char k=0;
int game=0;
lcd_init();
kbd_init();
lcd_putc("\fWelcome");
delay_ms(3000);
while(1)
{
lcd_putc("\fPlease Select Game:\nGame 1\nGame 2 \nGame 3");
delay_ms(2000);
lcd_putc("\f");
k=kbd_getc();
if(k!=0)
lcd_putc(k);
switch(k) {
case 1 : lcd_putc("\nGame 1 Selected");
game=1;
lcd_putc(k);break;
case 2 : lcd_putc("\Game 2 Selected");
game=2;
lcd_putc(k);break;
case 3 : lcd_putc("\fGame 3 Selected");
game=3;
lcd_putc(k);break;
}
}
} |
Any help, would really appreciate |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 30, 2010 5:20 pm |
|
|
You can't have any sizeable delays in the polling loop for kbd_getc().
kbd_getc() is unfortunately named. It's not like getc() for #use rs232.
It doesn't wait for a key press.
Here is a new 'keypad_getc' routine that does wait for a key:
Code: |
// Wait for a key to be pressed. Then return it.
char keypad_getc(void)
{
char retval;
while(1)
{
retval = kbd_getc();
if(retval)
return(retval);
delay_ms(10);
}
} |
In your main(), you don't have to validate 'k' any more (i.e., you don't
have to check if it's non-zero). Just use it. Example:
Code: |
k = keypad_getc();
lcd_putc(k);
|
Remember, this new keypad_getc() function waits for a key.
It won't return until a key is pressed. |
|
|
Chris007
Joined: 25 Mar 2010 Posts: 11
|
|
Posted: Tue Mar 30, 2010 5:32 pm |
|
|
Hi, thanks so much for your help. I'm kinda getting a lot of errors e.g. retval not declared when there is already a declaration... char retval;
Could you tell me where I could place the code u have provided within mine pls? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Chris007
Joined: 25 Mar 2010 Posts: 11
|
|
Posted: Tue Mar 30, 2010 6:02 pm |
|
|
Thanks for the examle, my code now looks like this:
Code: | #include<16f877a.h>
#fuses xt,noprotect,nowdt
#use delay(clock=4000000)
#include<lcd420.c>
#include<kbd44.c>
// Wait for a key to be pressed. Then return it.
char keypad_getc(void)
{
char retval;
while(1)
{
retval = kbd_getc();
if(retval)
return(retval);
delay_ms(10);
}
}
void main()
{
char k;
int game=0;
lcd_init();
kbd_init();
lcd_putc("\fWelcome!");
delay_ms(3000);
while(1)
{
lcd_putc("\fPlease Select Game:\nGame 1\nGame 2\nGame 3");
delay_ms(1000);
lcd_putc("\f");
k = keypad_getc();
lcd_putc(k);
switch(k) {
case 1 : lcd_putc("\nGame 1 Selected");
game=1;
lcd_putc(k);break;
case 2 : lcd_putc("\Game 2 Selected");
game=2;
lcd_putc(k);break;
case 3 : lcd_putc("\fGame 3 Selected");
game=3;
lcd_putc(k);break;
}
}
} |
I'm still unable to get the input [/code] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 30, 2010 6:48 pm |
|
|
Look at the array of key values in the kbd44.c driver. What type of
values are used as the elements of the array ? Then look at your
switch-case statement. What type of values are you testing for in
your case statements ?
Change the case statements so the tested values match what is
returned by kbd44.c, and then it will work. |
|
|
|