题目6:设计一个交通信号灯控制器
1、设计方案
输入为car车辆到来时为1,无车时为0,输出o_signal为交通信号灯,0001时为红灯,0010时为黄灯,0100时为绿灯,1000时为左拐灯,复位之后,交通灯在空闲状态,当车辆到来时交通灯到下一状态绿灯,等待40s,到下一状态左拐灯,等待15s,到黄灯,然后等待5s转到空闲态,无车时,转到红灯,等待55s之后,到黄灯等待5s,回到空闲态继续判断。
2、程序代码
module traffic6_signal(
input i_clk,
input i_rst,
input car, //当有车来时绿灯亮,之后左拐灯亮,无车时间红灯亮
output [3:0]o_signal //
);
parameter idle = 4'b0000,
s1 = 4'b0001, //红灯
s2 = 4'b0010,//黄灯
s3 = 4'b0100,//绿灯
s4 = 4'b1000;//左拐灯
reg [5:0] cnt;
reg [3:0]current_state;
reg [2:0] wait_cnt;
always@(posedge i_clk or negedge i_rst)begin
if(i_rst=='d1)begin
current_state <= idle;
cnt <= 'd0;
wait_cnt <= 'd0;
end
else begin
case(current_state)
idle:begin
if(car)begin
current_state <= s3;
cnt <= 'd0;end
else begin
current_state <= s1;
cnt <= 'd0;end
end
s1:begin //红灯
if(cnt=='d54)begin
current_state <= s2;
cnt <= 'd0;end
else begin
current_state <= s1;
cnt <= cnt+'d1;end
end
s2:begin //黄灯
if(cnt=='d4)begin
current_state <= idle;
cnt <= 'd0;end
else begin
current_state <= s2;
cnt <= cnt+'d1;end
end
s3:begin //绿灯
if(cnt=='d39)begin
current_state <= s4;
cnt <= 'd0;end
else begin
current_state <= s3;
cnt <= cnt+'d1;end
end
s4:begin //左拐灯
if(cnt=='d14)begin
current_state <= s2;
cnt <= 'd0;end
else begin
current_state <= s4;
cnt <= cnt+'d1;end
end
endcase
end
end
assign o_signal =current_state;
endmodule
3、仿真测试
module traffic6_signal_tb;
reg i_clk;
reg i_rst;
reg car ; //当有车来时绿灯亮,之后左拐灯亮,无车时间红灯亮
wire [3:0]o_signal ;
traffic6_signal traffic6_signal(
.i_clk (i_clk) ,
.i_rst (i_rst) ,
.car (car), //当有车来时绿灯亮,之后左拐灯亮,无车时间红灯亮
.o_signal(o_signal) //
);
always #1 i_clk = ~i_clk;
initial begin
i_clk = 0;
i_rst = 1;
#2
i_rst = 0;
car = 1;
#121
car = 0;
end
endmodule
4.结果分析
o_signal为交通信号灯,0001时为红灯,0010时为黄灯,0100时为绿灯,1000时为左拐灯,复位之后,交通灯在空闲状态,当车辆到来时交通灯到下一状态绿灯,等待40s,到下一状态左拐灯,等待15s,到黄灯,然后等待5s转到空闲态,无车时,转到红灯,等待55s之后,到黄灯等待5s,回到空闲态继续判断。
标签:clk,car,signal,绿灯,verilog,rst,设计,时为,实验报告 From: https://blog.csdn.net/mojixin123/article/details/139829449