13823761625

Technology

Use external interrupt mode to read the key, control the light on and off
Date:May 19, 2025    Views:50

    Here, we'll learn the use of external interrupts. That is, external IO interrupts INT0 and INT1. The corresponding pins are P32 and P33. In our diagram, P32 is the pin connected to K1. So when we press P32 to the ground, we can trigger an INT0 interrupt, which of course must be initialized before it starts.
    This interrupt mode of the key, can achieve the immediate response of the key. This is useful for situations that require a quick response. External IO interrupts are also commonly used in situations where IO is used to simulate communication and can respond immediately to the arrival of data.
    Here's the code:
―――――――――――――――――
    #define uchar unsigned char // Define it for ease of use
    #define uint unsigned int
    #define ulong unsigned long
    #include // Includes a header file for the 52 standard kernel
    sbit P10 = P1^0;  // IO that is not defined in the header file must be defined by itself
    sbit P11 = P1^1;
    sbit P12 = P1^2;
    sbit P13 = P1^3;
    sbit K1= P3^2;
    bit ldelay=0;  // Long periodic overflow flag. The preset value is 0
    uchar speed=10;  // Set a variable to save the default moving speed of the horse lamp
    uchar speedlever=0;  // Save the current speed level
    char code dx516[3] _at_ 0x003b; // This is set for simulation
    // Read key K1 in external interrupt mode and light an LED
    void main(void) // Main program
    {
    IT0=1;  // An external interrupt jump generates an interrupt
    EX0=1;
    EA=1;  // Turn on the total interrupt
    while(1) // Main program loop
    {
    }
    }
    // External interrupt 0
    int0() interrupt 0
    {
    P10=0;  // Light the LED in the interrupt
    }
――――――――――――――――――――――――――――――――――
    In this program, after pressing K1(P32), it triggers an INT0 interrupt, which lights up the LED.
    Please compile and run, and see the results. As you can see, after pressing K1, LED1 becomes lit.
    Homework:
    With the interrupt mode of the key K1, control an LED light on and off two states
    Tip: Need to shake the operation, otherwise the key jitter will cause a variety of interruptions.







    免责声明: 本文章转自其它平台,并不代表本站观点及立场。若有侵权或异议,请联系我们删除。谢谢!

    Disclaimer: This article is reproduced from other platforms and does not represent the views or positions of this website. If there is any infringement or objection, please contact us to delete it. thank you!
    矽源特科技ChipSourceTek

Copyright © 2017 copyright © 2017 ShenZhen ChipSourceTek Technology Co., Ltd. All Rights ReservedAll Rights Reserved 粤ICP备17060179号