Verilog 四层电梯设计
能跑就行系列。。
描述:设计一个4层楼的电梯控制系统,完成电梯向上或向下移动到被请求楼层(假设电梯每移动一层需要1秒)。请求可来自每层楼的呼叫按钮,也可来自电梯内的目的楼层选择。当电梯到达被请求楼层后,打开电梯门10秒(假设该电梯内只有楼层按键,没有开门和关门按键),然后关闭电梯门前往下一个被请求楼层;如果没有请求则停在本层。电梯运行中保持电梯门关闭。当同时有多个请求时,应答的优先原则为尽可能不改变电梯运动方向且距离当前楼层最近。
top_module模块
module top_module(
input clr, //清零端子
input clk, //时钟信号
input [3:0]in_floor, //电梯内楼层输入
input [2:0]out_up, //电梯上升信号
input [3:1]out_down, //电梯下降信号output [3:0]led, //小灯泡
output [3:0] pos,
output [7:0] seg);wire [15:0]dataBus; //数据传输
wire clk190hz;
wire clk_1s; //时钟信号(1s)
wire clk_10s; //时钟信号(10s)segMsgout U1(.clk190hz(clk190hz),.dataBus(dataBus),.pos(pos),.seg(seg));
counter U2(.clr(clr),.clk(clk),.clk_1s(clk_1s),.clk_10s(clk_10s));
clkDiv U3(.clk100mhz(clk),.clk190hz(clk190hz));
elevator_controller U4(.clr(clr),.clk(clk),.clk_1s(clk_1s),.clk_10s(clk_10s),.in_floor(in_floor),.out_up(out_up),.out_down(out_down),.dataBus(dataBus),.led(led));endmodule
elevator_controller模块
module elevator_controller(
input clr, //清零端子
input clk, //时钟信号
input clk_1s, //时钟信号(1s)
input clk_10s, //时钟信号(10s)
input [3:0]in_floor, //电梯内楼层输入
input [2:0]out_up, //电梯上升信号
input [3:1]out_down, //电梯下降信号output reg [15:0]dataBus,//数据传输
output [3:0]led //小灯泡);parameter stop=2'b00,down=2'b01,up=2'b10;parameter one_stop=4'b0000, one_up=4'b0001, /* x_stop 代表电梯停止状态 x_up 代表电梯向上运行状态 x_down 代表电梯向下运行状态 */ two_stop=4'b0010, two_up=4'b0011, two_down=4'b0100,three_stop=4'b0101,three_up=4'b0110,three_down=4'b0111,four_stop=4'b1000, four_down=4'b1001,Cstop=4'b1010; //Cstop 代表电梯运动过程中响应的停止状态reg [2:0]now_floor; // 楼层号reg [1:0]elevator; // 电梯运行状态reg [3:0]Cstate; // 电梯控制器状态机的状态值reg door; // 电梯门开关状态reg [3:0]present_state;reg [3:0]next_state;initial
beginnow_floor=0;elevator=stop;door=0;Cstate=0;present_state=0;next_state=0;
endalways@(posedge clk) //数据传输
begindataBus[15:0]<={1'b0,now_floor[2:0]+3'b001,2'b00,elevator[1:0],Cstate[3:0],3'b000,door};
endalways@(posedge clk or posedge clr) //状态控制beginif(clr)beginpresent_state<=one_stop;next_state<=one_stop;endelsebeginpresent_state<=next_state; //现状态转变为下一状态case(present_state)one_stop:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]))next_state<=one_stop;elsebeginif(clk_10s) next_state<=one_up;else next_state<=next_state;endend one_up:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]|in_floor[0]|in_floor[1]|in_floor[2]|in_floor[3]))beginnext_state<=one_stop;endelsebeginif(clk_1s)beginif(out_up[1]|out_down[1]|in_floor[1])next_state<=Cstop;else if(in_floor[0]|(out_up[0]&&!(in_floor[1]|in_floor[2]|in_floor[3])))next_state<=one_stop;else next_state<=two_up;endelse next_state<=next_state;endendtwo_stop:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]))next_state<=two_stop;elsebeginif(clk_10s)beginif(out_up[1]|out_up[2]|out_down[2]|out_down[3])next_state<=two_up;else if(out_up[0]|out_down[1])next_state<=two_down;endelse next_state<=next_state;endendtwo_up:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]|in_floor[0]|in_floor[1]|in_floor[2]|in_floor[3]))next_state<=two_stop;elsebeginif(clk_1s)beginif(out_up[2]|out_down[2]|in_floor[2])next_state<=Cstop;else if(in_floor[1]|(out_up[1]&&!(in_floor[2]|in_floor[3])))next_state<=two_stop;elsenext_state<=three_up;endelse next_state<=next_state;endendtwo_down:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]|in_floor[0]|in_floor[1]|in_floor[2]|in_floor[3]))next_state<=two_stop;elsebegin if(clk_1s)beginif(in_floor[1]|(out_down[1]&&(~in_floor[0]))) next_state<=two_stop; elsenext_state<=Cstop; endelse next_state<=next_state;end endthree_stop:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3])) next_state<=three_stop;elsebeginif(clk_10s)beginif(out_up[2]|out_down[3])next_state<=three_up;else if(out_up[0]|out_up[1]|out_down[1]|out_down[2])next_state<=three_down;endelse next_state<=next_state;endendthree_up:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]|in_floor[0]|in_floor[1]|in_floor[2]|in_floor[3]))next_state<=three_stop;elsebeginif(clk_1s)beginif(in_floor[2]|(out_up[2]&&(~in_floor[3])))next_state<=three_stop;elsenext_state<=Cstop;endelse next_state<=next_state;endendthree_down:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]|in_floor[0]|in_floor[1]|in_floor[2]|in_floor[3]))next_state<=three_stop;elsebeginif(clk_1s)beginif(out_down[1]|out_up[1]|in_floor[1])next_state<=Cstop;else if(in_floor[2]|(out_down[2]&&!(in_floor[0]|in_floor[1])))next_state<=three_stop;else next_state<=two_down;endelse next_state<=next_state;endendfour_stop:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]))next_state<=four_stop;elsebeginif(clk_10s) next_state<=four_down;else next_state<=next_state;endend four_down:beginif(!(out_up[0]|out_up[1]|out_up[2]|out_down[1]|out_down[2]|out_down[3]|in_floor[0]|in_floor[1]|in_floor[2]|in_floor[3]))next_state<=four_stop;elsebeginif(clk_1s)beginif(out_down[2]|out_up[2]|in_floor[2])next_state<=Cstop;else if(in_floor[3]|(out_down[3]&&!(in_floor[0]|in_floor[1]|in_floor[2])))next_state<=four_stop;else next_state<=three_down;endelse next_state<=next_state;endendCstop:beginif(now_floor[2:0]==3'b000) next_state<=one_stop;else if(now_floor[2:0]==3'b001) next_state<=two_stop;else if(now_floor[2:0]==3'b010) next_state<=three_stop;else if(now_floor[2:0]==3'b011) next_state<=four_stop; end endcase end endalways@(posedge clk or posedge clr) //输出控制beginif(clr)beginnow_floor[2:0]<=3'b000;elevator[1:0]<=stop;door<=1'b0;Cstate[3:0]<=one_stop;endelse if(clr!=1)begin case(present_state) //当前电梯所在的楼层、电梯运行状态、电梯门开关状态one_stop:beginnow_floor[2:0]<=3'b000;elevator[1:0]<=stop;door<=1;endone_up: beginelevator[1:0]<=up;door<=0;if(in_floor[0]==1) now_floor[2:0]<=3'b000;else now_floor[2:0]<=3'b001; endtwo_stop:beginnow_floor[2:0]<=3'b001;elevator[1:0]<=stop;door<=1;endtwo_up:beginelevator[1:0]<=up;door<=0;if(in_floor[1]==1) now_floor[2:0]<=3'b001;else now_floor[2:0]<=3'b010; end two_down:begin elevator[1:0]<=down;door<=0; if(in_floor[1]==1) now_floor[2:0]<=3'b001;else now_floor[2:0]<=3'b000; end three_stop:beginnow_floor[2:0]<=3'b010;elevator[1:0]<=stop;door<=1; end three_up:begin elevator[1:0]<=up;door<=0; if(in_floor[2]==1) now_floor[2:0]<=3'b010;else now_floor[2:0]<=3'b011; end three_down:begin elevator[1:0]<=down;door<=0; if(in_floor[2]==1) now_floor[2:0]<=3'b010;else now_floor[2:0]<=3'b001; end four_stop:beginnow_floor[2:0]<=3'b011;elevator[1:0]<=stop;door<=1; end four_down:begin elevator[1:0]<=down;door<=0; if(in_floor[3]==1) now_floor[2:0]<=3'b011;else now_floor[2:0]<=3'b010; enddefault:beginnow_floor[2:0]<=now_floor[2:0];elevator[1:0]<=elevator[1:0];door<=door;end endcasecase(next_state) //电梯控制器状态机的状态值one_stop:Cstate[3:0]<=one_stop;one_up: Cstate[3:0]<=one_up; two_stop:Cstate[3:0]<=two_stop; two_up:Cstate[3:0]<=two_up; two_down:Cstate[3:0]<=two_down; three_stop:Cstate[3:0]<=three_stop; three_up:Cstate[3:0]<=three_up; three_down:Cstate[3:0]<=three_up; four_stop:Cstate[3:0]<=four_stop; four_down:Cstate[3:0]<=four_down; Cstop:Cstate[3:0]<=Cstop; default:Cstate[3:0]<=Cstate[3:0]; endcase endelsebeginnow_floor[2:0]<=now_floor[2:0];elevator[1:0]<=elevator[1:0];door<=door;Cstate[3:0]<=Cstate[3:0]; end endassign led[0]=(now_floor[2:0]==3'b000)?1:0;
assign led[1]=(now_floor[2:0]==3'b001)?1:0;
assign led[2]=(now_floor[2:0]==3'b010)?1:0;
assign led[3]=(now_floor[2:0]==3'b011)?1:0;endmodule
counter模块
module counter(input clk,input clr,output clk_1s,output clk_10s);reg [13:0]cnt_first,cnt_second;reg [3:0]cnt_third;reg [13:0]cnt_second_t;initialbegincnt_second=14'd5;cnt_second_t=14'd0;endalways @(posedge clk or posedge clr)if(clr)cnt_first<=14'd0;else if(cnt_first==14'd10000)cnt_first<=14'd0;elsecnt_first<=cnt_first +1'b1;always @(posedge clk or posedge clr)beginif(clr)begincnt_second<=14'd5;cnt_second_t<=14'd0;endelse if(cnt_second ==14'd10000)cnt_second<=14'd0;else if(cnt_second_t==14'd10000)cnt_second_t<=14'd0;else if(cnt_first==14'd10000)begincnt_second<=cnt_second+1'b1;cnt_second_t<=cnt_second_t+1'b1;endendalways @(posedge clk or posedge clr)beginif(clr)cnt_third<=4'd0;else if(cnt_third==4'd10)cnt_third<=4'd0;else if(cnt_second_t==14'd10000)cnt_third<=cnt_third+1'b1; end assign clk_1s= (cnt_second==14'd10000)?1'b1 : 1'b0; assign clk_10s= (cnt_third==4'd10)?1'b1 : 1'b0;
endmodule
clkDiv模块
module clkDiv(
input clk100mhz,
output clk190hz);reg [25:0]count=0;assign clk190hz=count[18];always@(posedge clk100mhz)count<=count+1;
endmodule
segMsgout模块
module segMsgout(
input clk190hz,input [15:0] dataBus, output reg [3:0] pos,output reg [7:0] seg);reg [1:0] posC; reg [3:0] dataP;
always @(posedge clk190hz )begin
case(posC)0: beginpos<=4'b1000;dataP<=dataBus[15:12];end1:beginpos <=4'b0100;dataP <= dataBus[11:8];end2:beginpos <=4'b0010;dataP <= dataBus[7:4];end3:beginpos <=4'b0001;dataP <= dataBus[3:0];endendcaseposC = posC + 2'b01;endalways @(dataP)case(dataP)4'b0000:seg=8'b0011_1111;4'b0001:seg=8'b0000_0110;4'b0010:seg=8'b0101_1011;4'b0011:seg=8'b0100_1111;4'b0100:seg=8'b0110_0110;4'b0101:seg=8'b0110_1101;4'b0110:seg=8'b0111_1101;4'b0111:seg=8'b0000_0111;4'b1000:seg=8'b0111_1111;4'b1001:seg=8'b0110_1111;4'b1010:seg=8'b0111_0111;4'b1011:seg=8'b0111_1100;4'b1100:seg=8'b0011_1001;default:seg=8'b0000_1000;endcase
endmodule
有些地方不够完善,仅供参考
由于未使用消抖开关的原因,烧录后板上按键响应有时会失效,不过小心使用,过验收应该没问题
此外,本作品未考虑按键同时按下的情况,可进行相应补充
Verilog 四层电梯设计相关推荐
- C语言四层电梯控制系统,单片机四层电梯控制系统(课程设计).doc
单片机四层电梯控制系统(课程设计) PAGE 指导教师评定成绩: 审定成绩: 重 庆 邮 电 大 学 自 动 化 学 院 基于51单片机的电梯控制课程设计报告 单位(二级学院): 自动化学院 学 生 ...
- 四层电梯西门子S7-200PLC梯形图程序
四层电梯西门子S7-200PLC梯形图程序 . 一.电梯具有的功能 电梯内选和外选按钮的呼叫与对应指示灯的显示功能: 电梯开门和关门动作,开门到位: 3.电梯上升和下降的动作: 电梯停止在某一个楼层时 ...
- 4四层电梯三菱fx2n系列plc独立程序带注释新3plc接线图io表
4四层电梯三菱fx2n系列plc独立程序带注释新3plc接线图io表 ID:6910667455294557PLC组态设计2
- 基于单片机的四层电梯控制系统设计
目录 4层电梯单片机控制设计 4 设计任务 4 整体方案设计 5 系统硬件电路设计 6 3.1 单片机最小系统电路 6 图3-1 单片机最小系统电路图 6 3.2 开关控制电路 7 图3-2 开关控制 ...
- 【Verilog HDL学习之路】第一章 Verilog HDL 数字设计总论
1 Verilog HDL 数字设计总论 1.1 几个重要的概念 EDA(Electronic Design Automation) 电子技术自动化 EDA工具 类似于软件工程中的IDE(集成开发环境 ...
- (44)Verilog HDL 计数器设计
(44)Verilog HDL 计数器设计 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL 计数器设计 5)结语 1.2 FPGA简介 FPGA( ...
- (43)Verilog HDL 二分频设计
(43)Verilog HDL 二分频设计 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL 二分频设计 5)结语 1.2 FPGA简介 FPGA( ...
- (40)Verilog HDL锁存器设计
(40)Verilog HDL锁存器设计 1.1 目录 1)目录 2)FPGA简介 3)Verilog HDL简介 4)Verilog HDL锁存器设计 5)结语 1.2 FPGA简介 FPGA(Fi ...
- (39)System Verilog程序Program设计实例
(39)System Verilog程序Program设计实例 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog程序Program设计实 ...
最新文章
- 能量平衡_巴夏:平衡自己的能量,疗愈自己,疗愈世界
- Resco Photo Viewer for WinCE6.0
- 北航教授李帅:“VR+医疗”仿真系统及关键技术分享
- C#面试题(.net开发人员必备)
- matlab2010安装详细图解案例
- flutter 弹幕 yzl_flutter_bulletchat的使用
- Win10系统打开共享文件提示没有权限使用网络资源怎么处理
- 微生物测序分析LEfSe
- matlab从无到有系列(六):高级图形处理功能(多窗口绘图以及花瓶绘制)
- 3dmax和python做3d动画_3D动画和影视建模,用什么软件或者多个什么软件结合做比较好?...
- 在小程序中实现海报制作
- HTML+CSS简单漫画网页设计成品--(红猪(9页)带注释)
- 关于文档中的背景水印无法去除的解决办法
- android 通知历史,如何查看已随指尖划走的那些通知 -- Past Notifications #Android
- php视频打赏平台源码,2018最新PHP视频打赏平台 php源码
- 微型计算机2017年3月上,2017年3月份规模以上工业增加值增长7.6%
- 鸿蒙手机Beta版本官宣!我们带着成果和Code来了!!
- web前端期末大作业 html+css+javascript火影忍者网页设计实例 动漫网站制作
- Python中使用pickle库进行数据的序列化存储
- 抵御“黄貂鱼”攻击,谷歌使出禁用2G“大招”
热门文章
- ajax实现省市三级联动
- 病毒式营销活动策划书
- c#word文档输出
- 1074:津津的储蓄计划
- STC单片机对AT24C02进行数据读写操作
- 考研数二第十五讲 定积分和不定积分以及定积分中值定理
- unity android 性能分析,Unity Profiler 性能分析
- PHP网页代理cc攻击无需对接版源码
- 浅谈ThreadPoolExecute(JDK8)
- 【干货】Python中“\n”、“\t”、“end”的用法