当前位置:   article > 正文

基于FPGA的简单交通灯设计Verilog代码ISE 仿真_verilog交通灯代码

verilog交通灯代码

名称:基于FPGA的简单交通灯设计Verilog代码ISE  仿真(文末获取)

软件:Quartus

语言:Verilog

代码功能:

1.设计功能

设计的为十字路口交通灯,包括红、黄、绿三色信号灯。2条路口的绿灯时间为30秒,黄灯时间为3秒,红灯时间为33秒,红灯和绿灯最后3秒闪烁。

1. 设计功能

设计的为十字路口交通灯,包括红、黄、绿三色信号灯。2条路口的绿灯时间为30秒,黄灯时间为3秒,红灯时间为33秒,红灯和绿灯最后3秒闪烁。

2. 工程文件

3. 程序文件

4. 程序编译

5. Testbench

6. 仿真图

用对应颜色表示灯,如下:

部分代码展示:

/*
红->绿 绿->黄 黄->红
1、红--计时main_red_times------------------------绿--计时main_green_times---main_yellow_times黄灯---------------红
2、绿--计时branch_green_times---branch_yellow_times黄灯--------------------红--计时branch_reg_times-------------------绿
*/
//设路口A为主路,路口B为支路
module traffic_light(
input clk,//时钟500Hz
//led 1亮0灭
output reg main_red,//主路灯红灯
output reg main_green,//主路灯绿灯
output reg main_yellow,//主路灯黄灯
output reg branch_red,//支路灯红灯
output reg branch_green,//支路灯绿灯
output reg branch_yellow//支路灯黄灯
);
wire clk_1Hz;
//分频模块
//分频到1Hz
reg [31:0] cnt='d0;
reg clk_1hz=0;
assign clk_1Hz=clk_1hz;
always@(posedge clk)//分频
if(cnt==32'd250) begin//分频500,得1HZ,250
cnt<=32'd0;
clk_1hz<=~clk_1hz;
end
else begin
   cnt<=cnt+32'd1;
clk_1hz<=clk_1hz;
end
//定义路口个灯持续时间,修改此处时间
//主路绿灯+主路黄灯=支路红灯时间
//支路绿灯+支路黄灯=主路红灯时间
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'd30;//主路绿灯时间设置为30秒
assign main_yellow_time=8'd3;//主路黄灯时间设置为3秒
assign branch_yellow_time=8'd3;//支路黄灯时间设置为3秒
assign branch_green_time=8'd30;//支路绿灯时间设置为30秒
///

//交通灯控制模块
parameter main_green_state=3'd1;
parameter main_yellow_state=3'd2;
parameter branch_green_state=3'd3;
parameter branch_yellow_state=3'd4;
//定义路口个灯持续时间,修改此处时间
//主路绿灯+主路黄灯=支路红灯时间
//支路绿灯+支路黄灯=主路红灯时间
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)
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
default:state<=main_green_state;
endcase
//交通灯状态控制,state为相应状态时亮相应灯
always@(*)
begin
if(state==main_green_state)
if(main_green_cnt>27)//30秒绿灯,第27秒开始闪烁
main_green=clk_1Hz;//闪烁
else
main_green=1;//绿灯
else
main_green=0;
if(state==main_yellow_state )
main_yellow=1;//黄灯
else
main_yellow=0;
if(state==branch_green_state)
main_red=1;//红灯
else if(state==branch_yellow_state)
main_red=clk_1Hz;//闪烁
else
main_red=0;
end
//交通灯状态控制,state为相应状态时亮相应灯
always@(*)
begin
if(state==branch_green_state)
if(branch_green_cnt>27)//30秒绿灯,第27秒开始闪烁
branch_green=clk_1Hz;//闪烁
else
branch_green=1;//绿灯
else
branch_green=0;
if(state==branch_yellow_state )
branch_yellow=1;//黄灯
else
branch_yellow=0;
if(state==main_green_state)
branch_red=1;//红灯
else if(state==main_yellow_state)
branch_red=clk_1Hz;//闪烁
else
branch_red=0;
end
endmodule
源代码

 扫描文章末尾的公众号二维码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/796725
推荐阅读
相关标签
  

闽ICP备14008679号