module TL(clk,dig,seg,en,led);
input clk;
input en;
output [7:0] dig,seg;
output [2:0] led;
reg [2:0] led_r;
reg [7:0] dig_r,seg_r;
reg [7:0] disp_dat;
reg [24:0] count;
reg [7:0] red,yellow,green;
reg [7:0] num;
reg sec;
reg [1:0] cnt;
assign dig = dig_r;
assign seg = seg_r;
assign led = led_r;//定义段
always@(posedge clk)
begin
count = count + 1'b1;
if(count == 25'd24000000)
begin
count =25'd0;
sec = ~sec;
end
end//秒信号产生
always@(en)
begin
if(!en)
red <= 8'd30;
yellow <= 8'd5;
green <= 8'd20;
end//使能赋值
initial
begin
cnt <= 2'b0;
end
always @ (negedge sec)
begin
case (cnt)
0:begin num <= red ;begin
if(num>1)
begin
if(num[3:0] == 0)
begin
num[3:0] <= 4'b1001;
num[7:4]<=num[7:4]-4'b0001;
end
else num[3:0]<=num[3:0]-4'b0001;
end
end
led_r <= 5;cnt <= 1;end
1:begin num <= yellow;begin
if(num>1)
begin
if(num[3:0] == 0)
begin
num[3:0] <= 4'b1001;
num[7:4]<=num[7:4]-4'b0001;
end
else num[3:0]<=num[3:0]-4'b0001;
end led_r <= 3; cnt <= 2;end end
2:begin num <= green;
begin
if(num>1)
begin
if(num[3:0] == 0)
begin
num[3:0] <= 4'b1001;
num[7:4]<=num[7:4]-4'b0001;
end
else num[3:0]<=num[3:0]-4'b0001;
end led_r <= 6;cnt <= 0;end end
default: led_r <= 3;
endcase
end//点亮交通灯+倒计时赋值