尝试使用spi在LCD上打印实时浮点值 [英] Trying to print real time floating point values on an LCD using spi

查看:86
本文介绍了尝试使用spi在LCD上打印实时浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我使用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 using SPI then you are probably using a microcontroller. Now, on microcontroller libraries, it could happen that sprintf implementations are not full-featured. Check you compiler documentation.
Please note, floating point values handling could be an heavy task, expecially for low resources 8 bit and 16 bit MCUs. It is often feasible to use properly scaled integer arithmetic instead.


这篇关于尝试使用spi在LCD上打印实时浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆