按键控制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();
void fanma();
void zheshui();
void fanshu();
void quanm();
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();
}
void fanma()
{
unchar led;
led=0xdd;
P2=led;
delay();
}
void zheshui()
{
unchar led;
led=0x22;
P2=led;
delay();
led=0xfe;
}
void fanshu()
{
led=0xff;
P2=led;
delay();
led=0x7f;
P2=led;
delay();
}
void quanm()
{
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++);
}
#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();
void
void
void
void
void quanliang();
void delay();
void main()
{
}
}
unchar spyon()
{
void zhma()
{
}
void
{
}
void
{
}
void
{
void delay()
{
按键控制LE 相关文章:
- 单片机单按键控制led台灯(11-30)
- 一键多功能(按键控制LED亮度)(11-30)
- PIC12F508单片机用按键控制LED灯C语言程序(11-29)
- 标记的用法,用一个按键控制1个LED灯的亮灭,按键去抖(11-25)
- 51单片机程序——按键控制LED灯(11-23)
- 按键控制led闪烁程序(11-23)