名称:基于FPGA的交通灯控制器设计Verilog代码ISE仿真(文末获取)
软件:ISE
语言:Verilog
代码功能:
交通灯控制器
利用 VeriloghHDL语言设计一个十字路口交通灯控制器,其示意图如下图1所示A方向与B方向各设红(R),黄(Y),绿(G),和左拐(L)四种灯,四种灯按合理的顺序亮灭,并能将灯亮的时间以倒计时的形式显示出来。
两个方向这种灯亮的时间应该能够非常方便地进行设置和修改,此外假设A方向是主干路,车流量大,因此A方向同行的时间应该比B方向长一些
1. 工程文件
2. 程序文件
3. 程序编译
4. RTL图
5. Testbench(仿真代码)
6. 仿真图
整体仿真图
红绿灯控制模块仿真图
显示模块仿真图
部分代码展示:
//红绿灯控制模块 module led_ctrl( input clk,//时钟 input reset_n,//复位 output reg m_R,//主路红灯 output reg m_G,//主路绿灯 output reg m_Y,//主路黄灯 output reg m_L,//主路左转灯 output reg B_R,//支路红灯 output reg B_G,//支路绿灯 output reg B_Y,//支路黄灯 output reg B_L,//支路左转灯 output reg [7:0] m_downcnt,//主路倒计时 output reg [7:0] b_downcnt//支路倒计时 ); reg clk_1Hz;//1Hz reg [31:0] cnt=32'd0; always@(posedge clk) if(cnt>=32'd50)//50分频 cnt<=8'd0; else cnt<=cnt+8'd1;//计数 always@(posedge clk) if(cnt>=32'd25)//50分频 clk_1Hz<=1;//分频到1Hz else clk_1Hz<=0;// //状态机控制模块 reg [3:0] state=4'd0; //定义状态 parameter s_m_G=4'd0; parameter s_m_Y=4'd1; parameter s_m_L=4'd2; parameter s_m_Y2=4'd3; parameter s_b_G=4'd4; parameter s_b_Y=4'd5; parameter s_b_L=4'd6; parameter s_b_Y2=4'd7; parameter s_idle=4'd8; //定义计数器 reg [7:0] cnt_0=8'd0; reg [7:0] cnt_1=8'd0; reg [7:0] cnt_2=8'd0; reg [7:0] cnt_3=8'd0; reg [7:0] cnt_4=8'd0; reg [7:0] cnt_5=8'd0; reg [7:0] cnt_6=8'd0; reg [7:0] cnt_7=8'd0; wire [7:0] m_R_num;//主路红灯 wire [7:0] m_G_num;//主路绿灯 wire [7:0] m_Y_num;//主路黄灯 wire [7:0] m_L_num;//主路左转灯 //A方向红、绿、黄、左拐灯亮的时间分别为:55秒、40秒、5秒和15秒 assign m_R_num=55;//主路红灯 assign m_G_num=40;//主路绿灯 assign m_Y_num=5;//主路黄灯 assign m_L_num=15;//主路左转灯 wire [7:0] B_R_num;//主路红灯 wire [7:0] B_G_num;//主路绿灯 wire [7:0] B_Y_num;//主路黄灯 wire [7:0] B_L_num;//主路左转灯 //B方向红、绿、黄、左拐灯亮的时间分别为:65秒、30秒、5秒和15秒 assign B_R_num=65;//支路红灯 assign B_G_num=30;//支路绿灯 assign B_Y_num=5;//支路黄灯 assign B_L_num=15;//支路左转灯 //状态机控制 always@(posedge clk_1Hz or negedge reset_n) if(reset_n==0) state<=s_idle;//复位 else case(state) s_idle: state<=s_m_G; s_m_G://主路绿灯 if(cnt_0>=m_G_num-1)//40s state<=s_m_Y; else state<=s_m_G; s_m_Y://主路黄灯 if(cnt_1>=m_Y_num-1)//5s state<=s_m_L; else state<=s_m_Y; s_m_L://主路左转灯 if(cnt_2>=m_L_num-1)//15s state<=s_m_Y2; else state<=s_m_L; s_m_Y2://主路黄灯 if(cnt_3>=m_Y_num-1)//5s state<=s_b_G; else state<=s_m_Y2; s_b_G://支路绿灯 if(cnt_4>=B_G_num-1)//30s state<=s_b_Y; else state<=s_b_G; s_b_Y://支路黄灯 if(cnt_5>=B_Y_num-1)//5s state<=s_b_L; else state<=s_b_Y; s_b_L://支路左转灯 if(cnt_6>=B_L_num-1)//15s state<=s_b_Y2; else state<=s_b_L; s_b_Y2://支路黄灯 if(cnt_7>=B_Y_num-1)//5s state<=s_m_G; else state<=s_b_Y2; default:; endcase always@(posedge clk_1Hz) begin if(state==s_m_G)//主路绿灯计时 cnt_0<=cnt_0+1;//计时 else cnt_0<=8'd0; if(state==s_m_Y)//主路黄灯计时 cnt_1<=cnt_1+1;//计时 else cnt_1<=8'd0; if(state==s_m_L)//主路左转灯计时 cnt_2<=cnt_2+1;//计时 else cnt_2<=8'd0; if(state==s_m_Y2)//主路黄灯计时 cnt_3<=cnt_3+1;//计时 else cnt_3<=8'd0; end always@(posedge clk_1Hz) begin if(state==s_b_G)//支路绿灯计时 cnt_4<=cnt_4+1;//计时 else cnt_4<=8'd0; if(state==s_b_Y) //支路黄灯计时 cnt_5<=cnt_5+1;//计时 else cnt_5<=8'd0; if(state==s_b_L)//支路左转灯计时 cnt_6<=cnt_6+1;//计时 else cnt_6<=8'd0; if(state==s_b_Y2)//支路黄灯计时 cnt_7<=cnt_7+1;//计时 else cnt_7<=8'd0; end //控制红绿灯 always@(posedge clk_1Hz) case(state) s_m_G://主路绿灯 begin m_R<=0;m_G<=1;m_Y<=0;m_L<=0; B_R<=1;B_G<=0;B_Y<=0;B_L<=0; end s_m_Y://主路黄灯 begin m_R<=0;m_G<=0;m_Y<=1;m_L<=0; B_R<=1;B_G<=0;B_Y<=0;B_L<=0; end s_m_L://主路左转灯 begin m_R<=0;m_G<=0;m_Y<=0;m_L<=1; B_R<=1;B_G<=0;B_Y<=0;B_L<=0; end s_m_Y2://主路黄灯 begin m_R<=0;m_G<=0;m_Y<=1;m_L<=0; B_R<=1;B_G<=0;B_Y<=0;B_L<=0; end s_b_G://支路绿灯 begin m_R<=1;m_G<=0;m_Y<=0;m_L<=0; B_R<=0;B_G<=1;B_Y<=0;B_L<=0; end s_b_Y://支路黄灯 begin m_R<=1;m_G<=0;m_Y<=0;m_L<=0; B_R<=0;B_G<=0;B_Y<=1;B_L<=0; end s_b_L://支路左转灯 begin m_R<=1;m_G<=0;m_Y<=0;m_L<=0; B_R<=0;B_G<=0;B_Y<=0;B_L<=1; end s_b_Y2://支路黄灯 begin m_R<=1;m_G<=0;m_Y<=0;m_L<=0; B_R<=0;B_G<=0;B_Y<=1;B_L<=0; end s_idle://复位 begin m_R<=1;m_G<=0;m_Y<=0;m_L<=0; B_R<=1;B_G<=0;B_Y<=0;B_L<=0; end default:; endcase //倒计时控制 always@(posedge clk_1Hz) if(state==s_m_G)begin//主路绿灯 m_downcnt=m_G_num-cnt_0; b_downcnt=B_R_num-cnt_0; end else if(state==s_m_Y)begin//主路黄灯 m_downcnt=m_Y_num-cnt_1; b_downcnt=B_R_num-(m_G_num+cnt_1); end else if(state==s_m_L)begin//主路左转灯 m_downcnt=m_L_num-cnt_2; b_downcnt=B_R_num-(m_G_num+m_Y_num+cnt_2); end else if(state==s_m_Y2)begin//主路黄灯 m_downcnt=m_Y_num-cnt_3; b_downcnt=B_R_num-(m_G_num+m_Y_num+m_L_num+cnt_3); end else if(state==s_b_G)begin//支路绿灯 b_downcnt=B_G_num-cnt_4; m_downcnt=m_R_num-cnt_4; end else if(state==s_b_Y)begin//支路黄灯 b_downcnt=B_Y_num-cnt_5; m_downcnt=m_R_num-(B_G_num+cnt_5); end else if(state==s_b_L)begin//支路左转灯 b_downcnt=B_L_num-cnt_6; m_downcnt=m_R_num-(B_G_num+B_Y_num+cnt_6); end else if(state==s_b_Y2)begin//支路黄灯 b_downcnt=B_Y_num-cnt_7; m_downcnt=m_R_num-(B_G_num+B_Y_num+B_L_num+cnt_7); end else begin b_downcnt=8'd0; m_downcnt=8'd0; end endmodule
源代码
扫描文章末尾的公众号二维码