stm32f103 serial communication

When writing serial communication, I didn’t pay attention at first, and it was written as

void Send_OneByte(char Data)
{
	USART_SendData(USART1, Data);
	while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET ); //TXE空了就为1,非空为0
}

void Send_String(char *str)
{
	int8_t i=0;
	USART_ClearFlag(USART1,USART_FLAG_TC);
	do
	{
	  USART_SendData( USART1,*(str+i));
		i++;
	}while(*(str+i)!='\0');
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);   //TC完成就为1
}

In this way, it turns out that the received data is always only the last bit. I found out that the reason may be that the data in the sending data register has not been transferred to the sending shift register. At this time, the next data is coming. , So the previous data is overwritten. After reading some answers, the flag bit USART_FLAG_TXE means that there is no data in the send data register, it is 1, and there is data is 0.
USART_FLAG_TC means that all data has been sent, and there is no data in the sending shift register. It is 1.
Change the code to:

void Send_OneByte(char Data)
{
	USART_SendData(USART1, Data);
	while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET ); //TXE空了就为1,非空为0
}

void Send_String(char *str)
{
	int8_t i=0;
	USART_ClearFlag(USART1,USART_FLAG_TC);
	do
	{
	  Send_OneByte(*(str+i));
		i++;
	}while(*(str+i)!='\0');
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);   //TC完成就为1
}

Guess you like

Origin blog.csdn.net/qq_40831436/article/details/98882455