51单片机用俩个外部中断,控制俩个LED闪烁代码
时间: 2024-11-29 12:37:58 浏览: 77
在51单片机上,我们可以使用两个外部中断控制两个LED轮流闪烁。首先,你需要配置好外部中断源、中断向量表以及中断服务函数。以下是基本的步骤和一个简单的代码示例:
1. **硬件连接**:
- 将两个LED分别连接到中断引脚(比如INT0和INT1),通常通过电阻分压到5V或单片机IO口的高电平。
- 对于INT0和INT1,可能需要外接中断请求触发器,如磁敏开关或按键。
2. **软件部分**:
- **主程序**:
```c
#include <reg52.h>
sbit led1 = P1^0; // LED1连接P1.0
sbit led2 = P1^1; // LED2连接P1.1
void delay(unsigned int time) {
while(time--) {
for(int i=0; i<128; i++);
}
}
void EXTI0_IRQHandler() interrupt 0x04 {
led1=~led1; // 变换LED1状态
if(ETI0IF==0) return; // 没有新的中断就退出
EXTI0IF=0; // 清除中断标志
delay(DELAY_TIME); // 等待延时
}
void EXTI1_IRQHandler() interrupt 0x08 {
led2=~led2; // 变换LED2状态
if(EXTI1IF==0) return; // 没有新的中断就退出
EXTI1IF=0; // 清除中断标志
delay(DELAY_TIME);
}
void main(void) {
TRISB = 0xFF; // P1设置为输入模式
ETI0IE = 1; // 开启INT0中断
ETI1IE = 1; // 开启INT1中断
EA = 1; // 开启全局中断
while(1);
}
```
- **中断服务函数**:这里假设`DELAY_TIME`是一个预设的延时时间,用于控制LED的闪烁频率。
阅读全文
相关推荐


















