三层电梯控制器 verilog程序代码三层电梯控制器 verilog程序代码
`timescale 1ms/1ms
module threelift(clk,rst,du1,du2,wei,sw,sw_led,opdo); input clk,rst;
input [11:0] sw;//buttons:0~5,up and down buttons outside the lift; 6~9:floor buttons
inside the lift; 10~11,open and close buttons inside the lift ...
三层电梯控制器 verilog程序代码
`timescale 1ms/1ms
module threelift(clk,rst,du1,du2,wei,sw,sw_led,opdo); input clk,rst;
input [11:0] sw;//buttons:0~5,up and down buttons outside the lift; 6~9:floor buttons
inside the lift; 10~11,open and close buttons inside the lift output [6:0] sw_led;
wire [6:0] sw_led;
output [7:0] du1,du2,wei;
reg [7:0] du1,du2,wei;
output opdo; //open door led
//1s timer---------------------------------------------------------------- reg [9:0] scnt;
always @ (posedge clk or negedge rst)
if (!rst)
scnt <= 0;
else
begin
if( scnt == 10'd1000 )//1s
scnt <= 0;
else
scnt <= scnt + 1'b1;
end
//floor
control---------------------------------------------------------------------------
reg [1:0] lift; //floor: 00,first floor; 01,second; 10,third; 11,fouth
reg [1:0] lift_now; //floor state,which floor is the lift in reg [2:0] ud; //next move:up or down 001,up; 010,stop; 100,down reg rst; //you can push reset button to reset the lift, and if nobody
operates this lift, it will auto reset itself and stop on the first floor always @ (posedge clk or negedge rst)
if(!rst) //reset the lift, stop on the first floor
begin
lift <= 2'b00;
lift_now <= 2'b00;
ud <= 3'b010; //up-and-down state: stop
end
else if( (scnt == 10'd1000) && (km == 0) )
begin
lift_now <= lift;
case(lift)
2'b01:if( tw || th || tw_u || tw_d || th_d )
begin
lift <= lift + 1;
ud <= 3'b001;//上升
end
else //if( !(th && tw && on && th_d && tw_u && tw_d && on_u))
ud <= 3'b010;//停
2'b10:if( (th || th_d) && (on || on_u) )
begin
if( ud==3'b001 )
begin
lift <= lift + 1;
ud <= 3'b001;//上升
end
else
begin
lift <= lift - 1;
ud <= 3'b100;//下降
end
end
else if( (th || th_d) && !(on || on_u) )
begin
lift <= lift + 1;
ud <= 3'b001;//上升
end
else if( !(th || th_d) && (on || on_u) )
begin
lift <= lift - 1;
ud <= 3'b100;//下降
end
else if( !(th && tw && on && th_d && tw_u && tw_d && on_u))
ud <= 3'b010;//停
2'b11:if( tw || on || tw_u || tw_d || on_u )
begin
lift <= lift - 1;
ud <= 3'b100;//下降
end
else //if( !(th && tw && on && th_d && tw_u && tw_d && on_u))
ud <= 3'b010;//停
endcase
end
//数码管显示楼层
------------------------------------------------------------------
always @ (posedge clk)
begin
case(lift_now)//楼层
2'b01:du1 <= 8'b1001_1111;//1
2'b10:du1 <= 8'b0010_0101;//2
2'b11:du1 <= 8'b0000_1101;//3
endcase
case(ud)//电梯状态 上 停 下
3'b001:du2 <= 8'b0111_1111;//上
3'b010:du2 <= 8'b1111_1101;//停
3'b100:du2 <= 8'b1110_1111;//下
endcase
wei <= 8'b1110_1110;
end
//buttons-----------------------------------------------------------------------
----
reg [4:0] cnt; //counter reg
always @ (posedge clk or negedge rst)
if (!rst)
cnt <= 5'b0;
else
cnt <= cnt + 1'b1;
//buttons-----------------------------------------------------------------------
-----
reg [11:0] low_sw;
always @(posedge clk or negedge rst)
if (!rst)
low_sw <= 12'b0;
else if (cnt == 5'd20) //if counter counts to 20ms,lock sw to reg low_sw,that
means to scan those buttons every 20ms
low_sw <= sw;
//buttons-----------------------------------------------------------------------
----
reg [11:0] low_sw_r; //at the positive edge of clk, lock low_sw to
low_sw_r
always @ ( posedge clk or negedge rst )
if (!rst)
low_sw_r <= 12'b0;
else
low_sw_r <= low_sw;
//---------------------------------------------------------- wire [11:0] lift_ctr = low_sw[11:0] & ( ~low_sw_r[11:0]); //when low_sw turns from
0 to 1, assign lift_ctr 1. This will last for a clock circle
//because in the next
circle, low_sw_r's value will change.
//--------------------move state regs
reg four_d; //somebody in the fouth floor wants to go down
reg three_u; //third floor up reg three_d; //third floor down reg two_u; //second floor up reg two_d; //second floor down reg one_u; //first floor up reg four; //the fouth floor reg three; //the third floor reg two; //the second floor reg one; //the first floor reg open_in; //open button inside the lift
reg close_in; //close button inside the lift
//--------------------the former move state regs reg four_df;
reg three_uf;
reg three_df;
reg two_uf;
reg two_df;
reg one_uf;
reg fourf;
reg threef;
reg twof;
reg onef;
reg open_inf;
reg close_inf;
always @ (posedge clk or negedge rst) if(!rst)
begin
four_d <= 0;
three_u <= 0;
three_d <= 0;
two_u <= 0;
two_d <= 0;
one_u <= 0;
four <= 0;
three <= 0;
two <= 0;
one <= 0;
open_in <= 0;
close_in <= 0;
end
else
begin
if( scnt == 10'd1000 )
begin
four_df <= four_d;
three_uf <= three_u;
three_df <= three_d;
two_uf <= two_u;
two_df <= two_d;
one_uf <= one_u;
fourf <= four;
threef <= three;
twof <= two;
onef <= one;
open_inf <= open_in;
close_inf <= close_in;
end
if( (lift_ctr != 12'b000000_0000_00) ) //if one of those buttons were pushed,
we assign 1 to pushed buttons' corresponding regs
begin
if ( lift_ctr[11] ) close_in <= 1;
if ( lift_ctr[10] ) open_in <= 1;
if ( lift_ctr[9] ) four_d <= 1;
if ( lift_ctr[8] ) three_u <= 1;
if ( lift_ctr[7] ) three_d<= 1;
if ( lift_ctr[6] ) two_u<= 1;
if ( lift_ctr[5] ) two_d<= 1;
if ( lift_ctr[4] ) one_u<= 1;
if ( lift_ctr[3] ) four <= 1;
if ( lift_ctr[2] ) three<= 1;
if ( lift_ctr[1] ) two<= 1;
if ( lift_ctr[0] ) one<= 1;
end
if( opens == 2 )
begin
case( lift_now )
2'b01:begin
on <= 0;
on_u <= 0;
end
2'b10:begin
tw <= 0;
tw_d <= 0;
tw_u <= 0;
end
2'b11:begin
th <= 0;
th_d <= 0;
end
endcase
end
end
//状态灯
assign sw_led[6]=th_d; assign sw_led[5]=tw_u; assign sw_led[4]=tw_d; assign sw_led[3]=on_u; assign sw_led[2]=th;
assign sw_led[1]=tw;
assign sw_led[0]=on;
//电梯门控制
---------------------------------------------------------------------
reg opdo;//开门指示
reg [2:0] opens;//开门4s时间
reg km;//开门标志 1允许开
always @ (posedge clk or negedge rst)
if(!rst)
begin
opdo <= 0;
opens <=0;
km <= 0;
end
else if( scnt == 10'd1000 )
begin
if( (lift == 2'b01) && (on_uf == 1 || onf == 1) )//
km <= 1;
if( (lift == 2'b10) && (tw_uf == 1||tw_df == 1||twf == 1) )
km <= 1;
if( (lift == 2'b11) && (th_df == 1 || thf == 1) )
km <= 1;
if( km ==1 )
begin
opens <= opens + 1;
if( opens > 0)
opdo <= 1;
if( opens == 3'd4 )
begin
km <= 0;
opdo <= 0;
end
end
end
endmodule
本文档为【三层电梯控制器 verilog程序代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。