gpt4 book ai didi

c - 如何在LCD上写入浮点值?

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:27 25 4
gpt4 key购买 nike

我正在计算体重比​​,它是 float 值,如 862.145、56.21、664.008这个值我想放在 16x2 液晶显示器上我在 MPLAB IDE v8.83 中使用 PIC18F4550

这是我的代码

float BMR_Male(unsigned char Age,unsigned int Weight,unsigned int Height)
{
float BmrForMale = 0,term1=0,term2=0,term3=0;
int intValue = 0,anotherIntValue = 0;
float diffValue = 0;
char str1[20],str2[20];
unsigned age1 = Age;
unsigned int Weight1 = Weight;
unsigned int Height1 = Height;
term1 = 13.75 * Weight;
intValue = (int)term1;
diffValue = term1 - (float)intValue;
anotherIntValue = (int)(diffValue * 1000.0);
sprintf(str1, (const far rom char*)"%s", intValue);
sprintf(str2, (const far rom char*)"%s", anotherIntValue);
for(z=0;z!='\0';z++)
{
LCD_write(str1[z]);
myMsDelay(5);
}
LCD_write('.');
myMsDelay(5);
for(z=0;z!='\0';z++)
{
LCD_write(str2[z]);
myMsDelay(5);
}
// term2 = 5 + Height1;
// term3 = 6.76 * age1;
// BmrForMale = (term1)+(term2)-(term3)+66;
return BmrForMale ;
}

这里我只显示了一个词...为简单起见,对 term2 和 term3 进行了注释..

这里我只得到'.'在液晶显示器上……不是 str1 和 str2

最佳答案

在这种情况下绝对没有理由使用 float。除了在代码中写13.75,你还可以写1375。尝试将计算限制为 8 位和 16 位整数。当所有的计算都完成后,在任何你想要的地方打印小数逗号。

不同之处在于,如果您使用 float ,您的 PIC18 编译器将不得不拒绝一个极其缓慢且耗费内存的程序。你不是在给 PC 编程!您没有 FPU,而我们正在谈论的可能是世界上仍在生产的最慢的 CPU...

如果您的程序正在执行三角函数、信号处理、高等数学等操作,您需要使用 float 。在这种情况下,您选择了完全错误的 MCU。

同样,如果您使用 printf 函数族,您的程序也会变得极其缓慢和消耗内存。 sprintf 可能会杀死您的 PIC 中可用的每一个资源。在这种情况下,只需推出您自己的整数到字符串转换例程。这是微不足道的,SO 上有大量可用的代码。

鉴于您只有几 kb 的可用 RAM,您当前的代码很可能会发生堆栈溢出,并且可能仅通过此函数就消耗了所有可用的 RAM。

关于c - 如何在LCD上写入浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30724522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com