C_YANG_HEN 发表于 2018-2-27 13:23:38

STM32 无法打印%f


在stm32中执行如下代码
float a = 0.1;
printf("%f\n",a);

输出为 %f ,就是“%f”这两个字符
如果是%d就正常
怎么解决?

C_YANG_HEN 发表于 2018-2-28 09:43:05

感谢大家的回复。问题解决了。在IAR设置里,有对 printf 的设置,默认是不支持浮点数的。修改选项后便可。如下:

Angel_YY 发表于 2018-2-27 14:40:31

用的什么编译器?开发环境要说清楚些才好解决。
可以试试"%lf"

wudianjun2001 发表于 2018-2-27 15:23:39

进入函数里面看看,是不是做特殊处理了

C_YANG_HEN 发表于 2018-2-27 15:55:28

Angel_YY 发表于 2018-2-27 14:40
用的什么编译器?开发环境要说清楚些才好解决。
可以试试"%lf"

用的是 IAR。
试了用%lf,效果一样

C_YANG_HEN 发表于 2018-2-27 15:56:35

wudianjun2001 发表于 2018-2-27 15:23
进入函数里面看看,是不是做特殊处理了

printf函数就是重定向到串口输出而已,打印定点数,十六进制,字符,字符串都没问题。

wenyangzeng 发表于 2018-2-27 16:25:50

printf("%4f",a);试看看,
还有检查一下引号是否键入全角了。

C_YANG_HEN 发表于 2018-2-27 18:03:05

wenyangzeng 发表于 2018-2-27 16:25
printf("%4f",a);试看看,
还有检查一下引号是否键入全角了。

printf("a = %4f\n",a);
的结果是 a =   %f, 等号和百分号之间有三个空格。

如果符号是全角的,是不能编译通过的。

maxtch 发表于 2018-2-27 18:34:31

看来 IAR 的 printf 把打印浮点给精简掉了。这块代码的确是很占地方,我平时用 GCC + newlib 一般也是用去掉这个功能的 newlib-nano。你看看有没有那个编译或连接选项可以解决这个问题的,或者是换成 GCC + 完整 newlib 的组合。

pythonworld 发表于 2018-2-27 18:37:47

打印浮点数比较耗费内存,有些编译器需要加option,具体看看编译器的帮助。

nyszx 发表于 2018-2-27 21:14:19

应该是IAR编译器问题,参见IAR官方说明:
Technical Note 85413
Targets:
ARM
Component:
General
Updated:
2015/9/11 14:25
Introduction

In some cases, printf of floating point (%f) does not seem to work.
Solution

The IAR C/C++ Compiler for ARM requires the Stack Pointer to be aligned at 8 bytes. 8-byte alignment is required by AEABI (and some instructions generated by the compiler for some cores).

The default cstartup code automatically initializes the ARM core stack(s) to an 8-byte aligned address. Make sure that your application and/or RTOS also does this.
Background

According to the MQX release notes, stack alignment is not ensured in MQX 3.7. The behavior is corrected in MQX 3.8. This problem has also been observed with other RTOSes.


All product names are trademarks or registered trademarks of their respective owners.
页: [1] 2
查看完整版本: STM32 无法打印%f