按键点灯程序
时间:10-02
整理:3721RD
点击:
#include <REG51.H>
sbit duan=P2^6;
sbit wei=P2^7;
sbit led=P0^0;
sbit led1=P0^1;
sbit led2=P0^2;
sbit key=P1^2;
sbit key1=P1^3;
sbit key2=P1^4;
sbit key3=P1^5;
sbit key4=P1^6;
sbit key5=P1^7;
#define shuju P3
unsigned char i;
unsigned char duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned int k)
{
unsigned int j;
while(k--)
for(j=0;j<120;j++);
}
void main()
{
P3=0x00;
while(1)
{
if(key==0)
{
delay(10);
if(key==0)
{
while(!key);
led=!led; //红灯
}
}
if(key1==0)
{
delay(10);
if(key1==0)
{
while(!key1);
led1=!led1; //黄灯
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
while(!key2);
led2=!led2; //绿灯
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
while(!key3);
for(i=0;i<9;i++) // 红灯闪烁次数
{
P3=duanma[i];
delay(100);
led=0;
led1=1;
led2=1;
delay(100);
led=1;
}
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
while(!key4);
for(i=0;i<10;i++) // 黄灯闪烁次数
{
P3=duanma[i];
delay(100);
led=1;
led1=0;
led2=1;
delay(100);
led1=1;
}
}
}
if(key5==0)
{
delay(10);
if(key5==0)
{
while(!key5);
for(i=0;i<7;i++) // 绿灯闪烁次数
{
P3=duanma[i];
delay(100);
led=1;
led1=1;
led2=0;
delay(100);
led2=1;
}
}
}
}
}
sbit duan=P2^6;
sbit wei=P2^7;
sbit led=P0^0;
sbit led1=P0^1;
sbit led2=P0^2;
sbit key=P1^2;
sbit key1=P1^3;
sbit key2=P1^4;
sbit key3=P1^5;
sbit key4=P1^6;
sbit key5=P1^7;
#define shuju P3
unsigned char i;
unsigned char duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned int k)
{
unsigned int j;
while(k--)
for(j=0;j<120;j++);
}
void main()
{
P3=0x00;
while(1)
{
if(key==0)
{
delay(10);
if(key==0)
{
while(!key);
led=!led; //红灯
}
}
if(key1==0)
{
delay(10);
if(key1==0)
{
while(!key1);
led1=!led1; //黄灯
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
while(!key2);
led2=!led2; //绿灯
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
while(!key3);
for(i=0;i<9;i++) // 红灯闪烁次数
{
P3=duanma[i];
delay(100);
led=0;
led1=1;
led2=1;
delay(100);
led=1;
}
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
while(!key4);
for(i=0;i<10;i++) // 黄灯闪烁次数
{
P3=duanma[i];
delay(100);
led=1;
led1=0;
led2=1;
delay(100);
led1=1;
}
}
}
if(key5==0)
{
delay(10);
if(key5==0)
{
while(!key5);
for(i=0;i<7;i++) // 绿灯闪烁次数
{
P3=duanma[i];
delay(100);
led=1;
led1=1;
led2=0;
delay(100);
led2=1;
}
}
}
}
}