简体   繁体   中英

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:

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

but the value of temp doesn't appear in front of "temp=" at LCD!

在此处输入图像描述

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

more info is in avr-libc documentation

对于 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. In your case you told it to print the symbol table character equivalent to hex number 0xdfc which is not the intention. 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

IT WORKED FOR ME!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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