在51单片机中,按一下按键1,执行一个程序功能1,按按键2,执行程序功能2

2024-11-27 04:18:11
推荐回答(1个)
回答1:

#include
#include
unsigned char fun,led1=0xfe,led2=0x55;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
void t0isr() interrupt 1
{
TH0=(65536-60000)/256;
TL0=(65536-60000)%256;
switch(fun)
{
case 1:P0=led1;led=_crol_(led1,1);break;
case 2:P0=led1;led=_cror_(led1,1);break;
case 3:P0=~led2;break;
default: break;
}
}
main()
{
TMOD=0x01;
TH0=(65536-60000)/256;
TL0=(65536-60000)%256;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(key1==0)
{
while(key1==0);
fun=1;
}

if(key2==0)
{
while(key2==0);
fun=2;
}
if(key3==0)
{
while(key3==0);
fun=3;
}
}
}