includereg51h>defineuncharunsignedcharsbitk1=P3^0;sbitk2=P3^1;sbitk3=P3^2;sbitk4=P3^3;sbitk5=P3^4
#include
#define unchar unsigned char
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit k5=P3^4;
sbit k6=P3^5;
sbit k7=P3^6;
sbit k8=P3^7;//按键号码
unchar spyon();
unchar led,a,b;
static unchar flag=0;
void zhma();
voidfanma();
voidzheshui();
voidfanshu();
voidquanm();
void quanliang();
void delay();
void main()
{
while(1)
{
P3=0xff;
if(flag==0)
{
P2=0;
delay();
P2=0xff;
delay();
}
flag=spyon();
switch(flag)
{
case 1://D2,D7灭
{
zhma();
break;
}
case 2://D3,D7亮
{
fanma();
break;
}
case 3://D3,D7灭
{
zheshui();
break;
}
case 4://一号灯闪亮
{
fanshu();
break;
}
case 5://闪亮
{
quanm();
break;
}
case 6://全亮
{
quanliang();
break;
}
case 7://亮后4个灯
{
led=0xf0;
P2=led;
break;
}
case 8://亮前四个灯
{
led=0x0f;
P2=led;
break;
}
default:
{
break;
}
}
}
}
unchar spyon()
{
a=P3;
a=a&0xf0;
if(a!=0xf0)
{
a=~((a>>4)|0xf0);
if(a==1)
return 5;
else if(a==2)
return 6;
else if(a==4)
return 7;
else if(a==8)
return 8;
}
else
{
a=P3;
a=a&0x0f;
if(a!=0x0f)
{
a=~(a|0xf0);
if(a==1)
return 1;
else if(a==2)
return 2;
else if(a==4)
return 3;
else if(a==8)
return 4;
}
}
}
void zhma()
{
unchar led;
led=0x42;
P2=led;
delay();
}
voidfanma()
{
unchar led;
led=0xdd;
P2=led;
delay();
}
voidzheshui()
{
unchar led;
led=0x22;
P2=led;
delay();
led=0xfe;
}
voidfanshu()
{
led=0xff;
P2=led;
delay();
led=0x7f;
P2=led;
delay();
}
voidquanm()
{
led=0x7e;
P2=led;
delay();
led=0xdd;
P2=led;
delay();
}
void quanliang()
{
P2=0;
}
void delay()
{
for(a=0;a<200;a++)
for(b=0;b<200;b++);
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/257940.html