今天在调试程序时,发现了ARM-GCC下sprintf()的一个问题,它不支持浮点数。以前从来没碰到这个问题,而且认为这肯定不会有问题,折腾了半天,最后在ADL_User_Guide中找到了答案。ARM-GCC下的sprintf()确实不支持%f,文档中给出了相应的解决方法。
Important remark about GCC compiler:
When using GCC compiler, due to internal standard C library architecture, it is strongly not recommended to use the "%f" mode in the wm_sprintf function in order to convert a float variable to a string. This leads to an ARM exception (product reset).
A way around for this conversion is:
float MyFloat; // float to display
ascii MyString [ 100 ]; // destination string
s16 d,f;
d = (s16) MyFloat * 1000; // Decimal precision: 3 digits
f = ( MyFLoat * 1000 ) - d; // Decimal precision: 3 digits
wm_sprintf ( MyString, "%d.%03d", (s16)MyFloat, f ); // Decimal precision: 3 digits
实践证明,经验主义要不得,看文档很重要,而且要看仔细。