|
#include"reg52.h"
#include"dj.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit cg1x=P3^0;
sbit cg1s=P3^1;
sbit cg2x=P3^2;
sbit cg2s=P3^3;
sbit cg3x=P3^4;
sbit cg3s=P3^5;
sbit cg4x=P3^6;
sbit cg4s=P3^7;//傳感器
sbit beep=P2^4;
//sbit PWM=P2^2;//舵
sbit in0=P2^0;
sbit in1=P2^1;//電機 01上 10下
sbit wx2=P2^3;//數(shù)碼管位選
#define PS P1//實物數(shù)碼管
#define PJ P0//實物矩陣
u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
static u16 bz=0,djzt=0,zt=2,shu=99,nei[4],shang[3],xia[3],szlc=1,jimiao=0,i,k,daodapanduan=0,kaishipanduan=0,max00,max11,max0,min0,min00,min11,zuida=0,zuixiao=5,zuihou,chu=1,qi=0;
u8 ding,dong,flag,stop;
u16 n;
void xunhuan(u16 i)
{
while(i--);
}
void saomiao() //按鍵掃描
{
PJ=0x0f;
if(PJ!=0x0f)
{
xunhuan(1000);
if(PJ!=0x0f)
{
PJ=0x0f;
switch(PJ)
{
case 0x07:shu=0;break;
case 0x0b:shu=1;break;
case 0x0d:shu=2;break;
case 0x0e:shu=3;break;
}
PJ=0xf0;
switch(PJ)
{
case 0x70:shu=shu;break;
case 0xb0:shu=shu+4;break;
case 0xd0:shu=shu+8;break;
case 0xe0:shu=shu+12;break;
}
}
}
}
void panduan() //按鍵判斷
{
switch(shu)
{case 0:
if(nei[0]==0)
nei[0]=1;
else
nei[0]=0;
break;
case 1:
if(nei[1]==0)
nei[1]=2;
else
nei[1]=0;
break;
case 2:
if(nei[2]==0)
nei[2]=3;
else
nei[2]=0;
break;
case 3:
if(nei[3]==0)
nei[3]=4;
else
nei[3]=0;
break;
case 4:
if(shang[0]==0)
shang[0]=1;
else
shang[0]=0;
break;
case 5:
if(shang[1]==0)
shang[1]=2;
else
shang[1]=0;
break;
case 6:
if(shang[2]==0)
shang[2]=3;
else
shang[2]=0;
break;
case 9:
if(xia[0]==0)
xia[0]=2;
else
xia[0]=0;
break;
case 10:
if(xia[1]==0)
xia[1]=3;
else
xia[1]=0;
break;
case 11:
if(xia[2]==0)
xia[2]=4;
else
xia[2]=0;
break;
}
shu=99;
}
void sjk() //數(shù)據(jù)庫建立
{
saomiao();
panduan();
xunhuan(30000);
}
void shumaguan() //數(shù)碼管顯示
{
wx2=0;
PS=smgduan[szlc];
// xunhuan(10000);
// LSA=0;
// LSB=1;
// LSC=1;
// P0=smgduan[zt];
// xunhuan(10000);
// LSA=0;
// LSB=0;
// LSC=1;
// P0=smgduan[zuida];
// xunhuan(10000);
// LSA=0;
// LSB=0;
// LSC=0;
// P0=smgduan[zuixiao];
// xunhuan(10000);
// LSA=0;
// LSB=1;
// LSC=0;
// P0=smgduan[chu];
// xunhuan(1000);
}
void dingshi() //開啟定時
{
EA=1;
TR0=1;
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
ET0=1;
}
void dingshi_() interrupt 1 //延時函數(shù)
{
TH0=0XFC;
TL0=0X18;
i++;
}
void tzjs() //停止計時
{
TR0=0;
i=0;
}
void sxys() //電梯停止后開啟延時,延時兩秒繼續(xù)原來的狀態(tài)
{
if(i>=2000)
{
if(zt==0)
{
in0=0;
in1=1;
}
if(zt==1)
{
in0=1;
in1=0;
}
tzjs();
i=0;
djzt=0;
}
}
void min() ////zt=0,取需要到達的最小樓層,判斷電梯狀態(tài)是否需要改變
{
min0=5;
for(k=0;k<4;k++)
{
if(nei[k]!=0&&nei[k]<min0&&nei[k]>=szlc)
{
min0=nei[k];
}
}
for(k=0;k<3;k++)
{
if(shang[k]!=0&&shang[k]<min0&&shang[k]>=szlc)
{
min0=shang[k];
}
}
if(min0==5)
{
min0=0;///////////
for(k=0;k<3;k++)
{
if(xia[k]>=szlc&&xia[k]>min0)
{
min0=xia[k];
if(min0!=0)
{
qi=1;
}
}
}
}
if(min0==0)
{
zt=2;
chu=0;
tzjs();
}
}
void max() ////zt=0
{
max0=0;
for(k=0;k<4;k++)
{
if(nei[k]!=0&&nei[k]>max0&&nei[k]<=szlc)
{
max0=nei[k];
}
}
for(k=0;k<3;k++)
{
if(xia[k]!=0&&xia[k]>max0&&xia[k]<=szlc)
{
max0=xia[k];
}
}
if(max0==0)
{
max0=5;///////////
for(k=0;k<3;k++)
{
if(shang[k]<=szlc&&shang[k]!=0&&shang[k]<max0)
{
max0=shang[k];
if(max0!=5)
{
qi=1;
}
}
}
}
if(max0==5)
{
zt=2;
chu=1;
tzjs();
}
}
void shanglou() ///上樓中的燈光及到達樓層判斷,改變daodapanduan值
{
if(szlc==min0)
{
in0=0;
in1=0;
djzt=1;
TR2=1;
ET2=1; //打開定時器0
while(stop);
dingshi();
//TR2=1;
//ET2=1; //打開定時器2
//while(stop);
TR1=1;
timer1 =15;//舵機90°
count=0;//定時器重新計數(shù)
xunhuan(100000);
timer1 =5;//旋轉(zhuǎn)0°
count=0;
xunhuan(100000);
TR1=0;
//TR2=1;
//ET2=1; //打開定時器2
//while(stop);
zuixiao=5;
nei[szlc-1]=0;
shang[szlc-1]=0;
if(qi==1)
{
xia[szlc-2]=0;
qi=0;
}
}
}
void xialou() ///上樓中的燈光及到達樓層判斷,改變daodapanduan值
{
if(szlc==max0)
{
in0=0;
in1=0;
djzt=1;
TR2=1;
ET2=1;
while(stop);
dingshi();
//TR2=1;
//ET2=1; //打開定時器0
//while(stop);
TR1=1;
timer1 =15;//舵機90°
count=0;//定時器重新計數(shù)
xunhuan(100000);
timer1 =5;//旋轉(zhuǎn)0°
count=0;
xunhuan(100000);
TR1=0;
//TR2=1;
//ET2=1; //打開定時器2
//while(stop);
zuida=0;
nei[szlc-1]=0;
xia[szlc-2]=0;
if(qi==1)
{
shang[szlc-1]=0;
qi=0;
}
}
}
void zuida2()////////////////zt=2
{
zuida=0;
for(k=0;k<=3;k++)
{
if(nei[k]>zuida&&nei[k]!=0)
zuida=nei[k];
}
for(k=0;k<=2;k++)
{
if(shang[k]>zuida&&shang[k]!=0)
zuida=shang[k];
}
for(k=0;k<=2;k++)
{
if(xia[k]>zuida&&xia[k]!=0)
zuida=xia[k];
}
}
void zuixiao2()/////////zt=2
{
for(k=0;k<=3;k++)
{
if(nei[k]<zuixiao&&nei[k]!=0)
zuixiao=nei[k];
}
for(k=0;k<=2;k++)
{
if(shang[k]<zuixiao&&shang[k]!=0)
zuixiao=shang[k];
}
for(k=0;k<=2;k++)
{
if(xia[k]<zuixiao&&xia[k]!=0)
zuixiao=xia[k];
}
}
void jiancelouceng()
{
if(cg1x==0&&cg1s==0)
szlc=1;
if(cg2x==0&&cg2s==0)
szlc=2;
if(cg3x==0&&cg3s==0)
szlc=3;
if(cg4x==0&&cg4s==0)
szlc=4;
}
void time2init() //定時器2初始化
{
CP_RL2=0;
TCLK=0;
RCLK=0;
C_T2=0;
TF2=0;
RCAP2L=0X06;
RCAP2H=0XFF;
//TR2=1;
ET2=1;
EA=1;
}
void biaohaoinit() //各個標(biāo)號初始化
{
ding=0; //叮聲音 計數(shù)標(biāo)志
dong=0; //咚聲音 計數(shù)標(biāo)志
n=0; //定時0.5s標(biāo)志
flag=0;
stop=0; //結(jié)束標(biāo)志
}
void time2() interrupt 5
{
RCAP2H=0XFF;
RCAP2L=0X06;
n++;
if(n==5000)
{
n=0;
if(flag==0)
{
flag=~flag;
}
else
{
flag=0;
stop=~stop;
TR2=0;
ET2=0; //關(guān)閉定時器2
}
}
if(flag==0)
{ //通過改變定時計數(shù)時間可以改變門鈴的聲音
ding++; //叮
if(ding==1)
{
ding=0;
beep=~beep;
}
}
else
{
dong++;
if(dong==2) //咚
{
dong=0;
beep=~beep;
}
}
}
void main() //主函數(shù)
{
Timer1_Init();
time2init();
biaohaoinit();
while(1)
{
jiancelouceng();
shumaguan();
if(zt==2)///////////狀態(tài)為2停止
{
in0=0;
in1=0;
//電機狀態(tài)停止
sjk();
if(chu==0)////////////上到最高
{
while(1)
{
in0=0;
in1=0;
jiancelouceng();
shumaguan();
zuida=0;
sjk();
zuida2();
if(zuida!=0)
{
if(zuida>szlc)
{
zt=0;
djzt=0;
break;
}
if(zuida<szlc)
{
zt=1;
djzt=0;
break;
}
}
if(zuida==szlc)
{
TR1=1;
timer1 =15;//舵機90°
count=0;//定時器重新計數(shù)
xunhuan(100000);
timer1 =5;//旋轉(zhuǎn)0°
count=0;
xunhuan(100000);
TR1=0;
shang[szlc-1]=0;
xia[szlc-2]=0;
nei[szlc-1]=0;
break;
}
}
}
if(chu==1)
{
while(1)
{
in0=0;
in1=0;
jiancelouceng();
shumaguan();
zuixiao=5;
sjk();
zuixiao2();
if(zuixiao!=5)
{
if(zuixiao>szlc)
{
zt=0;
djzt=0;
break;
}
if(zuixiao<szlc)
{
zt=1;
djzt=0;
break;
}
}
if(zuixiao==szlc)
{
TR1=1;
timer1 =15;//舵機90°
count=0;//定時器重新計數(shù)
xunhuan(100000);
timer1 =5;//旋轉(zhuǎn)0°
count=0;
xunhuan(100000);
TR1=0;
shang[szlc-1]=0;
xia[szlc-2]=0;
nei[szlc-1]=0;
break;
}
}
}
}
if(zt==0)
{ if(djzt==0)
{in0=0;
in1=1;}//電機正轉(zhuǎn)
sjk();
//若daodapanduan!=0,根據(jù)電梯狀態(tài)賦值給K
min();
jiancelouceng();
shanglou();
sxys();
}
if(zt==1)
{if(djzt==0)
{in0=1;
in1=0;}
sjk();
//若daodapanduan!=0,根據(jù)電梯狀態(tài)賦值給K
max();
xialou();
sxys();
}
}
}
|
|