|
自己写的一个交通灯的程序,一个十字路口,
red_o,blue_o,green_o, 表示主交通道上的红,绿,黄灯,1表示亮
red_1_o,blue_1_o,green_1_0,表示人行道上的红,绿,黄灯
用Modelsim仿真结果不好,请各位帮帮我。
module trafficlight (clk,rst,c,red_o,blue_o,green_o,
red_1_o,blue_1_o,green_1_o);
input clk,rst,c; //c=1,表示有人过路
output red_o,blue_o,green_o,red_1_o,blue_1_o,green_1_o;
reg red_o,blue_o,green_o,red_1_o,blue_1_o,green_1_o;
reg cnt;
`define DA_LIGHT {red_o,blue_o,green_o}
`define XIAO_LIGHT {red_1_o,blue_1_o,green_1_o}
parameter b_g_n=20; //绿灯转黄灯的计数时间
parameter g_r_n=10; //黄灯装红灯的计数时间
parameter stt=30; //小马路上的行车时间
//公路上的红绿灯
always @ (posedge clk or negedge rst)begin
if(!rst)begin
`DA_LIGHT<=3'b010;
`XIAO_LIGHT<=3'b100;
end
else begin
case ({`DA_LIGHT,`XIAO_LIGHT})
6'b010100:
begin
if (c==1) begin
delay (b_g_n,cnt);
wait (cnt)
`DA_LIGHT<=3'b001;
`XIAO_LIGHT<=3'b100;
end
end
6'b001100:
begin
delay (g_r_n,cnt);
wait (cnt);
`DA_LIGHT<=3'b100;
`XIAO_LIGHT<=3'b010;
end
6'b100010:
begin
delay(stt,cnt);
wait(cnt);
`DA_LIGHT<=3'b100;
`XIAO_LIGHT<=3'b001;
end
6'b100001:
begin
delay(g_r_n,cnt);
wait(cnt);
`DA_LIGHT<=3'b010;
`XIAO_LIGHT<=3'b100;
end
default:
begin
`DA_LIGHT<=3'b010;
`XIAO_LIGHT<=3'b100;
end
endcase
end
end
// 延时的程序
task delay;
input tics;
output cnt;
reg cnt;
begin
repeat (tics)
@(posedge clk);
cnt<=1;
end
endtask
endmodule |
|