定义一个串口打印函数
单片机软件开发调试时,经常用到printf函数打印程序运行状态信息,经常需要修改硬件串口号和打开或者屏蔽打印测试代码行,通过下面这种方式快速配置:
调试时需要输出打印信息,就将头文件usart.h里的DEBUG定义为1(define DEBUG 1),不需要就定义为0;
#define DEBUG 0//0:关闭串口调试功能 1:打开串口调试功能
修改串口号直接宏定义printf使用的串口号
#define DEBUG_UART USART2//将打印调试信息串口号定义成串口2
#include "usart.h"
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((DEBUG_UART->SR&0X40)==0);//循环发送,直到发送完毕
DEBUG_UART->DR = (u8) ch;
return ch;
}
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "sys.h"
/*IF DEBUG*/
#define DEBUG 0
#define DEBUG_UART USART2
#if DEBUG == 1
#define MODBUS_DEBUG(fmt, args...) \
fprintf(stderr, " MODBUS_DEBUG(%s:%d): \t" fmt, __func__, __LINE__, ##args)
#elif DEBUG == 0
#define MODBUS_DEBUG(fmt, args...) \
do { \
} while (0)
#endif
#endif
#include "usart.h"
int main(void)
{
while(1)
{
MODBUS_DEBUG("测试\r\n");
}
}
有帮助就点个赞吧,感谢支持!