此教程用于HAL库,CubeIDE MDK
实验前准备
使用CubeMx创建一个STM32的项目 将串口1打开
MDK
打开创建完成的项目
打开魔术棒 将UseMicroLIB打勾
添加重定向函数 并添加头文件#include “stdio.h”
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
这样就OK了 可以测试一下 我这里就不测试了。
CubeIDE
usart.c中添加
#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART3 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//------->根据串口更改
return ch;
}
打开设置project->properties
在使用的地方加上#include"stdio.h"就可以使用printf函数了
总结
HAL_UART_Transmit
是租塞的方式发送一个缓冲的数据,发送完成后或者超时后才返回。