Example 1:
Â
void USART1_IRQHandler (u8 GetData) {u8 BackData; if (! USART_GetITStatus (USART1, USART_IT_RXNE) = RESET) // interrupt is generated {USART_ClearITPendingBit (USART1, USART_IT_RXNE); // Clear interrupt flag GetData = UART1_GetByte (BackData);. // also Line GetData=USART1->DR; USART1_SendByte(GetData); //Send data GPIO_SetBits(GPIOE, GPIO_Pin_8); //LED flashes, receive successfully sent delay(1000); GPIO_ResetBits(GPIOE, GPIO_Pin_8 ); } }
This is the most basic. After the data is received, it is sent out again. The receiving and sending are executed in the interrupt function. There is no other processing in the main function.
Advantages: Simple, suitable for very small data transfers.
Disadvantages: There is no buffer area, and there is no judgment on the correctness of the data. A slightly larger amount of data may result in data loss.
Example 2:
Void USART2_IRQHandler(){if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //Interrupt generation {USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Clear interrupt flag Uart2_Buffer[Uart2_Rx_Num] = USART_ReceiveData(USART2);Uart2_Rx_Num++;}
If((Uart2_Buffer[0] == 0x5A)&&(Uart2_Buffer[Uart2_Rx_Num-1] == 0xA5))) / / Determine whether the last received data is the set value, determine the data correctness Uart2_Sta = 1;
If(USART_GetFlagStatus(USART2,USART_FLAG_ORE) == SET) //Overflow{USART_ClearFlag(USART2, USART_FLAG_ORE); //Read SRUSART_ReceiveData(USART2); //Read DR}}
If( Uart2_Sta ){for(Uart2_Tx_Num=0;Uart2_Tx_Num USART2_SendByte(Uart2_Buffer[Uart2_Tx_Num]); //Transmit data Uart2_Rx_Num = 0; //Initialize Uart2_Tx_Num = 0; Uart2_Sta = 0;}
This is the way to receive the data header and data tail, the number of data headers and tails can be increased, here only for debugging purposes. The interrupt function is used to receive the data and determine the head and tail of the data. The second function is executed in the main function according to the query mode.
Advantages: Simpler, using buffer area reception, there is a certain improvement in improving the correct line of data.
Disadvantages: If the first data reception error, the initialization state is not returned, the operation must be reset.
Example three:
void USART2_IRQHandler () {if (! USART_GetITStatus (USART2, USART_IT_RXNE) = RESET) // interrupt {USART_ClearITPendingBit (USART2, USART_IT_RXNE); // Clear interrupt flag Uart2_Buffer [Uart2_Rx] = USART_ReceiveData (USART2 );. Uart2_Rx ++; Uart2_Rx & = 0x3F; // determines whether the count to the maximum} if (USART_GetFlagStatus (USART2, USART_FLAG_ORE ) == SET) // overflow {USART_ClearFlag (USART2, USART_FLAG_ORE); // read SR USART_ReceiveData (USART2); // read DR}} if ( Uart2_Tx != Uart2_Rx ) { USART2_SendByte(Uart2_Buffer[Uart2_Tx]); //Send data Uart2_Tx++; Uart2_Tx &= 0x3F; //Determine whether to count to the maximum }
The data is received by the FIFO method. From 0x3F, the maximum receiving amount is 64, which is variable. The interrupt function is only responsible for receiving, and the other function is executed in the main function and sent in FIFO mode.
Advantages: Both sending and receiving are free, and the interrupt takes less time, which is beneficial for the MCU to handle other.
Disadvantages: There is no judgment on the correctness of the data, and all are received.
Example four:
void USART2_IRQHandler () {if (! USART_GetITStatus (USART2, USART_IT_RXNE) = RESET) // interrupt {USART_ClearITPendingBit (USART2, USART_IT_RXNE); // Clear interrupt flag Uart2_Buffer [Uart2_Rx] = USART_ReceiveData (USART2 ); Uart2_Rx ++; Uart2_Rx & = 0xFF ;} if (Uart2_Buffer [Uart2_Rx- 1] == 0x5A) // head Uart2_Tx = Uart2_Rx-1; if ( (Uart2_Buffer [Uart2_Tx] == 0x5A) && (Uart2_Buffer [Uart2_Rx-1] == 0xA5)) // detection In the case of the end, the tail is detected { Uart2_Len = Uart2_Rx-1- Uart2_Tx; // length Uart2_Sta=1; //flag bit } if(USART_GetFlagStatus(USART2, USART_FLAG_ORE) == SET) //overflow { USART_ClearFlag(USART2, USART_FLAG_ORE) ; // read SR USART_ReceiveData (USART2); // read DR}} if (Uart2_Sta) { for (tx2 = 0; tx2 <= Uart2_Len; tx2 ++, Uart2_Tx ++) USART2_SendByte (Uart2_Buffer [Uart2_Tx]); // send data Uart2_Rx = 0; //Initialize Uart2_Tx = 0; Uart2_Sta = 0; }
The data is received in the form of a data packet, stored in the buffer area after reception, and the "packet" and validity of the data are judged by judging the data header and the data tail (variable), and the interrupt function is used to receive the data and determine the head and tail and the data packet. Length, another function is executed in the main function, responsible for sending the segment data.
Advantages: Suitable for package transmission, stability and reliability are guaranteed, can be sent at will, and automatically select valid data.
Disadvantages: The length of the buffer data should be set according to the length of the "package". If there is no head and tail after receiving multiple times, the data with the head and tail will just cross the front and last positions of the buffer, which may result in the loss of this data, but this This situation is almost impossible.
Car Super Tweeter,Audio Car Tweeter,Car Tweeter Speakers,Audio Multimedia Driver
NINGBO BOILINGSOUND ELECTRONICS CO.,LTD , https://www.tweeterspeaker.com