交通灯实验报告
1 / 17
一, 实验目的
实现两路信号灯交替亮起,并利用两组数码管分别对两路信号进行倒计时。
两路信号时间分别为:
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) 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)
end
begin
Cnt_1Hz<=1;
Clk_1Hz<=~Clk_1Hz; end else
Cnt_1Hz<=Cnt_1Hz + 1;
begin case(state) S1: end
always@(posedge Clk_1Hz) begin
case(stateV) S1:
begin
if(CntV>=30) CntV<=1; begin
if(Cnt30>=30) Cnt30<=1;
else
Cnt30<=Cnt30 + 1; S2:
begin if(Cnt30>=5) Cnt30<=1; end
else
Cnt30<=Cnt30 + 1; S3: begin
if(Cnt30>=30) Cnt30<=1; end
else
Cnt30<=Cnt30 + 1; S4:
begin if(Cnt30>=5) Cnt30<=1; end
else
Cnt30<=Cnt30 + 1;
end endcase
else
CntV<=CntV + 1; S2:
end
end
begin if(CntV>=5) CntV<=1;
else
CntV<=CntV + 1;
end
S3:
begin
if(CntV>=35) CntV<=1;
else
CntV<=CntV + 1;
end endcase
always@(posedge Clk_1Hz) begin
case(stateH) S1: end
always@(negedge Clk_50M or negedge Rst)
begin
if(CntH>=35) CntH<=1;
else
CntH<=CntH + 1; S2:
begin if(CntH>=30) CntH<=1; end
else
CntH<=CntH + 1;
end
S3:
begin
if(CntH>=5) CntH<=1;
else
CntH<=CntH + 1;
end endcase
begin case(state) S1: end
always@(negedge Clk_50M or negedge Rst) begin case(state) S1: 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; S2: S3:
CntHH=30-CntH; CntHH=5-CntH; CntHH=35-CntH; CntHH=35-CntH; S2: S4:
CntVV=35-CntV; endcase
CntVV=5-CntV; CntVV=35-CntV; CntVV=30-CntV;
S3:
S4:
endcase
end
CntDis[3:0]<=CntVV - 10; end else
CntDis<=CntVV;
SEG7_LUT hex4(Seg7_VL,CntDis[3:0]); SEG7_LUT hex5(Seg7_VH,CntDis[7:4]);
always@(posedge Clk_50M) begin
if(CntHH>29) 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; S2: begin
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 if(Cnt30>=5)
end
state<=S3; end S3: begin state<=S4; end S4: begin state<=S1; end default: begin state<=S1; end endcase
if(Cnt30>=30)
if(Cnt30>=5)
always@(posedge Clk_1Hz) begin case(state) S1: end
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
always@(posedge Clk_50M or negedge Rst) begin if(!Rst) begin
LedR_H<=0; else
LedG_H<=0;
LedY_H<=0; LedR_V<=0; LedG_V<=0; end
LedY_V<=0;
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; end
S3: begin
LedR_H<=0; LedG_H<=1; LedY_H<=0; LedR_V<=1; LedG_V<=0; LedY_V<=0; end
S4: begin
LedR_H<=0;
end
LedG_H<=0; LedY_H<=1; LedR_V<=1; LedG_V<=0; LedY_V<=0; end
default: begin end
LedR_H<=0; LedG_H<=0; LedY_H<=0; LedR_V<=0; LedG_V<=0; LedY_V<=0; end
endcase
assign led15=state;
endmodule
module SEG7_LUT input [3:0] iDIG; output reg
always @(iDIG) begin
case(iDIG)
4'h1: oSEG = 7'b1111001;
// ---t----
4'h2: oSEG = 7'b0100100; // | |
4'h3: oSEG = 7'b0110000; // lt rt 4'h4: oSEG = 7'b0011001; // | | 4'h5: oSEG = 7'b0010010; // ---m---- 4'h6: oSEG = 7'b0000010; // | | 4'h7: oSEG = 7'b1111000; // lb rb 4'h8: oSEG = 7'b0000000; // | | 4'h9: oSEG = 7'b0011000; // ---b---- 4'ha: oSEG = 7'b0001000; 4'hb: oSEG = 7'b0000011; 4'hc: oSEG = 7'b1000110; 4'hd: oSEG = 7'b0100001;[6:0] oSEG; [6:0] oSEG;
(
oSEG,iDIG );
end
4'he: oSEG = 7'b0000110; 4'hf: oSEG = 7'b0001110; 4'h0: oSEG = 7'b1000000; endcase
endmodule
编译工程
保存文件,将文件放在所建工程所在路径下 点击主界面工具栏中的
图标
也可点击菜单栏中“Processing”,点击“Start Compilation”
分配关键如下:
Clk_50M Input PIN_AD15 LedG_H Output PIN_AD9 LedG_V Output PIN_AJ6 LedR_H Output PIN_AJ7 ) LedR_V Output PIN_AJ5 ) LedY_H Output PIN_AD8 LedY_V Output PIN_AK5 Rst Input PIN_AA23 Seg7_HH[6] Output PIN_G1 Seg7_HH[5] Output PIN_H3 Seg7_HH[4] Output PIN_H2 Seg7_HH[3] Output PIN_H1 Seg7_HH[2] Output PIN_J2 Seg7_HH[1] Output PIN_J1
Seg7_HH[0] Seg7_HL[6] Seg7_HL[5] Seg7_HL[4] Seg7_HL[3] Seg7_HL[2] Seg7_HL[1] Seg7_HL[0] Seg7_VH[6] Seg7_VH[5] Seg7_VH[4] Seg7_VH[3] Seg7_VH[2] Seg7_VH[1] Seg7_VH[0] Seg7_VL[6] Seg7_VL[5] Seg7_VL[4] Seg7_VL[3] Seg7_VL[2] Seg7_VL[1] Seg7_VL[0] Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output Output PIN_K3 PIN_E4 PIN_F4 PIN_G4 PIN_H8 PIN_H7 PIN_H4 PIN_H6 PIN_AD17 PIN_AF17 7 PIN_AE17 7 PIN_AG16 PIN_AF16 7 PIN_AE16 7 PIN_AG13 PIN_AD12 PIN_AD11 PIN_AF10 8 PIN_AD10 PIN_AH9 8 PIN_AF9 8 PIN_AE8 8
烧写代码
在管脚配置完成后,还需将工程再编译一次,成功后,点击主界面工具栏中的
亦可点击主界面菜单栏中“Tools”,点击“Programmer”
进入代码烧写界面后,点击“Start”,当“Progress”为100%
时,表示烧写完成,这是可观察DE2-70板现象
获得预期的效果,两组的信号红黄绿灯交替切换,计数器记为零时信号灯切换状态,红灯35s,黄灯5s,绿灯30s。 三, 心得体会
通过本次实验初步了解了EDA技术,熟悉了FPGA开发板的开发流程,锻炼了动手能力。
因篇幅问题不能全部显示,请点此查看更多更全内容