/* USER CODE BEGIN PM */#ifdef __GNUC__/* With GCC, small printf(option LD Linker->Libraries->Small printfset to 'Yes') calls __io_putchar() */#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else#define PUTCHAR_PROTOTYPE intfputc(int ch, FILE *f)#endif/* __GNUC__ *//* USER CODE END PM */
然后在用户代码区添加下面代码:
/* USER CODE BEGIN 4 *//* @brief Retargets the C library printffunction to the USART.*/PUTCHAR_PROTOTYPE{/* Place yourimplementation of fputc here */HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);return ch;}/* USER CODE END 4 */