This routine sends data to STM32 through the PC's serial port debugging assistant, and after receiving the data, sends the received data back to the PC.
Example 1:
void USART1_IRQHandler(u8 GetData)
{
u8 BackData;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //Interrupt generated
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //Clear interrupt flag.
GetData = UART1_GetByte(BackData); //Also GetData=USART1->DR;
USART1_SendByte(GetData); //Send data
GPIO_SetBits(GPIOE, GPIO_Pin_8 ); //LED flashes, receiving successfully and sending completed
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, and there is nothing else to process in the main function.
Advantages: Simple, suitable for small amount of data transmission.
Disadvantages: There is no cache area, and there is no judgment on the correctness of the data. A slightly larger amount of data may lead to data loss.
Example 2:
void USART2_IRQHandler()
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //Interrupt generated
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Clear interrupt flag
Uart2_Buffer[Uart2_Rx_Num] = USART_ReceiveData(USART2);
Uart2_Rx_Num++;
}
//Judge whether the last received data is the set value and confirm the correctness of the data
if((Uart2_Buffer[0] == 0x5A)&&(Uart2_Buffer[Uart2_Rx_Num-1] == 0xA5))
Uart2_Sta=1;
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(Uart2_Tx_Num=0;Uart2_Tx_Num < Uart2_Rx_Num;Uart2_Tx_Num++)
USART2_SendByte(Uart2_Buffer[Uart2_Tx_Num]); //Send data
Uart2_Rx_Num = 0; // Initialization
Uart2_Tx_Num = 0;
Uart2_Sta = 0;
}
This is the receiving method with data header and data tail added. The number of data header and data tail can be increased. It is only used for debugging. The interrupt function is used to receive data and determine the data header and tail. The second function is executed in the main function according to the query method.
Advantages: It is relatively simple and uses buffer area reception, which can improve the accuracy of data.
Disadvantage: If the first data reception is wrong, it cannot return to the initialization state and a reset operation is required.
Example 3:
void USART2_IRQHandler()
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //Interrupt generated
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Clear interrupt flag.
Uart2_Buffer[Uart2_Rx] = USART_ReceiveData(USART2);
Uart2_Rx++;
Uart2_Rx &= 0x3F; //Judge whether the count reaches 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; //Judge whether the count reaches the maximum
}
The FIFO method is used to receive data. From 0x3F, we can know that the maximum number of data received here is 64, which is variable. The interrupt function is only responsible for receiving, and the other function is executed in the main function, sending in FIFO mode.
Advantages: Both sending and receiving are very free, interrupts take up less time, which is beneficial for MCU to handle other things.
Disadvantages: There is no judgment on the correctness of the data, and all data is accepted.
Example 4:
void USART2_IRQHandler()
{
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //Interrupt generated
{
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) //Header
Uart2_Tx = Uart2_Rx-1;
if((Uart2_Buffer[Uart2_Tx] == 0x5A)&&(Uart2_Buffer[Uart2_Rx-1] == 0xA5)) //The tail is detected when the head is detected
{
Uart2_Len = Uart2_Rx-1- Uart2_Tx; //length
Uart2_Sta=1; //Flag
}
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; // Initialization
Uart2_Tx = 0;
Uart2_Sta = 0;
}
The data is received in the form of data packets and stored in the buffer area. The data "packet" and validity are determined by judging the data header and data tail (variable). The interrupt function is used to receive data and judge the header, tail and data packet length. Another function is executed in the main function and is responsible for sending the data.
Advantages: suitable for package transmission, stability and reliability are guaranteed, can be sent at will, and valid data can be automatically selected.
Disadvantages: The length of the buffer data must be set according to the length of the "package". If there is no beginning or end after multiple receptions, and the data with a beginning and an end happens to span the front and end of the buffer, it may cause the data to be lost. However, this situation is almost impossible.
Previous article:Detailed explanation of STM32 serial port usage
Next article:Several important file descriptions in the STM32 library
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications