[英]Print floating point number using printf() at ARM Cortex M0 uart console
I configured Atmel's ARM Cortex M0's UART for printing strings and integers at the console using std C function printf() in Atmel studio 7.我在 Atmel Studio 7 中使用 std C 函数 printf() 配置了 Atmel 的 ARM Cortex M0 的 UART,以便在控制台上打印字符串和整数。
Case I案例一
I am trying to make printf() type of function print floating point values and for that I followed following suggestions to do so:我正在尝试使 printf() 类型的函数打印浮点值,为此我遵循了以下建议:
arm-none-eabi-gcc : Printing float number using printf arm-none-eabi-gcc :使用 printf 打印浮点数
and later on I edited/added the linker flags following texts separately at different time :后来我在不同的时间分别编辑/添加了以下文本的链接器标志:
-lc -u _printf_float
-lc -lrdimon -u _printf_float
Case II案例二
Though I could not understand everything they said but I followed some of the suggestions to edit makefile from this forum too.虽然我无法理解他们所说的一切,但我也遵循了一些建议来编辑来自该论坛的 makefile。
Printf/Sprintf for float point variables not working 浮点变量的 Printf/Sprintf 不起作用
and added following text inside the makefile并在 makefile 中添加了以下文本
ldflags-gnu-y += -lc -u _printf_float
Makefile Path (Atmel Studio 7, using ASF) : ../src/ASF/sam0/utils/make/Makefile.sam.in Makefile 路径(Atmel Studio 7,使用 ASF): ../src/ ASF/ sam0/utils/make/Makefile.sam.in
Now in the main.c I used printf() for printing floating point number as :现在在 main.c 中,我使用 printf() 将浮点数打印为:
float a = 345.65412;
char buffr[20];
/* --- Print Float using printf only --- */
printf("Float Number 1 : %f\r\n", a);
/* --- Print Float using sprintf ---*/
sprintf(buffr, "Float Number ( Using Sprintf) : %3.3f\r\n", a);
printf(buffr);
Output on a UART console app.: UART 控制台应用程序上的输出。:
Case I :案例一:
Float Number 1 : 2.000000
Float Number ( Using Sprintf) : -0.000
Case II :案例二:
Float Number 1 :
Float Number ( Using Sprintf) :
Does anyone know configuring linker to make printf(), sprintf() or vprintf() work for printing floating point number on the console for ARM Cortex M0 (SAM B 11) in Atmel Studio 7 ?有谁知道配置链接器使 printf()、sprintf() 或 vprintf() 工作在 Atmel Studio 7 的 ARM Cortex M0 (SAM B 11) 控制台上打印浮点数?
You buffer is too small for your data您的缓冲区对于您的数据来说太小了
char buffr[20];
have to be at least (considering 0.0
as format output)必须至少(考虑
0.0
作为格式输出)
char buffr[38];
The small buffer causes a "write out of bounds" of the array.小缓冲区会导致数组“越界写入”。 It means you have a Undefined Behavior and
a
variable is polluted.这意味着你有一个未定义的行为并且
a
变量被污染了。
sprintf is different than printf
. sprintf与
printf
不同。 It copy all chars to the output buffer and you must grant space for text and for all chars that will be added by format specifiers.它将所有字符复制到输出缓冲区,您必须为文本和将由格式说明符添加的所有字符授予空间。
In your specific case:在您的具体情况下:
Float Number ( Using Sprintf) :
--> takes 32 chars Float Number ( Using Sprintf) :
--> 需要 32 个字符%3.3f
--> takes 7 chars due to the real output of 345.654
%3.3f
--> 由于345.654
的实际输出,需要 7 个字符\\r\\n
--> takes 2 chars \\r\\n
--> 需要 2 个字符'\\0'
'\\0'
需要最后一个字节That menas your buffer have to be:那意味着您的缓冲区必须是:
char buffr[42];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.