- --
通行时间可变的交通灯控制器设计
module tr1(ng,clk,reset,resets,emergency,lighta,lightb,seg,select); input ng,clk,reset,emergency,resets; output[6:0]seg;//显示用的
output[3:0] lighta,lightb;//a是主干道,b是支干道 output [3:0] select;//选择那一个管子进行显示 reg clk1,clk2;//clk1要5HZ clk2要几千HZ reg [3:0] select;
reg tim1,tim2;//这是看你的等有没有变过颜色的控制信号 reg [1:0] cont;
reg[2:0]state1,state2,ste;两个控制颜色变化状态的信号。 reg[3:0]lighta,lightb;//a是主干道,b是支干道 reg[3:0]num;//译码器是根据这个东西来译码的 reg [35:0] fout; reg[6:0]seg;//显示 reg[7:0] numa,numb;
reg[7:0] red1,red2,green1,green2,yellow1,yellow2,left1,left2; always @(ng ) if(!ng)
begin //设置计数初值 green1 <=8'b00110000;//30S red1 <=8'b01010001;//51S yellow1<=8'b00000011;//3S left1 <=8'b00010101; //15S green2 <=8'b00110000;//30S red2 <=8'b01010001;//51S
- . -word资料-
- --
yellow2<=8'b00000011;//3S left2 <=8'b00010101; //15S end
always @ (posedge clk) begin
if (fout==36'b111111111111111111111111111111111111) fout<=0;
else begin fout<=fout+1; clk1=fout[23];5HZ clk2=fout[13];几千HZ end end
always @(posedge clk1 ) begin
if(reset) //复位与特殊情况控制 这是主干道的复位 begin
lighta<=4'b1000;
//lighta是主干道的交通灯,lightb是支干道的交通灯,1000是红灯,0100是直行绿灯,0010是黄灯,0001是左转绿灯
numa<=red1; //主干道的数码管从红灯开始记 state1<=0;//这时候灯变化的控制信号为0 end
else if(emergency)//紧急情况 begin
lighta<=4'b1000;//主干道红了
- . -word资料-
- --
numa<=red1;//计时也从红灯记 end
else if(ng)//这下开始正常工作了 begin //使能有效开始控制计数
if(!tim1) //你有没有变过颜色啊?没有?那开始变颜色吧。 begin //主干道交通灯点亮控制
tim1<=1;//主干道的灯的开关,行了,我变过了。
case(state1)//state1是用来控制主干道亮灯的各个状态的。
//1000(8)是红灯,0100(4)是直行绿灯,0010(2)是黄灯,0001(1)是左转绿灯
3'b000:begin numa<=green1; lighta<=4; state1<=3'b001;end//直行绿 3'b001:begin numa<=yellow1;lighta<=2; state1<=3'b010;end//黄 3'b010:begin numa<=left1; lighta<=1; state1<=3'b011;end//左转绿 3'b011:begin numa<=yellow1;lighta<=2; state1<=3'b100;end//黄 3'b100:begin numa<=red1; lighta<=8; state1<=3'b000;end//红 default:lighta<=8;//接二极管正极,负极接地 endcase end
else//tm1=1,我变过颜色了,可以开始计数了。 begin //倒数计时 if(numa>0)
if(numa[3:0]==0) begin
numa[3:0]<=4'b1001; numa[7:4]<=numa[7:4]-1;
- . -word资料-
- --
end
else numa[3:0]<=numa[3:0]-1;
if(numa==1) tim1<=0;好了,差不多计完了,再让我变一次吧。 end end
else //如果电路没有启动 begin
lighta<=4'b0100;//默认为直行绿灯 numa=0;//默认计数为0 tim1<=0; //默认我没有变过颜色 end end
always @(posedge clk1 )//下面就和主干道的一样了 begin
if(reset) //复位与特殊情况控制 begin
lightb<=4'b1000; numb<=red2;
state2<=0; end
else if(emergency) begin
lightb<=4'b1000; numb<=red2; end
- . -word资料-
- --
else if(ng) begin if(!tim2) begin tim2<=1; case(state2)
0:begin numb<=red2; lightb<=8; 1:begin numb<=green2; lightb<=4; 2:begin numb<=yellow2;lightb<=2; 3:begin numb<=left2; lightb<=1; 4:begin numb<=yellow2; lightb<=2; default:lightb<=8; endcase end else
begin //倒数计时 if(numb>0) if(numb[3:0]==0) begin
numb[3:0]<=4'b1001; numb[7:4]<=numb[7:4]-1; end
else numb[3:0]<=numb[3:0]-1; if(numb==1) tim2<=0; end end
- . state2<=1; end state2<=2; end state2<=3;end state2<=4;end state2<=0;end -word资料-
- --
else begin tim2<=0; state2<=0; lightb<=4'b0100; end end
always @(posedge clk2) begin //数码管扫描 if(resets) begin cont=0; select=4'b1111;; end else begin case(cont)
2'b00:begin num<=numa[3:0]; select<=4'b1101;cont<=cont+1; end 2'b01:begin num<=numa[7:4]; select<=4'b1011;cont<=cont+1; end 2'b10:begin num<=numb[3:0]; select<=4'b0111;cont<=cont+1; end 2'b11:begin num<=numb[7:4]; select<=4'b1110;cont<=cont+1; end endcase end end
- . -word资料-
- --
always @(posedge clk2) begin //数码管译码显示 case(num)
4'b0000: seg<=7'b0111111; //0 4'b0001: seg<=7'b0000110; //1 4'b0010: seg<=7'b1011011; //2 4'b0011: seg<=7'b1001111; //3 4'b0100: seg<=7'b1100110; //4 4'b0101: seg<=7'b1101101; //5 4'b0110: seg<=7'b1111101; //6 4'b0111: seg<=7'b0000111; //7 4'b1000: seg<=7'b1111111; //8 4'b1001: seg<=7'b1101111; //9 default: seg<=7'b0111111; //0 endcase end endmodule
- . -word资料-