为什么重写printf函数没有用?


以前在网上找了无数方法去重写printf函数,但发现都没效果,今天偶然发现重写printf函数可以了,原因是以前没有勾选微库(Use MicroLlB)!

这里是使用stm32cubmx创建程序,重写了printf语句。

在main.h中添加头文件

#include <stdio.h>

在usart.c中加上以下程序

/* USER CODE BEGIN 1 */
#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;
}
/* USER CODE END 1 */

前面大同小异

但一定要勾选微库。 亲测有效!我验证了很多次。
image-20211214221002872

猜你喜欢

转载自blog.csdn.net/qq_45945548/article/details/121941336
今日推荐