#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
const tab1[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
const tab2[]={0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00};
const tab3[]={0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00};
const tab4[]={0xFF,0xFE,0xFA,0xEA,0xAA,0x2A,0x0A,0x02,0x00}; //1,3,5,7
const tab5[]={0xFF,0x7F,0x5F,0x57,0x55,0x54,0x50,0x40,0x00}; //2,4,6,8
uchar key=0;
uchar state=0;
uchar stateTemp=0;
uchar flag=0;
void delay(uint n)//延时
{ uint i;
for(i=0;i<n;i++)
;
}
void fun1(void)
{
P0=0x00;
P2=0x00;
delay(30000); // fun1全亮
P0=0xff;
P2=0xff;
delay(30000);
}
void fun2(void)
{
uchar i;
for(i=0;i<8;i++) //向上逐次单个点亮
{
P2=tab1[7-i];
P2=tab1[7-i];
delay(10000);
if(state!=2)
{
P0=0xFF;
P2=0xFF;
return;
}
}
P2=0xFF;
for(i=0;i<8;i++)
{
P0=tab1[7-i];
delay(10000);
if(state!=2)
{
P0=0xFF;
P2=0xFF;
return;
}
}
P0=0xFF;
for(i=0;i<8;i++) //向下逐次单个点亮
{
P0=tab1[i];
delay(10000);
if(state!=2)
{
P0=0xFF;
P2=0xFF;
return;
}
}
P0=0xFF;
for(i=0;i<8;i++)
{
P2=tab1[i];
delay(10000);
if(state!=2)
{
P0=0xFF;
P2=0xFF;
return;
}
}
P2=0xFF;
}
void fun3(void)
{
uchar i;
for(i=0;i<9;i++)
{
P0=tab4[i];
P2=tab5[i];
delay(25000);
if(state!=3)
{
P0=0xFF;
P2=0xFF;
return;
}
}
for(i=0;i<3;i++)
{
P0=0xAA;
P2=0xAA;
delay(20000);
P0=0x55;
P2=0x55;
delay(20000);
if(state!=3)
{
P0=0xFF;
P2=0xFF;
return;
}
}
for(i=0;i<9;i++)
{
P0=tab4[8-i];
P2=tab5[8-i];
delay(25000);
if(state!=3)
{
P0=0xFF;
P2=0xFF;
return;
}
}
}
void fun4(void)
{
uchar i;
for(i=0;i<9;i++) //向上逐个点亮
{
P2=tab3[i];
delay(10000);
if(state!=4)
{
P0=0xFF;
P2=0xFF;
return;
}
}
for(i=0;i<9;i++)
{
P0=tab3[i];
delay(10000);
if(state!=4)
{
P0=0xFF;
P2=0xFF;
return;
}
}
for(i=0;i<9;i++) //向下逐个熄灭
{
P0=tab3[8-i];
delay(10000);
if(state!=4)
{
P0=0xFF;
P2=0xFF;
return;
}
}
P0=0xFF;
for(i=0;i<9;i++)
{
P2=tab3[8-i];
delay(10000);
if(state!=4)
{
P0=0xFF;
P2=0xFF;
return;
}
}
P2=0xFF;
for(i=0;i<9;i++) //向下逐个点亮
{ P0=tab2[i];
delay(10000);
if(state!=4)
{
P0=0xFF;
P2=0xFF;
return;
}
}
for(i=0;i<9;i++)
{
P2=tab2[i];
delay(10000);
if(state!=4)
{
P0=0xFF;
P2=0xFF;
return;
}
}
for(i=0;i<9;i++) //向上逐个熄灭
{ P2=tab2[8-i]; delay(10000);
if(state!=4)
{
P0=0xFF; P2=0xFF; return;
}
}
P2=0xFF;
for(i=0;i<9;i++)
{ P0=tab2[8-i]; delay(10000);
if(state!=4)
{ P0=0xFF; P2=0xFF; return;
}
}
P0=0xFF;
}
void timer0(void)interrupt 1 using 1
{ TL0 = 0x00; //重置初值
TH0 = 0xBE;
TL0 = 0xE5;
key=P1;
switch(key)
{
case 0xF6:state=1;stateTemp=1;break;
case 0xFD:state=2;stateTemp=2;break;
case 0xFB:state=3;stateTemp=3;break;
//case 0xF7:state=4;stateTemp=4;break;
default:break;
}
}
void _int()interrupt 2 using 2
{
if(flag==0)
{state=5; flag|=1; }
else
{ state=stateTemp; flag&=0; }
}
void main()
{
IT1=1;
IE|=0x86;
TMOD= 0x21;
TH0 = 0xBE;
TL0 = 0xE5;
ES = 0; //禁止串口中断
TR0 = 1; //开启定时器0,开始定时
while(1)
{
switch(state)
{
case 1:fun1();break;
case 2:fun2();break;
case 3:fun3();break;
case 4:fun4();break;
case 5:P0=0x00;P2=0x00;break;//中断全亮
default: break;//fun1();fun2();fun3();fun4();break;
}
}
}
|