重映射Printf功能
在剛剛的例子中,需要把字符裝填到數組中,再通過數組來發送數據,比較麻煩,能否直接打印字符串?可以通過重映射C語言中的printf函數來實現。
建立一個UART.c文件,添加到工程中。輸入以下代碼:
#include "stdio.h"
#include "main.h"
#ifdef __GNUC_
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
//重映射printf的功能
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}