用verilog HDL语言编写一个六层电梯控制运行 波形仿真不出啊
eda吧
全部回复
仅看楼主
level 1
用verilog HDL语言编写一个六层电梯控制运行 波形仿真不出啊
module elevator_controller(//input ports:
cp_50M,clk,reset,forbid,
call_up_1, call_up_2, call_up_3,
call_up_4, call_up_5,
call_down_2, call_down_3, call_down_4,
call_down_5,call_down_6,
request_1, request_2, request_3,
request_4, request_5, request_6,
//output ports:
out,LiftState,over_alarm,count_out,count
);
//output ports:
output [6:0]out;//输出楼层
output [6:0] LiftState;//输出电梯状态
output over_alarm;//出错
output [6:0]count_out;//计数器输出,便于我控制开关门的状态显示
output [2:0]count;//计数器
//input ports:
input cp_50M;
input clk,reset,call_up_1,call_up_2,call_up_3,call_up_4,call_up_5,call_down_2,call_down_3,call_down_4,call_down_5,call_down_6,request_1,request_2,request_3,request_4,request_5,request_6;
input forbid;
//pos与Posout的关系:当前状态一改变,pos立即作相应改变,而PosOut则是当前状态在要变到下一状态的瞬间
//,将PosOut作相应改变。如从WAIT切换到第一个UP时,pos立即加一,而PosOut则要等到第一个UP
//即将切换到第二个UP时,才加一。
//这个说的简单一点,就是移位寄存器,便于对电梯状态的实时的控制
wire over_alarm;
wire [6:0]out;
wire [6:0]count_out;
reg [5:0]pos,PosOut;
reg [2:0] count;//门开后要持续5个时钟周期,用count来计数
reg DoorFlag;
reg [1:0] UpDnFlag;
reg [6:0] LiftState,NextState;//分别表示电梯的当前状态和下一状态
wire real_clk;
//up_all,down_all,request_all分别用来合并向上请求的各信号,向下请求的各信号和停靠请求的各信号
//以便判断电梯在状态间如何转移时更简洁
reg [5:0] up_all,down_all,request_all;
//定义电梯7种状态的符号常量
parameter WAIT=7'b0000001, UP=7'b0000010, DOWN=7'b0000100, UPSTOP=7'b0001000
, DOWNSTOP=7'b0010000, OPENDOOR=7'b0100000, CLOSEDOOR=7'b1000000;
//定义楼层的符号常量
parameter FLOOR1=6'b000001, FLOOR2=6'b000010, FLOOR3=6'b000100, FLOOR4=6'b001000, FLOOR5=6'b010000, FLOOR6=6'b100000;
parameter TRUE=1'b1, FALSE=1'b0;
//定义门打开和门关闭的符号常量
parameter OPEN=1'b1, CLOSED=1'b0;
//定义电梯上升,下降和静止的符号常量
parameter UPFLAG=2'b01,DNFLAG=2'b10,STATIC=2'b00;
assign real_clk=(clk&(~forbid));
//采用屏蔽时钟的方法来实现“电梯强制禁止运行”。方法虽然违反了“同步电路最好
//没有时钟门控”的要求,但是在此只要“负面影响”不是很大还是可以接受的。
//将各上升请求信号实时地合并(6楼为顶层,无上升请求,考虑到通用性,将第4位填零)
always @(call_up_1 or call_up_2 or call_up_3 or call_up_4 or call_up_5)
up_all={1'b0, call_up_5, call_up_4, call_up_3, call_up_2, call_up_1};
//将各下降请求信号实时地合并(1楼为底层,无下降请求,考虑到通用性,将第1位填零)
always @(call_down_2 or call_down_3 or call_down_4 or call_down_5 or call_down_6)
down_all={call_down_6, call_down_5, call_down_4, call_down_3, call_down_2, 1'b0};
//将各停靠请求信号实时地合并
always @(request_1 or request_2 or request_3 or request_4 or request_5 or request_6)
request_all={request_6, request_5, request_4, request_3, request_2, request_1};
always @(posedge real_clk or posedge reset)
if(reset)
count<=0;
else if((NextState==OPENDOOR)&&(count<5))//or else if(NextState==OPENDOOR)
count<=count+1;//如果计数不到5且下一状态为OPENDOOR的话,继续计数
else
count<=0;//清零,以备下次计数
//display which floor the elevator is....
display_decode h1(cp_50M,PosOut,out,over_alarm);
display_decode_count h2(cp_50M,count,count_out);
// finite state machine in the style of three segments ,三段式
//1st StateShift always block,sequential state transition,有限状态机第一段
always @(posedge real_clk or posedge reset)
if(reset)
begin
LiftState<=WAIT;
end
else
LiftState<=NextState;
//2nd StateShift always block,combinational condition judgement,有限状态机第二段
always @(LiftState or up_all or down_all or request_all or pos or count or UpDnFlag)
case(LiftState)
WAIT:
begin
if(request_all>0)//有停靠请求否
begin
if((request_all&pos)>0)//停靠请求中有当前楼层停靠请求否
NextState=OPENDOOR;//有当前楼层请求,则下一状态转开门
else if(request_all>pos)//有当前楼层之上的停靠请求否(优先向上的请求)
NextState=UP;//有则,下一状态转上升
else
NextState=DOWN;//只有当前楼层之下的停靠请求,下一状态转下降
end
else if((up_all&pos)||(down_all&pos))//上下请求中有当前楼层请求否
begin
NextState=OPENDOOR;
end
else if((up_all>pos)||(down_all>pos))//上下请求中有当前楼层之上的请求否
NextState=UP;
else if(up_all||down_all)//上下请求中有当前楼层之下的请求否
NextState=DOWN;
else
NextState=WAIT;//无任何请求,继续处于WAIT模式
end
UP:
begin
if((request_all&pos)||(up_all&pos))//停靠或上升请求中有当前楼层的请求否
NextState=UPSTOP;//有,下一状态转为UPSTOP(停靠后要1s才开门,UPSTOP即为这1s的过渡期)
else if((request_all>pos)||(up_all>pos))//停靠或上升请求中有当前楼层之上的请求否
NextState=UP;
else if(down_all>0)//有下降请求否
begin
if((down_all>pos)&&((down_all^pos)>pos))//下降请求中有当前楼层的请求否
NextState=UP;
else if((down_all&pos)||(pos<FLOOR6)) //其实这样做意义不大,只是使上下标志提前变化
NextState=UPSTOP;
else if((down_all&pos)&&(pos==FLOOR6))//事实上不满足第一条件的话,电梯根本不会到达第四层
NextState=DOWNSTOP;
else//下降请求中只有当前楼层之下的请求
NextState=DOWN;
end
else if(request_all||up_all)//只有当前楼层之上的停靠或上升请求否
NextState=DOWN;
else
NextState=WAIT;//无任何请求,转为WAIT模式
end
//DOWN的状态转移请参见UP的状态转移
DOWN:
begin
if((request_all&pos)||(down_all&pos))
NextState=DOWNSTOP;
else if(((request_all&FLOOR1)<pos&&(request_all&FLOOR1))||((request_all&FLOOR2)<pos&&(request_all&FLOOR2))||((request_all&FLOOR3)<pos&&(request_all&FLOOR3))||((request_all&FLOOR4)<pos&&(request_all&FLOOR4))||((request_all&FLOOR5)<pos&&(request_all&FLOOR5))||((request_all&FLOOR6)<pos&&(request_all&FLOOR6)))
NextState=DOWN;
else if(((down_all&FLOOR1)<pos&&(down_all&FLOOR1))||((down_all&FLOOR2)<pos&&(down_all&FLOOR2))||((down_all&FLOOR3)<pos&&(down_all&FLOOR3))||((down_all&FLOOR4)<pos&&(down_all&FLOOR4))||((down_all&FLOOR5)<pos&&(down_all&FLOOR5))||((down_all&FLOOR6)<pos&&(down_all&FLOOR6)))
NextState=DOWN;
else if(up_all>0)
begin
if(((up_all&FLOOR1)<pos&&(up_all&FLOOR1))||((up_all&FLOOR2)<pos&&(up_all&FLOOR2))||((up_all&FLOOR3)<pos&&(up_all&FLOOR3))||((up_all&FLOOR4)<pos&&(up_all&FLOOR4))||((up_all&FLOOR5)<pos&&(up_all&FLOOR5))||((up_all&FLOOR6)<pos&&(up_all&FLOOR6)))
NextState=DOWN;
else if((up_all&pos)&&(pos>FLOOR1))
NextState=DOWNSTOP;
else if((up_all&pos)&&(pos==FLOOR1))
NextState=UPSTOP;
else
NextState=UP;
end
else if(request_all||down_all)
NextState=UP;
else
NextState=WAIT;
end
UPSTOP:
begin
NextState=OPENDOOR;//停靠1时钟周期后开门
end
DOWNSTOP:
begin
NextState=OPENDOOR;//停靠1时钟周期后开门
end
OPENDOOR:
begin
if(count<5)//开门不足5周期,则继续转移到开门状态
NextState=OPENDOOR;
else
NextState=CLOSEDOOR;//开门5周期后,关门
end
CLOSEDOOR:
begin
if(UpDnFlag==UPFLAG)//开门关门前电梯是处于上升状态吗
begin
if((request_all&pos)||(up_all&pos))//上升或停靠请求中有当前楼层的请求否,有可能关门的瞬间又有新的请求
NextState=OPENDOOR;
else if((request_all>pos)||(up_all>pos))//上升或停靠请求中有当前楼层之上的请求否
NextState=UP;
else if(down_all>0)//有下降请求否
begin
if((down_all>pos)&&((down_all^pos)>pos))
NextState=UP;//有当前楼层之上的下降请求,则下一状态转移上升
else if((down_all&pos)>0)//有当前楼层的下降请求信号,且更上层无下降请求
NextState=OPENDOOR;
else//只有低于当前层的下降请求
NextState=DOWN;
end
else if(request_all||up_all)//上升和停靠请求中有当前层的请求且只有当前层下的请求
NextState=DOWN;
else
NextState=WAIT;//无任何请求,转为WAIT模式
end
//请参考UpDnFlag=UPFLAG的注释
else if(UpDnFlag==DNFLAG)
begin
if((request_all&pos)||(down_all&pos))
NextState=OPENDOOR;
else if(((request_all&FLOOR1)<pos&&(request_all&FLOOR1))||((request_all&FLOOR2)<pos&&(request_all&FLOOR2))||((request_all&FLOOR3)<pos&&(request_all&FLOOR3))||((request_all&FLOOR4)<pos&&(request_all&FLOOR4))||((request_all&FLOOR5)<pos&&(request_all&FLOOR5))||((request_all&FLOOR6)<pos&&(request_all&FLOOR6)))
NextState=DOWN;
else if(((down_all&FLOOR1)<pos&&(down_all&FLOOR1))||((down_all&FLOOR2)<pos&&(down_all&FLOOR2))||((down_all&FLOOR3)<pos&&(down_all&FLOOR3))||((down_all&FLOOR4)<pos&&(down_all&FLOOR4))||((down_all&FLOOR5)<pos&&(down_all&FLOOR5))||((down_all&FLOOR6)<pos&&(down_all&FLOOR6)))
NextState=DOWN;
else if(up_all>0)
begin
if(((up_all&FLOOR1)<pos&&(up_all&FLOOR1))||((up_all&FLOOR2)<pos&&(up_all&FLOOR2))||((up_all&FLOOR3)<pos&&(up_all&FLOOR3))||((up_all&FLOOR4)<pos&&(up_all&FLOOR4))||((up_all&FLOOR5)<pos&&(up_all&FLOOR5))||((up_all&FLOOR6)<pos&&(up_all&FLOOR6)))
NextState=DOWN;
else if((up_all&pos)>0)
NextState=OPENDOOR;
else
NextState=UP;//
end
else if(request_all||down_all)
NextState=UP;
else
NextState=WAIT;//
end
//请参考WAIT模式的注释
else
begin
if(request_all>0)
begin
if((request_all&pos)>0)
NextState=OPENDOOR;
else if(request_all>pos)
NextState=UP;
else
NextState=DOWN;
end
else if((up_all&pos)||(down_all&pos))
begin
NextState=OPENDOOR;
end
else if((up_all>pos)||(down_all>pos))
NextState=UP;
else if(up_all||down_all)
NextState=DOWN;
else
begin
NextState=WAIT;
end
end
end
default:
NextState=WAIT;
endcase
//3rd StateShift always block,the sequential FSM output,有限状态机第三段
always @(posedge real_clk or posedge reset)//output
if(reset)
//复位后初始化当前楼层为第一层,门是关闭的,电梯是静止的
begin
pos<=FLOOR1;
DoorFlag<=CLOSED;
UpDnFlag<=STATIC;
end
else
begin
PosOut<=pos;//PosOut的输出慢pos一个时钟周期
case(NextState)
WAIT:
//状态为WAIT时,楼层不变,门是关闭的,电梯是静止的,其他模式的情况请大家自己推导
begin
pos<=pos;
DoorFlag<=CLOSED;
UpDnFlag<=STATIC;
end
UP:
begin
pos<=pos<<1;
DoorFlag<=CLOSED;
UpDnFlag<=UPFLAG;
end
DOWN:
begin
pos<=pos>>1;
DoorFlag<=CLOSED;
UpDnFlag<=DNFLAG;
end
UPSTOP:
begin
pos<=pos;
DoorFlag<=CLOSED;
UpDnFlag<=UPFLAG;
end
DOWNSTOP:
begin
pos<=pos;
DoorFlag<=CLOSED;
UpDnFlag<=DNFLAG;
end
OPENDOOR:
begin
pos<=pos;
DoorFlag<=OPEN;
UpDnFlag<=UpDnFlag;
end
CLOSEDOOR:
begin
pos<=pos;
DoorFlag<=CLOSED;
UpDnFlag<=UpDnFlag;
end
default:
//默认情况
begin
pos<=FLOOR1;
DoorFlag<=CLOSED;
UpDnFlag<=STATIC;
end
endcase
end
endmodule
2015年04月19日 06点04分 1
1