将以下代码放到main.c文件中,之后就可以使用printf函数进行串口输出了
#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;
}
需要注意的是,利用这个操作重定向printf函数,每次使用printf的时候,打印的数据末尾都要添加\n才能实现。例如
printf("123123\n");才能看到串口打印出123123