Added in the protues STM32F103R6 chip, add COMPIM serial port, LED display light, use the U5 write a serial port communication program to download the write good program to protues, clocks are set to 8 m, a serial port baud rate is set to 9600, started simulation, can receive a serial port to send data, but after send STM32 serial data, then the data sent again, only the first data is correct,
A, Protues configuration
Protues8.0,
Chip select STM32103R6, choose 8 m crystals, COMPIM choose 9600 baud rate, D1, flashing for sending data on D2 on the electric light, after receiving data,
Second, the program
1. The serial port interrupt program
The HTML code is as follows (sample) :
Void USART1_IRQHandler (void)//serial port 1 interrupt service routines
{
U8 Res;
# if SYSTEM_SUPPORT_OS//if SYSTEM_SUPPORT_OS is true, then you need to support OS.
OSIntEnter ();
# endif
If (USART_GetITStatus (USART1, USART_IT_RXNE)!=RESET)//receiving interrupt (the received data must be 0 x0d 0 x0a end)
{
Res=USART_ReceiveData (USART1);//read the received data
USART_RX_BUF [USART_RX_STA]=Res;
USART_RX_STA++;
If (USART_GetITStatus (USART1, USART_IT_RXNE)==RESET)
{
Bj=1; LED1=1; return;
}
Res=0;
}
# if SYSTEM_SUPPORT_OS//if SYSTEM_SUPPORT_OS is true, then you need to support OS.
OSIntExit ();
# endif
}
# endif
Judgment serial port has received signal, receiving data, when there is no sign to receive time out of a serial port interrupt, and bj=1 (mark position 1),
Note: according to the order modified atomic function library, USART_RX_STA 15 cannot setting, testing found the if (Res!=0 x0a) the judge cannot succeed,
2. The main function to receive send data
While (1)
{
If (bj==1)
{
Len=USART_RX_STA & amp; 0 x3fff;//get the received data length
Printf (" \ r \ n the message you send to: \ r \ n \ r \ n ");
For (t=0; t
USART_SendData (USART1, USART_RX_BUF [t]);//send data to the serial port 1
While (USART_GetFlagStatus (USART1, USART_FLAG_TC)!=SET);//wait for sending end
}
Printf (" \ r \ n \ r \ n ");//insert line breaks
Printf (" % c ", USART_RX_BUF [0]). 1/send/receive data
Printf (" \ r \ n \ r \ n ");//insert line breaks
Printf (" % c ", USART_RX_BUF [1])./send/receive second data
Printf (" \ r \ n \ r \ n ");//insert line breaks
Printf (" % c ", USART_RX_BUF [2])./send/receive a third data
Printf (" \ r \ n \ r \ n ");//insert line breaks
Printf (" % c ", USART_RX_BUF [3])./send/receive the fourth data
Printf (" \ r \ n \ r \ n ");//insert line breaks
Printf (" % d ", USART_RX_STA);//send received several data (counting from zero beginning, the actual data - 1)
Printf (" \ r \ n \ r \ n ");//insert line breaks
Bj=0;//send over, marked as zero
USART_RX_STA=0;//counter reset
} the else
{
Times++;
If (times %==0 5000)
{
Printf (" \ r \ n elite STM32 serial experimental development board \ r \ n ");
Printf (" the punctual atomic @ ALIENTEK \ r \ n \ r \ n ");
}
If (times %==0 200) printf (" please input data to the enter key to end \ n ");
If (30 times %==0) LED0=! LED0;//flashing LED, prompt system is running.
Delay_ms (10);
}
3. The serial data show the results
After receiving the data sent again, only the first data is right, the amount of data is correct, I don't know why, ask the punctual atomic service also don't know why,
O great god answers,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Copyright statement: this article to CSDN blogger (purple Chen fengling) of the original article, follow the CC BY 4.0 - SA the copyright agreement, reproduced and this statement, please attach the original source link
The original link: https://blog.csdn.net/weixin_55781693/article/details/114534737