实现两个垂直方向的交通信号灯及等待时间数码管显示
`timescale 1s / 1ms
//////////////////////////////////////////////////////////////////////////////////
// ************************************************************************************
// LaoWang produced Must be a boutique!
//////////////////////////////////////////////////////////////////////////////////
module LedTraffic #
(
parameter MILLISEC_CLOCKNUM = 16'D5000 , // 1ms clock num
parameter TIME_GREEN = 8'H40 , // green time
parameter TIME_RED = 8'H65 , // red time
parameter TIME_YELLOW = 8'H05 , // yellow time
parameter TIME_LEFT = 8'H15 // left-hand time
)
(
input I_Clk , // user clk
input I_En , // trffic led working
output reg [7:0] O_Acount_H , // east-west direction 8-segment numeric display
output reg [7:0] O_Acount_L , // east-west direction 8-segment numeric display
output reg [7:0] O_Bcount_H , // north-south direction 8-segment numeric display
output reg [7:0] O_Bcount_L , // north-south direction 8-segment numeric display
output reg O_Aleftled , // east-west direction left-hand led
output reg O_Agreenled , // east-west direction green led
output reg O_Aredled , // east-west direction red led
output reg O_Ayellowled , // east-west direction yellow led
output reg O_Bleftled , // north-south direction left-hand led
output reg O_Bgreenled , // north-south direction green led
output reg O_Bredled , // north-south direction red led
output reg O_Byellowled , // north-south direction yellow led
output [63:0] O_Monitor
);
assign O_Monitor = { O_Acount_H,O_Acount_L,O_Bcount_H,O_Bcount_L };
reg s_Enin , s_Enins ;
reg [15:0] s_Cntmilli = 0 ;
reg [9:0] s_Cnt1000milli = 0 ;
wire w_Flag_Onesec ;
reg [3:0] s_NumA_h = 0 ;
reg [3:0] s_NumA_l = 0 ;
reg [3:0] s_NumB_h = 0 ;
reg [3:0] s_NumB_l = 0 ;
reg [8:0] CurStage ;
reg [8:0] NexStage ;
localparam AGREEN = 9'B0_0000_0001 ,
AYELLOW1 = 9'B0_0000_0010 ,
ALEFT = 9'B0_0000_0100 ,
AYELLOW2 = 9'B0_0000_1000 ,
BGREEN = 9'B0_0001_0000 ,
BYELLOW1 = 9'B0_0010_0000 ,
BLEFT = 9'B0_0100_0000 ,
BYELLOW2 = 9'B0_1000_0000 ;
localparam IDLE = 9'B1_0000_0000 ;
always @ ( posedge I_Clk )
begin
s_Enin <= I_En ;
s_Enins <= s_Enin ;
end
always @ ( posedge I_Clk )
begin
if ( s_Enins == 1'b1 )
begin
if ( s_Cntmilli == MILLISEC_CLOCKNUM - 1 )
s_Cntmilli <= 0 ;
else
s_Cntmilli <= s_Cntmilli + 1 ;
if ( s_Cntmilli == MILLISEC_CLOCKNUM - 1 )
if ( s_Cnt1000milli == 999 )
s_Cnt1000milli <= 0 ;
else
s_Cnt1000milli <= s_Cnt1000milli + 1 ;
else
s_Cnt1000milli <= s_Cnt1000milli ;
end
else
begin
s_Cnt1000milli <= 0 ;
s_Cntmilli <= 0 ;
end
end
assign w_Flag_Onesec = ( s_Cntmilli == MILLISEC_CLOCKNUM - 1 ) &
( s_Cnt1000milli == 999 ) &
( s_Enins == 1'b1 ) ;
always @ ( posedge I_Clk )
begin
if ( s_Enins == 1'b1 )
CurStage <= NexStage ;
else
CurStage <= IDLE ;
end
always @ ( * )
begin
case ( CurStage )
IDLE : begin NexStage = AGREEN ; end
AGREEN : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
NexStage = AYELLOW1 ;
else
NexStage = AGREEN ;
end
AYELLOW1 : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
NexStage = ALEFT ;
else
NexStage = AYELLOW1 ;
end
ALEFT : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
NexStage = AYELLOW2 ;
else
NexStage = ALEFT ;
end
AYELLOW2 : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
NexStage = BGREEN ;
else
NexStage = AYELLOW2 ;
end
BGREEN : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
NexStage = BYELLOW1 ;
else
NexStage = BGREEN ;
end
BYELLOW1 : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
NexStage = BLEFT ;
else
NexStage = BYELLOW1 ;
end
BLEFT : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
NexStage = BYELLOW2 ;
else
NexStage = BLEFT ;
end
BYELLOW2 : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
NexStage = AGREEN ;
else
NexStage = BYELLOW2 ;
end
default : begin NexStage = IDLE ; end
endcase
end
always @ ( posedge I_Clk )
begin
case ( CurStage )
IDLE : begin
s_NumA_h <= TIME_GREEN[7:4] ;
s_NumA_l <= TIME_GREEN[3:0] ;
s_NumB_h <= TIME_RED[7:4] ;
s_NumB_l <= TIME_RED[3:0] ;
end
AGREEN : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
begin
s_NumA_h <= TIME_YELLOW[7:4] ;
s_NumA_l <= TIME_YELLOW[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
end
else
begin
end
end
AYELLOW1 : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
begin
s_NumA_h <= TIME_LEFT[7:4] ;
s_NumA_l <= TIME_LEFT[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
end
else
begin
end
end
ALEFT : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
begin
s_NumA_h <= TIME_YELLOW[7:4] ;
s_NumA_l <= TIME_YELLOW[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
end
else
begin
end
end
AYELLOW2 : begin
if (( s_NumA_h == 4'd0 )&&( s_NumA_l == 4'd0 ))
begin
s_NumA_h <= TIME_RED[7:4] ;
s_NumA_l <= TIME_RED[3:0] ;
s_NumB_h <= TIME_GREEN[7:4] ;
s_NumB_l <= TIME_GREEN[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
end
else
begin
end
end
BGREEN : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
begin
s_NumB_h <= TIME_YELLOW[7:4] ;
s_NumB_l <= TIME_YELLOW[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
end
else
begin
end
end
BYELLOW1 : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
begin
s_NumB_h <= TIME_LEFT[7:4] ;
s_NumB_l <= TIME_LEFT[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
end
else
begin
end
end
BLEFT : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
begin
s_NumB_h <= TIME_YELLOW[7:4] ;
s_NumB_l <= TIME_YELLOW[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
end
else
begin
end
end
BYELLOW2 : begin
if (( s_NumB_h == 4'd0 )&&( s_NumB_l == 4'd0 ))
begin
s_NumB_h <= TIME_RED[7:4] ;
s_NumB_l <= TIME_RED[3:0] ;
s_NumA_h <= TIME_GREEN[7:4] ;
s_NumA_l <= TIME_GREEN[3:0] ;
end
else if ( w_Flag_Onesec )
begin
if ( s_NumB_l == 4'd0 )
s_NumB_l <= 4'd9 ;
else
s_NumB_l <= s_NumB_l - 4'd1 ;
if ( s_NumB_l == 4'd0 )
if ( s_NumB_h == 4'd0 )
s_NumB_h <= 4'd9 ;
else
s_NumB_h <= s_NumB_h - 4'd1 ;
else
s_NumB_h <= s_NumB_h ;
if ( s_NumA_l == 4'd0 )
s_NumA_l <= 4'd9 ;
else
s_NumA_l <= s_NumA_l - 4'd1 ;
if ( s_NumA_l == 4'd0 )
if ( s_NumA_h == 4'd0 )
s_NumA_h <= 4'd9 ;
else
s_NumA_h <= s_NumA_h - 4'd1 ;
else
s_NumA_h <= s_NumA_h ;
end
else
begin
end
end
default : begin end
endcase
end
always @ ( * )
begin
case ( CurStage )
IDLE : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B0 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B0 ;
O_Byellowled = 1'B1 ;
end
AGREEN : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B0 ;
O_Aredled = 1'B1 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B0 ;
O_Byellowled = 1'B1 ;
end
AYELLOW1 : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B1 ;
O_Ayellowled = 1'B0 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B0 ;
O_Byellowled = 1'B1 ;
end
ALEFT : begin
O_Aleftled = 1'B0 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B1 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B0 ;
O_Byellowled = 1'B1 ;
end
AYELLOW2 : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B1 ;
O_Ayellowled = 1'B0 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B0 ;
O_Byellowled = 1'B1 ;
end
BGREEN : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B0 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B0 ;
O_Bredled = 1'B1 ;
O_Byellowled = 1'B1 ;
end
BYELLOW1 : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B0 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B1 ;
O_Byellowled = 1'B0 ;
end
BLEFT : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B0 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B0 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B1 ;
O_Byellowled = 1'B1 ;
end
BYELLOW2 : begin
O_Aleftled = 1'B1 ;
O_Agreenled = 1'B1 ;
O_Aredled = 1'B0 ;
O_Ayellowled = 1'B1 ;
O_Bleftled = 1'B1 ;
O_Bgreenled = 1'B1 ;
O_Bredled = 1'B1 ;
O_Byellowled = 1'B0 ;
end
default : begin end
endcase
end
// 7-segment encoding
// 0
// ---
// 5 | | 1
// --- <--6
// 4 | | 2
// --- . <-- 8
// 3
always @ ( * )
begin
case ( s_NumA_h )
4'b0001 : O_Acount_H = 8'b11111001; // 1
4'b0010 : O_Acount_H = 8'b10100100; // 2
4'b0011 : O_Acount_H = 8'b10110000; // 3
4'b0100 : O_Acount_H = 8'b10011001; // 4
4'b0101 : O_Acount_H = 8'b10010010; // 5
4'b0110 : O_Acount_H = 8'b10000010; // 6
4'b0111 : O_Acount_H = 8'b11111000; // 7
4'b1000 : O_Acount_H = 8'b10000000; // 8
4'b1001 : O_Acount_H = 8'b10010000; // 9
4'b1010 : O_Acount_H = 8'b10001000; // A
4'b1011 : O_Acount_H = 8'b10000011; // b
4'b1100 : O_Acount_H = 8'b11000110; // C
4'b1101 : O_Acount_H = 8'b10100001; // d
4'b1110 : O_Acount_H = 8'b10000110; // E
4'b1111 : O_Acount_H = 8'b10001110; // F
default : O_Acount_H = 8'b11000000; // 0
endcase
end
always @ ( * )
begin
case ( s_NumA_l )
4'b0001 : O_Acount_L = 8'b11111001; // 1
4'b0010 : O_Acount_L = 8'b10100100; // 2
4'b0011 : O_Acount_L = 8'b10110000; // 3
4'b0100 : O_Acount_L = 8'b10011001; // 4
4'b0101 : O_Acount_L = 8'b10010010; // 5
4'b0110 : O_Acount_L = 8'b10000010; // 6
4'b0111 : O_Acount_L = 8'b11111000; // 7
4'b1000 : O_Acount_L = 8'b10000000; // 8
4'b1001 : O_Acount_L = 8'b10010000; // 9
4'b1010 : O_Acount_L = 8'b10001000; // A
4'b1011 : O_Acount_L = 8'b10000011; // b
4'b1100 : O_Acount_L = 8'b11000110; // C
4'b1101 : O_Acount_L = 8'b10100001; // d
4'b1110 : O_Acount_L = 8'b10000110; // E
4'b1111 : O_Acount_L = 8'b10001110; // F
default : O_Acount_L = 8'b11000000; // 0
endcase
end
always @ ( * )
begin
case ( s_NumB_h )
4'b0001 : O_Bcount_H = 8'b11111001; // 1
4'b0010 : O_Bcount_H = 8'b10100100; // 2
4'b0011 : O_Bcount_H = 8'b10110000; // 3
4'b0100 : O_Bcount_H = 8'b10011001; // 4
4'b0101 : O_Bcount_H = 8'b10010010; // 5
4'b0110 : O_Bcount_H = 8'b10000010; // 6
4'b0111 : O_Bcount_H = 8'b11111000; // 7
4'b1000 : O_Bcount_H = 8'b10000000; // 8
4'b1001 : O_Bcount_H = 8'b10010000; // 9
4'b1010 : O_Bcount_H = 8'b10001000; // A
4'b1011 : O_Bcount_H = 8'b10000011; // b
4'b1100 : O_Bcount_H = 8'b11000110; // C
4'b1101 : O_Bcount_H = 8'b10100001; // d
4'b1110 : O_Bcount_H = 8'b10000110; // E
4'b1111 : O_Bcount_H = 8'b10001110; // F
default : O_Bcount_H = 8'b11000000; // 0
endcase
end
always @ ( * )
begin
case ( s_NumB_l )
4'b0001 : O_Bcount_L = 8'b11111001; // 1
4'b0010 : O_Bcount_L = 8'b10100100; // 2
4'b0011 : O_Bcount_L = 8'b10110000; // 3
4'b0100 : O_Bcount_L = 8'b10011001; // 4
4'b0101 : O_Bcount_L = 8'b10010010; // 5
4'b0110 : O_Bcount_L = 8'b10000010; // 6
4'b0111 : O_Bcount_L = 8'b11111000; // 7
4'b1000 : O_Bcount_L = 8'b10000000; // 8
4'b1001 : O_Bcount_L = 8'b10010000; // 9
4'b1010 : O_Bcount_L = 8'b10001000; // A
4'b1011 : O_Bcount_L = 8'b10000011; // b
4'b1100 : O_Bcount_L = 8'b11000110; // C
4'b1101 : O_Bcount_L = 8'b10100001; // d
4'b1110 : O_Bcount_L = 8'b10000110; // E
4'b1111 : O_Bcount_L = 8'b10001110; // F
default : O_Bcount_L = 8'b11000000; // 0
endcase
end
endmodule
|