Verilog之交通灯的设计

本文介绍了使用Verilog设计的交通灯控制系统。主干道上的绿灯周期为40秒,随后是5秒黄灯和15秒左转绿灯,再接5秒黄灯。非主干道在主干道绿灯期间始终显示红灯,其绿灯周期为30秒,同样伴有5秒黄灯和15秒左转绿灯,最后是5秒黄灯。
摘要由CSDN通过智能技术生成

![Alt]为了便于观察,将各个交通灯的亮灭时间设置的短一些为了便于观察,将各个交通灯的亮灭时间设置的短一些

功能:主干道上:绿灯:40秒 非主干道在此期间一直红灯。
黄灯:5秒
左转:15秒
黄灯:5秒
非主干道:绿灯:30秒 此时主干道一直红灯
黄灯:5秒
左转:15秒
黄灯:5秒

// An highlighted block
module traffic_light(rst,clk,light1,light2);

input rst,clk;

output[3:0] light1,light2;
reg[3:0] light1,light2;//四位的信号灯,从高位到低位分别是绿、黄、左转、红。

parameter IDLE=4'd0, G1=4'd1, Y1_1=4'd2, L1=4'd3, Y1_2=4'd4;//表示主干道的信号灯的状态
parameter G2=4'd5, Y2_1=4'd6, L2=4'd7, Y2_2=4'd8;//表示非主干道的信号灯

reg[3:0] cur_sta;
reg[7:0] cnt;
always@(posedge clk)
begin
if(!rst) begin 
cur_sta<=IDLE;
cnt<=0;
end
else begin
case(cur_sta)
IDLE : begin cur_sta<=G1;end
G1   : if(cnt==40) begin cnt<=0;cur_sta<=Y1_1;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b1000;
       light2 <= 4'b0001;       
       end
Y1_1 : if(cnt==5) begin cnt<=0;cur_sta<=L1;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0100;
       light2 <= 4'b0001;       
       end 
L1   : if(cnt==15) begin cnt<=0;cur_sta<=Y1_2;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0010;
       light2 <= 4'b0001;       
       end
Y1_2 : if(cnt==5) begin cnt<=0;cur_sta<=G2;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0100;
       light2 <= 4'b0001;       
       end
G2   : if(cnt==30) begin cnt<=0;cur_sta<=Y2_1;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0001;
       light2 <= 4'b1000;       
       end
Y2_1 : if(cnt==5) begin cnt<=0;cur_sta<=L2;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0001;
       light2 <= 4'b0100;       
       end
L2   : if(cnt==15) begin cnt<=0;cur_sta<=Y2_2;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0001;
       light2 <= 4'b0010;       
       end 
Y2_2 : if(cnt==5) begin cnt<=0;cur_sta<=G1;end
       else begin 
       cnt<=cnt+1;
       light1 <= 4'b0001;
       light2 <= 4'b0100;       
       end 
default: cur_sta<=IDLE;
endcase
end
end
endmodule



  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值