基于FPGA的数字时钟(使用vivado)
使用两个四位数码管,可以实现时钟分钟秒钟显示,高两位设置不显示。
换了一个新开发板,nexys4ddr,资料不多,最多使用的就是一本英文Reference Manual。
其实是老师觉得我计数器还差点,得再练练。
原件选择
Digilent NEXYS4DDR
Vivado2018.3
设计方案
60进制秒钟计数然后进1分钟
60进制分钟计数然后进1小时
设计思想
1、设计时钟一,用于计数器计一秒,根据芯片晶振选择计算
2、设计时钟二,用于计数60秒
3、设计时钟三,用于计数60分
4、设计时钟四,用于计数24时
5、设计时钟五,用于选择数码管刷新频率
代码
module clock(
input sclk ,
input s_rst_n ,
input YEAR ,
input MONTH ,
input DAY ,
output reg [7:0] SEG ,
output reg [7:0] SMG
);
localparam DELAY_1S = 'd9_999_999;
localparam DELAY_1M = 'd59;
localparam DELAY_1H = 'd59;
localparam DELAY_1D = 'd23;
localparam DELAY_10MS = 'd199_999;
reg [3:0] TIME_8 ;
reg [25:0] cnt_1s ;
reg [5:0] cnt_1m ;
reg [5:0] cnt_1h ;
reg [4:0] cnt_1d ;
reg [18:0] cnt_10ms ;
reg [7:0] SEG1 ;
reg [7:0] SEG2 ;
reg [7:0] SEG3 ;
reg [7:0] SEG4 ;
reg [7:0] SEG5 ;
reg [7:0] SEG6 ;
reg [7:0] SEG7 ;
reg [7:0] SEG8 ;
localparam NULL = 8'b1111_1111;
localparam num0 = 8'b1100_0000;
localparam num9 = 8'b1001_0000;
localparam num8 = 8'b1000_0000;
localparam num7 = 8'b1111_1000;
localparam num6 = 8'b1000_0010;
localparam num5 = 8'b1001_0010;
localparam num4 = 8'b1001_1001;
localparam num3 = 8'b1011_0000;
localparam num2 = 8'b1010_0100;
localparam num1 = 8'b1111_1001;
//秒计时
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_1m <= 'd0;
else if (cnt_1s == DELAY_1S && cnt_1m == DELAY_1M)
cnt_1m <= 'd0;
else if (cnt_1s == DELAY_1S)
cnt_1m <= cnt_1m + 1'b1;
end
//时计时
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'b0)
cnt_1h <= 'd0;
else if (cnt_1s == DELAY_1S && cnt_1m == DELAY_1M && cnt_1h == DELAY_1H)
cnt_1h <= 'd0;
else if (cnt_1s == DELAY_1S && cnt_1m == DELAY_1M)
cnt_1h <= cnt_1h + 1'b1;
end
//天计时
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'b0)
cnt_1d <= 'd0;
else if (cnt_1s == DELAY_1S && cnt_1m == DELAY_1M && cnt_1h == DELAY_1H && cnt_1d == DELAY_1D)
cnt_1d <= 'd0;
else if (cnt_1s == DELAY_1S && cnt_1m == DELAY_1M && cnt_1h == DELAY_1H)
cnt_1d <= cnt_1d + 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
//位选1
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
TIME_8 <= 1'd0;
else if (TIME_8 == 8 && cnt_10ms == DELAY_10MS)
TIME_8 <= 1'd1;
else if (cnt_10ms == DELAY_10MS)
TIME_8 <= TIME_8 + 1'd1;
end
//位选2
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
SEG <= 8'b0000_0000;
else if (TIME_8 == 01)
SEG <= 8'b1111_1110;
else if (TIME_8 == 02)
SEG <= 8'b1111_1101;
else if (TIME_8 == 03)
SEG <= 8'b1111_1011;
else if (TIME_8 == 04)
SEG <= 8'b1111_0111;
else if (TIME_8 == 05)
SEG <= 8'b1110_1111;
else if (TIME_8 == 06)
SEG <= 8'b1101_1111;
else if (TIME_8 == 07)
SEG <= 8'b1011_1111;
else if (TIME_8 == 08)
SEG <= 8'b0111_1111;
end
//段选
always @ (posedge sclk or negedge s_rst_n)
begin
if(s_rst_n == 1'd0)
SMG <= 8'b0000_0000;
else if (TIME_8 == 01)
SMG <= SEG4;
else if (TIME_8 == 02)
SMG <= SEG3;
else if (TIME_8 == 03)
SMG <= SEG2;
else if (TIME_8 == 04)
SMG <= SEG1;
else if (TIME_8 == 05)
SMG <= SEG6;
else if (TIME_8 == 06)
SMG <= SEG5;
else if (TIME_8 == 07)
SMG <= NULL;
else if (TIME_8 == 08)
SMG <= NULL;
end
//秒显示
always@(*)
begin
case(cnt_1m)
00: {SEG3,SEG4} <={num0,num0};
01: {SEG3,SEG4} <={num0,num1};
02: {SEG3,SEG4} <={num0,num2};
03: {SEG3,SEG4} <={num0,num3};
04: {SEG3,SEG4} <={num0,num4};
05: {SEG3,SEG4} <={num0,num5};
06: {SEG3,SEG4} <={num0,num6};
07: {SEG3,SEG4} <={num0,num7};
08: {SEG3,SEG4} <={num0,num8};
09: {SEG3,SEG4} <={num0,num9};
10: {SEG3,SEG4} <={num1,num0};
11: {SEG3,SEG4} <={num1,num1};
12: {SEG3,SEG4} <={num1,num2};
13: {SEG3,SEG4} <={num1,num3};
14: {SEG3,SEG4} <={num1,num4};
15: {SEG3,SEG4} <={num1,num5};
16: {SEG3,SEG4} <={num1,num6};
17: {SEG3,SEG4} <={num1,num7};
18: {SEG3,SEG4} <={num1,num8};
19: {SEG3,SEG4} <={num1,num9};
20: {SEG3,SEG4} <={num2,num0};
21: {SEG3,SEG4} <={num2,num1};
22: {SEG3,SEG4} <={num2,num2};
23: {SEG3,SEG4} <={num2,num3};
24: {SEG3,SEG4} <={num2,num4};
25: {SEG3,SEG4} <={num2,num5};
26: {SEG3,SEG4} <={num2,num6};
27: {SEG3,SEG4} <={num2,num7};
28: {SEG3,SEG4} <={num2,num8};
29: {SEG3,SEG4} <={num2,num9};
30: {SEG3,SEG4} <={num3,num0};
31: {SEG3,SEG4} <={num3,num1};
32: {SEG3,SEG4} <={num3,num2};
33: {SEG3,SEG4} <={num3,num3};
34: {SEG3,SEG4} <={num3,num4};
35: {SEG3,SEG4} <={num3,num5};
36: {SEG3,SEG4} <={num3,num6};
37: {SEG3,SEG4} <={num3,num7};
38: {SEG3,SEG4} <={num3,num8};
39: {SEG3,SEG4} <={num3,num9};
40: {SEG3,SEG4} <={num4,num0};
41: {SEG3,SEG4} <={num4,num1};
42: {SEG3,SEG4} <={num4,num2};
43: {SEG3,SEG4} <={num4,num3};
44: {SEG3,SEG4} <={num4,num4};
45: {SEG3,SEG4} <={num4,num5};
46: {SEG3,SEG4} <={num4,num6};
47: {SEG3,SEG4} <={num4,num7};
48: {SEG3,SEG4} <={num4,num8};
49: {SEG3,SEG4} <={num4,num9};
50: {SEG3,SEG4} <={num5,num0};
51: {SEG3,SEG4} <={num5,num1};
52: {SEG3,SEG4} <={num5,num2};
53: {SEG3,SEG4} <={num5,num3};
54: {SEG3,SEG4} <={num5,num4};
55: {SEG3,SEG4} <={num5,num5};
56: {SEG3,SEG4} <={num5,num6};
57: {SEG3,SEG4} <={num5,num7};
58: {SEG3,SEG4} <={num5,num8};
59: {SEG3,SEG4} <={num5,num9};
endcase
end
//分显示
always@(*)
begin
case(cnt_1h)
00: {SEG1,SEG2} <={num0,num0};
01: {SEG1,SEG2} <={num0,num1};
02: {SEG1,SEG2} <={num0,num2};
03: {SEG1,SEG2} <={num0,num3};
04: {SEG1,SEG2} <={num0,num4};
05: {SEG1,SEG2} <={num0,num5};
06: {SEG1,SEG2} <={num0,num6};
07: {SEG1,SEG2} <={num0,num7};
08: {SEG1,SEG2} <={num0,num8};
09: {SEG1,SEG2} <={num0,num9};
10: {SEG1,SEG2} <={num1,num0};
11: {SEG1,SEG2} <={num1,num1};
12: {SEG1,SEG2} <={num1,num2};
13: {SEG1,SEG2} <={num1,num3};
14: {SEG1,SEG2} <={num1,num4};
15: {SEG1,SEG2} <={num1,num5};
16: {SEG1,SEG2} <={num1,num6};
17: {SEG1,SEG2} <={num1,num7};
18: {SEG1,SEG2} <={num1,num8};
19: {SEG1,SEG2} <={num1,num9};
20: {SEG1,SEG2} <={num2,num0};
21: {SEG1,SEG2} <={num2,num1};
22: {SEG1,SEG2} <={num2,num2};
23: {SEG1,SEG2} <={num2,num3};
24: {SEG1,SEG2} <={num2,num4};
25: {SEG1,SEG2} <={num2,num5};
26: {SEG1,SEG2} <={num2,num6};
27: {SEG1,SEG2} <={num2,num7};
28: {SEG1,SEG2} <={num2,num8};
29: {SEG1,SEG2} <={num2,num9};
30: {SEG1,SEG2} <={num3,num0};
31: {SEG1,SEG2} <={num3,num1};
32: {SEG1,SEG2} <={num3,num2};
33: {SEG1,SEG2} <={num3,num3};
34: {SEG1,SEG2} <={num3,num4};
35: {SEG1,SEG2} <={num3,num5};
36: {SEG1,SEG2} <={num3,num6};
37: {SEG1,SEG2} <={num3,num7};
38: {SEG1,SEG2} <={num3,num8};
39: {SEG1,SEG2} <={num3,num9};
40: {SEG1,SEG2} <={num4,num0};
41: {SEG1,SEG2} <={num4,num1};
42: {SEG1,SEG2} <={num4,num2};
43: {SEG1,SEG2} <={num4,num3};
44: {SEG1,SEG2} <={num4,num4};
45: {SEG1,SEG2} <={num4,num5};
46: {SEG1,SEG2} <={num4,num6};
47: {SEG1,SEG2} <={num4,num7};
48: {SEG1,SEG2} <={num4,num8};
49: {SEG1,SEG2} <={num4,num9};
50: {SEG1,SEG2} <={num5,num0};
51: {SEG1,SEG2} <={num5,num1};
52: {SEG1,SEG2} <={num5,num2};
53: {SEG1,SEG2} <={num5,num3};
54: {SEG1,SEG2} <={num5,num4};
55: {SEG1,SEG2} <={num5,num5};
56: {SEG1,SEG2} <={num5,num6};
57: {SEG1,SEG2} <={num5,num7};
58: {SEG1,SEG2} <={num5,num8};
59: {SEG1,SEG2} <={num5,num9};
endcase
end
//时显示
always@(*)
begin
case(cnt_1d)
00: {SEG5,SEG6} <={num0,num0};
01: {SEG5,SEG6} <={num0,num1};
02: {SEG5,SEG6} <={num0,num2};
03: {SEG5,SEG6} <={num0,num3};
04: {SEG5,SEG6} <={num0,num4};
05: {SEG5,SEG6} <={num0,num5};
06: {SEG5,SEG6} <={num0,num6};
07: {SEG5,SEG6} <={num0,num7};
08: {SEG5,SEG6} <={num0,num8};
09: {SEG5,SEG6} <={num0,num9};
10: {SEG5,SEG6} <={num1,num0};
11: {SEG5,SEG6} <={num1,num1};
12: {SEG5,SEG6} <={num1,num2};
13: {SEG5,SEG6} <={num1,num3};
14: {SEG5,SEG6} <={num1,num4};
15: {SEG5,SEG6} <={num1,num5};
16: {SEG5,SEG6} <={num1,num6};
17: {SEG5,SEG6} <={num1,num7};
18: {SEG5,SEG6} <={num1,num8};
19: {SEG5,SEG6} <={num1,num9};
20: {SEG5,SEG6} <={num2,num0};
21: {SEG5,SEG6} <={num2,num1};
22: {SEG5,SEG6} <={num2,num2};
23: {SEG5,SEG6} <={num2,num3};
endcase
end
endmodule
实物展示
由于这个代码将速率方法了十倍,故手机录像时,无法捕捉到动态刷新,只能贴一张图片了,不能做成pdf。
文章来源:https://www.toymoban.com/news/detail-780119.html
小结
换了开发板和平台,vivado编译一次是真的慢,也得慢慢琢磨。文章来源地址https://www.toymoban.com/news/detail-780119.html
到了这里,关于基于FPGA的数字时钟(使用vivado)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!