How to store a number in decimal format in avr

陌路散爱 提交于 2019-12-18 09:45:26

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/18506198/how-to-store-a-number-in-decimal-format-in-avr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!