来源:
单片机简单程序_zhjysx的博客-优快云博客https://blog.youkuaiyun.com/zhjysx/category_11558658.html
目录
作用:
用一个开关切换LED闪烁模式以及关闭。
思路:
设置一个初始值为1的变量count,只有当count=1时,LED才会闪烁,当按键按下,即触发外部中断,count=~count,LED熄灭。
仿真原理图
头文件
#include <STC8.H>
#include <intrins.h>
sbit LED=P1^0;
char count=1;
延时函数
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
外部中断开启程序
void open_out(){
EA=1;//打开中断总开关
EX0=1;//打开外部中断INT0
IT0=1;//下降沿触发
}
打开EA总开关,打开外部中断INT0,设置为下降沿触发
注意:IT0=0为低电平触发模式,设置此模式,按下按键时可能会导致多次触发
主函数
void main(){
open_out();
while(1){
if(count==1){
LED=~LED;
Delay1000ms();
}
else{
LED=1;//当count不为1时,LED熄灭。count的状态由外部中断改变
}
}
}
在主函数中声明外部中断开启函数,设置变量count=1时,LED闪烁,当count不是1时,LED熄灭。count的值将在外部中断中改变。
外部中断函数
void timer() interrupt 0{
count=~count;
}
按键按下,即触发外部中断,变量count=~count。
仿真:ff外部中断控制LED闪烁.zip-嵌入式文档类资源-优快云文库https://download.youkuaiyun.com/download/zhjysx/73126187