#include "stdio.h"
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t*)&ch,1,HAL_MAX_DELAY);
return ch;
}
在main.c主函数体前插入以上代码
stm32cubeide采用的是GCC编译器
GCC编译器中通过重新定义_IO_putchar()函数可以重定向printf函数
printf函数要到\n才会输出字符串,所以
要注意
在printf字符串的结尾添加 \n,比如printf("hello,world\n")
或在printf后使用fflush(stdout)