CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Small Code Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Chris007



Joined: 25 Mar 2010
Posts: 11

View user's profile Send private message

Small Code Problem
PostPosted: Tue Mar 30, 2010 5:08 pm     Reply with quote

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 Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 5:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 5:32 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 5:35 pm     Reply with quote

Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=41848&start=4
Chris007



Joined: 25 Mar 2010
Posts: 11

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 6:02 pm     Reply with quote

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 Confused[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 6:48 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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