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

#### Lohitha Wickramasinghe

Joined Apr 20, 2017
3
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:
while(1)
{
if(key != 16) // If a key is pressed
{
hold_var = key ; // holds (remember) the value of the key
// this sequence shows the value of the key that is pressed, in 2 digits
for (i=0;i<20;i++){
PTP = 0x0f;
PTB = SegPat[hold_var/10];
PTP &= ~0x04;
delayby1ms(10);
PTP = 0x0f;
PTB = SegPat[hold_var%10];
PTP &= ~0x08;
delayby1ms(10);
}
}

// If a new key is not pressed (DEFAULT)
if(key = 16)// this sequence keeps showing the same number if a new key is not pressed
{
PTP = 0x0f;
PTB = SegPat[hold_var/10];
PTP &= ~0x04;
delayby1ms(10);
PTP = 0x0f;
PTB = SegPat[hold_var%10];
PTP &= ~0x08;
delayby1ms(10);
}
}

#### MrChips

Joined Oct 2, 2009
24,173
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:
hold_var = (hold_var * 10 + key)%100;

#### Lohitha Wickramasinghe

Joined Apr 20, 2017
3
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:
hold_var = (hold_var * 10 + key)%100;
That worked! Thank you!!

#### ErnieM

Joined Apr 24, 2011
8,177
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:
unsigned char Digit_0 = 0;
unsigned char Digit_1 = 0;
int Digit_Value = 0;
...

if (key != 16)
{
Digit_1 = Digit_0;
Digit_0 = key;
Digit_Value = Digit_1 * 10 + Digit_0;    // if you need to use this elsewhere
...

}
then pass Digit_0 or Digit_1 to the SegPat routine.