Interrupt is a b
asic function set for the microcontroller to handle external and internal random events in real time. The existence of interrupt function greatly improves the ability of microcontroller to handle external and internal events. At present, almost all microcontrollers are equipped with this basic function, and the strength of interrupt function has become one of the important indicators to measure whether a microcontroller is powerful. As a learner of microcontroller, the concept of interrupt and programming ability must be flexibly mastered and applied.
The strength of the interrupt function of the MCU varies with the type of MCU. The interrupt function of the 51 MCU is relatively simple, with 5 internal interrupt sources, while some advanced MCUs such as Freescale have hundreds of interrupt sources. But the knowledge is interoperable. Once you understand the concept of the 51 MCU interrupt, it is just a process of understanding and getting familiar with other MCUs.
There are 5 interrupt sources in the 51 MCU, namely external interrupt 0, timer interrupt 0, external interrupt 0, timer interrupt 1, and serial interrupt. We will introduce them separately in the following chapters. In this lesson, we will introduce the main external interrupt 0.
Here we first introduce some basic concepts,
1. The concept of interruption
Let's take an example from daily life, as shown in Figure 1. You are concentrating on reading a book, and suddenly the phone rings, so you write down the page number of the book you are reading, go to answer the phone, and then come back to continue reading. This is actually an example of interruption.
In a single-chip microcomputer, when the CPU is processing an event A, another event B occurs (an interrupt occurs); the CPU temporarily interrupts the current work and switches to processing event B (interrupt response and interrupt service); after processing, it returns to the place where event A was interrupted and continues to process event A (interrupt return). This process is called an interrupt, as shown in Figure 2.
Figure 1: Examples of interrupts in daily life Figure 2: Examples of interrupts in microcontrollers
Here are a few interrupt concepts to remember:
Interrupt source: An event that can cause an interrupt is called an interrupt source, such as a phone ringing. The 51 microcontroller has a total of 5 interrupt sources, namely external interrupt 0, timer interrupt 0, external interrupt 1, timer interrupt 1, and serial port interrupt.
Breakpoint: The place where the interrupt occurs is called a breakpoint.
Main program: The program currently running by the CPU is called the main program, such as reading a book.
Interrupt service subroutine: A program that handles random events, such as answering a phone call, is called an interrupt service subroutine.
Interrupt system: The component that implements the interrupt function is called the interrupt system.
2. MCS-51 Interrupt System
The structure diagram of the 51 single-chip microcomputer interrupt system is shown in Figure 3. There are 51 interrupt sources in total, which are as follows.
Figure 3 51 MCU interrupt system structure diagram
(1) INT0 - external interrupt 0, introduced by P3.2 port, can be set to low level trigger or falling edge trigger.
(2) INT1 - external interrupt 1, introduced by P3.3 port, can be set to low level trigger or falling edge trigger.
(3) T0 - Timer/Counter Interrupt 0, triggered by the internal counter overflow.
(4) T1 - Timer/Counter Interrupt 1, triggered by the internal counter overflow.
(5) TI/RI - serial port interrupt, triggered by the serial port completing a frame of character transmission/reception.
The interrupt system is an important functional module inside the microcontroller. From the perspective of program development, there is no need to have a deep understanding of the structure of the internal functional modules of the microcontroller. Because in order for each functional module to play its powerful role, it is only necessary to correctly set the corresponding registers. Therefore, I will not analyze the specific structure of our interrupt here. The following introduces the relevant registers.
3.51 MCU related registers
There are 21 special function registers inside the 51 single-chip microcomputer. When programming in C language, we only need to master 15 registers: IP, IE, SCON, TCON, P1, P2, P3, P4, PCON TMOD, TL0, TH0, TL1, TH1, and SBUF, as shown in Figure 4. They are marked with red and blue lines. Note that the red lines can perform bit operations, while the blue lines cannot.
Figure 4 51 MCU special function registers [page]
These 15 registers can be divided into four parts according to the four major functional modules inside the 51 microcontroller mentioned above:
I/O port related: P1, P2, P3, P4
Interrupt related: IP, IE
Timer related: TMOD, TCON, TL0, TH0, TL1, TH1
Serial communication related: SCON, PCON, SBUF
It should be noted that, among these four parts, except for P1, P2, P3, and P4 related to I/O port operations which are relatively independent, the other 11 registers are usually used in combination with each other. That is to say, interrupts, timers, and serial port communications are usually used in combination. For example, when an external interrupt occurs, whether to set edge triggering or level triggering requires setting the TR0 and TR1 bits in the TCON register. When using a timer, interrupts may be used, and setting the baud rate during serial port communication is directly related to the timer.
In this section, we mainly introduce external interrupt 0, so there are only three registers related to external interrupts: IE, IP, and TCON. For beginners, the IP register (interrupt priority register) is generally not used, so only IE (interrupt enable register) and TCON (timer control register) are introduced here.
(1) IE interrupt enable register
EA: When EA=0, all interrupts are disabled (i.e. no interrupts are generated); when EA=1, the generation of each interrupt is determined by the individual enable bits
ET2: Timer 2 overflow interrupt enable (8052 uses 1 to enable, 0 to disable)
ES: Serial port interrupt enable (1 enables, 0 disables)
ET1): Timer 1 interrupt enable (1 enables, 0 disables)
EX1: External interrupt INT1 interrupt enable (1 enables, 0 disables)
ET0: Timer 0 interrupt enable (1 enables, 0 disables)
EX0: External interrupt INT0 interrupt enable (1 to enable, 0 to disable)
(2) TCON timer control register
TF1: Timer T1 overflow flag, which can be queried and cleared by the program. TF1 is also an interrupt request source and is cleared by hardware when the CPU responds to the T1 interrupt.
TF0: Timer T0 overflow flag, which can be queried and cleared by the program. TF0 is also an interrupt request source and is cleared by hardware when the CPU responds to the T0 interrupt.
TR1: T1 count enable control bit, when it is 1, T1 count is enabled (timing).
TR0: T0 count enable control bit, when it is 1, T0 count is enabled (timing).
IE1: External interrupt 1 request source (INT1, P3.3) flag. IE1 = 1, external interrupt 1 is requesting an interrupt from the CPU, and when the CPU responds to the interrupt, IE1 is cleared to "0" by hardware (edge-triggered mode).
IT1: External interrupt source 1 trigger mode control bit. IT1=0, external interrupt 1 is programmed as level trigger mode. When INT1 (P3.3) inputs a low level, IE1 is set. This bit is set to level trigger when it is 1, and to falling edge trigger when it is 0.
IE0: External interrupt 0 request source (INT0, P3.2) flag. IE0 = 1, external interrupt 1 is requesting an interrupt from the CPU, and when the CPU responds to the interrupt, IE0 is cleared to "0" by hardware (edge-triggered mode).
IT0: External interrupt source 0 trigger mode control bit. IT0=0, external interrupt 1 is programmed as level trigger mode. When INT0 (P3.2) inputs a low level, IE0 is set. This bit is set to level trigger when it is 1, and set to falling edge trigger when it is 0.
4. External interrupt program example
After understanding the register, I will write a program taking external interrupt 0 as an example. The function of the program is: in the main program, the L1 light is always on, and when the external pin P32 detects a falling edge, the L1 light goes out.
Let's first analyze the writing ideas of this program. First, in the main program, we should make the L1 light always on; then we need to set the interrupt-related registers, mainly to turn on the general interrupt, turn on the external interrupt 0, and also need to set the external interrupt to a low-level trigger mode; finally, we need to write an interrupt subroutine.
The corresponding procedure is shown in Example 1.
Example 1 External interrupt 0 program
#include
#define uint unsigned int
#define uchar unsigned char
sbit D1=P1^0;
void delay(uint z);
void main()
{
IT0=0; //Set to 0 low level trigger
Eaa=1; //Open the general interrupt
EX0=1; //Open external interrupt 0
while(1)
{
D1=1;
}
}
void exter0() interrupt 0
{
D1=0;
delay(500); //If there is no delay when the falling edge is triggered, no phenomenon will be observed
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
The phenomenon of downloading to the experimental board is shown in Figure 5.
Here we should pay attention to the writing method of interrupt sub-function. The format of interrupt sub-function in 51 single-chip microcomputer is as follows:
void function name () interrupt interrupt number
{
Contents of the interrupt service routine
}
Interrupt functions cannot return any value, so void is used in front; followed by the function name, which can be any name, but it should not be the same as the keyword in C language; interrupt functions do not take any parameters, so the small brackets after the function name are empty; the interrupt number refers to the serial number of several interrupt sources in the microcontroller, and the interrupt serial number of each interrupt source is shown in Figure 6. This interrupt number is unique for the compiler to identify different interrupts, and must be written correctly, otherwise the microcontroller cannot enter the interrupt subroutine.
Figure 6 52 MCU interrupt number