-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.v
310 lines (296 loc) · 5.04 KB
/
clock.v
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 20:34:52 04/01/2017
// Design Name:
// Module Name: clock
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module clock(
input wire[0:1] sw,
input clk,
input clr,
input seth, //设置小时
input setm, //设置分钟
//下面的output变量都是用来输出显示的
output reg[3:0] an,
output reg[6:0] atog,
output reg[3:0] secondLow, //秒数个位
output reg[3:0] secondHigh //秒数十位
);
reg [3:0] hourLow; //小时个位
reg [3:0] hourHigh; //小时十位
reg [3:0] minuteLow;//分钟个位
reg [3:0] minuteHigh;//分钟十位
reg [26:0] counter; //计数器
reg [7:0] hour; //小时数字
reg [7:0] minute; //分钟数字
reg [7:0] hourc; //闹钟小时
reg [7:0] minutec; //闹钟分钟
reg [7:0] second; //秒数字
reg [3:0] digit; //对于的数码管
reg [1:0] s;
integer i; //用于求余数等时
always@(posedge clk or posedge clr)
begin
if (clr == 1)
begin
counter <= 0;
hourLow <= 0;
hourHigh <=0;
minuteLow <= 0;
minuteHigh <= 0;
secondLow <= 0;
secondHigh <= 0;
hour <= 0;
minute <= 0;
second <= 0;
end
else if(counter == 50000000)
begin
counter <= 0;
if (setm == 1 && sw[1] == 0) //设置分钟
begin
if (minute == 59)
begin
minute <= 0;
if (hour == 23 && sw[0] == 0)
begin
hour <= 0;
end
else if (hour == 11 && sw[0] == 1)
begin
hour <= 0;
end
else
begin
hour <= hour + 1;
end
end
else if(minute < 59)
begin
minute <= minute + 1;
end
end
if (setm == 1 && sw[1] == 1) //设置闹钟分钟
begin
if (minutec == 59)
begin
minutec <= 0;
end
else if (minutec < 59)
begin
minutec <= minutec + 1;
end
end
if (seth ==1 && sw[1] == 0) //设置小时
begin
if (hour == 23 && sw[0] == 0)
begin
hour <= 0;
minute <= 0;
second <= 0;
end
else if (hour == 11 && sw[0] == 1)
begin
hour <= 0;
minute <= 0;
second <= 0;
end
else
begin
hour <= hour + 1;
end
end
if (seth == 1 && sw[1] == 1) //设置闹钟小时
begin
if (hourc == 23 && sw[0] == 0)
begin
hourc <= 0;
end
else if (hourc == 11 && sw[0] == 1)
begin
hourc <= 0;
end
else
begin
hourc <= hourc + 1;
end
end
// 普通的计时
if (second == 59)
begin
second <= 0;
if (minute == 59)
begin
minute <= 0;
if (hour == 23 && sw[0] == 0)
begin
hour <= 0;
minute <= 0;
second <= 0;
end
else if (hour == 11 && sw[0] == 1)
begin
hour <= 0;
minute <= 0;
second <= 0;
end
else
begin
hour <= hour + 1;
end
end
else if (minute < 59)
begin
minute <= minute + 1;
end
end
else if(second < 59)
begin
second <= second + 1;
end
if (sw[0] == 0)
begin
if (sw[1] == 0)
begin
for (i=0; i<3; i=i+1)//小时处理
begin
if ((hour-i*10)<10)
begin
hourLow <= hour - i*10;
hourHigh <= i;
i = 3;
end
end
end
else if(sw[1] == 1)
begin
for (i=0; i<3; i=i+1)//小时处理
begin
if ((hourc-i*10)<10)
begin
hourLow <= hourc - i*10;
hourHigh <= i;
i = 3;
end
end
end
end
else if (sw[0] == 1)
begin
if (sw[1] == 0)
begin
for (i=0; i<2; i=i+1)//小时处理
begin
if ((hour-i*10)<10)
begin
hourLow <= hour - i*10;
hourHigh <= i;
i = 2;
end
end
end
else if (sw[1] == 1)
begin
for (i=0; i<2; i=i+1)//小时处理
begin
if ((hourc-i*10)<10)
begin
hourLow <= hourc - i*10;
hourHigh <= i;
i = 2;
end
end
end
end
for (i=0; i<6; i=i+1)//分处理
begin
if (sw[1] == 0)
begin
if((minute-i*10) < 10)
begin
minuteLow <= minute - i*10;
minuteHigh <= i;
i = 6;
end
end
else if (sw[1] == 1)
begin
if((minutec-i*10) < 10)
begin
minuteLow <= minutec - i*10;
minuteHigh <= i;
i = 6;
end
end
end
if (minute == 0 || (hourc == hour && minute == minutec))
begin
if (secondLow == 0)
begin
secondLow <= 4'b1111;
secondHigh <= 4'b1111;
end
else if (secondLow > 0)
begin
secondLow <= 4'b0000;
secondHigh <= 4'b0000;
end
end
else if (minute > 0)
begin
for(i=0; i<6; i=i+1)// 秒处理
begin
if((second-i*10)<10)
begin
secondLow <= second - i*10;
secondHigh <= i;
i = 6;
end
end
end
end
else
begin
counter <= counter+1;
end
end
always@(*)
begin
an = 4'b1111;
s<=counter[14:13];
an[s]=0;
case(s)
0:digit<=minuteLow;
1:digit<=minuteHigh;
2:digit<=hourLow;
3:digit<=hourHigh;
endcase
case(digit)
0:atog=7'b0000001;
1:atog=7'b1001111;
2:atog=7'b0010010;
3:atog=7'b0000110;
4:atog=7'b1001100;
5:atog=7'b0100100;
6:atog=7'b0100000;
7:atog=7'b0001111;
8:atog=7'b0000000;
9:atog=7'b0000100;
default:atog=7'b0000001;
endcase
end
endmodule