xiake017 发表于 2010-6-3 14:51:01

请教ICCAVR中sprintf转换为字符串的问题

#include <iom8v.h>
#include <stdio.h>
#define uchar unsigned char
#define uintunsigned int
#include "USART.c"


void main()
{       

       float i=32.2;
       uchar num;
       uart_init(9600);//初始化串口
       while(1)
       {
        // send_str("asdfa\r");//测试串口
       sprintf(num,"%f",i);//格式化成为字符串
       send_str(num);//串口打印
       }
}
我这是一个简单的例程,就是把浮点数转换为字符串,按照标准格式写的,但是显示不出来,我在KEIL里面做测试能够转换出来,为什么在ICCAVR编译环境里面不行,请高手指教,

biansf2001 发表于 2010-6-3 14:53:51

ICCAVR里好像sprintf是空函数

xiake017 发表于 2010-6-3 15:02:10

不是空函数,转换为无符号字符型都可以,就是转换浮点型的不行

xiake017 发表于 2010-6-8 12:43:54

怎么,没有人遇到过着样的问题吗·?

hecklerkwan 发表于 2011-12-21 10:58:39

回复【楼主位】xiake017
-----------------------------------------------------------------------

unsigned long DWORD = 0xffffffff;

我用sprintf(buffer, "%lx", DWORD);

居然BUFFER里头的字符串数据是-1;搞到我焦头烂额,莫名其妙啊。

然后参考help,貌似写得很马虎喔。

...
The flags are:
...
The conversion characters are as follows. If an 'l' (letter el) appears before an integer conversion character, then the argument is taken as a long integer.

...

但是输出的数据确实不太准确。

hecklerkwan 发表于 2011-12-21 11:15:07

回复【楼主位】xiake017
#include &lt;iom8v.h&gt;
#include &lt;stdio.h&gt;
#define uchar unsigned char
#define uintunsigned int
#include "usart.c"
void main()
{   
float i=32.2;
uchar num;
uart_init(9600);//初始化串口
while(1)
{
// send_str("asdfa\r");//测试串口
sprintf(num,"%f",i);//格式化成为字符串
send_str(num);//串口打印
}
}
我这是一个简单的例程,就是把浮点数转换为字符串,按照标准格式写的,但是显示不出来,我在keil里面做测试能够转换出来,为什么在iccavr编译环境里面不行,请高手指教,
-----------------------------------------------------------------------


printf is supplied in three versions, depending on your code size and feature requirements (the more features, the higher the code size):

http://cache.amobbs.com/bbs_upload782111/files_49/ourdev_706351T42WHS.jpg
(原文件名:允许sprintf()转换32位数字串.jpg)

藏得很坑爹。。

canaan 发表于 2012-6-2 23:56:31

6楼好样的,佩服

luozyou 发表于 2014-9-14 21:53:47

我最近也在写类似的程序,坑爹得很试了几个函数都不行 ICCAVR 竟然没有GCVT
页: [1]
查看完整版本: 请教ICCAVR中sprintf转换为字符串的问题