[While(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET) in STM32-Jundian Atomic Serial Port Experiment; statement position adjustment]

	while(1)
	{ 
		if(USART_RX_STA&0x8000)  //如果接收结束
		{					   
			len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
			printf("\r\n您发送的消息为:\r\n");
			for(t=0;t<len;t++)
			{
				USART_SendData(USART1, USART_RX_BUF[t]);               //向串口1发送数据                       
				while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET); //等待发送结束,这句必须有
			} 
			printf("\r\n\r\n");//插入换行
			USART_RX_STA=0;//传输完毕一串数据后 清零,不清零,串口就会一直重复向电脑发送数据
		}else
		{
			times++;
			if(times%200==0)
			{
				printf("请输入数据,以回车键结束\r\n");  
			    if(times%200==0)LED1=!LED1;//闪烁LED,提示系统正在运行.
			}
			delay_ms(10);  //每一个times+1后,延迟一下,要不然打印速度很快,延迟了200个10ms
		}
	}

Use the redirected printf function to send the statement "\r\nThe message you sent is:\r\n" to the host computer.

The "\r\n" at the end of the statement in the result displayed by the statement has no effect, that is, the "backlight" is displayed after the cursor moves to the next line.

Modification method: The redirected printf() function essentially sends data to the host computer through USART_SendData()

The reason for this phenomenon is that the sending of "\r\n" in the printf statement is not completed, and the register is overwritten by the data to be sent later. The solution is to change while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET) ;

Move to the front, first determine whether the previous data to be sent has been sent, and then call

USART_SendData(USART1, USART_RX_BUF[t]);

for(t=0;t<len;t++)
			{
				while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
                USART_SendData(USART1, USART_RX_BUF[t]);         //向串口1发送数据	
			}

In this way, there will be no problems, and line wrap display 12345 can be realized.

Guess you like

Origin blog.csdn.net/ggbb_4/article/details/129921998