1. Create a project
2. Core: add a new file usar_fputc.c (name whatever you want), add the file to the project Go
#include “stdio.h”
#include “stm32f1xx_hal.h”
extern UART_HandleTypeDef huart1;
uint8_t ch;
uint8_t ch_r;
//Rewrite this Function, redirect printf function to serial port
/*fputc*/
< span style="color: #0000ff">int fputc(int c, FILE * f)
{
ch=c;
HAL_UART_Transmit(&huart1,&ch,1,1000);//Send serial port span>
return c;
}
< span style="color: #339966">
//Redirecting the scanf function to the serial port means to accept the data sent by the serial port
/*fgetc*/
int fgetc(FILE * F)
{
HAL_UART_Receive (&huart1,&ch_r,1,0xffff);//receive
return ch_r;
}
3. Modify main.c file< /p>
#include “stdio.h” /*Add header file*/
Add test code in the main() function: printf(”
===function Printf function sends data===
“);< /span> //Test content
4. Open the serial port assistant to test the final result as shown in the figure: