背景:
使用STM32CubeIDE可以很方便的生产驱动代码,工程可直接编译下载,默认配置uart1后不能使用标准输出printf进行打印,需要自行添加支持。
方法:
使用STM32CubeIDE选择uart1,配置好后生成工程代码 将如下代码添加到usart.c(MX中需要勾选每个外设生成独立文件功能)文件中
#include "stdio.h"
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif
main函数中调用printf函数进行打印测试(此时不能用于打印浮点数)
浮点数支持:
依次打开下面配置页面:
Project Properties C/C++ Build > Settings Tool Settings MCU Settings
勾选下面选项
Use float with printf from newlib-nano (-u printf_float) Use float with scanf from newlib-nano (-u scanf_float) 输出只勾选第一项即可 输入勾选第二项