简体   繁体   English

为什么“lcd_puts”不显示 LCD 16*2 上的变量值?

[英]Why "lcd_puts" doesn't display the value of a variable at LCD 16*2?

I tried to display a value at a LCD in proteus simulator by the following code which is written in Codevision:我试图通过以下用 Codevision 编写的代码在 proteus 模拟器的 LCD 上显示一个值:

sprintf(buffer,"Temp=%f\xdfC\n",temp);
lcd_puts(buffer);

but the value of temp doesn't appear in front of "temp=" at LCD!但是 temp 的值并没有出现在 LCD 的 "temp=" 前面!

在此处输入图像描述

the whole code is:整个代码是:

 #include <mega16.h>
 #include <delay.h>
 #include <stdio.h>
 #include <alcd.h> 

 char buffer[32];
 float temp=26.3457;  


void main(void){    
    lcd_init(16);

    while(1){

      lcd_clear();
      sprintf(buffer,"Temp=%f\xdfC\n",temp);
      lcd_puts(buffer);
      delay_ms(1500);
    }
}

need add this argument to compiler:需要将此参数添加到编译器:

-Wl,-u,vfprintf -lprintf_flt -lm

without this will be %f ignored没有这个将被%f忽略

more info is in avr-libc documentation更多信息在avr-libc 文档中

对于 CodeVision,您需要将Configure Project对话框中的(s)printf Features设置更改为float, width, precision

The \\x escape sequence is problematic, because it keeps reading the string for as long as there are valid digits. \\x转义序列是有问题的,因为只要有有效数字,它就会一直读取字符串。 In your case you told it to print the symbol table character equivalent to hex number 0xdfc which is not the intention.在您的情况下,您告诉它打印等效于十六进制数0xdfc的符号表字符,这不是本意。 If you are unlucky, this will corrupt the buffer somehow, which would lead to some random output.如果你不走运,这会以某种方式破坏缓冲区,这会导致一些随机输出。

Fix the code by ending the literal and start a new one just after it:通过结束文字来修复代码并在它之后开始一个新的:

sprintf(buffer, "Temp=%f\xdf" "C\n", temp);

A decent compiler would warn you against accessing the symbol table out-of-bounds.一个体面的编译器会警告您不要越界访问符号表。

For CodeVision you need to change the (s)printf Features settings in the Configure Project dialog to float, width, precision对于 CodeVision,您需要将 Configure Project 对话框中的 (s)printf Features 设置更改为 float、width、precision

IT WORKED FOR ME!它对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM