51 Single Chip Microcomputer (XIV) - Introduction to Interrupt Function

Publisher:开国古泉Latest update time:2021-07-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

      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.

image.png

        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.

image.png

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.

image.png

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.

Reference address:51 Single Chip Microcomputer (XIV) - Introduction to Interrupt Function

Previous article:51 MCU (XV) - Introduction to the Timer Module
Next article:51 MCU (Thirteen) - Watchdog Function Test

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号