4 ways to receive data from STM32 serial port interrupt

Publisher:blazingsLatest update time:2018-09-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM32 Reference address:4 ways to receive data from STM32 serial port interrupt

Previous article:Detailed explanation of STM32 serial port usage
Next article:Several important file descriptions in the STM32 library

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号