尝试使用spi在LCD上打印实时浮点值 [英] Trying to print real time floating point values on an LCD using spi
问题描述
您好,
我使用sprintf格式化包含实时浮点值的字符串。然后,我将格式化的字符串作为参数传递给SPI数据传输函数,以写入LCD。我遇到了将实时浮点变量替换为我的字符串中的%.2f的问题。这是以下的一段代码
我尝试过:
Hello,
I am using sprintf to format a string that contains real time floating point values. I then pass the formated string as an argument to an SPI data trasnfer function to write onto an LCD. I am encountering an issue with the substitution of the real time float variables into the %.2f in my string. This is the following piece of code
What I have tried:
float Ref1,Iref1;
char buffer1[20];
char stringC[20];
sprintf(buffer1,"%.2f VDC %.2f A",Ref1,Iref1);
strcpy(stringC,buffer1);
在此之后,我将stringC作为参数传递并执行我的SPI传输。
我希望读取类似于lcd的内容,
20.20 VDC 10.08 A
而不是我最终得到这个,
2f VDC 2f A
取代永远不会发生!任何想法我可能做错了什么?
谢谢你,
Srini
After this, i pass stringC as an argument and do my SPI transfer.
I expect to read something like on the lcd,
20.20 VDC 10.08 A
instead i end up getting this,
2f VDC 2f A
The substitution never happens !! any idea what i could be doing incorrectly?
Thank you,
Srini
推荐答案
我自己尝试使用在线C编译器:
I tried it myself using an online C compiler:
int main()
{
float f = 20.20, g = 10.08;
char buff[100];
printf("%.2f %.2f\n", f, g);
sprintf(buff, "%.2f %.2f\n", f, g);
printf(buff);
return 0;
}
并且完全符合我的预期:
And got exactly what I expected:
20.20 10.08
20.20 10.08
检查编译器的特定文档和printf / sprintf格式字符串 - 它可能有一些奇怪之处。同时尝试:
Check the specific documentation for your compiler and the printf / sprintf format string - it may have some oddities in it. Also try:
sprintf(buff, "%0.2f %0.2f\n", f, g);
看看是否修复了它。
如果您使用SPI
那么你是可能使用微控制器。现在,在微控制器库上,可能会发生sprintf
实现并不是全功能的。检查编译器文档。
请注意,浮点值处理可能是一项繁重的任务,特别是对于低资源8
位和16
位MCU
s。通常可以使用正确的缩放整数运算。
If you are usingSPI
then you are probably using a microcontroller. Now, on microcontroller libraries, it could happen thatsprintf
implementations are not full-featured. Check you compiler documentation.
Please note, floating point values handling could be an heavy task, expecially for low resources8
bit and16
bitMCU
s. It is often feasible to use properly scaled integer arithmetic instead.
这篇关于尝试使用spi在LCD上打印实时浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!