在STM32CUBEMX生成的串口通讯代码里实现printf输出

/* USER CODE BEGIN PV */

#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;
}

/* USER CODE END PV */

在main.c文件中,这个位置插入这段代码。用途是将

 HAL_UART_Transmit(&huart1,(uint8_t *)&ch, 1, 0xFFFF);

替换掉原本的这两个输出流函数

int __io_putchar(int ch)
int fputc(int ch, FILE *f)

如果开启了中断或者DMA,可使用下面另外两个函数进行替换即可

HAL_UART_Transmit(&huart1,(uint8_t *)&ch, 1, 0xFFFF);
HAL_UART_Transmit_IT(&huart1,(uint8_t *)&ch, 1, 0xFFFF);
HAL_UART_Transmit_DMA(&huart1,(uint8_t *)&ch, 1, 0xFFFF);

 

你可能感兴趣的:(STM32)