微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 按键控制LED

按键控制LED

时间:11-26 来源:互联网 点击:
#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++);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top