sbit smg1=P2^0;//數(shù)碼管
sbit smg2=P2^1;
sbit smg3=P2^2;
sbit smg4=P2^3;
sbit smg5=P2^4;
sbit smg6=P2^5;
sbit led1=P2^6;//LED
sbit beep=P2^7;//蜂鳴器
//數(shù)碼管編碼
uchar code smgduan[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uchar time=0,mode=0;//系統(tǒng)變量
uchar flag1[]={0,0,0,0,0,0};//上升
uchar flag2[]={0,0,0,0,0,0};//下降
uchar flag3[]={0,0,0,0,0,0};
uchar louceng=1,next=1;//樓層
uchar fang=0;//方向,0停止,1上,2下
//函數(shù)聲明
uchar key_scan();
//延時
void delay(uint i)
{
while(i--);
}
//顯示樓層
void disp()
{
uchar i=0;
i=key_scan();//按鍵檢測
switch(i)
{
case 1:flag1[0]=1;break;
case 2:flag1[1]=1;break;
case 3:flag2[1]=1;break;
case 4:flag1[2]=1;break;
case 5:flag2[2]=1;break;
case 6:flag1[3]=1;break;
case 7:flag2[3]=1;break;
case 8:flag1[4]=1;break;
case 9:flag2[4]=1;break;
case 10:flag2[5]=1;break;
case 11:flag3[0]=1;break;
case 12:flag3[1]=1;break;
case 13:flag3[2]=1;break;
case 14:flag3[3]=1;break;
case 15:flag3[4]=1;break;
case 16:flag3[5]=1;
}
P0=smgduan[louceng];//當前樓層
smg1=1;
delay(100);
smg1=0;
//目標樓層
if(fang>0)
{
P0=smgduan[next];
smg3=1;
delay(100);
smg3=0;
}
switch(fang)
{
case 0:P0=0x8c;break;//上升下降
case 1:P0=0xc1;break;
case 2:P0=0xa1;
}
smg5=1;
delay(100);
smg5=0;
switch(fang)
{
case 0:P0=0x8c;break;//上升下降
case 1:P0=0x8c;break;
case 2:P0=0xab;
}
smg6=1;
delay(100);
smg6=0;
}
//開門控制
void open()
{
led1=0;//開門
time=20;
beep=0;
while(time>0)
{
disp();
}
beep=1;
time=80;
while(time>0)
{
disp();
}
led1=1;
}
uchar key=0;
uchar key_scan()//按鍵檢測
{
uchar i;
i=0;
P1=0x7f;
switch(P1)//檢測第1行
{
case 0x7e:i=4;break;
case 0x7d:i=3;break;
case 0x7b:i=2;break;
case 0x77:i=1;
}
P1=0xBf;
switch(P1)//檢測第2行
{
case 0xBe:i=8;break;
case 0xBd:i=7;break;
case 0xBb:i=6;break;
case 0xB7:i=5;
}
P1=0xDf;
switch(P1)//檢測第3行
{
case 0xDe:i=12;break;
case 0xDd:i=11;break;
case 0xDb:i=10;break;
case 0xD7:i=9;
}
P1=0xEf;
switch(P1)//檢測第4行
{
case 0xEe:i=16;break;
case 0xEd:i=15;break;
case 0xEb:i=14;break;
case 0xE7:i=13;
}
if(key==i)
return 0;
else
{
key=i;
return(key);
}
}
//串口發(fā)送
void send(uchar i)
{
SBUF=i;
while(!TI);
TI=0;
}
//發(fā)送字符串
void fa(uchar *p)
{
while(*p!='?')
{
send(*p);
p++;
}
}
void main() //主函數(shù)
{
uchar i=0,j=0;
smg1=0;smg2=0;smg3=0;smg4=0;smg5=0;smg6=0;
閱讀全文