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 routine

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



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

keypad routine
PostPosted: Sat Oct 31, 2009 12:21 am     Reply with quote

Hi,
I have made a routine which scan for keypad and display data on lcd.
The range of numbers entered from keypad is between 0-999. My code is as follows. The code seems to be working fine. Can I make it more effective or reduce the code by using other techniques?
I also want to enter floating numbers and display it on the lcd e.g 14.5, 587.32, 1.2 etc.
What will be the technique for that?
Code:

VOID main()
{
 int16 array[3]={0,0,0};
 int16 value;
 int16 unit,ten,hundred,add;
 int i=0;
 lcd_init();
 
 setup_adc_ports (NO_ANALOGS);
 setup_adc (ADC_OFF);
 setup_psp (PSP_DISABLED);
 setup_spi (FALSE);
 setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_1);
 setup_timer_1 (T1_INTERNAL|T1_DIV_BY_1);
 setup_timer_2 (T2_DISABLED, 0, 1);
 
 while (1)
 {
  value = readkeypad ();//get value from readkeypad function.
  delay_ms(100);//for debouncing
  if(value != 255)//if value is other than 255
  {
   if(value==10)//for indication that data is completed.
   {
    if(i==1)//means that entered value is consist of only 1 number e.g 3
    {
    unit=array[0];
    array[1]=0;
    array[2]=0;
    add=unit;//arrange data and display it
    printf(lcd_putc,"\f\nsum is %lu",add);
    i=0;
    }
    if(i==2)//means that entered value is consist of 2 numbers e.g 25
    {
    unit=array[0]*10;
    ten=array[1];
    array[2]=0;
    add=unit + ten;//arrange data and display it
    printf(lcd_putc,"\f\nsum is %lu",add);
    i=0;
    }
    if(i==3)//means that entered value is consist of 2 numbers e.g 285
    {
    unit=array[0]*100;
    ten=array[1]*10;
    hundred=array[2];
    add=unit + ten + hundred;//arrange data and display it
    printf(lcd_putc,"\f\nsum is %lu",add);
    i=0;
    }
   }
   else
   {
    array[i]=value;//get values and put it in array.
    lcd_gotoxy(i+1,1);//increament lcd cursor position according to i.
    printf(lcd_putc,"%lu",value);
    i++;
    if(i>3)//i require only three digit number.
    {
     i=0;
    }
   }
  }
 }
}

int readkeypad (void)
{
 output_bit (r1, 0);
 output_bit (r2, 0);
 output_bit (r3, 0);
 output_bit (r4, 0);
 output_bit (r1, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 10;
 IF (input (c2)) RETURN 0;
 IF (input (c3)) RETURN 11;
 IF (input (c4)) RETURN 12;
 output_bit (r1, 0);
 output_bit (r2, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 7;
 IF (input (c2)) RETURN 8;
 IF (input (c3)) RETURN 9;
 IF (input (c4)) RETURN 13;
 output_bit (r2, 0);
 output_bit (r3, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 4;
 IF (input (c2)) RETURN 5;
 IF (input (c3)) RETURN 6;
 IF (input (c4)) RETURN 14;
 output_bit (r3, 0);
 output_bit (r4, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 1;
 IF (input (c2)) RETURN 2;
 IF (input (c3)) RETURN 3;
 IF (input (c4)) RETURN 15;
 output_bit (r4, 0);
 RETURN 255;
}
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Mon Nov 02, 2009 10:20 pm     Reply with quote

any suggestions for floating numbers .
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 03, 2009 2:44 pm     Reply with quote

Does your keypad have a decimal point key on it ?

If not, post the keys that it does have.
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Tue Nov 03, 2009 11:06 pm     Reply with quote

I have done a little bit for floating numbers.
My code is consuming lot of memory.
I want to restrict some things.
Before decimal point I want 0-3 digits. No more than 3 digits and 0-2 digits after decimal point.
When user press like this .01, .1, 1.25, 10.4, 999.9 it has to display data as they are entered.
I have tried all this in my below code, but I am confused how to do these.
I have added comments so that you guys understand it.

I have 16 keys in keypad. I have assigned values for (F1=10 for completion of data) and (F2=11 for decimal). I am not using P1,P2,P3,P4 right now.
Code:

///////////////////////////////////////////////////////////////////////////////
//////                           keypad keys                          ////////////////
//////                           1   2   3  P1                           ////////////////
//////                           4   5   6  P2                           ////////////////
//////                           7   8   9  P3                           ////////////////
//////                           F1  0   F2 P4                          ////////////////
//////                                                                       ////////////////
///////////////////////////////////////////////////////////////////////////////

#include "k1.h"
#include <E:\abdul hayee\pic controller\my examples\current interrupter\Flexlcd.c>
#include <string.h>
#include <stdlib.h>

#define r1 PIN_B2//row
#define r2 PIN_B3//row
#define r3 PIN_B4//row
#define r4 PIN_B5//row
#define c1 PIN_A2//column
#define c2 PIN_A3//column
#define c3 PIN_A4//column
#define c4 PIN_A5//column

int readkeypad(void);

VOID main()
{
 int16 array[6]={0,0,0,0,0,0};
 int16 value;
 float unit,ten,hundred,onetenth,onehundred,add;
 int i=0,j=5,k=1;
 
 lcd_init();
 
 setup_adc_ports (NO_ANALOGS);
 setup_adc (ADC_OFF);
 setup_psp (PSP_DISABLED);
 setup_spi (FALSE);
 setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_1);
 setup_timer_1 (T1_INTERNAL|T1_DIV_BY_1);
 setup_timer_2 (T2_DISABLED, 0, 1);
 
 while (1)
 {
  value = readkeypad ();//get value from readkeypad function.
  delay_ms(200);//for debouncing
  if(value != 255)//if value is other than 255
  {
   if(value==10)//for indication that data is completed.
   {
    switch(j)
    {
    case 4://means data is like that .01
    onehundred=array[5]*.01;
    add=onehundred;
    printf(lcd_putc,"\f\nsum is %3.2f",add);
    i=0;j=5;k=1;
    break;
   
    case 3://means data is like that .10
    onetenth=array[4]*.1;
    onehundred=array[5]*.01;
    add= onetenth + onehundred;
    printf(lcd_putc,"\f\nsum is %3.2f",add);
    i=0;j=5;k=1;
    break;
   
    case 2://means data is like that 1.23
    unit=array[0];
    onetenth=array[4]*.1;
    onehundred=array[5]*.01;
    add=unit + onetenth + onehundred;
    printf(lcd_putc,"\f\nsum is %3.2f",add);
    i=0;j=5;k=1;
    break;
   
    case 1://means data is like that 10.23
    unit=array[0]*10;
    ten=array[1];
    onetenth=array[4]*.1;
    onehundred=array[5]*.01;
    add=unit + ten + onetenth + onehundred;
    printf(lcd_putc,"\f\nsum is %3.2f",add);
    i=0;j=5;k=1;
    break;
   
    case 0://means data is like that 123.45
    unit=array[0]*100;
    ten=array[1]*10;
    hundred=array[2];
    onetenth=array[4]*.1;
    onehundred=array[5]*.01;
    add=unit + ten + hundred + onetenth + onehundred;//arrange data and display it
    printf(lcd_putc,"\f\nsum is %3.2f",add);
    i=0;j=5;k=1;
    break;
    }
   }
   else if(value==11)
   {
    if(k==1)//this restrict to only one decimal
    {
     printf(lcd_putc,".");
    }
    k++;
    i=4;
   }
   else
   {
    array[i]=value;//get values and put it in array.
    lcd_gotoxy(i+1,1);//increament lcd cursor position according to i.
    printf(lcd_putc,"%lu",value);
    i++;//
    j=j-1;//for switch case
    if(i>5)//restrict to only 5 digits.
    {
     i=5;
    }
   }
  }
 }
}

int readkeypad (void)
{
 output_bit (r1, 0);
 output_bit (r2, 0);
 output_bit (r3, 0);
 output_bit (r4, 0);
 output_bit (r1, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 10;
 IF (input (c2)) RETURN 0;
 IF (input (c3)) RETURN 11;
 IF (input (c4)) RETURN 12;
 output_bit (r1, 0);
 output_bit (r2, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 7;
 IF (input (c2)) RETURN 8;
 IF (input (c3)) RETURN 9;
 IF (input (c4)) RETURN 13;
 output_bit (r2, 0);
 output_bit (r3, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 4;
 IF (input (c2)) RETURN 5;
 IF (input (c3)) RETURN 6;
 IF (input (c4)) RETURN 14;
 output_bit (r3, 0);
 output_bit (r4, 1);
 delay_ms(10);
 IF (input (c1)) RETURN 1;
 IF (input (c2)) RETURN 2;
 IF (input (c3)) RETURN 3;
 IF (input (c4)) RETURN 15;
 output_bit (r4, 0);
 RETURN 255;
}
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