Question

I am trying to enter the decimal values using a keypad in ATMega8 Till now I have been able to enter only the integer values The code is given below

switch (keyCode)            
{

    case (0xee): 
            keyPressed="1";
            b=1;
            a=a*10+b; 
            break;
    case (0xed): 
            keyPressed="4";
            b=4;
            a=a*10+b; 
            break;
    case (0xeb): k
            keyPressed="7";
            b=7;
            a=a*10+b; 
            break;

    case (0xde): 
            keyPressed="2";
            b=2;
            a=a*10+b; 
            break;
    case (0xdd): 
            keyPressed="5";
            b=5;
            a=a*10+b; 
            break;
    case (0xdb): 
            keyPressed="8";
            b=8;
            a=a*10+b; 
            break;
    case (0xd7): 
            keyPressed="0";
            b=0;
            a=a*10+b; 
            break;
    case (0xbe): 
            keyPressed="3";
            b=3;
            a=a*10+b; 
            break;
    case (0xbd): 
            keyPressed="6";
            b=6;
            a=a*10+b; 
            break;
    case (0xbb): 
            keyPressed="9";
            b=9;
            a=a*10+b;
}       

Using the above code I am able to store an integer in register a

case (0xe7): keyPressed=".";

Above case is the keyCode for "." Now I want after pressing "." on keypad it stores all the keys pressed in the integer a

Was it helpful?

Solution

Its basic high school math, you need represent numbers in powers of 10.

Example - 
138.25 = (1 * 10^2) + (3 * 10^1) + (8 * 10^0) +         // integer part
         (2 * 10^-1) + (5 * 10^-2)                      // Float part

I will not give you complete code, but you can use this idea

if (decimal)
{
    a = a + b / (10 ^ pow);
}
else
{
    a = a * 10 + b
}

pow is the decimal digit - In above example (138.25) pow 2 is 1, and pow for 5 is 2. So you need to maintain counter for pow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top