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

keypad help, please

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



Joined: 24 Feb 2008
Posts: 2

View user's profile Send private message

keypad help, please
PostPosted: Sun Feb 24, 2008 10:47 pm     Reply with quote

good day everyone, i'm trying to use a keypad with 16F877A but to no avail, i just can't make it work right. it's still unfinished, but completing a subroutine for the first part is already very complicated for me.Confused please help.

it should go like:
1) input 1-10, and then store the entered value and proceed to a subroutine
(i have created a subroutine for the "1" yet)
2) at the specified subroutine, it should choose from 0 or #. 0 means that you actually input 10, and pressing # means that the input is 1 only.
3) another subroutine goes whether the user sets 1 or 10. the problem, i think, goes with the program itself, maybe someone can help me spot where the fault is..

another problem is when i am using BREAK in switch, isn't it that the other conditions (or cases) should not be followed? i mean, it should terminate itself on that case where it fits, right? that doesn't happen with my code...

here's the datasheet of my LCD:
http://pdf1.alldatasheet.com/datasheet-pdf/view/127934/ETC/JHD162A.html

and for my keypad, it's an alphanumeric one, 4x4 with ABCD's at the rightmost row.. (blue keypad, is that how you guys call it?)

here's my code, written in CCS...

Code:
#include "D:\All SP\all programs\keypad\keypad.h"
#include <LCD.C>
#include <KBD.C>

void time10()
{
 char t10;
 lcd_putc("\finterval: 0=exit");
 lcd_putc("\n3,4,6,8,12,24");
 do
 {
   t10=kbd_getc();
   while(1)
   {
      if (t10==3 || t10==4 || t10==6 || t10==1 || t10==2)
      {
         if (t10==1 || t10==2)
         {
            if (t10==1)
            {
               lcd_putc("\fevery 12 hrs");
            }
            else
            {
               lcd_putc("\fevery 24 hrs");
            }       
         }
         else
         {
            lcd_putc("\fevery ");
            lcd_putc(t10);
            lcd_putc(" hours");
         }
      }
   }
 }  while(t10!=0);
}

void two_digit()
{
   char digit2;
   lcd_putc("\f0 or #\n");
   while(TRUE)
   {
      digit2=kbd_getc();
      if(digit2=='*')
      {
      lcd_putc("\fexit");
      }
      else
      {
         switch(digit2)
         {
         case '0':
            lcd_putc("\fpt 10");
            delay_ms(300);
            time10();
            break;
         case '#':
            lcd_putc("\fpt 1");
            break;
         }   
      }
   }
}
void main()
{
   char k;
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   lcd_init();
   kbd_init();

   // TODO: USER CODE!!
   port_b_pullups(TRUE);
   lcd_putc("\fenter patient #\n");
   while (TRUE)
   {
      k=kbd_getc();
      if(k!=0)
      switch(k)
      {
      case '1':
         lcd_putc("\f1");
         two_digit();
         break;
      case '2':
         lcd_putc("\f2"); //still working on a subroutine for this
         break;     
      case '3':
         lcd_putc("\f3"); //still working on a subroutine for this
         break;
      default:
         lcd_putc("\fInvalid");
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 25, 2008 12:26 am     Reply with quote

I didn't try to figure out how your program works. I just looked for errors.
In the code below, you're calling kbd_getc() and comparing the result to
various integers. But that routine doesn't return integers. It returns
ASCII values from '0' to '9'. You need to add single quotes around the
numbers below, so they will be ASCII values.
Code:

t10=kbd_getc();
 while(1)
   {
      if (t10==3 || t10==4 || t10==6 || t10==1 || t10==2)
      {
         if (t10==1 || t10==2)
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Feb 25, 2008 3:11 am     Reply with quote

You have 2 other problems as well.

As far as I can tell your switch routine should work fine. The problem is that you have an infinite loop which you will never exit.

The same goes for the time10() routine.

you shouldn't need either of them unless this is your intention.

You can also tidy up the if conditions in time10() to make it more efficient (sp).

Code:
   
  do {
    t10=kbd_getc();
    if (t10 == '1') {
        lcd_putc("\fevery 12 hrs");
     } else if (t10 == '2') {
        lcd_putc("\fevery 24 hrs");
     } else if (t10== '3' || t10== '4' || t10== '6') {     
        lcd_putc("\fevery ");
        lcd_putc(t10);
        lcd_putc(" hours");
    }
  } while(t10!=0);


Are you trying to exit the two_digit routine if '*' is prressed ? Then try this.
Code:

      digit2=kbd_getc();
      if(digit2=='*') {
        lcd_putc("\fexit");
        break;  // Exit the while loop! You could put return; here instead but this is cleaner.
      }
freyanicole



Joined: 24 Feb 2008
Posts: 2

View user's profile Send private message

PostPosted: Fri Feb 29, 2008 11:19 pm     Reply with quote

solved the problem, keypad is now working and i can now correctly loop the routines the way i wanted them to...

thanks a lot for the help, everyone.... Very Happy
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