1、功能需求:
该交通信号灯控制器用于一条主干道和一条乡村公路的交叉口的交通,它需要下面的功能:
(1)、主干道优先级别最高;
(2)、乡村公路没有车通过时,主干道一直为绿灯;
(3)、当乡村公路检测到有车通过时,主干道的交通灯,由绿灯变成黄灯,再变成红灯,此时,乡村公路的交通灯变成了绿灯;
(4)、当乡村公路再次没有车通过时,乡村公路的交通灯,由绿灯变成黄灯,再变成红灯,此时,主干道的交通灯变成了绿灯;
2、交通灯模块设计:
`define True 1'b1
`define False 1'b0
`define Y_R_delay 3
`define R_G_delay 2
module sig_control(hwy,cntry,X,clock,clear);
input X,clock,clear;
output [1:0]hwy,cntry;
reg [1:0]hwy,cntry;
parameter Red=2'd0,Yellow=2'd1,Green=2'd2;
//需设计好该电路的状态转换图,状态转换表,后面编程的设计都是根据这个状态转换图/表得来的;
parameter S0=3'd0,S1=3'd1,S2=3'd2,S3=3'd3,S4=3'd4;
reg [2:0]state;
reg [2:0]nextState;
//时钟的控制
always @(posedge clock)
if(clear)
state<=S0;
else
state<=nextState;
//状态的跳变
always @(state)
begin
hwy=Green;
cntry=Red;
case(state)
S0: ;
S1:hwy=Yellow;
S2:hwy=Red;
S3:begin
hwy=Red;
cntry=Green;
end
S4:begin
hwy=Red;
cntry=Yellow;
end
endcase
end
//监控器x的控制(检测乡村公路是否有车通过)
always @(state or X)
begin
case(state)
S0:if(X)
nextState=S1;
else
nextState=S0;
S1:begin
repeat(`Y_R_delay)@(posedge clock);
nextState=S2;
end
S2:begin
repeat(`R_G_delay)@(posedge clock);
nextState=S3;
end
S3:if(X)
nextState=S3;
else
nextState=S4;
S4:begin
repeat(`Y_R_delay)@(posedge clock);
nextState=S0;
end
default:nextState=S0;
endcase
end
endmodule
(2)、仿真模块:
`include "sig_control.v"
module sig_stimulus;
wire [1:0]Main_Sig,Cntry_Sig;
reg Car_on_cntry;
reg clock,clear;
sig_control s(Main_Sig,Cntry_Sig,Car_on_cntry,clock,clear);
initial
begin
clock=`False;
forever #5 clock=~clock;
end
initial
begin
clear=`True;
repeat (5) @(posedge clock);
clear=`False;
end
initial
begin
Car_on_cntry=`False;
repeat (20) @(negedge clock);
Car_on_cntry=`True;
repeat (10) @(negedge clock);
Car_on_cntry=`False;
repeat (20) @(negedge clock);
Car_on_cntry=`True;
repeat (10) @(negedge clock);
Car_on_cntry=`False;
repeat (20) @(negedge clock);
Car_on_cntry=`True;
repeat (10) @(negedge clock);
Car_on_cntry=`False;
repeat (10) @(negedge clock);
$stop;
end
endmodule
(3)、仿真波形: