如何在AVR中以十进制格式存储数字 [英] How to store a number in decimal format in avr
问题描述
我正在尝试使用ATMega8中的键盘输入十进制值 到现在为止,我只能输入整数值 代码在下面给出
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;
}
使用上面的代码,我可以在寄存器a中存储一个整数
Using the above code I am able to store an integer in register a
case (0xe7): keyPressed=".";
以上是."的键控代码.现在,我要在按"之后.在小键盘上,它存储了所有按整数a按下的键.
Above case is the keyCode for "." Now I want after pressing "." on keypad it stores all the keys pressed in the integer a
推荐答案
其基本的中学数学,您需要以10的幂表示数字.
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
是十进制数字-在上面的示例(138.25)中,pow 2为1,而5的pow
为2.
因此,您需要维护pow
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
这篇关于如何在AVR中以十进制格式存储数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!