STM32 serial port interrupt receive and interrupt send

Source: Internet
Author: User

STM32 Serial USART1 interrupt receive and interrupt send


First post the interrupt function:

void Usart1_irqhandler (void) {
        if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET) {
                 Usart_ Clearitpendingbit (USART1, usart_it_rxne);
                 Usart1_buffer[i++]=usart_receivedata (USART1);  Usart1_buffesh is a self-defined receive array
                 if (i>3) {
                      sendflag = 1;
                 }
         }
        if (Usart_getitstatus (USART1, usart_it_txe)! = RESET) {    //USART_IT_TC can also be replaced here
                if (open_send_flag) {     // This is a flag sent by
                      uart1_sendstring (temp_data);
                     Usart_itconfig (USART1, Usart_it_txe, DISABLE);   USART_IT_TC
                     open_send_flag = 0;
                 }
                else{
                     usart_itconfig (USART1, Usart_it_txe, DISABLE);   USART_IT_TC
                 }}}

function to send a string:

void Uart1_sendstring (uint8_t* cp)
{
    while ((*CP)! = ') ')
    {
        Usart1_send_isr (*CP);
        cp++;
    }
}

A function that sends a single character:

void Usart1_send_isr (uint8_t ch) {
     usart_senddata (USART1, (uint8_t) ch);
     while (Usart_getflagstatus (USART1, usart_flag_txe) = = RESET); Do you want the buffer to be sent after the decision?
}

Then open the send interrupt when the data is sent outside the interrupt function.    Usart_itconfig (USART1, Usart_it_txe, ENABLE); Usart_it_tc

Then put out two simple serial reception and transmission of the test small example:


The first type: Interrupt Receive and send the received data directly.

void Usart1_irqhandler (void) {
      unsigned char RxData;
      if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET) {
            usart_clearitpendingbit (USART1, usart_it_rxne);
            Rxdata=usart_receivedata (USART1); 
            RxData = RxData + 1;
           Usart_senddata (Usart1,rxdata);
      }
}

Advantages: simple, suitable for very small data transmission.
Disadvantage: No buffer, and the correctness of the data is not judged, the amount of data is slightly larger may lead to data loss.
The second kind: After the interrupt receives to judge the data head and the data end, if correctly sends out directly.
void Usart1_irqhandler (void) {


 if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET) {
       Usart_ Clearitpendingbit (USART1, usart_it_rxne);
       Usart1_buffer[i++]=usart_receivedata (USART1); 
   }
 if ((usart1_buffer[0] = = 0x01) && (usart1_buffer[i-1] = = 0x02)) {
      Flag = 1;
 }
 if (Usart_getflagstatus (usart1,usart_flag_ore) = = SET) {  //overflow, not understood.
  Usart_clearflag (usart1,usart_flag_ore);
  Usart_receivedata (USART1); 
 }
 if (Flag) {for
  (j = 0;j<20;j++) {
        usart_senddata (usart1,usart1_buffer[j]);
  }
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.