使用任意huart重写
int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff); return ch; } /** * 函数功能: 重定向c库函数getchar,scanf到DEBUG_USARTx * 输入参数: 无 * 返 回 值: 无 * 说 明:无 */ int fgetc(FILE *f) { uint8_t ch = 0; HAL_UART_Receive(&huart2, &ch, 1, 0xffff); return ch; }需要打开micoLib
#include <stdio.h> #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int _io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__*/ /****************************************************************** *@brief Retargets the C library printf function to the USART. *@param None *@retval None ******************************************************************/ PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart3, (uint8_t *)&ch,1,0xFFFF); return ch; }如果不使用MicoLib则需要关闭半主机模式,详细见Keil MDK