Dragon Board 12Plus- HCS12 micro- KeyPad and 7Segment LED

Discussion in 'Embedded Systems and Microcontrollers' started by Lohitha Wickramasinghe, Apr 20, 2017.

  1. Lohitha Wickramasinghe

    Thread Starter New Member

    Apr 20, 2017
    3
    0
    I'm trying to display two digits on the 7 segment LED display for the project I'm doing. So far I can only display one digit and when I press the second button the first number completely erases and display the 2nd digit. Here is the code I wrote so far. The function "ScanKeyPad()" outputs 0-15 which corresponds to the keypad number. So basically when I click 4 and 8 it should display 48. Below is the C program.
    Code (Text):
    1.  
    2. while(1)
    3.      {
    4.         key = ScanKeyPad();
    5.         if(key != 16) // If a key is pressed
    6.        {
    7.         hold_var = key ; // holds (remember) the value of the key
    8.         // this sequence shows the value of the key that is pressed, in 2 digits
    9.         for (i=0;i<20;i++){
    10.         PTP = 0x0f;
    11.         PTB = SegPat[hold_var/10];
    12.         PTP &= ~0x04;
    13.         delayby1ms(10);
    14.         PTP = 0x0f;
    15.         PTB = SegPat[hold_var%10];
    16.         PTP &= ~0x08;
    17.         delayby1ms(10);
    18.         }
    19.        }
    20.  
    21.  
    22.       // If a new key is not pressed (DEFAULT)
    23.         if(key = 16)// this sequence keeps showing the same number if a new key is not pressed
    24.        {
    25.         PTP = 0x0f;
    26.         PTB = SegPat[hold_var/10];
    27.         PTP &= ~0x04;
    28.         delayby1ms(10);
    29.         PTP = 0x0f;
    30.         PTB = SegPat[hold_var%10];
    31.         PTP &= ~0x08;
    32.         delayby1ms(10);
    33.         }          
    34.    }
    35.  
    36.  
    37.  
     
  2. MrChips

    Moderator

    Oct 2, 2009
    13,943
    4,033
    When a new key is pressed you are replacing hold_var with key.
    Instead, you need to multiply the old value by 10 and then add key.

    Try:

    Code (Text):
    1. hold_var = (hold_var * 10 + key)%100;
     
  3. Lohitha Wickramasinghe

    Thread Starter New Member

    Apr 20, 2017
    3
    0
    That worked! Thank you!!
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    I see some rudimentary code to provide some key debouncing, but I wonder what happens when you either press a key very quickly, or leave it down for several seconds.

    Also there are a lot of calculation just to save a memory position you don't really save anyway. I'd hold the keys in two unsigned char variables:

    Code (Text):
    1. unsigned char Digit_0 = 0;
    2. unsigned char Digit_1 = 0;
    3. int Digit_Value = 0;
    4. ...
    5.  
    6. if (key != 16)
    7. {
    8.     Digit_1 = Digit_0;
    9.     Digit_0 = key;
    10.     Digit_Value = Digit_1 * 10 + Digit_0;    // if you need to use this elsewhere
    11.     ...
    12.  
    13. }
    then pass Digit_0 or Digit_1 to the SegPat routine.
     
Loading...