交通灯实验
一, 实验目的
实现两路信号灯交替亮起,并利用两组数码管分别对两路信号进行倒计时。
两路信号时间分别为:
V:绿灯(30S) H:红灯(35S)
黄灯(5s) 绿灯(30S)
红灯(35S) 黄灯(5S)
二, 实验步骤
建立工程
可在欢迎界面点击“Creat a New Project”进入工程建立界面,亦可关闭欢迎界面,点击菜单栏的“File”,点击“New Project Wizard”进入建立工程界面。
右侧为建立工程界面,点击next。
在此界面选定工程路径,取好工程名,点击“Next”。注意:路径中不能有中文,工程名也不能有中文。
一直点击“Next”进入器件设置界面,DE2-70开发工具采用的Cyclone II系列的EP2C70F896C6N。点击“Finish”,完成工程建立
1、 点击“File”,点击“New” 选择“Verilog HDL”
2, 点击主界面工具栏中的 选择“Verilog HDL”
3、写入verilog代码。
代码如下:
module traffic(Clk_50M,Rst,LedR_H,LedG_H,LedY_H,LedR_V,LedG_V,LedY_V,Seg7_VH,Seg7_VL,Seg7_HH,Seg7_HL,led15);
parameter S1=2'b00;
parameter S2=2'b01;
parameter S3=2'b10;
parameter S4=2'b11;
input Clk_50M,Rst;
output LedR_H,LedG_H,LedY_H,LedR_V,LedG_V,LedY_V;
output[6:0] Seg7_VH,Seg7_VL,Seg7_HH,Seg7_HL;
output led15;
//-------------div for 1Hz-------start----
reg Clk_1Hz;
reg [31:0] Cnt_1Hz;
always@(posedge Clk_50M or negedge Rst)
begin
if(!Rst)
begin
Cnt_1Hz<=1;
Clk_1Hz<=1;
end
else
begin
if(Cnt_1Hz>=25000000)
begin
Cnt_1Hz<=1;
Clk_1Hz<=~Clk_1Hz;
end
else
Cnt_1Hz<=Cnt_1Hz + 1;
end
end
//-----------div for 1Hz------end-----
reg[7:0] Cnt30,CntH,CntV,CntHH,CntVV;
reg[7:0] CntDis,CntDiss;
//-----------30 counter and seg7---start---
reg LedR_H,LedG_H,LedY_H,LedR_V,LedG_V,LedY_V;
always@(posedge Clk_1Hz)
begin
case(state)
S1:
begin
if(Cnt30>=30)
Cnt30<=1;
else
Cnt30<=Cnt30 + 1;
end
S2:
begin
if(Cnt30>=5)
Cnt30<=1;
else
Cnt30<=Cnt30 + 1;
end
S3:
begin
if(Cnt30>=30)
Cnt30<=1;
else
Cnt30<=Cnt30 + 1;
end
S4:
begin
if(Cnt30>=5)
Cnt30<=1;
else
Cnt30<=Cnt30 + 1;
end
endcase
end
always@(posedge Clk_1Hz)
begin
case(stateV)
S1:
begin
if(CntV>=30)
CntV<=1;
else
CntV<=CntV + 1;
end
S2:
begin
if(CntV>=5)
CntV<=1;
else
CntV<=CntV + 1;
end
S3:
begin
if(CntV>=35)
CntV<=1;
else
CntV<=CntV + 1;
end
endcase
end
always@(posedge Clk_1Hz)
begin
case(stateH)
S1:
begin
if(CntH>=35)
CntH<=1;
else
CntH<=CntH + 1;
end
S2:
begin
if(CntH>=30)
CntH<=1;
else
CntH<=CntH + 1;
end
S3:
begin
if(CntH>=5)
CntH<=1;
else
CntH<=CntH + 1;
end
endcase
end
always@(negedge Clk_50M or negedge Rst)
begin
case(state)
S1:
CntVV=30-CntV;
S2:
CntVV=5-CntV;
S3:
CntVV=35-CntV;
S4:
CntVV=35-CntV;
endcase
end
always@(negedge Clk_50M or negedge Rst)
begin
case(state)
S1:
CntHH=35-CntH;
S2:
CntHH=35-CntH;
S3:
CntHH=30-CntH;
S4:
CntHH=5-CntH;
endcase
end
//16进制计数器转换为用于显示的10进制计数器
always@(posedge Clk_50M)
begin
if(CntVV>29)
begin
CntDis[7:4]<=3;
CntDis[3:0]<=CntVV - 30;
end
else if(CntVV>19)
begin
CntDis[7:4]<=2;
CntDis[3:0]<=CntVV - 20;
end
else if(CntVV>9)
begin
CntDis[7:4]<=1;
CntDis[3:0]<=CntVV - 10;
end
else
CntDis<=CntVV;
end
SEG7_LUT hex4(Seg7_VL,CntDis[3:0]);
SEG7_LUT hex5(Seg7_VH,CntDis[7:4]);
always@(posedge Clk_50M)
begin
if(CntHH>29)
begin
CntDiss[7:4]<=3;
CntDiss[3:0]<=CntHH - 30;
end
else if(CntHH>19)
begin
CntDiss[7:4]<=2;
CntDiss[3:0]<=CntHH - 20;
end
else if(CntHH>9)
begin
CntDiss[7:4]<=1;
CntDiss[3:0]<=CntHH - 10;
end
else
CntDiss<=CntHH;
end
SEG7_LUT hex1(Seg7_HL,CntDiss[3:0]);
SEG7_LUT hex2(Seg7_HH,CntDiss[7:4]);
//-----------30 counter and seg7----end----
reg [1:0]state,stateH,stateV;
always@(posedge Clk_1Hz)
begin
case(state)
S1:
if(Cnt30>=30)
begin
state<=S2;
end
S2:
if(Cnt30>=5)
begin
state<=S3;
end
S3:
if(Cnt30>=30)
begin
state<=S4;
end
S4:
if(Cnt30>=5)
begin
state<=S1;
end
default:
begin
state<=S1;
end
endcase
end
always@(posedge Clk_1Hz)
begin
case(state)
S1:
begin
stateH<=S1;
stateV<=S1;
end
S2:
begin
stateH<=S1;
stateV<=S2;
end
S3:
begin
stateH<=S2;
stateV<=S3;
end
S4:
begin
stateH<=S3;
stateV<=S3;
end
endcase
end
always@(posedge Clk_50M or negedge Rst)
begin
if(!Rst)
begin
LedR_H<=0;
LedG_H<=0;
LedY_H<=0;
LedR_V<=0;
LedG_V<=0;
LedY_V<=0;
end
else
begin
case(state)
S1:
begin
LedR_H<=1;
LedG_H<=0;
LedY_H<=0;
LedR_V<=0;
LedG_V<=1;
LedY_V<=0;
end
S2:
begin
LedR_H<=1;
LedG_H<=0;
LedY_H<=0;
LedR_V<=0;
LedG_V<=0;
LedY_V<=1;