51 microcontroller external interrupt experiment

Publisher:innovator8Latest update time:2024-03-21 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Earlier, we talked about the input and output functions of the 51 microcontroller. Today we will talk about something different, external interrupt testing.

First of all, we have to talk about the role of interruptions in dealing with internal and external random events and responding to emergencies. While the CPU is executing the program, if an external emergency event occurs, the CPU stops its work to handle the emergency event. After processing, it returns to the place where it was originally interrupted and continues working. It's like receiving a phone call while eating, going out to answer the phone and then sitting back at the table to eat. Stopping what you were doing and doing other things in the middle is an interruption.


The interrupt function of the 51 microcontroller is relatively simple. You only need to configure the corresponding registers to complete the initialization.

In this article, the external interrupt is tested using button P3.2 because its port has the second function of external interrupt.

picture

picture

Before configuring the interrupt structure of the 51 microcontroller, you need to look at the internal structure diagram. You can see that there are IE, IP, TCON and other registers to control the interrupt function.

picture

Different registers have different functions. Each register has 8-bit configuration bits, which are responsible for different functions. Among them, the most important external interrupts are the IE and TCON registers. For details, you can read the 51 microcontroller data manual, which will provide detailed introduction.


The detailed code is as follows:


#include



sbit ExternalInterruptPin = P3^2; // External interrupt pin definition



void ExternalInterrupt_Init() {

    IT0 = 1; // Set external interrupt 0 to falling edge trigger mode

    EX0 = 1; // Enable external interrupt 0

    EA = 1; // Global interrupt enable

}



void ExternalInterrupt_Service() interrupt 0 {

    // Process logic in the interrupt service routine

    // ...

}



void main() {

    ExternalInterrupt_Init();


    while (1) {

        //Other logic in the main loop

        // ...

    }

}


Among them, void ExternalInterrupt_Service () interrupt 0

One line is the 51 microcontroller interrupt service function, which has been designed by the semiconductor manufacturer. We only need to use it according to regulations, unless we are working on the kernel, we will not consider the design issues.

There are many other interrupt functions as shown below. Just choose different functions according to different ports and functions:

picture

Note: The so-called falling edge is the edge that changes from high level to low level, that is, the time when the transition occurs.


Reference address:51 microcontroller external interrupt experiment

Previous article:Simulation design of intelligent desk lamp for infrared detection and light detection based on 51 microcontroller
Next article:Punch controller based on AT89S51 microcontroller

Recommended Content
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号