GCC STM32 串口重定向
#include "stdio.h"
int _write(int fd, char *pBuffer, int size)
{
while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) == 0);
HAL_UART_Transmit(&huart1, (uint8_t *)pBuffer, size, 0xff);
return size;
}
可重定向,使用printf,但是无法输出float格式
参考他人博客修改makefile编译失败
#Makefile中添加
-u _printf_float
例:# libraries
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -u _printf_float -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
打印float
float转string然后printf。示例如下,不选择sprintf,因为它也没法用。
#include <stdio.h>
#include <stdlib.h>
int main() {
float num = 123.456;
char str[20];
gcvt(num, 6, str);
printf("The float number as a string: %s\n", str);
return 0;
}
gcvt
函数的第一个参数是要转换的浮点数,第二个参数是要保留的小数位数,第三个参数是结果字符串。只能暂时这样可以打印float数据。
[参考]
https://blog.csdn.net/for_dream1205/article/details/101556546
https://www.cnblogs.com/Luad/p/12635132.html