printf函数主要用于数据输出和调试信息输出
在DEBUG需要调试数据时使能PRINTF_ENABLE 1U,RELEASE版本则失能PRINTF_ENABLE 0U,编译器高等级优化选项会将宏定义((void)0)优化,不会影响程序效率
Printf.h
#ifndef __PRINTF_H
#define __PRINTF_H
#define PRINTF_ENABLE 1U //使能串口输出信息
#if (PRINTF_ENABLE != 0U)
#define PRINTF(fmt,arg...) printf(fmt,##arg)
/** EEPROM辅助信息显示 */
#define EEPROM_INFO(fmt,arg...) printf("<<-EEPROM-INFO->> "fmt"\n",##arg)
#define EEPROM_ERROR(fmt,arg...) printf("<<-EEPROM-ERROR->> "fmt"\n",##arg)
#else
#define PRINTF(fmt,arg...) ((void)0)
#define EEPROM_INFO(fmt,arg...) ((void)0)
#define EEPROM_ERROR(fmt,arg...) ((void)0)
#endif
Printf.c
/**
* @file printf.c
* @brief printf驱动程序
* 重定义了stdio模块中的printf函数用到的“int fputc(int ch, FILE *f)”,该函数名称 和参数不可随意更改。
* 该文件无需头文件
* @author 吴文博
* @version
* @date 2019-04-26
* @note 勾选MicroLib
*/
#include "stm32f4xx_hal.h"
#include "stdio.h"
/** 定义printf使用的串口 */
extern UART_HandleTypeDef huart1;
/**
* @brief 仅供printf函数调用的函数
* @param ch 字符
* @param f 文件指针
*/
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}