基于51单片机的电梯控制系统
2、怎么样去获取到达平层的信息,我现在在protues仿真中用的是按键,手动去获取。
这是仿真的电路图
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define MAX_FLOOR 5//楼层高度
uchar count,count1;
uchar disnum,pwm1h,pwm2h,num,cycle,high,low;
uchar gf,b;//gf 表示目的层,b 为等待计数累加位
uint nf;//当前所在楼层
uint up_call[MAX_FLOOR],down_call[MAX_FLOOR],in_call[MAX_FLOOR];//存放呼叫事件
uchar code dis[]= {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//0-16 数码显示代码
sbit F5=P0^0; // 电梯内5 楼
sbit F4=P0^1; // 电梯内4 楼
sbit F3=P0^2; // 电梯内3 楼
sbit F2=P0^3; // 电梯内2 楼
sbit F1=P0^4; // 电梯内1 楼
sbit F1U=P0^5; //1 楼向上按钮
sbit F2U=P0^6; //2 楼向上按钮
sbit F2D=P0^7; //2 楼向下按钮
sbit F3U=P2^0; //3 楼向上按钮
sbit F3D=P2^1; //3 楼向下按钮
sbit F4U=P2^2; //4 楼向上按钮
sbit F4D=P2^3; //4 楼向下按钮
sbit F5D=P2^4; //5 楼向下按钮
sbit pwm1=P3^5; //正转控制位
sbit pwm2=P3^6; //反转控制位
sbit EN=P3^7; //L298 使能端信号
sbit YI= P3^0; //一楼霍尔输出信号
sbit ER=P3^1; //二楼霍尔输出信号
sbit SAN=P3^2; //三楼霍尔处输出信号
sbit SI=P3^3; //四楼霍尔处输出信号
sbit WU=P3^4; //五楼霍尔处输出信号
//sbit wela=P2^7; //数码管位选1
sbit RING=P2^7;
sbit U=P2^5; //上行指示灯
sbit D=P2^6; //下行指示灯
void init(); // 初始化程序
void check(); //霍尔查询楼层
void display(uchar); //显示函数
void floor(); //楼层控制函数
void downer(); //下降函数
void uper(); //上升函数
void stop(); //停车函数
void getinput(); //按键查询函数
//************************************
// 初始化函数
void init()
{
TMOD=0x11;
EA=1;
ET0=1;
ET1=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TR1=1;
nf=1;
pwm1=0;
pwm2=0;
U=0;
D=0;
EN=0;
YI=1;
ER=1;
SAN=1;
SI=1;
WU=1;
RING=0;
}
//******************************************
//1ms 延时程序
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
//**************************
// 停车报警等待程序
void stop()
{
U=0;
D=0;
EN=0;
pwm1=0;
pwm2=0;
while(b!=120)
{
b++;
if(b>30)
{RING=0;}
else
RING=1;
display(nf);
}
TR0=0;
RING=0;
b=0;
disnum=0;
}
//按键检测程序;
// *********************************
void getinput()
{
if(F5D==0)
{
down_call[4]=1;
}
else if(F4D==0)
{
down_call[3]=1;
}
else if(F4U==0)
{
up_call[3]=1;
}
else if(F3D==0)
{
down_call[2]=1;
}
else if(F3U==0)
{
up_call[2]=1;
}
else if(F2D==0)
{
down_call[1]=1;
}
else if(F2U==0)
{
up_call[1]=1;
}
else if(F1U==0)
{
up_call[0]=1;
}
else if(F1==0)
{
in_call[0]=1;
}
else if(F2==0)
{
in_call[1]=1;
}
else if(F3==0)
{
in_call[2]=1;
}
else if(F4==0)
{
in_call[3]=1;
}
else if(F5==0)
{
in_call[4]=1;
}
}
//**************************
//上升程序;
void uper()
{
TR0=1;
U=1;
if(nf==1&&gf==5)
{
getinput(); //电梯在一楼去往五楼查询是否有顺路乘坐
check();
if(up_call[1]||in_call[1])
{
for(high=50;high<=100;high++)
{
pwm2=0;
pwm1=1;
delay(high);
pwm1=0;
low=cycle-high;
delay(low);
}
if(high<=50)
{
for(high=50;high>=0;high--)
{
pwm2=0;
pwm1=1;
delay(high);
pwm1=0;
low=cycle-high;
delay(low);
}
}
up_call[1]=0;
in_call[1]=0;
nf=2;
ER=0;
display(nf);
stop(); //停车
}
if(up_call[2]||in_call[2])
{
for(high=50;high<=100;high++)
{
pwm2=0;
pwm1=1;
delay(high);
pwm1=0;
low=cycle-high;
delay(low);
}
if(high<=50)
{
for(high=50;high>=0;high--)
{
pwm2=0;
pwm1=1;
delay(high);
pwm1=0;
low=cycle-high;
delay(low);
}
}
up_call[2]=0;
in_call[2]=0;
nf=3;
SAN=0;
display(nf);
stop(); //停车
}
if(up_call[3]||in_call[3])
{
for(high=50;high<=100;high++)
{
pwm2=0;
pwm1=1;
delay(high);
pwm1=0;
low=cycle-high;
delay(low);
}
if(high<=50)
{
for(high=50;high>=0;high--)
{
pwm2=0;
pwm1=1;
delay(high);
pwm1=0;
low=cycle-high;
delay(low);
}
}
up_call[3]=0;
in_call[3]=0;
nf=4;
SI=0;
display(nf);
stop(); //停车
}
}
while(nf!=gf)
{
TR0=1;
U=1;
getinput();
check();
pwm1=1;
pwm2=0;
display(nf);
}
stop();
}
//**************************
//下降程序
void downer()
{
TR0=1;
D=1;
if(nf==5&&gf==1)
{
getinput();//电梯在5 楼去往1 楼查询是否有顺路
check();
if(down_call[3]==1||(in_call[3]==1))
{
for(high=50;high<=100;high++)
{
pwm1=0;
pwm2=1;
delay(high);
pwm2=0;
low=cycle-high;
delay(low);
}
if(high<=50)
{
for(high=50;high>=0;high--)
{
pwm1=0;
pwm2=1;
delay(high);
pwm2=0;
low=cycle-high;
delay(low);
}
}
down_call[3]=0;
in_call[3]=0;
nf=4;
SI=0;
display(nf);
stop();
}
if(down_call[2]==1||(in_call[2]==1))
{
for(high=50;high<=100;high++)
{
pwm1=0;
pwm2=1;
delay(high);
pwm2=0;
low=cycle-high;
delay(low);
}
if(high<=50)
{
for(high=50;high>=0;high--)
{
pwm1=0;
pwm2=1;
delay(high);
pwm2=0;
low=cycle-high;
delay(low);
}
}
down_call[2]=0;
in_call[2]=0;
nf=3;
SAN=0;
display(nf);
stop();
}
if(down_call[1]==1||(in_call[1]==1))
{
for(high=50;high<=100;high++)
{
pwm1=0;
pwm2=1;
delay(high);
pwm2=0;
low=cycle-high;
delay(low);
}
if(high<=50)
{
for(high=50;high>=0;high--)
{
pwm1=0;
pwm2=1;
delay(high);
pwm2=0;
low=cycle-high;
delay(low);
}
}
down_call[1]=0;
in_call[1]=0;
nf=2;
ER=0;
display(nf);
stop();
}
}
while(nf!=gf)
{
TR0=1;
D=1;
getinput();
check();
pwm2=1;
pwm1=0;
display(nf);
}
stop();
}
//**************************
//霍尔楼层查询
void check()
{
if(YI==0)
{
nf=1;
display(nf);
}
if(ER==0)
{
nf=2;
display(nf);
}
if(SAN==0)
{
nf=3;
display(nf);
}
if(SI==0)
{
nf=4;
display(nf);
}
if(WU==0)
{
nf=5;
display(nf);
}
}
//**************************
//电梯运行方式控制程序
void floor()
{
if(nf==1) // 电梯在一楼
{
if(down_call[4]||in_call[4]) //5 楼为目的层
{
gf=5;
uper();
down_call[4]=0;
in_call[4]=0;
}
else
if(down_call[3]||up_call[3]||in_call[3]) //4 楼为目的层
{
gf=4;
uper();
down_call[3]=0;
up_call[3]=0;
in_call[3]=0;
}
else
if(down_call[1]||up_call[1]||in_call[1]) //2 楼为目的层
{
gf=2;
uper();
down_call[1]=0;
up_call[1]=0;
in_call[1]=0;
}
else
if(down_call[2]||up_call[2]||in_call[2]) //3 楼为目的层
{
gf=3;
uper();
down_call[2]=0;
up_call[2]=0;
in_call[2]=0;
}
}
if(nf==2) //电梯在二楼
{
if(down_call[4]||in_call[4]) //5 楼为目的层
{
gf=5;
uper();
down_call[4]=0;
in_call[4]=0;
}
else
if(down_call[3]||up_call[3]||in_call[3]) //4 楼为目的层
{
gf=3;
uper();
down_call[3]=0;
up_call[3]=0;
in_call[3]=0;
}
else
if(down_call[2]||up_call[2]||in_call[2]) //3 楼为目的层
{
gf=3;
uper();
down_call[2]=0;
up_call[2]=0;
in_call[2]=0;
}
if(up_call[0]||in_call[0]) //1 楼为目的层
{
gf=1;
downer();
up_call[0]=0;
in_call[0]=0;
}
}
if(nf==3)//电梯在三楼
{
if(down_call[4]||in_call[4]) //5 楼为目的层
{
gf=5;
uper();
down_call[4]=0;
in_call[4]=0;
}
else
if(down_call[3]||up_call[3]||in_call[3]) //4 楼为目的层
{
gf=4;
uper();
down_call[3]=0;
up_call[3]=0;
in_call[3]=0;
}
else
if(down_call[1]||in_call[1]||up_call[1]) //2 楼为目的层
{
gf=2;
downer();
down_call[1]=0;
in_call[1]=0;
up_call[1]=0;
}
else
if(in_call[0]||up_call[0]) //1 楼为目的层
{
gf=1;
downer();
in_call[0]=0;
up_call[0]=0;
}
}
if(nf==4)//电梯在四楼
{
if(in_call[4]||down_call[4]) //5 楼为目的层
{
gf=5;
uper();
in_call[4]=0;
down_call[4]=0;
}
else
if(in_call[2]||up_call[2]||down_call[2]) //3 楼为目的层
{
gf=3;
downer();
in_call[2]=0;
up_call[2]=0;
down_call[2]=0;
}
else
if(down_call[1]||in_call[1]||up_call[1]) //2 楼为目的层
{
gf=2;
downer();
down_call[1]=0;
in_call[1]=0;
up_call[1]=0;
}
else
if(in_call[0]||up_call[0]) //1 楼为目的层
{
gf=1;
downer();
in_call[0]=0;
up_call[0]=0;
}
}
if(nf==5)//电梯在五楼
{
if(in_call[3]||up_call[3]||down_call[3]) //4 楼为目的层
{
gf=4;
downer();
in_call[3]=0;
up_call[3]=0;
down_call[3]=0;
}
else
if(in_call[2]||up_call[2]||down_call[2]) //3 楼为目的层
{
gf=3;
downer();
in_call[2]=0;
up_call[2]=0;
down_call[2]=0;
}
else
if(down_call[1]||in_call[1]||up_call[1]) //2 楼为目的层
{
gf=2;
downer();
down_call[1]=0;
in_call[1]=0;
up_call[1]=0;
}
else
if(in_call[0]||up_call[0]) //1 楼为目的层
{
gf=1;
downer();
in_call[0]=0;
up_call[0]=0;
}
}
}
//显示函数
//*************************************
void display(uchar nf)
{
P1=dis[nf];
delay(5);
}
//主函数
//**************************************
void main()
{
init();
getinput();
while(1)
{
getinput();
check();
floor();
display(nf);
}
}
//中断定时计数程序
//***********************************
void t0(void) interrupt 1 using 0
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
disnum++;
if(disnum==1000)
{
disnum=0;
}
}
}
//************************************
void t1(void) interrupt 3 using 1
{
TH1=(65536-100)/256;
TL1=(65536-100)%256;
count1++;
EN=1;
}
请将源程序发来一份 yongxi13@163.com
用PWM控制293模块啊
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
请问你到了几层是怎么判断的?单片机怎么知道它是到了4层而不是5层?