STM32Cube实现printf串口打印

使用不同的工具创建项目工程添加的代码略有不同!!!

  • STM32CubeMX生成的Keil工程

usart.c

/* USER CODE BEGIN 1 */
int fputc(int ch, FILE *f) 
{ 
	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff); 
	return ch; 
}
/* USER CODE END 1 */
  • STM32CubeIDE创建的工程

usart.c

/* USER CODE BEGIN 1 */
#ifdef __GNUC__

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

PUTCHAR_PROTOTYPE
{
    HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
    return ch;
}

#endif
/* USER CODE END 1 */

发布了5 篇原创文章 · 获赞 3 · 访问量 562

猜你喜欢

转载自blog.csdn.net/zyjtx321/article/details/104049494
今日推荐