1. 在工程项目中加上函数fputc
例如:
库函数版
int fputc( int ch, FILE *f )
{
USART_SendData(USART2,(u8) ch );
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
return ch;
}
寄存器版
int fputc(int ch, FILE *f)
{
while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
USART2->DR = (u8)ch;
return ch;
}
Hal库版本
int fputc(int ch, FILE *f)
{
uint8_t temp[1]={ch};
HAL_UART_Transmit(&huart2, temp, 1, 2);
}
LL库版本
int fputc(int ch, FILE *f)
{
LL_USART_TransmitData8(USART2, ch);
/* Wait for TXE flag to be raised */
while (!LL_USART_IsActiveFlag_TXE(USART2))
{
}
return (ch);
}
2. 在使用printf函数的.C文件张工包含头文件stdio.h
#include<stdio.h>