#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit K0=P2^0;
sbit K1=P2^1;
sbit K2=P2^2;
sbit K3=P2^3;
sbit K4=P2^4;
sbit K5=P2^5;
sbit K6=P2^6;
sbit K7=P2^7;
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
uchar flag0,flag1,flag2,flag3; //flag=0:????????flag=1:???????flag=2:???
char count0,count1,count2,count3;
uchar P0L,P0H,P3L,P3H;
/*
flag0 flag1 flag2 flag3
P0L P0H P3L P3H
count0 count1 count2 count3
K0=P1^0 K2=P1^2 K4=P1^4 K6=P1^6
K1=P1^1 K3=P1^3 K5=P1^5 K7=P1^7
*/
uchar code table[] ={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
void shuchu();
void shuru();
void xianshi();
void delay(uint x);
void main()
{
while(1)
{
shuru();
shuchu();
xianshi();
delay(7);
}
}
void shuru()
{
if(!((K0==0&&K1==0)||(K0==1&&K1==1)))
{
if(K0==0&&K1==1) flag0 =1;
else flag0=2;
}
else flag0=0;
if(!((K2==0&&K3==0)||(K2==1&&K3==1)))
{
if(K2==0&&K3==1) flag1 =1;
else flag1=2;
}
else flag1=0;
if(!((K4==0&&K5==0)||(K4==1&&K5==1)))
{
if(K4==0&&K5==1) flag2 =1;
else flag2=2;
}
else flag2=0;
if(!((K6==0&&K7==0)||(K6==1&&K7==1)))
{
if(K6==0&&K7==1) flag3 =1;
else flag3=2;
}
else flag3=0;
}
void shuchu()
{
if(flag0==1)
{
P0L=table[count0++];
P0=P0L+P0H*16;
}
if(flag0==2)
{
P0L=table[count0--];
P0=P0L+P0H*16;
}
if(flag1==1)
{
P0H=table[count1++];
P0=P0L+P0H*16;
}
if(flag1==2)
{
P0H=table[count1--];
P0=P0L+P0H*16;
}
if(flag2==1)
{
P3L=table[count2++];
P3=P3L+P3H*16;
}
if(flag2==2)
{
P3L=table[count2--];
P3=P3L+P3H*16;
}
if(flag3==1)
{
P3H=table[count3++];
P3=P3L+P3H*16;
}
if(flag3==2)
{
P3H=table[count3--];
P3=P3L+P3H*16;
}
if(count0>7) count0=0;
if(count0<0) count0=7;
if(count1>7) count1=0;
if(count1<0) count1=7;
if(count2>7) count2=0;
if(count2<0) count2=7;
if(count3>7) count3=0;
if(count3<0) count3=7;
}
void xianshi()
{
// if(flag0==0){LED0=0,LED1=0;}
// if(flag0==1){LED0=1,LED1=0;}
// if(flag0==2){LED0=0,LED1=1;}
// if(flag1==0){LED2=0,LED3=0;}
// if(flag1==1){LED2=1,LED3=0;}
// if(flag1==2){LED2=0,LED3=1;}
// if(flag2==0){LED4=0,LED5=0;}
// if(flag2==1){LED4=1,LED5=0;}
// if(flag2==2){LED4=0,LED5=1;}
// if(flag3==0){LED6=0,LED7=0;}
// if(flag3==1){LED6=1,LED7=0;}
// if(flag3==2){LED6=0,LED7=1;}
P1=P0L;
}
void delay(uint x)
{
uchar y;
for(x;x>0;x--)
for(y=115;y>0;y--);
}
|