stm32hal库使用printf函数输出
1Cube_mx配置
![](https://img-blog.csdnimg.cn/img_convert/20a0bb76f37031ba606dbb94ecfdd660.png)
![](https://img-blog.csdnimg.cn/img_convert/ad1f242bc86cdd2611f0280d20314348.png)
![](https://img-blog.csdnimg.cn/img_convert/fb9a3c612fe3e43e2d20f317b46aefa3.png)
2 打开usart.c
添加以下代码(&huart2改为自己的串口,我用的是串口2)
int fputc(int ch, FILE *fp)
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
3打开usart.h 添加c语言库的头文件#include<stdio.h>
![](https://img-blog.csdnimg.cn/img_convert/c2c284de760bfecfe1a397fefd2ee30e.png)
![](https://img-blog.csdnimg.cn/img_convert/369dd07873b20a3700f2df0670423598.png)
5 打开keil的魔术棒 点击 target 将将Use Microl LIB勾选上,不然程序会卡住
![](https://img-blog.csdnimg.cn/img_convert/30ef650aaeab9277f3ea6efe5e519a53.png)