?? traffic.v
字號:
/*
本實驗?zāi)M路口的紅黃綠交通燈的變化過程,用LED燈表示交通燈,并在數(shù)碼管上顯示當(dāng)前狀態(tài)剩余時間。
紅燈持續(xù)時間為30秒,黃燈10秒,綠燈30秒
*/
module traffic(clk,rst,dataout,en,light);
input clk,rst;
output[7:0] dataout;//數(shù)碼管段數(shù)據(jù)
reg[7:0] dataout;
output[1:0] en; //數(shù)碼管使能
output[2:0] light; //紅,黃,綠燈
reg[1:0] en;
reg[2:0] light;
reg[25:0] cnt;
reg[15:0] cnt_scan;
reg[3:0] dataout_buf;
reg[3:0] first,second;//時間的個位和十位
reg[1:0] state;
parameter red=2'b00,
yellow=2'b01,
green=2'b10;
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt<=0;
else if(cnt!=26'd40000000)
cnt<=cnt+1;
else
cnt<=0;
end
always@(posedge clk or negedge rst)
begin
if(!rst) begin
state<=red;
first<=0;
second<=3;
end
else begin
if(cnt==24'd40000000) begin
case(state)
red: begin
if(first!=0)
first<=first-1;
else begin
if(second!=0) begin
second<=second-1;
first<=9;
end
else begin
state<=green;
second<=3;
end
end
end
green: begin
if(first!=0)
first<=first-1;
else begin
if(second!=0) begin
first<=9;
second<=second-1;
end
else begin
state<=yellow;
second<=1;
end
end
end
yellow:begin
if(first!=0||second!=0) begin
if(second!=0) begin
first<=9;
second<=second-1;
end
else
first<=first-1;
end
else begin
state<=red;
second<=3;
end
end
default: begin
first<=0;
second<=0;
end
endcase
end
end
end
always@(state)
begin
case(state)
red:
light=3'b011;
yellow:
light=3'b101;
green:
light=3'b110;
default:
light=3'b111;
endcase
end
/////////////////////////////////顯示部分////////////////
always@(posedge clk or negedge rst)
begin
if(!rst) begin
en<=2'b10;
cnt_scan<=0;
end
else begin
cnt_scan<=cnt_scan+1;
if(cnt_scan==16'hffff)
en<=~en;
end
end
always@(en or first or second)
begin
case(en)
2'b10:
dataout_buf=first;
2'b01:
dataout_buf=second;
default:
dataout_buf=second;
endcase
end
always@(dataout_buf)
begin
case(dataout_buf)
4'b0000:
dataout=8'b0000_0011;
4'b0001:
dataout=8'b1001_1111;
4'b0010:
dataout=8'b0010_0101;
4'b0011:
dataout=8'b0000_1101;
4'b0100:
dataout=8'b1001_1001;
4'b0101:
dataout=8'b0100_1001;
4'b0110:
dataout=8'b0100_0001;
4'b0111:
dataout=8'b0001_1111;
4'b1000:
dataout=8'b0000_0001;
4'b1001:
dataout=8'b0001_1001;
4'b1010:
dataout=8'b0001_0001;
4'b1011:
dataout=8'b1100_0001;
4'b1100:
dataout=8'b0110_0011;
4'b1101:
dataout=8'b1000_0101;
4'b1110:
dataout=8'b0110_0001;
4'b1111:
dataout=8'b0111_0001;
endcase
end
endmodule
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -