Clock Dividers
Divide a clock by 2, by N and by an odd N at 50% duty. Then see why logic should use an enable instead.
You Will Be Able To
- Divide a clock by 2^k with a counter bit.
- Divide by any N with a counter and a terminal count.
- Build an odd divider with a 50% duty cycle from both clock edges.
- Explain why logic should run on the main clock with an enable.
- Lesson
- G6.6
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A UART needs a bit clock 16 times slower than the system clock. A display needs a refresh tick every millisecond. Both come from a counter on the system clock.
- Cycle 0
At the rising edge at 15 ns, p goes high for one period.
- Cycle 1
Half a period later, at the falling edge, n copies p.
- Cycle 2
At the next rising edge p falls. n holds clk_out high for another half period.
- Cycle 3
clk_out was high for 3 half periods, 15 ns, and is now low for 15 ns.
Built from the lab: clk_out rises at 15 ns and falls at 30 ns. It is high and low for 15 ns each.
Divide by 2^k: A Counter Bit
Bit k of a binary counter toggles once every 2^k edges. So it is a square wave with 2^(k+1) times the clock period and a 50% duty cycle.
Divide by N: A Modulo-N Counter
A modulo-N counter (G6.5) wraps every N edges. Its terminal count is a pulse one clock period wide, once every N periods. That is a divided rate, but not a square wave.
For an even N, toggle a flop at every wrap of a counter modulo N/2. It is then high for N/2 periods and low for N/2.
For an odd N, rising edges alone cannot split the period evenly. The output can be high for 2 of 5 periods, or 3 of 5, never 2.5.
Odd N with a 50% Duty Cycle
The falling edge comes half a period after the rising edge. A flop on the falling edge can delay a signal by exactly half a period.
- Clock / reset
- Storage
- Clock
- Data
- Rising edges
A modulo-N counter makes p, high for (N - 1)/2 periods. For N = 3 that is one period.
- Falling edge
A flop on the falling edge copies p into n, half a period later.
- OR
clk_out = p OR n is high for (N - 1)/2 + 1/2 = N/2 periods: exactly half.
Read the diagram as text
- clk: input clock
- Counter: modulo N, rising edge
- p flop: rising edge
- n flop: falling edge
- OR: clk_out = p | n
- clk → Counter
- Counter → p flop: count
- p flop → n flop: p
- p flop → OR: p
- n flop → OR: n
The n flop holds a delayed copy of p, never an independent signal. So the two pulses overlap by exactly half a period.
The lab builds this for N = 3. Its bench measures 15 ns high and 15 ns low with a 10 ns clock.
Use an Enable and Keep One Clock
A divided output is a flop output. It changes a clock-to-Q delay after the main clock edge. Logic clocked by it sits on a second, skewed clock.
Divided clock for logic
always_ff @(posedge clk_div4)
slow <= slow + 1;
A second clock, late by a clock-to-Q delay. Data crossing between the two domains needs care (G5.9, G5.12).
One clock and an enable
always_ff @(posedge clk)
if (tick) slow <= slow + 1;
One clock for everything. tick is 1 for one cycle in four, so slow still moves at a quarter of the rate.
A divided clock that leaves the chip, or feeds an external part, is fine. Timing tools must still be told about it as a generated clock (STA T4.2).
Asked in interviews
Design a divide-by-3 clock with a 50% duty cycle.
A modulo-3 counter on the rising edge makes p, high for one period. A falling-edge flop copies p half a period later into n. The output p OR n is high for 1.5 periods out of 3.
Why not use a divided clock to run part of the design?
It adds a second clock, skewed by the divider flop's delay, so the paths between the two need clock-crossing care. A clock enable on the main clock gives the same rate with one clock.
How do you divide a clock by 2?
A T flip-flop with T = 1 toggles at every edge, and so does a D flip-flop fed with its own inverted output. That gives half the frequency at 50% duty.
Try It
clk_out rises at the first rising edge after the reset, 15 ns. From then on it is high for 15 ns and low for 15 ns. That is a 30 ns period, three clock periods, at 50%.
Delete | n from the last line and run again. clk_out is now high for 10 ns of every 30: rising edges alone give 1 period in 3.
Change the counter to wrap at 4 for divide by 5. Then work out how long p must stay high.
t=15 rise t=30 fall (high for 15 ns) t=45 rise (low for 15 ns) t=60 fall (high for 15 ns) t=75 rise (low for 15 ns) t=90 fall (high for 15 ns)Read included file: tb.sv
// tb.sv: a 10 ns clock; prints each edge of clk_out and how long the level before it lasted (read-only).
`timescale 1ns/1ns
module tb;
logic clk = 0, rst = 1;
wire clk_out;
time t_rise = 0, t_fall = 0;
div3 dut (.clk(clk), .rst(rst), .clk_out(clk_out));
always #5 clk = ~clk; // rising edges at 5, 15, 25, ...
always @(posedge clk_out) if (!rst) begin
if (t_fall > 0) $display("t=%0d rise (low for %0d ns)", $time, $time - t_fall);
else $display("t=%0d rise", $time);
t_rise = $time;
end
always @(negedge clk_out) if (!rst) begin
$display("t=%0d fall (high for %0d ns)", $time, $time - t_rise);
t_fall = $time;
end
initial begin
#12 rst = 0; // the first rising edge after the reset is at 15
#80 $finish;
end
endmodule
Check Yourself
Common Mistakes
Wrong Make an odd divider from rising edges only and expect a 50% duty cycle.
Rising edges come once per period, so the output can only be high for a whole number of periods. Divide by 5 gives 2 of 5: 40%.
Fix Add a falling-edge flop that delays the output by half a period, and OR the two.
Wrong OR a rising-edge signal with a falling-edge signal made by an unrelated counter.
The two pulses do not overlap where they should. The output gets the wrong length or a pulse that is far too short.
Fix Make the falling-edge signal a half-period-delayed copy of the rising-edge one.
Wrong Clock part of the logic with a divided clock "to save power".
The divided clock arrives late after its flop, so the two domains are skewed (G5.9). Tools must also be told about the new clock.
Fix Keep one clock and give the slow logic an enable that is 1 once every N cycles.
Remember
- Counter bit k divides the clock by 2^(k+1) with a 50% duty cycle.
- A modulo-N counter gives a one-period pulse every N periods; even N gives 50% by toggling at N/2.
- Odd N at 50%: OR a rising-edge pulse of (N - 1)/2 periods with its falling-edge copy.
- Inside a design, run slow logic on the main clock with an enable, not on a divided clock.