使用外部中断0 即P3.2口一个按键切换点亮不同LED
#include <reg52.h>
unsigned int Mode=0;
sbit key1=P3^2;
void delay(unsigned int i)
{
while(i--);
}
void Int0init()
{
IT0=1;
EX0=1;
EA=1;
}
void main()
{
while(1)
{
Int0init();
switch(Mode)
{
case(0):if(Mode==0){P2=0xfe;}break;
case(1):if(Mode==1){P2=0xfd;}break;
case(2):if(Mode==2){P2=0xfb;}break;
case(3):if(Mode==3){P2=0xf7;}break;
case(4):if(Mode==4){P2=0xef;}break;
case(5):if(Mode==5){P2=0xdf;}break;
case(6):if(Mode==6){P2=0xbf;}break;
case(7):if(Mode==7){P2=0x7f;}break;
}
}
}
void Int0() interrupt 0
{
if(key1==0)
{
delay(1000);
if(key1==0)
{
Mode++;
if(Mode>=8)
{
Mode=0;
}
while(!key1);
}
}
}
|