问题描述

计时器:在6个七段管上分别显示 小时(0-23或11)、分(0-59)、秒(0-59),各占2个管。外部时钟50Mhz。可以用按键来产生一个复位信号key,当按键按下立刻(异步)将时间复位成0小时、0分、0秒重新开始计时

Verilog代码

主模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module clock(clk50,key,reset,clk1,out5,out4,out3,out2,out1,out0);
input clk50,key,reset;
output clk1;
output reg [6:0] out5=0; // 时_十位
output reg [6:0] out4=0; // 时_个位
output reg [6:0] out3=0; // 分_十位
output reg [6:0] out2=0; // 分_个位
output reg [6:0] out1=0; // 秒_十位
output reg [6:0] out0=0; // 秒_个位
reg [6:0] hour=0; // 时(0-23)
reg [6:0] minutes=0; // 分(0-59)
reg [6:0] seconds=0; // 秒(0-59)

divclk1hz dc(reset,clk50,clk1);
always@(posedge clk1,posedge key)
begin
if(key)
begin
hour=0;
minutes=0;
seconds=0;
out5=hour/10;
out4=hour%10;
out3=minutes/10;
out2=minutes%10;
out1=seconds/10;
out0=seconds%10;
end
else
begin
if(seconds<59) seconds=seconds+1;
else
begin
if(seconds==59)
begin
seconds=0;
if(minutes<59) minutes=minutes+1;
else
begin
if(minutes==59)
begin
minutes=0;
if(hour<23) hour=hour+1;
else
begin
if(hour==23) hour=0;
end
end
end
end
end
out5=hour/10;
out4=hour%10;
out3=minutes/10;
out2=minutes%10;
out1=seconds/10;
out0=seconds%10;
end
end

task dec_out;
input integer decc; // 输入,十进制数
output reg[6:0] outt; // 输出,7位二进制数值
if(decc==0) outt=7'b1000000; // 七段管显示0
else if(decc==1) outt=7'b1111001; // 七段管显示1
else if(decc==2) outt=7'b0100100; // 七段管显示2
else if(decc==3) outt=7'b0110000; // 七段管显示3
else if(decc==4) outt=7'b0011001; // 七段管显示4
else if(decc==5) outt=7'b0010010; // 七段管显示5
else if(decc==6) outt=7'b0000010; // 七段管显示6
else if(decc==7) outt=7'b1111000; // 七段管显示7
else if(decc==8) outt=7'b0000000; // 七段管显示8
else if(decc==9) outt=7'b0011000; // 七段管显示9
else outt=7'b1111111; // 七段管不显示
endtask

endmodule

分频模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module divclk1hz(reset,clk50,clk1); 
input clk50,reset;
output reg clk1=1;
integer i=0;
always@(posedge clk50)
begin
if(!reset)
begin
i=1;
end
else
begin
if(i==25000000)
begin
i=1;
clk1=~clk1;
end
else i=i+1;
end
end
endmodule

仿真测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
`timescale 1 ps/ 1 ps
module clock_vlg_tst();
reg eachvec;
reg clk50;
reg key;
reg reset;

wire clk1;
wire [6:0] out0;
wire [6:0] out1;
wire [6:0] out2;
wire [6:0] out3;
wire [6:0] out4;
wire [6:0] out5;

clock i1 (

.clk1(clk1),
.clk50(clk50),
.key(key),
.out0(out0),
.out1(out1),
.out2(out2),
.out3(out3),
.out4(out4),
.out5(out5),
.reset(reset)
);
initial
begin
$display("Running testbench");
clk50=1;
key=0;
reset=1;
$monitor($realtime/1000000000,,,"%d %d : %d %d : %d %d",out5,out4,out3,out2,out1,out0);
end



always
begin
#10 clk50=~clk50;

end
endmodule

仿真测试结果