新聞中心

按鍵控制LED

作者: 時間:2016-11-26 來源:網(wǎng)絡 收藏
#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++);
}


關鍵詞: 按鍵控制LE

評論


技術專區(qū)

關閉