Draft preview. Lessons are being written and reviewed.

Digital Design / Module 5: Counters and frequency dividers

30. Odd division with 50% duty cycle

Solve a favourite interview problem.

Lecture coming soon

Lab: Frequency divider lab

The idea

Dividing by an odd number with a 50% duty cycle is a favourite interview question. It is hard because the output must change at a mid-cycle point. For divide-by-3, the output should be high for 1.5 input cycles, and using only rising edges you can only change on whole cycles. The answer is to use both clock edges.

The method for divide-by-3

  • With rising-edge flops, make a waveform p that is high for 1 cycle and low for 2.
  • Pass it through a falling-edge flop, to get n: the same waveform, half a cycle later.
  • OR the two together. p | n stays high for 1.5 cycles out of 3, which is exactly 50%.
module div3 (input clk, rst, output out);
  reg [1:0] cnt;
  reg p, n;

  always_ff @(posedge clk)
    if (rst) begin
      cnt <= 2'd0;
      p   <= 1'b0;
    end else begin
      cnt <= (cnt == 2'd2) ? 2'd0 : cnt + 2'd1;
      p   <= (cnt == 2'd2);
    end

  always_ff @(negedge clk)
    if (rst) n <= 1'b0;
    else     n <= p;

  assign out = p | n;
endmodule

Other odd numbers

For any odd N, make a rising-edge waveform high for (N-1)/2 cycles. Its half-cycle-delayed copy overlaps it, and the OR is high for N/2 cycles. Check divide-by-5: high for 2, plus half a cycle, gives 2.5 of 5.

Caveats in silicon

The result depends on both clock edges, so its duty cycle follows the input clock's duty cycle. The two paths into the OR should also be balanced, since any mismatch shifts the output edges. Combining signals with a gate on a clock path also needs care, which is why real designs use a specially built clock divider cell.

Common mistake

Quoting the design without saying how long it takes to start. It needs a reset to give a known phase. Mention that the input duty cycle must be close to 50%, too. These details are what interviewers listen for.

Key ideas

  • Plain odd division can't be 50% from one edge.
  • Combine a rising-edge and a falling-edge waveform.
  • OR-ing them gives the 50% output, assuming matching delays.

Your exercise

Design a divide-by-3 with 50% duty cycle.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: The output runs at one third of the input frequency, but it is high for 2 of every 3 cycles, not 1.5.

Interview questions

coreDivide-by-3 with 50% duty?

basicWhy can't a divide-by-3 with 50% duty cycle be built from rising-edge flops alone?

basicFor a divide-by-5 with 50% duty cycle using the rising-edge and falling-edge method, how long is the rising-edge waveform high, and how long is the final output high?

coreIn a divide-by-3, someone takes the copy n from a rising-edge flop instead of a falling-edge flop. What duty cycle results?

always @(posedge clk) n <= p;   // should be negedge
assign out = p | n;

coreA 300 MHz clock is divided by 3 with 50% duty cycle. What are the output frequency and its high time?

coreThe divide-by-3 is simulated without a reset, and its output stays x. Why, and what does that tell you about the hardware?

coreThis divider works for any odd N. For N = 7, how long is p high and how long is out high?

always @(posedge clk)
  if (rst) begin cnt <= 0; p <= 0; end
  else begin
    cnt <= (cnt == N-1) ? 0 : cnt + 1;
    p   <= (cnt < (N-1)/2);
  end

always @(negedge clk)
  if (rst) n <= 0; else n <= p;

assign out = p | n;

advancedThe divide-by-3 is fed a clock with a 40% duty cycle: high for 40% of each period, low for 60%. What duty cycle does the output have?

advancedCould you build the 50% divide-by-3 with an AND gate instead of an OR?

Practise these 9 questions with animations