The operation of the interrupt system must be coordinated with the interrupt service subroutine to be used correctly. To design an interrupt service subroutine, the following issues must be clarified first.
Tasks of interrupt service subroutine design
The basic tasks of interrupt service subroutine design are as follows:
(1) Set the interrupt enable control register IE to allow the corresponding interrupt request source to interrupt.
(2) Set the interrupt priority register IP to determine and assign the priority of the interrupt source used.
(3) If it is an external interrupt source, the trigger mode of the interrupt request IT1 or IT0 must also be set to determine whether to use the level trigger mode or the edge trigger mode.
(4) Write an interrupt service subroutine to handle the interrupt request.
The first three items are generally placed in the initialization program segment of the main program.
For example: Example 1, assume that external interrupt 0 is allowed to interrupt, and it is set as a high priority interrupt, and other interrupt sources are low priority interrupts, using the edge trigger mode. The following initialization program segment can be written in the main program:
Main program structure when using interrupts
Since the addresses of each interrupt population are fixed, and the program must first be executed from the main program starting address OOOOH. Therefore, in the few bytes of the starting address OOOOH, an unconditional transfer instruction must be used to jump to the main program. In addition, the difference between each interrupt entry address is 8 bytes. If the interrupt service subroutine is slightly longer, it will exceed 8 bytes. In this way, the interrupt service subroutine occupies other interrupt entry addresses, affecting the interrupt processing of other interrupt sources. For this reason, generally after entering the interrupt, an unconditional transfer instruction is used to jump the interrupt service subroutine to the entry address far away from other interrupt entries.
The commonly used main program structure is as follows:
Note: In the above main program structure, if there are multiple interrupt sources, there will be multiple interrupt entry addresses of "ORG XlX2X3X4H". Multiple "interrupt entry addresses" must be arranged from small to large. The starting address Y1Y2Y3 Y4H of the main program MAIN is arranged according to the specific situation.
The process of the interrupt service subroutine
After the AT89S51 responds to the interrupt, it enters the interrupt service subroutine. The basic process of the interrupt service subroutine is shown in the figure on the right.
The following is an explanation of some issues related to the execution process of the interrupt service subroutine. (1) Context protection and context recovery The so-called context refers to the data or status in certain registers and memory units in the microcontroller
when entering the interrupt . In order to prevent the execution of the interrupt service subroutine from destroying these data or states, so as to avoid affecting the operation of the main program after the interrupt returns, they must be saved in the stack. This is called scene protection. Scene protection must be located before the scene interrupt handler. After the interrupt processing is completed, before returning to the main program, the saved scene content needs to be popped from the stack to restore the original content in those registers and memory units. This is called scene recovery. Scene recovery must be located after the interrupt processing. The stack operation instructions "PUSH direct" and "POP direct" of AT89S51 are mainly used for scene protection and scene recovery. As for what content to protect, it should be decided by the user according to the specific situation of the interrupt handler. (2) Turning off and on interrupts Turning off interrupts before scene protection and scene recovery in the right figure is to prevent a higher-level interrupt from entering at this time and avoid the scene from being destroyed; turning on interrupts after scene protection and scene recovery is to prepare for the next interrupt and to allow a higher-level interrupt to enter. The result of this is that interrupt processing can be interrupted, but the original scene protection and scene recovery are not allowed to be changed. Except for the moment of scene protection and scene recovery, the interrupt nesting function is still maintained. But sometimes, for an important interrupt, it must be executed to completion and cannot be nested by other interrupts. For this, you can turn off the general interrupt switch bit before scene protection, completely turn off other interrupt requests, and then turn on the general interrupt switch bit after the interrupt is processed. In this way, it is necessary to remove the two processes of "turning on interrupt" and "turning off interrupt" before and after the "interrupt processing" step in Figure 5-9. (3) Interrupt processing Interrupt processing is the specific purpose of the interrupt source requesting an interrupt. Application designers should write the interrupt processing part of the program according to the specific requirements of the task. (4) Interrupt return The last instruction of the interrupt service subroutine must be the return instruction RETI, which is the sign of the end of the interrupt service program. After the CPU executes this instruction, it clears the non-addressable priority status trigger that was set to 1 when responding to the interrupt to 0, then pops the two-byte breakpoint address on the top of the stack from the stack and sends it to the program counter PC. The first byte popped out is sent to PCH, and the second byte popped out is sent to PCL. The CPU re-executes the interrupted main program from the breakpoint. Example 2: Write an interrupt service program based on the interrupt service subroutine flow in the figure below. Assume that the scene protection only requires pushing the contents of the PSW register and accumulator A into the stack for protection. A typical interrupt service subroutine is as follows: There are several points to note about the above program: (1) The scene protection in this example assumes that only the contents of PSW and A are involved. If there are other contents that need to be protected, just add a few more PUSH and POP instructions at the corresponding positions. Note that the operation on the stack is first in, last out, and the order cannot be reversed. (2) The "interrupt handling program segment" in the interrupt service subroutine, the application designer should write this part of the interrupt handling program according to the specific requirements of the interrupt task. (3) If the interrupt service subroutine is not allowed to be interrupted by other interrupts, the two instructions "SETB EA" and "CLR EA" before and after the "interrupt handling program segment" can be removed. (4) The last instruction of the interrupt service subroutine must be the return instruction RETI, which is indispensable. It is the sign of the end of the interrupt service subroutine. After the CPU executes this instruction, it returns to the breakpoint and re-executes the interrupted main program.
Previous article:Design of AT89S51 Multiple External Interrupt Source System
Next article:AT89S51 interrupt enable and interrupt priority control
Recommended ReadingLatest update time:2024-11-16 14:25
- Popular Resources
- Popular amplifiers
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
- EEWORLD University ---- Computer Architecture (Huazhong University of Science and Technology)
- Innovative application of capacitive touch buttons in the elevator industry
- The role of inductance
- EEWORLD University ---- Motor Control
- Overload protector related
- Sub-library: Summary of official Chinese technical documents on temperature and humidity sensors
- Rainbow Arch designed with Circuit Playground Express
- An interview with Roger Hall, General Manager of Qorvo High Performance Solutions Division, to help you learn more about 5G
- 【ST NUCLEO-H743ZI Review】——by bigbat
- How long have you not spent the Lantern Festival at home?