-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_div.v
67 lines (55 loc) · 1.82 KB
/
clock_div.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
`timescale 1ns / 1ps
// The `clock_div` module describes the frequency divider.
// It takes a 100MHz clock signal (clk) and divides its frequency
// into 25MHz, 100Hz, and 4Hz. Other modules are driven by these
// divided signals for different but synchronized behaviors.
module clock_div(
input wire clk,
output wire clk_25MHz,
output reg clk_1KHz = 0,
output reg clk_100Hz = 0,
output reg clk_50Hz = 0,
output reg clk_25Hz = 0,
output reg clk_4Hz = 0
);
reg [31:0] clk_counter = 0;
reg [31:0] clk_counter_1KHz = 0;
reg [31:0] clk_counter_100Hz = 0;
reg [31:0] clk_counter_50Hz = 0;
reg [31:0] clk_counter_25Hz = 0;
reg [31:0] clk_counter_4Hz = 0;
always @ (posedge clk) begin
clk_counter <= clk_counter + 1;
if (clk_counter_1KHz == 49_999) begin
clk_counter_1KHz <= 0;
clk_1KHz <= ~clk_1KHz;
end
else
clk_counter_1KHz <= clk_counter_1KHz + 1;
if (clk_counter_100Hz == 499_999) begin
clk_counter_100Hz <= 0;
clk_100Hz <= ~clk_100Hz;
end
else
clk_counter_100Hz <= clk_counter_100Hz + 1;
if (clk_counter_50Hz == 999_999) begin
clk_counter_50Hz<= 0;
clk_50Hz <= ~clk_50Hz;
end
else
clk_counter_50Hz <= clk_counter_50Hz + 1;
if (clk_counter_25Hz == 1_999_999) begin
clk_counter_25Hz<= 0;
clk_25Hz <= ~clk_25Hz;
end
else
clk_counter_25Hz <= clk_counter_25Hz + 1;
if (clk_counter_4Hz == 12_499_999) begin
clk_counter_4Hz <= 0;
clk_4Hz <= ~clk_4Hz;
end
else
clk_counter_4Hz <= clk_counter_4Hz + 1;
end
assign clk_25MHz = clk_counter[1];
endmodule