#include <AT89X52.H>
unsigned char;
unsigned int;
unsigned int ct1 ,i,ct2,temp;
unsigned char sx1,sx2,sx3,sx4,sx5,sx6,gw,sw ,time;
bit falg,m0,m1,m2,m3,m4,m5,m6,m7,m10,m11,m12,m13,wb,t0,t2;
//P1 P3輸人 P0 P2輸出 地址
bdata char X66,X88,Y99,Y88;
unsigned char code LED[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,
~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};//共陽極
sbit Y0=P0^0;
sbit Y1=P0^1;
sbit Y2=P0^2;
sbit Y3=P0^3;
sbit Y4=P0^4;
sbit Y5=P0^5;
sbit Y6=P0^6;
sbit Y7=P0^7;
sbit Y10=P2^0;
sbit Y11=P2^1;
sbit Y12=P2^2;
sbit Y13=P2^3;
sbit Y14=P2^4;
sbit Y15=P2^5;
sbit Y16=P2^6;
sbit Y17=P2^7;
sbit X1=P1^1;
sbit X2=P1^2;
sbit X3=P1^3;
sbit X4=P1^4;
sbit X5=P1^5;
sbit X6=P1^6;
sbit X7=P1^7;
sbit X0=P3^0;
sbit X11=P3^1;//上升按鍵
sbit X12=P3^2;//下行按鍵
sbit X13=P3^3;//上限
sbit X14=P3^4;//下限
sbit X15=P3^5;//急停
sbit X16=P3^6;//光電
sbit X17=P3^7;//
sbit X10=P3^0;//
sbit x1=X66^1;
sbit x2=X66^2;
sbit x3=X66^3;
sbit x4=X66^4;
sbit x5=X66^5;
sbit x6=X66^6;
sbit x7=X66^7;
sbit x0=X66^0;
sbit x11=X88^1;//上升按鍵
sbit x12=X88^2;//下行按鍵
sbit x13=X88^3;//上限
sbit x14=X88^4;//下限
sbit x15=X88^5;//急停
sbit x16=X88^6;//光電
sbit x17=X88^7;//
sbit x10=X88^0;//
sbit y0=Y88^0;
sbit y1=Y88^1;
sbit y2=Y88^2;
sbit y3=Y88^3;
sbit y4=Y88^4;
sbit y5=Y88^5;
sbit y6=Y88^6;
sbit y7=Y88^7;
sbit y10=Y99^0;//上行
sbit y11=Y99^1;//下行
sbit y12=Y99^2;//運行
sbit y13=Y99^3;
sbit y14=Y99^4;
sbit y15=Y99^5;
sbit y16=Y99^6;
sbit y17=Y99^7;
void dey(unsigned int time);
void inio();
void uint();
void out();
void cl();
void sx();
void main()
{
uint();
while(1)
{
inio();
cl();
sx();
out();
}
}
void inio()
{
sx1=P1;
sx4=P3;
dey(1);
sx5=P3;
sx2=P1;
sx3=sx1^sx2;
sx6=sx4^sx5;
if(sx3|sx6==0)
{
X66=~sx2;//P1
X88=~sx5;//P3
}
}
void dey(unsigned int time)
{
unsigned int j;
for(time;time>0;time--)
{
for(j=0;j<11;j++);
}
}
void uint()
{
P1=0xff;
P3=0xff;
P0=0x00;
P2=0x00;
TMOD=0x01;
TH0=-(50000/256);
TL0=-(50000%256);
EA=1;
ET0=1;
TR0=1;
}
void out()
{
//P0=Y88;
P2=Y99;
}
T0_t0() interrupt 1 using 0
{
TH0=-(50000/256);
TL0=-(50000%256);
i++;
if(i==10)
{
i=0;
falg=1;
}
}
void cl()
{
y10=(y10|x11|x16)&((~x13)&(~x12)&(~y11)&(~x15));//上行
y11=(y11|x12|t2)&((~x14)&(~x11)&(~y10)&(~x15)&(~x16))&(~m12);//下行
if(y11){t2=0;}
y13=y10|y11;//剎車
y14=(y14|y13)&(~y15)&(~m12);//高
y15=(y15|t0)&(~x13)&(~x14);//低速
if(y14==0){t0=0;} //高速度運行時間
if (y14)//為了節省CPU掃描時間
{
m1=y14&(~m2);
m2=y14;//LDP
if (m1){ct2=0;i=0;}
if(falg){ct2++;falg=0;}
if(ct2==18){ct2=0;t0=1;}
}
if(x13==0){t2=0;}
if(x13)
{
m10=x13&(~m11);
m11=x13;
if(m10){ct1=0;i=0;}//上限
if(falg==1){falg=0;ct1++;}
if(ct1==12){ct1=0;t2=1;}
}
}
void sx()
{
if(x13){temp=ct1;}
if(y14){temp=ct2;}
P0=LED[temp/10];
Y16=1;//sw
dey(1);
Y16=0;
P0=LED[temp%10];
Y17=1;//gw
dey(1);
Y17=0;
}