串口设置
代码配置
#include "dr_debug.h"
//重定向c库函数printf到串口,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{
#if DEBUG_ENABLE
while ((DEBUG_USARTx->SR & 0X40) == 0)
{
}
DEBUG_USARTx->DR = (uint8_t)ch;
return ch;
#endif
}
///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
#if DEBUG_ENABLE
while (((DEBUG_USARTx->SR) & 0x20) == 0)
{
}
return (uint8_t)DEBUG_USARTx->DR;
#endif
}
int DEBUG_Info(const char *format, ...)
{
int retval=0;
#if DEBUG_ENABLE
va_list ap;
va_start(ap, format);
printf("DEBUG>");
retval = vprintf(format, ap);
va_end(ap);
#endif
return retval;
}
头文件
#ifndef _DR_DEBUG_H_
#define _DR_DEBUG_H_
#include <stdio.h>
#include <stdarg.h>
#include "main.h"
#include "stm32f1xx_hal.h"
#define DEBUG_ENABLE 1
#define DEBUG_USARTx USART1
int DEBUG_Info(const char *format, ...);
#endif // _DR_DEBUG_H_