天天看点

STM32--串口配置 重定向fputc()实现串口printf(0

重定义fputc()函数,在STM32串口中实现printf():

在usart.c文件的末尾添加如下代码

#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, 0xFFFF);
		return ch;
}
           

在程序中添加printf(),就可以在PC端串口助手中看到打印信息

(注:。。。换行使用\r\n。)

继续阅读