help with a keypad reader

Discussion in 'Embedded Systems and Microcontrollers' started by eessoo, Feb 10, 2012.

  1. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    pic 16f877A
    keypad 16
    i want the program to read from the user the input and put it back on the lcd
    so far if i input 53.1 the program send out 5331.999
    or 20.1 it gives 2001.999
    but it works fine with out the (.)
    here is the program
    (part of it) the other part is taking the percentage the the user enter and so some calculation to it

    please explain your self in details coz i'm not very good in english



    int key=0;
    int kp1;
    float x;
    int h=10;
    int kp2=0;
    float kp=0;
    float blue ,red ,green;
    char txt[3];
    float e;
    int keypad(int key)
    {
    switch (key)
    {case 9:
    key=49;
    kp1=1;
    break;
    case 10:
    key=50;
    kp1=2;
    break;
    case 11:
    key=51;
    kp1=3;
    break;
    case 5:
    key=52;
    kp1=4;
    break;
    case 6:
    key=53;
    kp1=5;
    break;
    case 7:
    key=54;
    kp1=6;
    break;
    case 1:
    key=55;
    kp1=7;
    break;
    case 2:
    key=56;
    kp1=8;
    break;
    case 3:
    key=57;
    kp1=9;
    break;

    case 14:
    key=48;
    kp1=0;
    break;
    case 15:
    key=46;
    }
    return (key);
    }
    char keypadPort at PORTD;
    // Lcd pinout settings
    sbit LCD_RS at RB4_bit;
    sbit LCD_EN at RB5_bit;
    sbit LCD_D7 at RB3_bit;
    sbit LCD_D6 at RB2_bit;
    sbit LCD_D5 at RB1_bit;
    sbit LCD_D4 at RB0_bit;

    // Pin direction
    sbit LCD_RS_Direction at TRISB4_bit;
    sbit LCD_EN_Direction at TRISB5_bit;
    sbit LCD_D7_Direction at TRISB3_bit;
    sbit LCD_D6_Direction at TRISB2_bit;
    sbit LCD_D5_Direction at TRISB1_bit;
    sbit LCD_D4_Direction at TRISB0_bit;

    Code ( (Unknown Language)):
    1.  
    2. void main() {
    3.  
    4.  Keypad_Init();
    5.  lcd_init();
    6.  while(1)
    7.  {
    8.          do
    9.          key=keypad_key_click();
    10.          while(!key);
    11.                       key=keypad(key);
    12.                         if(key==13)                      \\if the use enter (enter)
    13.                               {
    14.                               lcd_cmd(_lcd_clear);
    15.                               break;
    16.                               }
    17.                         else if(key==15)                  \\if the user enter '.'
    18.                          {
    19.                          while(1)
    20.                                         {lcd_chr_cp(key);
    21.                                           do
    22.                                          key=keypad_key_click();
    23.                                          while(!key);
    24.                                          key=keypad(key);
    25.                                           kp=kp1/10+kp;                  
    26.  
    27.                                                          if(key==13)                  \\ if the user use Enter
    28.                                                          {lcd_cmd(_lcd_clear);
    29.                                                          break;}
    30.                                          }
    31.                                         break;
    32.                          }
    33.                          else
    34.                          {lcd_chr_cp(key);
    35.                           kp=kp*10+kp1;}
    36. }
    37. floattostr(kp,txt);
    38. Lcd_Out_CP(txt);
    39. }
    40.  
    41.  
     
    Last edited: Feb 10, 2012
  2. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    and this is not a home work (i'm simply trying to improve my self) :D
     
    Last edited: Feb 10, 2012
  3. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    guys i'm using MikroC PRO PIC
    come on it can't be than hard
    plzzzz this program is KILLING MEE
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    If it is not that hard then why are you asking for help?
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you tried some debugging. Like single stepping, adding some watches, or using breakpoints. Be able to debug is a skill the beginner often underestimate. But debugging skills are as well important as understand how to program. Are you using MPLAB?
     
  6. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    i'm using Proteus
    can u tell me more about the debugging?what watches?
    the program compiles correctly no syntax error
    and if u do the equations by hand it will give correct results
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    MikroC itself does has a very nice software simulator/debugger built into the compiler. But that sums up all I know about it. Since I do not use Micro C. but MPLAB and HI-Tech C. If you think it is worth it. Start a new thread. In Programmer's Corner. And ask for debugging tips using MikroC
     
    eessoo likes this.
  8. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    Code ( (Unknown Language)):
    1.  
    2. else if(key==15)                  \\if the user enter '.'
    3.  
    key is never 15.
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    Code ( (Unknown Language)):
    1.  
    2. kp=kp1/10+kp;  
    is also not the correct algorithm.
     
  10. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    there is 16 buttons on the keypad
    and why is it no correct algorithm?
    if u do it manually it will work
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    What do you mean by do it manually? Tell us exactly what you do manually.
     
  12. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    the input in the lcd is in char
    so i take each digit and add it the equation
    for example if the use enter (123)

    (output=(output*10)+input)
    ( kp = kp * 10 + kp1)
    0 = 0 * 10 + 1 >>kp=1
    1 = 1 * 10 + 2 >> kp = 12
    12 = 12*10 + 3 >> kp=123
    u get it?! :D

    ( kp=kp1/10+kp)<<< this only works if u add (.) to your number

    i modified it Little bit (less error)


    Code ( (Unknown Language)):
    1.  
    2. int key=0;
    3. float x;
    4. int h=10;
    5. char txt[15];
    6. float kpt=0;
    7.  
    8.  int keypad(int key)
    9.  {
    10.            switch (key)
    11.              {case 9:
    12.              key=49;
    13.              break;
    14.               case 10:
    15.               key=50;
    16.              break;
    17.               case 11:
    18.               key=51;
    19.              break;
    20.               case 5:
    21.               key=52;
    22.              break;
    23.               case 6:
    24.               key=53;
    25.              break;
    26.               case 7:
    27.               key=54;
    28.              break;
    29.               case 1:
    30.               key=55;
    31.              break;
    32.               case 2:
    33.               key=56;
    34.              break;
    35.               case 3:
    36.               key=57;
    37.              break;
    38.              case 13:
    39.              key=13;
    40.              break;
    41.               case 14:
    42.               key=48;
    43.               break;
    44.               case 15:
    45.               key=46;
    46.               }
    47.               return (key);
    48.  }
    49. char keypadPort at PORTD;
    50. // Lcd pinout settings
    51. sbit LCD_RS at RB4_bit;
    52. sbit LCD_EN at RB5_bit;
    53. sbit LCD_D7 at RB3_bit;
    54. sbit LCD_D6 at RB2_bit;
    55. sbit LCD_D5 at RB1_bit;
    56. sbit LCD_D4 at RB0_bit;
    57.  
    58. // Pin direction
    59. sbit LCD_RS_Direction at TRISB4_bit;
    60. sbit LCD_EN_Direction at TRISB5_bit;
    61. sbit LCD_D7_Direction at TRISB3_bit;
    62. sbit LCD_D6_Direction at TRISB2_bit;
    63. sbit LCD_D5_Direction at TRISB1_bit;
    64. sbit LCD_D4_Direction at TRISB0_bit;
    65.  
    66.  
    67. void main() {
    68.  
    69.  Keypad_Init();
    70.  lcd_init();
    71.  while(1)
    72.  {
    73.          do
    74.          key=keypad_key_click();
    75.          while(!key);
    76.                            key=keypad(key);
    77.                         if(key==13)
    78.                               {
    79.                               lcd_cmd(_lcd_clear);
    80.                               break;
    81.                               }
    82.  
    83.                         else if(key==46)
    84.                          {
    85.                                         while(1)
    86.                                         {lcd_chr_cp(key);
    87.                                           do
    88.                                          key=keypad_key_click();
    89.                                          while(!key);
    90.                                           key=keypad(key);
    91.                                           [COLOR="DarkOrange"]kpt=((key-48)/10)+kpt;[/COLOR]
    92.  
    93.                                                          if(key==13)
    94.                                                          {lcd_cmd(_lcd_clear);
    95.                                                          goto[COLOR="Red"] finaly[/COLOR];}
    96.  
    97.                                          }
    98.                                         break;
    99.                          }
    100.                         else if(key!=13)
    101.                          {lcd_chr_cp(key);
    102.                          [COLOR="DarkOrange"] kpt=kpt*10+(key-48[/COLOR]);}
    103. }
    104. [COLOR="red"]finaly:[/COLOR]
    105. floattostr(kpt,txt);
    106. Lcd_Out_CP(txt);
    107. }
    108.  
     
    Last edited: Feb 11, 2012
  13. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    Code ( (Unknown Language)):
    1.  
    2. ( kp=kp1/10+kp)
    3.  
    You still have not demonstrated how this works in manual operation.

    If the user types in 1.23
    the result would be 1.5 (ignoring floating-point errors for the moment).
     
    eessoo likes this.
  14. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    this can be easily fixed
    kp=kp1/V+kp
    v=v*10
    when the user enter 2 the equation will be( kp=kp1/10+kp) first value of V=10 >>>>0.2
    when enter 3 it will be (kp=kp1/100+kp) >>>0.03

    but i want the user to enter only one number after the (dot)
     
  15. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    That's ok, but why risk the limitation and possible source of error. There are many ways to fix this. One way is as you suggested.

    Did you correct the problem with key is never 15?
     
  16. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    there are 16 buttons on my keypad
    i made key number 15 for (.)
    and key number 13 for (enter)
    i can't see anything wrong with it :(

    tell me what u are thinking. in details plzz :)
     
  17. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    Here is your original code:
    Code ( (Unknown Language)):
    1.  
    2. case 15:
    3.               key=46;
    4.  
    You have replaced key=15 with key=46.
    Hence your main program never sees key == 15.
     
  18. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    Code ( (Unknown Language)):
    1.  
    2.  
    3.                         else if(key==46)
    4.                          {
    5.                                         while(1)
    6.                                         {lcd_chr_cp(key);
    7.                                           do
    8.                                          key=keypad_key_click();
    9.                                          while(!key);
    10.                                           key=keypad(key);
    11.                                           [COLOR="DarkOrange"]kpt=((key-48)/10)+kpt;[/COLOR]
    12.  
    13.                                                          if(key==13)
    14.                                                          {lcd_cmd(_lcd_clear);
    15.                                                          goto[COLOR="Red"] finaly[/COLOR];}
    16.  
    17.                                          }
    18.                                         break;
    19.                          }
    20.            
    21.  
    yes
    and i change my if statment to if(key==46)
     
  19. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    Ok, now that you have fixed that, your code is messy.
    There is no need to check for (key == 13) in two different places.
    Write your code so that there is only one process loop.

    What is the largest value the user is expected to enter?
    If the value does not exceed the range of the variable type, one solution is to accumulate the number as an integer. When the "." is pressed, accumulate a power of 10 value and do the division after "ENTER" is pressed.

    (This is only one of many possible solutions.)
     
  20. MrChips

    Moderator

    Oct 2, 2009
    12,441
    3,361
    Instead of a switch statement in keypad( ), use an array, a look-up-table.
    This is much more simple and efficient.
     
    eessoo likes this.
Loading...