下你所需,载你所想!
汇集开发技术源码资料

电梯(入门级实例代码)

:1.548KB :1 :2022-10-09 14:35:44

部分简介

电梯(入门级实例代码)如果开发者对于本文件有需要的可以参考。
#include
sbit LED=P2^0;
void delay(int i)
{
while(i--);
}
int shu=99;
int anxia[7]={0,0,0,0,0,0,0};
int time=0;
int times=0;
int zhuangtai=0;//电梯停滞状态
int dangqianlouceng=1;
int i=0,max=0,jixu=0;
int jishu;
int k;
int flag=0;
char code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void dingshi() //开启定时
{
EA=1;
TR0=1;
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
ET0=1;
}
void dings() interrupt 1
{
TH0=0XFC;
TL0=0X18;
time ;
if(time>1000)
{
time=0;
times ;
}
}

void anjianjiance()
{
int i;
int a=0;
P1=0x0f;
if(P1!=0x0f)
{
delay(100);
if(P1!=0x0f)
{
P1=0x0f;
switch(P1)
{
case 0x07:shu=0;break;
case 0x0b:shu=1;break;
case 0x0d:shu=2;break;
case 0x0e:shu=3;break;
}
P1=0xf0;
switch(P1)
{
case 0x70:shu=shu;break;
case 0xb0:shu=shu 4;break;
case 0xd0:shu=shu 8;break;
case 0xe0:shu=shu 12;break;
}
// while((a<50)&&(P1!=0xf0))
// {
// delay(1000);
// a ;
// }
while(P1!=0xf0)
{
P2=0;
P0=smgduan[dangqianlouceng];
delay(30);
P2=4;
P0=0;
P0=smgduan[zhuangtai];
delay(30);
P0=0;
for(i=0;i<6;i )//按键检测判断修改数组
{
if(shu==i&&shu<6)
{
shu=99;
if(anxia[i 1]==0)
{
anxia[i 1]=1;
}
else
{
for(k=1;k<=6;k )
{
if(anxia[k]==1)
jishu ;
}
if(jishu>1)
{
anxia[i 1]=0;
}
}
jishu=0;
}
}
for(i=1;i<7;i )//数码管显示
{
P2=(7-i 1)*4;
if(anxia[i]==1)
{
P0=0X00;
P0=smgduan[i];
delay(30);
P0=0;
}
else
{
P0=0X00;
}
delay(30);
}



if(zhuangtai==0)//电梯停止时
{

TR0=0;
times=0;
time=0;
if(flag==0)//每一次状态改变都要把按键清零
{
for(i=1;i<7;i )
{
anxia[i]=0;
}
flag=1;
}
for(i=1;i<7;i )//检测第一个按下的按键,给对应的状态
{
if(anxia[i]==1)
{
if(i>dangqianlouceng)
zhuangtai=1;
if(i zhuangtai=7;
flag=0;
}
}
anxia[dangqianlouceng]=0;

}
if(zhuangtai==1)//上升状态中
{
if(anxia[dangqianlouceng]==1)
{
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
for(i=6;i>dangqianlouceng;i--)
{
if(anxia[i]==1)
{
jixu=1;
break;
}
}
if(jixu==1)
{
dingshi();
jixu=0;
if(times>=3)
{
times=0;
dangqianlouceng ;
if(dangqianlouceng==6)
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
}
else
{
if(dangqianlouceng==6)//到顶清零
{
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
zhuangtai=0;
}
}
if(anxia[dangqianlouceng]==0)//路过未按下楼层
{
dingshi();
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
dingshi();
if(times>=2)
{
times=0;
dangqianlouceng ;
}
}
}
if(zhuangtai==7)//电梯下降状态
{
if(anxia[dangqianlouceng]==1)
{
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
for(i=1;i {
if(anxia[i]==1)
{
jixu=1;
break;
}
}
if(jixu==1)
{
dingshi();
jixu=0;
if(times>=3)
{
times=0;
dangqianlouceng--;
if(dangqianlouceng==1)
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
}
else
{
if(dangqianlouceng==1)
{
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
zhuangtai=0;
}
}
if(anxia[dangqianlouceng]==0)//下降中遇到没有按下的楼层
{
dingshi();
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
dingshi();
if(times>=2)
{
times=0;
dangqianlouceng--;
}
}
}
if(zhuangtai==1)
{
for(i=dangqianlouceng;i<=6;i )
{
if(anxia[i]==1)
{
jishu ;
}

}
if(jishu==0)
{
zhuangtai=0;
TR0=0;
time=0;
times=0;
}
jishu=0;
}
if(zhuangtai==7)
{
for(i=dangqianlouceng;i>=1;i--)
{
if(anxia[i]==1)
{
jishu ;
}

}
if(jishu==0)
{
zhuangtai=0;
TR0=0;
time=0;
times=0;
}
jishu=0;
}








}
}
}

}
void main()
{
// 1 上升 7 下降 0停止
while(1)
{
P0=0;
P2=0;
P0=smgduan[dangqianlouceng];
delay(30);
P2=4;
P0=0;
P0=smgduan[zhuangtai];
delay(30);
P0=0;
anjianjiance();
if(dangqianlouceng<1||dangqianlouceng>6)//防止飞天遁地
{
if(dangqianlouceng>6)
dangqianlouceng=6;
if(dangqianlouceng<1)
dangqianlouceng=1;
}
for(i=0;i<6;i )//按键检测判断修改数组
{
if(shu==i&&shu<6)
{
shu=99;
if(anxia[i 1]==0)
{
anxia[i 1]=1;
}
else
{
for(k=1;k<=6;k )
{
if(anxia[k]==1)
jishu ;
}
if(jishu>1)
{
anxia[i 1]=0;
}
}
jishu=0;
}
}
for(i=1;i<7;i )//数码管显示
{
P2=(7-i 1)*4;
if(anxia[i]==1)
{
P0=0X00;
P0=smgduan[i];
delay(30);
P0=0;
}
else
{
P0=0X00;
}
delay(30);
}
if(zhuangtai==0)//电梯停止时
{

TR0=0;
times=0;
time=0;
if(flag==0)//每一次状态改变都要把按键清零
{
for(i=1;i<7;i )
{
anxia[i]=0;
}
flag=1;
}
for(i=1;i<7;i )//检测第一个按下的按键,给对应的状态
{
if(anxia[i]==1)
{
if(i>dangqianlouceng)
zhuangtai=1;
if(i zhuangtai=7;
flag=0;
}
}
anxia[dangqianlouceng]=0;

}
if(zhuangtai==1)//上升状态中
{
if(anxia[dangqianlouceng]==1)
{
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
for(i=6;i>dangqianlouceng;i--)
{
if(anxia[i]==1)
{
jixu=1;
break;
}
}
if(jixu==1)
{
dingshi();
jixu=0;
if(times>=3)
{
times=0;
dangqianlouceng ;
if(dangqianlouceng==6)
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
}
else
{
if(dangqianlouceng==6)//到顶清零
{
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
zhuangtai=0;
}
}
if(anxia[dangqianlouceng]==0)//路过未按下楼层
{
dingshi();
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
dingshi();
if(times>=2)
{
times=0;
dangqianlouceng ;
}
}
}
if(zhuangtai==7)//电梯下降状态
{
if(anxia[dangqianlouceng]==1)
{
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
for(i=1;i {
if(anxia[i]==1)
{
jixu=1;
break;
}
}
if(jixu==1)
{
dingshi();
jixu=0;
if(times>=3)
{
times=0;
dangqianlouceng--;
if(dangqianlouceng==1)
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
}
else
{
if(dangqianlouceng==1)
{
for(i=1;i<=6;i )
{
anxia[i]=0;
}
}
zhuangtai=0;
}
}
if(anxia[dangqianlouceng]==0)//下降中遇到没有按下的楼层
{
dingshi();
P2=(7-dangqianlouceng 1)*4;
P0=0X00;
anxia[dangqianlouceng]=0;
dingshi();
if(times>=2)
{
times=0;
dangqianlouceng--;
}
}
}
if(zhuangtai==1)
{
for(i=dangqianlouceng;i<=6;i )
{
if(anxia[i]==1)
{
jishu ;
}

}
if(jishu==0)
{
zhuangtai=0;
TR0=0;
time=0;
times=0;
}
jishu=0;
}
if(zhuangtai==7)
{
for(i=dangqianlouceng;i>=1;i--)
{
if(anxia[i]==1)
{
jishu ;
}

}
if(jishu==0)
{
zhuangtai=0;
TR0=0;
time=0;
times=0;
}
jishu=0;
}
}
}

 

热门推荐

相关文章