Interrupt serial communication

Publisher:DelightfulGazeLatest update time:2012-08-06 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First example:

Avoid infinite loop, keep sending data (use the self-set receiving data flag method, do not turn off the serial port interrupt ES method) The flag must be placed at the last position in the if statement, otherwise an infinite loop will occur

#include
#define uchar unsigned char
#define uint unsigned int
uchar flag=0,temp;
void init()
{
 TMOD=0X20; //Timer 1 mode 2 as baud rate generator
 TH1=0XF3; //Install the initial baud rate to 4800bps
 TL1=0XF3;
 TR1=1; //Start timer 1
 SCON=0X50; //Set the serial port working mode to mode 1
 PCON=0X80; // Double the baud rate to 4800
 EA=1; //Open the general interrupt
 ES=1; //Enable serial port interrupt
 TI=0; // Clear the send interrupt flag
 RI=0; // Clear the receive interrupt flag
}
void main()
{
 init();
 while(1)
 {
  if(flag==1)
  {
   //ES=0; //flag is 1, indicating that the reception caused an interrupt
   //flag=0; //clear interrupt flag
   RI=0; // Clear the receive interrupt flag to prepare for the next frame of data reception
   temp=SBUF; //Read in received data. Without disabling interrupt ES, the CPU will not cause an interrupt when reading received data.
The receiving data is automatically executed by the receiving SBUF receiving buffer
   P0=temp; //Read data and send to P0 port
   SBUF=temp; //Return received data to the sender
   //TI=0; //Wait for the data to be sent, otherwise TI=0, flag=0 has been executed before the data is sent.
   //flag=0; //After sending, it causes an interrupt, and the flag is set to 1 again, entering an infinite loop and continuously sending data
   while(!TI);//Wait for sending to complete! ! Keep waiting! !
      TI=0; //After data transmission, clear the transmission interrupt flag to prepare for the next frame of data transmission
   flag=0; //Must have otherwise enter an infinite loop, put last
   //ES=1; //Allow serial port interrupt to continue receiving data
  }
 }
}
void serial() interrupt 4
{
 flag=1;
}

----------------------Example 2:-------------------------------------------------------

/**********************************************
      Interrupt serial communication (using the ES method to disable serial interrupts) is recommended to avoid frame loss
*************************************************/
#include
#define uchar unsigned char
#define uint unsigned int
uchar flag=0,temp;
void init()
{
 TMOD=0X20; //Timer 1 mode 2 as baud rate generator
 TH1=0XF3; //Install the initial baud rate to 4800bps
 TL1=0XF3;
 TR1=1; //Start timer 1
 SCON=0X50; //Set the serial port working mode to mode 1
 PCON=0X80; // Double the baud rate to 4800
 EA=1; //Open the general interrupt
 ES=1; //Enable serial port interrupt
 TI=0; // Clear the send interrupt flag
 RI=0; // Clear the receive interrupt flag
}
void main()
{
 init();
 while(1)
 {
  if(flag==1)
  {
   ES=0; //flag is 1, indicating that the reception has completed and caused an interrupt, turn off the serial port interrupt, and process the current frame
   flag=0; // clear interrupt flag
   RI=0; // Clear the receive interrupt flag to prepare for receiving the next frame of data. Clearing it here will not cause frame loss.
   temp=SBUF; //Read in received data. If the interrupt ES is not disabled, the CPU will not cause an interrupt when reading received data.
The receiving data is automatically executed by the receiving SBUF receiving buffer
   P0=temp; //Read data and send to P0 port
   SBUF=temp; //Return received data to the sender
   //TI=0; //Wait for the data to be sent, otherwise TI=0, flag=0 has been executed before the data is sent.
   //flag=0; //After sending, it causes an interrupt, and the flag is set to 1 again, entering an infinite loop and continuously sending data
   while(!TI);//Wait for sending to complete! ! Keep waiting! !
      TI=0; //After data transmission, clear the transmission interrupt flag to prepare for the next frame of data transmission
   //flag=0; //Must have otherwise enter an infinite loop, put last
   ES=1; // Allow serial port interrupt to continue receiving data
  }
 }
}
void serial() interrupt 4 //Execute this sentence to generate an interrupt indicating that the data has been received
{
    //RI=0; //Do not clear the receive interrupt flag here, otherwise the receiving SUBF will be in the receiving state.
The interruption continues to affect other instructions, causing the return frame loss phenomenon. The experiment shows that
 flag=1;
}
Reference address:Interrupt serial communication

Previous article:Lookup table matrix keyboard
Next article:A brief discussion on how to eliminate key jitter

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

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号