acerfgshca 2023-07-10 18:27 采纳率: 0%
浏览 25

关于#外部中断#的问题,如何解决?

#include "bsp_led.h"

include "bsp_init.h"

include "Delay.h"

#include "Sys_Init.h"
unsigned char ucLed;

void main(void)
{
Cls_Peripheral();

  while(1)
    {
        ucLed=0x01;
        Led_Disp(ucLed);
      Delay(200);
        
        ucLed ^=1;
      Led_Disp(ucLed);
      Delay(200);
    }
}

 void isr_intr_0() interrupt 0
{
    
    Cls_Peripheral();
    ucLed=0x80;
    Led_Disp(ucLed);
    Delay(1000);
    ucLed=0;
    
}
    
  • 写回答

1条回答 默认 最新

  • 会飞的石头。 2023-07-12 15:57
    关注

    根据你提供的代码,我注意到你似乎想要使用外部中断功能来改变 LED 的状态。然而,在你提供的代码中,缺少了头文件和一些函数的实现,这使得代码无法正常工作。下面是一个示例的改进代码:

    #include <reg51.h>
    
    sbit LED = P1 ^ 0;  // 定义LED引脚,假设LED接在P1.0上
    
    void Init_Interrupt() {
        IT0 = 1;    // 设置外部中断0为边沿触发方式
        EX0 = 1;    // 允许外部中断0
        EA = 1;     // 全局中断使能
    }
    
    void Delay(unsigned int count) {
        unsigned int i, j;
        for (i = 0; i < count; i++) {
            for (j = 0; j < 1000; j++) {
                // 空循环延时
            }
        }
    }
    
    void main(void) {
        Init_Interrupt();  // 初始化外部中断
        while (1) {
            LED = 1;  // 点亮LED
            Delay(500);
            LED = 0;  // 熄灭LED
            Delay(500);
        }
    }
    
    void ISR_INT0(void) interrupt 0 {
        LED = !LED;  // 切换LED状态
    }
    

    这个改进后的代码中,添加了 Init_Interrupt 函数来初始化外部中断。在 main 函数中,通过循环不断地点亮和熄灭 LED。当外部中断触发时,中断服务函数 ISR_INT0 会被调用,从而切换 LED 的状态。
    上述代码是基于8051单片机的C语言编写的,需要确保使用的编译器和开发环境支持该单片机型号和相应的中断功能。如果需要在其他型号的51单片机上使用外部中断,请根据具体的芯片手册和编译器文档进行相应的调整和配置。

    评论

报告相同问题?

问题事件

  • 请详细说明问题背景 7月11日
  • 创建了问题 7月10日