赞
踩
目录
(1) 以车为主体,绿灯、黄灯、红灯、绿灯依次点亮;
(2)十字路口,具有两组红绿灯;
(3)采用倒计时显示剩余时间,数码管动态显示;
(4)红绿灯时间按键可调。
用六位数码管显示,靠左和靠右两位数码管分别显示东西和南北方向的倒计时显示,我开发板刚好6个LED灯,但是是竖着的,上面三位表示南北方向的红黄绿灯,下面三位表示东西方向的红黄绿灯。平面图如下:
首先需要一个1hz频率进行倒计时计数,然后对倒计时在不同时间段做出不同的判断即可,为了方便理解,我画出流程图如下:(需要注意的是红灯亮的时间应该是黄灯和绿灯时间之和)
- module traffic(
- input clk,
- input rstn,
- input [6:0]d2,//绿灯点亮时间
- output reg [3:0] data1,
- output reg [3:0] data2,
- output reg [3:0] data5,
- output reg [3:0] data6,
- output reg [5:0] led
- );
-
- wire [6:0]d3; //红灯点亮时间
- assign d3=d2+5;
- //1hz生成
- reg [26:0]cn1;
- reg clk1hz;
- always@(posedge clk or negedge rstn)
- begin
- if(!rstn)
- begin
- cn1<=0;
- clk1hz<=0;
- end
- else if(cn1>=24_999_999)//1hz频率生成
- begin
- clk1hz<=!clk1hz;
- cn1<=0;
- end
- else
- cn1<=cn1+1;
- end
- //计数
- reg [6:0]count;
- always@(posedge clk1hz or negedge rstn)
- begin
- if(!rstn)
- count<=0;
- else if(count>=(d3+d3))//红灯时间d3,加上黄灯和绿灯时间d3
- count<=0;
- else
- count<=count+1;
- end
- //南北方向显示
- reg [6:0]north_red,north_green,north_yellow;
- always@(posedge clk1hz or negedge rstn)
- begin
- if(!rstn)
- begin
- north_red<=0;
- north_green<=0;
- north_yellow<=0;
- led[2:0]<=0;
- data1<=0;
- data2<=0;
- end
- else if(count<=d3)//南北方向红灯点亮
- begin
- north_red<=d3-count;
- led[2:0]<=3'b110;
- data1<=north_red%10;//取时间低位
- data2<=north_red/10;//取时间高位
- end
- else if((count>d3)&&(count<=(d3+d3-5)))//南北方向绿灯点亮
- begin
- north_green<=d3+d3-5-count;
- led[2:0]<=3'b011;
- data1<=north_green%10;
- data2<=north_green/10;
- end
- else
- begin
- north_yellow<=d3+d3-count;//南北方向黄灯点亮
- led[2:0]<=3'b101;
- data1<=north_yellow%10;
- data2<=north_yellow/10;
- end
- end
- //东西方向显示
- reg [6:0]east_red,east_green,east_yellow;
- always@(posedge clk1hz or negedge rstn)
- begin
- if(!rstn)
- begin
- east_red<=0;
- east_green<=0;
- east_yellow<=0;
- led[5:3]<=0;
- data5<=0;
- data6<=0;
- end
- else if(count<=d3-5)
- begin
- east_green<=d3-5-count;
- led[5:3]<=3'b011;
- data5<=east_green%10;
- data6<=east_green/10;
- end
- else if((count>=(d3-5))&&(count<=d3))
- begin
- east_yellow<=d3-count;
- led[5:3]<=3'b101;
- data5<=east_yellow%10;
- data6<=east_yellow/10;
- end
- else
- begin
- east_red<=d3+d3-count;
- led[5:3]<=3'b110;
- data5<=east_red%10;
- data6<=east_red/10;
- end
- end
- endmodule
工程.v文件https://download.csdn.net/download/m0_59487432/85684464?spm=1001.2014.3001.5503
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。