Interrupt is an important function of the microcontroller. In this article, we will introduce the interrupt of 51 microcontroller.
The so-called "interrupt", in layman's terms, means that the microcontroller stops the task it is executing and switches to execute other tasks that need to be solved urgently. When this task is completed, it returns to the original task and continues to execute. Interrupts are designed to enable the microcontroller to process random external or internal events in real time. The existence of the interrupt function has greatly improved the microcontroller's ability to handle external or internal events. The interrupt function is something we must master in the process of learning microcontrollers.
There are 6 interrupt sources inside the STC89C516 microcontroller, that is, when 6 situations occur, the microcontroller will process the interrupt program.
To make it easier for everyone to understand the concept of interruption, let's take a common example: you turn on the fire, boil a pot of water, and then go to sweep the floor. While sweeping the floor, you suddenly hear the kettle's water boiling alarm. At this time, you stop sweeping the floor, turn off the fire immediately, and then pour the boiling water into the thermos. After filling the water, you go back to sweeping the floor. This process is an interruption. This process is shown in the figure below. Sweeping the floor is the main task. The water boiling alarm interrupts the sweeping, causing you to fill the water. After filling the water, you return to sweeping the floor.
For a single-chip microcomputer, an interrupt means that when the CPU is processing an event A, another event B occurs, requesting the CPU to quickly process event B (interrupt occurs). The CPU temporarily stops the current work (interrupt response) and switches to process event B (interrupt service). After the CPU processes event B, it returns to the place where event A was interrupted and continues to process event A (interrupt return). This process is called an interrupt, and its flow is shown in the figure below.
Let's analyze the real-life example we just mentioned. Your main task is to sweep the floor. The water boiling alarm is an interrupt request. This moment is equivalent to the breakpoint. You respond to the interrupt to turn off the fire, and then pour the boiling water into the thermos. This action is actually processing the interrupt program. After filling the water, you go back to continue sweeping the floor, which is equivalent to returning to the main program after processing the interrupt processing program. It should be noted here that the water may boil at any time, but no matter when it happens, as long as the water boils, it must be handled immediately. After processing, continue sweeping the floor. When the microcontroller is executing a program, interrupts may also occur at any time. But no matter when it happens, once it happens, the microcontroller will immediately suspend the current program and rush to handle the interrupt program. After processing the interrupt program, it will return to the pause point and continue to execute the original program.
The source of the CPU interrupt is called the interrupt source. The interrupt source CPU raises an interrupt request, and the CPU temporarily interrupts the original event A and turns to process event B. After processing event B, it returns to the original interrupt location, which is called an interrupt return. The functional component that implements the above interrupt is called an interrupt system.
The interruption opening and closing, setting which interrupt to enable, etc. are all determined by some special function registers inside the microcontroller.
Another knowledge point related to interrupts is interrupt nesting, which means: if the MCU is processing an interrupt program, and at this time, another interrupt with a higher interrupt priority occurs, the MCU will stop the current interrupt program and switch to execute the new interrupt program. After the new interrupt program is processed, it will return to the interrupt program that was just stopped and continue to execute. After executing this interrupt, it will return to the main program to continue executing the main program. The flowchart of interrupt nesting is shown in the figure below.
When designing interrupts, there is another important issue: interrupt priority. When the microcontroller is executing a program, it may encounter two interrupts at the same time. Which interrupt should the microcontroller execute first? This depends on the setting of a special function register inside the microcontroller - the interrupt priority register. By setting the interrupt priority register, we can tell the microcontroller which interrupt program to execute first when two interrupts occur at the same time. If the operation priority register is not set, the microcontroller will automatically process it according to the default set of priorities.
There are 6 interrupt sources inside the STC89C516 microcontroller, which are:
INT0—external interrupt 0, introduced by P32 port line, low level or falling edge triggers interrupt.
INT1—external interrupt 1, introduced by P33 port line, low level or falling edge triggers interrupt.
T0—Timer/Counter 0 interrupt, caused by the T0 counter counting back to zero.
T1—Timer/Counter 1 interrupt, caused by the T1 counter counting back to zero.
T2—Timer/Counter 2 interrupt, caused by the T2 counter counting back to zero.
TI/RI—Serial port interrupt, caused by the serial port completing a frame of character transmission/reception.
The default interrupt priority levels of the MCU's six interrupt sources are shown in the following table.
Next, we will introduce two registers that are often used in the process of using microcontroller interrupts: interrupt enable register IE and interrupt priority register IP.
(1) Interrupt enable register IE
The interrupt enable register is used to set the opening and closing of each interrupt source. The byte address of IE in the special function register is A8H, and the bit address (from low to high) is A8H~AFH. This register can be bit-addressed, that is, each bit of this register can be operated. When the microcontroller is reset, all IE registers are cleared to 0. The bits of this register are shown in the following table.
EA—Global interrupt enable bit.
EA=1, turn on the global interrupt control. Under this condition, the opening and closing of the corresponding interrupt are determined by each interrupt control bit.
EA=0, turn off all interrupts.
--—Invalid bit.
ET2—Timer/Counter 2 interrupt enable bit.
ET2=1, turn on T2 interrupt.
ET2=0, turn off T2 interrupt.
ES—Serial port interrupt enable bit.
ES=1, turn on serial port interrupt.
ES=0, turn off serial port interrupt.
ET1—Timer/Counter 1 interrupt enable bit.
ET1=1, turn on T1 interrupt.
ET1=0, turn off T1 interrupt.
EX1—External interrupt 1 interrupt enable bit.
EX1=1, turn on external interrupt 1 interrupt.
EX1=0, turn off external interrupt 1.
ET0—Timer/Counter 0 interrupt enable bit.
ET0=1, turn on T0 interrupt.
ET0=0, turn off T0 interrupt.
EX0—External interrupt 0 interrupt enable bit.
EX0=1, turn on external interrupt 0 interrupt.
EX0=0, turn off external interrupt 0.
(2) Interrupt priority register IP
The interrupt priority register is in the special function register, the byte address is B8H, the bit address (from low to high) is B8H~BFH, the IP register is used to set which level of the two-level interrupt each interrupt source belongs to. This register can be bit-addressed, that is, each bit of the register can be operated separately. When the microcontroller is reset, all IPs are cleared to 0. The definition of this register is shown in the following table.
PS—Serial port interrupt priority control bit.
PS=1, serial port interrupt is defined as high priority interrupt.
PS=0, serial port interrupt is defined as low priority interrupt.
PT1—Timer/Counter 1 interrupt priority control bit.
PT1=1, timer/counter 1 interrupt is defined as a high priority interrupt.
PT1=0, timer/counter 1 interrupt is defined as a low priority interrupt.
PX1—External interrupt 1 interrupt priority control bit.
PX1=1, external interrupt 1 is defined as a high priority interrupt.
PX1=1, external interrupt 1 is defined as a high priority interrupt.
PT0—Timer/Counter 0 interrupt priority control bit.
PT0=1, timer/counter 0 interrupt is defined as a high priority interrupt.
PT0=0, timer/counter 0 interrupt is defined as a low priority interrupt.
PX0—External interrupt 0 interrupt priority control bit.
PX0=1, external interrupt 0 is defined as a high priority interrupt.
PX0=1, external interrupt 0 is defined as a high priority interrupt.
In 51 single-chip microcomputers, high-priority interrupts can interrupt low-priority interrupts to form interrupt nesting, but interrupts of the same priority level or between low-level and high-level interrupts cannot form interrupt nesting. If several interrupts of the same level request interrupt response from the CPU at the same time, the interrupt will be responded to according to the default interrupt level without setting any interrupt priority. After setting the interrupt priority, the order of response is determined according to the setting order.
In this article, we introduced the basic principles of interrupts and related registers. The specific usage of interrupts will be introduced in later articles.
Previous article:51 MCU (XV) - Introduction to the Timer Module
Next article:51 MCU (Thirteen) - Watchdog Function Test
- Popular Resources
- Popular amplifiers
- 【ON Semiconductor】LC87F7932B8-bit single-chip microcontroller
- Introduces the CAN interrupt structure function of C167CR and the application of the standard CAN interrupt structure
- USB Complete The Developer\'s Guide 5ed (USB Development Complete 5th Edition)
- Detailed I2C interface SI114 datasheet for proximity/ambient light sensor chip
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- About the sending problem of single chip microcomputer serial port mode 3
- imx6ull transplant alsa
- Recommend a fast source code style conversion tool AStyle
- Talk about the future of Wi-Fi 6 and predict who will lead the wireless connection
- This circuit doesn't seem to work!
- TI Texas Instruments CCD
- Design scheme based on embedded processor PowerPC7447
- [Xiao Meige SoC] How to turn off the LED flashing when AC501-SoC development board is turned on in the default Linux system
- There are some problems with this flyback power supply circuit
- 2. Reading and demonstration of all sensor data based on Nucleo-L476L