Design of Interrupt Service Subroutine for AT89S51

Publisher:哈哈哈33Latest update time:2018-03-09 Source: eefocusKeywords:AT89S51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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:
  932
  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:
  940
  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.
  941
  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.
  

  

  

  

  

  

  

  

  950

  

  

  

  

Keywords:AT89S51 Reference address:Design of Interrupt Service Subroutine for AT89S51

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

Design of Single Chip Microcomputer in Angle Measuring Instrument
    This paper analyzes the hardware composition and circuit design principle of angle measurement based on AT89S51 single chip microcomputer. The workflow and source code of the main function are given. The design prototype has passed the test of various environmental experiments.   The angle measuring instrument is
[Microcontroller]
Design of Single Chip Microcomputer in Angle Measuring Instrument
Design of LED display circuit of ultrasonic distance meter based on single chip microcomputer
1 Introduction With the rapid development of science and technology, ultrasonic waves will be used more and more widely in rangefinders. However, in terms of the current technical level, the rangefinder technology that people can use is still very limited. Therefore, this is a booming technology and industry fi
[Test Measurement]
Design of temperature acquisition and display system based on AT89S51 microcontroller and LM35 temperature sensor
With the rapid development of electronics and sensing technology, temperature measurement and control have been widely used in civil, industrial, aerospace technology and other fields. Small, low-power, cheap, and highly reliable temperature sensors have attracted widespread attention. In actual production, life and o
[Microcontroller]
Design of temperature acquisition and display system based on AT89S51 microcontroller and LM35 temperature sensor
AT89S51 serial port working mode 0
The working mode 0 of the serial port is the synchronous shift register input/output mode. This mode is not used for asynchronous serial communication between   two AT89S51 microcontrollers    , but is used to connect a shift register to the serial port to expand the parallel I/O port.   Mode 0 uses 8-bit data as a fr
[Microcontroller]
AT89S51 serial port working mode 0
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号