#include <REGX51.H>
#define LED_PORT P1
void Delay(unsigned char xms)
{
unsigned char i,j;
while(xms--)
{
i=2;
j=199;
do
{
while(--j);
}while(--i);
}
}
void main()
{
IT0=0;
EX0=1;
EA=1;
EX1=1;
IT1=1;
IP=0x04;
LED_PORT=0xff;
EX0=0;
Delay(20);
EX0=1;
while(1)
{
unsigned char i,temp;
temp = 0x01;
for(i = 0;i < 5;i++)
{
LED_PORT = ~temp;
Delay(500);
temp = temp<<1;
}
}
}
void int1() interrupt 1
{
unsigned char i,tamp;
if(P3^2==0)
{
for(i=0;i<10;i++){
LED_PORT = tamp;
Delay(500);
tamp = tamp>>1;
}
}
}
void int2() interrupt 2
{
int g;
if(P3^3==0)
{
for(g=0;g<5;g++)
{
Delay(400);
P1_0=0;P1_1=0;P1_2=0;P1_3=0;P1_4=0;
Delay(50);
P1_0=1;P1_1=1;P1_2=1;P1_3=1;P1_4=1;
Delay(50);
}
}
}
博主用的是51单片机ST89C51系列,流水灯代码为外部中断(P3^2)和(P3^3)两个按键;
程序逻辑如下:

高优先级中断2打断低优先级中断1,即程序从单向流水灯开始,当按键1(P3^2)按下时进入中断1,流水灯全量;紧接着按下中断按键2(P3^3),打断中断1的程序进入中断2,LED灯闪烁5次;当中断2程序执行完成后返回到中断1,执行中断1未完成(被中断2打断后剩余)的程序,最后返回main主函数。返回主函数后如果没有再次按下任何按键,程序会反复执行单向流水灯的程序。
5116

被折叠的 条评论
为什么被折叠?



