![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