名称:状态机控制的交通灯设计Verilog代码Quartus仿真(文末获取)
软件:Quartus
语言:Verilog
代码功能:
2、输入输出
clk:输入时钟信号,频率为50MH
rst_n:复位信号.低电平有效,高电平系统开始工作。输出
r_sn:南北方向红灯,高电平点亮,低电平熄灭y_sn:南北方向黄灯.高电平点亮,低电平熄灭g_sn:南北方向绿灯,高电平点亮,低电平熄灭r_ew:东西方向红灯,高电平点亮,低电平熄灭y_ew:东西方向黄灯,高电平点亮,低电平熄灭g_ew:东西方向绿灯,高电平点亮,低电平熄灭
3、设计要求
使用 verilog HDL语言编写, Quartus、 Modelsim软件开发复位信号有效
南北方向与东西方向均为红灯。复位信号无效,系统正常工作时
南北方向:1-27秒绿灯亮,第28、29、30秒闪烁黄灯.31-60秒变红灯;东西方向:1~30秒红灯亮,31~57绿灯亮,第58、59、60秒闪烁黄灯;1-60秒如此循环。
4、设计思路
根据设计要求,划分状态机,确定状态个数、状态名称;确定控制信号,状态转移条件;确定输岀信号;绘制状态转移图;根据状态转移图进行代码编写、仿真验证。
使用计数器实现计时功能,也可作为状态转移条件
1. 工程文件
2. 程序文件
3. 程序编译
4. RTL图
状态图
5. Testbench
6. 仿真图
部分代码展示:
红->绿 绿->黄 黄->红 1、红--计时main_red_times------------------------绿--计时main_green_times---main_yellow_times黄灯---------------红 2、绿--计时branch_green_times---branch_yellow_times黄灯--------------------红--计时branch_reg_times-------------------绿 */ module traffic_light( input clk,//50MMhz input rst_n,//复位 output r_sn,//主路灯 output g_sn,//主路灯-- output y_sn,//主路灯-- output r_ew,//支路灯-- output g_ew,//支路灯-- output y_ew//支路灯-- ); wire clk_1Hz; reg main_red;//主路灯 reg main_green;// reg main_yellow;// reg branch_red;//支路灯 reg branch_green;// reg branch_yellow; //分频到1Hz reg [31:0] cnt='d0; reg clk_1hz=0; assign clk_1Hz=clk_1hz; always@(posedge clk) if(cnt==32'd25) begin//分频50000000,得1HZ,25000000,仿真改小为25 cnt<=32'd0; clk_1hz<=~clk_1hz; end else begin cnt<=cnt+32'd1; clk_1hz<=clk_1hz; end //定义状态 parameter all_red=3'd0; parameter main_green_state=3'd1; parameter main_yellow_state=3'd2; parameter branch_green_state=3'd3; parameter branch_yellow_state=3'd4; wire [7:0] main_green_time; wire [7:0] main_yellow_time; wire [7:0] branch_green_time; wire [7:0] branch_yellow_time; //定义路口个灯持续时间,修改此处时间 assign main_green_time=8'd27;//主路黄灯时间设置为27秒 assign main_yellow_time=8'd3;//主路黄灯时间设置为3秒 assign branch_yellow_time=8'd3;//支路黄灯时间设置为3秒 assign branch_green_time=8'd27;//主路黄灯时间设置为27秒 //主路绿灯+主路黄灯=支路红灯时间 //支路绿灯+支路黄灯=主路红灯时间 reg [2:0] state=3'd0; reg [7:0] main_green_cnt=8'd1; reg [7:0] main_yellow_cnt=8'd1; reg [7:0] branch_green_cnt=8'd1; reg [7:0] branch_yellow_cnt=8'd1; //主路绿灯+主路黄灯=支路红灯时间 //支路绿灯+支路黄灯=主路红灯时间 always@(posedge clk_1Hz or negedge rst_n) if(!rst_n) state<=all_red;//rst_n else case(state) main_green_state: if(main_green_cnt<main_green_time) begin//主路绿灯 state<=main_green_state; main_green_cnt<=main_green_cnt+'d1; end else begin state<=main_yellow_state;//计数到后到下一状态 main_green_cnt<='d1; end main_yellow_state: if(main_yellow_cnt<main_yellow_time) begin//主路黄灯 state<=main_yellow_state; main_yellow_cnt<=main_yellow_cnt+'d1; end else begin state<=branch_green_state;//计数到后到下一状态 main_yellow_cnt<='d1; end branch_green_state: if(branch_green_cnt<branch_green_time) begin//支路绿灯 state<=branch_green_state; branch_green_cnt<=branch_green_cnt+'d1; end else begin state<=branch_yellow_state;//计数到后到下一状态 branch_green_cnt<='d1; end branch_yellow_state: if(branch_yellow_cnt<branch_yellow_time) begin//支路3s黄灯 state<=branch_yellow_state; branch_yellow_cnt<=branch_yellow_cnt+'d1; end else begin state<=main_green_state;//计数到后到下一状态 branch_yellow_cnt<='d1; end
源代码
扫描文章末尾的公众号二维码