Lesson 8 MCU Interrupts

Publisher:EnchantedMagicLatest update time:2015-05-14 Source: 51heiKeywords:MCU  Interrupt Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Interrupt is a basic 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
Keywords:MCU  Interrupt Reference address:Lesson 8 MCU Interrupts

Previous article:Lesson 10 Keyboard Detection and Application
Next article:Lesson 6 Digital tube static display

Recommended ReadingLatest update time:2024-11-16 20:58

Design of Stereo Audio Interface Based on Kinetis Microcontroller and 16-bit/24-bit MAX5556
This paper presents a stereo audio interface design based on the Kinetis MK60N512 microcontroller and the 16-bit/24-bit audio digital-to-analog converter MAX5556. The MK60N512 transmits audio data to the MAX5556 via the I2S bus, and the output audio signal is filtered by an active filter to ensure audio quality whil
[Embedded]
Design of Stereo Audio Interface Based on Kinetis Microcontroller and 16-bit/24-bit MAX5556
PIC16F84A MCU Tutorial (PCB Diagram and Schematic Diagram) Detailed Explanation
This is a new design, based on the popular PIC16F84A microcontroller tutorial board. It features 8 individual LEDs, a 7-segment display, an LCD display and five buttons. It is an ideal solution for a beginner to take/her first programming steps in the world of microcontrollers. There is an In-Circuit Programming (IC
[Microcontroller]
PIC16F84A MCU Tutorial (PCB Diagram and Schematic Diagram) Detailed Explanation
Software Anti-interference Technology in Single-Chip Microcomputer System
In recent years, the application of single-chip microcomputer systems in the field of industrial measurement and control has become more and more extensive. For industrial sites with harsh environments, the reliability and safety of this new type of microcontroller has become a big problem. Programs that run normall
[Microcontroller]
Transplantation and Development of μCOS-II on ATmega128 MCU
  Introduction   This article introduces the process of porting μC/OS-Ⅱ to ATMEL's 8-bit microcontroller ATmega128. The so-called porting is to make a real-time kernel run on a certain microprocessor, and develop drivers on this basis to make it a practical embedded system. Embedded systems include hardware and soft
[Microcontroller]
PIC18 microcontroller configuration bit settings
As shown in the figure:
[Microcontroller]
PIC18 microcontroller configuration bit settings
MCU prices cut in half, has the automotive chip panic reached a turning point?
Semiconductor chips, which once had high prices, have recently reached a turning point. According to media reports, the storm of semiconductor chip order cuts and price cuts has intensified, and even the quotations of MCUs, which were previously in short supply, have begun to drop sharply. There are also reports that
[Automotive Electronics]
MCU prices cut in half, has the automotive chip panic reached a turning point?
Design of data acquisition card based on C8051F021 microcontroller and PCI interface
1 Introduction In his speech at the closing ceremony of the 26th China Power Grid Dispatching Operation, Deputy General Manager Lu Yanchang of the State Power Corporation pointed out the issues that should be focused on research and solution in the development of power grid technology in the future. Among them, power
[Microcontroller]
Design of data acquisition card based on C8051F021 microcontroller and PCI interface
Portable data acquisition system composed of ADuC812 and K9S6408V0A
    Abstract: K9S6408V0A is a flash memory produced by Samsung. It has the characteristics of large capacity and simple interface. The ADuC812 is a multi-channel 12-bit AD converter with embedded MCU. This article introduces a portable data acquisition system based on ADuC812 and equipped with K9S6408V0A flash memory
[Analog Electronics]
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号