功能代码:
module traffic(CLK,EN,LAMPA,LAMPB,ACOUNT,BCOUNT,Ra,Ga,Ya,LGa,Rb,Gb,Yb,LGb); //端口说明
output [7:0] ACOUNT,BCOUNT;
output Ra,Ga,Ya,LGa,Rb,Gb,Yb,LGb;
output [3:0] LAMPA,LAMPB;
input CLK,EN; //内部信号说明
reg [7:0] numa,numb; //ACOUNT和BCOUNT的内部信号
reg tempa,tempb;
reg [2:0]counta,countb; //方向A和方向B的灯的状态
reg [7:0]ared,ayellow,agreen,aleft,bred,byellow,bgreen,bleft;
reg [3:0]LAMPA,LAMPB;
//设置各交通灯的持续时间初始化值,红灯的值由另一个方向的黄灯和绿灯计算得出。
assign {Ra,Ya,Ga,LGa}=LAMPA;
assign {Rb,Yb,Gb,LGb}=LAMPB;
always @(EN)
if(!EN) begin //使能信号EN无效时,对交通灯的计数值进行初始化
ared <=8'd55; //55 s , 30 + 5 + 15 + 5
ayellow <=8'd5; //5 s
agreen <=8'd40; //40 s
aleft <=8'd15; //15 s
bred <=8'd65; //65 s , 40 + 5 + 15 + 5
byellow <=8'd5; //5 s
bleft <=8'd15; //15 s
bgreen <=8'd30; //30 s