题外话
好久没更新,转眼大二下了,去年我的城市经历新冠疫情,我自己也沉迷于某csgo游戏开摆500小时,终于在上个月醒悟,正式开始fpga的学习,是跟这实验室的一位带工程师学习的,话不多说开始。
学习路线
主要看着B站开源骚客和小梅哥的视频,开源骚客的视频我看完了,最近在看小梅哥,其实我发现野火的电子文档写的也不错,然后还有特权同学的fpga教程,我没有怎么看数电,所以学起来感觉有点磕磕绊绊,比如D触发器,pwm,时钟那里。
首先fpga学习确实跟单片机不太一样,可以把fpga理解一块面包板,需要我们自己设定输入输出端口。fpga学习确实跟单片机不一样,时钟是时序逻辑的灵魂,我们要考虑好如何设计时钟,搞懂非阻塞赋值,并行执行思想。
方案设计
原件选择
开发板选择altera MAXII epm240t100c5n,其实这是一块cpld还不是fpga。
软件选择quartusII 13.1 这里注意看自己的器件库 我的芯片过于古老,软件不兼容,我还额外去intel官网下载了maxII的devices。
同时建议提前了解rgb和数码管原理。
设计方案
0、复位时 数码管包括小数点全部亮 同时也方便检测器件是否损坏
1、红灯10秒倒计时
2、然后黄灯3秒,
3、然后绿灯5秒
4、再黄灯3秒
设计思想
1、设计时钟一,用于选择10+3+3+21个状态
2、设计时钟二,用于数码管动态刷新一个周期
3、对四位数码管位选和段选的设计
4、对数码管共阴极共阳极的选择
代码
module traffic(
input sclk ,
input s_rst_n ,
output reg [3:0] SEG ,
output reg [7:0] SMG ,
output reg beep ,
output reg [ 2:0] RGB
);
localparam DELAY_1S = 'd24_999_999;
localparam DELAY_10MS = 'd299_999;
reg [4:0] TIME_21 ;
reg [4:0] TIME_4 ;
reg [25:0] cnt_1s ;
reg [18:0] cnt_10ms ;
reg [7:0] SEG1 ;
reg [7:0] SEG2 ;
reg [7:0] SEG3 ;
reg [7:0] SEG4 ;
localparam NULL = 8'b0000_0000;
localparam num0 = 8'b0011_1111;
localparam num9 = 8'b0110_1111;
localparam num8 = 8'b0111_1111;
localparam num7 = 8'b0000_0111;
localparam num6 = 8'b0111_1101;
localparam num5 = 8'b0110_1101;
localparam num4 = 8'b0110_0110;
localparam num3 = 8'b0100_1111;
localparam num2 = 8'b0101_1011;
localparam num1 = 8'b0000_0110;
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'b0)
cnt_1s <= 'd0;
else if (cnt_1s == DELAY_1S)
cnt_1s <= 'd0;
else
cnt_1s <= cnt_1s + 1'b1;
end
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'b0)
cnt_10ms <= 'd0;
else if (cnt_10ms == DELAY_10MS)
cnt_10ms <= 'd0;
else
cnt_10ms <= cnt_10ms + 1'b1;
end
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
TIME_21 <= 1'd1;
else if (TIME_21 == 21 && cnt_1s == DELAY_1S)
TIME_21 <= 1'd1;
else if (cnt_1s == DELAY_1S)
TIME_21 <= TIME_21 + 1'd1;
end
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
TIME_4 <= 1'd0;
else if (TIME_4 == 4 && cnt_10ms == DELAY_10MS)
TIME_4 <= 1'd1;
else if (cnt_10ms == DELAY_10MS)
TIME_4 <= TIME_4 + 1'd1;
end
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
SEG <= 4'b0000;
else if (TIME_4 == 01)
SEG <= 4'b0111;
else if (TIME_4 == 02)
SEG <= 4'b1011;
else if (TIME_4 == 03)
SEG <= 4'b1101;
else
SEG <= 4'b1110;
end
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
SMG <= 8'b1111_1111;
else if (TIME_4 == 01)
SMG <= SEG4;
else if (TIME_4 == 02)
SMG <= SEG3;
else if (TIME_4 == 03)
SMG <= SEG2;
else
SMG <= SEG1;
end
always@(*)
begin
case (TIME_21)
01: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,num1,num0};
02: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num9};
03: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num8};
04: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num7};
05: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num6};
06: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num5};
07: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num4};
08: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num3};
09: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num2};
10: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num1};
11: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num3};
12: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num2};
13: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num1};
14: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num5};
15: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num4};
16: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num3};
17: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num2};
18: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num1};
19: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num3};
20: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num2};
21: {SEG1,SEG2,SEG3,SEG4} <={NULL,NULL,NULL,num1};
endcase
end
always@(*)
begin
if(s_rst_n == 1'b0)
RGB <= 3'b000;
else if (TIME_21>=1 &&TIME_21<=10)
RGB <= 3'b110; //红
else if (TIME_21>=11 &&TIME_21<=13)
RGB <= 3'b100; //黄
else if (TIME_21>=14 &&TIME_21<=18)
RGB <= 3'b101; //绿
else if (TIME_21>=19 &&TIME_21<=21)
RGB <= 3'b100; //黄
end
endmodule
实物展示
忘了录复位时白灯了,大家自己想象。
文章来源:https://www.toymoban.com/news/detail-500252.html
小结
搞电子要耐得住寂寞,要学习的很多,大家加油。欢迎大家互相交流学习方法路线心得,csdn不能及时回复,抱歉!文章来源地址https://www.toymoban.com/news/detail-500252.html
到了这里,关于基于FPGA的倒计时交通信号灯系统(使用quartusII)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!