实现两个垂直方向的交通信号灯及等待时间数码管显示
`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 ;
共3条
1/1 1 跳转至页
交通信号灯
共3条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【换取逻辑分析仪】自制底板并驱动ArduinoNanoRP2040ConnectLCD扩展板被打赏47分 | |
【分享评测,赢取加热台】RISC-V GCC 内嵌汇编使用被打赏38分 | |
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 | |
【分享评测,赢取加热台】使用8051单片机驱动WS2812被打赏40分 |