问题描述

将时分秒计时器,变成可调的计时器。可以分别对小时、分、秒设定初始值。 设定的方法可以采用,从外部输入一个值,比如小时可以从5开始;也可以通过按键把小时当前的值增加或者减少来实现值的调整

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module clock2(clk50,key,reset,clk1,out5,out4,out3,out2,out1,out0,flagclk,up,down);
input clk50,key,reset,flagclk,up,down;
output clk1; // clk1:新产生的1Hz信号
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)
reg [3:0] flag=0;

divclk1hz dc(reset,clk50,clk1);
always@(posedge clk1,posedge key,posedge flagclk,posedge up,posedge down)
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 if(flagclk)
begin
flag=(flag+1)%4;
end
else if(up)
begin
if(flag==1)
begin
if(hour<23) hour=hour+1;
else hour=0;
end
if(flag==2)
begin
if(minutes<59) minutes=minutes+1;
else minutes=0;
end
if(flag==3)
begin
if(seconds<59) seconds=seconds+1;
else seconds=0;
end
out5=hour/10;
out4=hour%10;
out3=minutes/10;
out2=minutes%10;
out1=seconds/10;
out0=seconds%10;
end
else if(down)
begin
if(flag==1)
begin
if(hour>0) hour=hour-1;
else hour=23;
end
if(flag==2)
begin
if(minutes>0) minutes=minutes-1;
else minutes=59;
end
if(flag==3)
begin
if(seconds>0) seconds=seconds-1;
else seconds=59;
end
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==250)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
`timescale 1 ps/ 1 ps
module clock2_vlg_tst();

reg eachvec;

reg clk50;
reg down;
reg flagclk;
reg key;
reg reset;
reg up;

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;

clock2 i1 (

.clk1(clk1),
.clk50(clk50),
.down(down),
.flagclk(flagclk),
.key(key),
.out0(out0),
.out1(out1),
.out2(out2),
.out3(out3),
.out4(out4),
.out5(out5),
.reset(reset),
.up(up)
);
initial
begin
$display("Running testbench");
clk50=1;
key=0;
reset=1;
flagclk=0;
up=0;
down=0;
$monitor($realtime/10000,,,"%d %d : %d %d : %d %d",out5,out4,out3,out2,out1,out0);
#1000001
$display("hour up");
flagclk=1;
#1 flagclk=0;
#1 up=1;
#1 up=0;
#1000
$display("minutes down");
flagclk=1;
#1 flagclk=0;
#1 down=1;
#1 down=0;


end

always
begin
#10 clk50=~clk50;
end
endmodule

仿真测试结果