STM32CubeIde 实现printf打印输出,在IDE生成的程序的main中的/* USER CODE BEGIN 4 /和/ USER CODE END 4 */之间放下面代码:
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#define GETCHAR_PROTOTYPE int __io_getchar(FILE *f)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);return ch;
}
GETCHAR_PROTOTYPE
{uint8_t ch = 0;HAL_UART_Receive(&huart1,(uint8_t *)&ch, 1, 0xFFFF);if (ch == '\r'){__io_putchar('\r');ch = '\n';}return __io_putchar(ch);
}
然后在菜单-》项目-》属性菜单改一下:
就可以实现printf功能了,调试程序还得用printf比较方便。