← Back to blogFrequency division

Frequency Division: From Flip-Flops to Modulo-N Counters

Design divide-by-2 chains, modulo-N timing ticks, UART baud generators, and PWM timers without off-by-one or generated-clock mistakes.

DennyDenny
10 min read
Chalkboard feature illustration for “Frequency Division: From Flip-Flops to Modulo-N Counters”

TL;DR: A toggling flip-flop divides a clock by 2, and bit QkQ_k of a binary counter divides it by 2k+12^{k+1}. For an arbitrary ratio NN, a counter that visits 0 through N−1 can emit one tick every NN input cycles. A tick is not automatically a 50% duty-cycle clock: toggling an output once per terminal count divides by 2N2N. In FPGA and synchronous RTL designs, a clock-enable tick is usually safer than sending counter outputs through ordinary routing as new clocks.

Frequency division sounds like one operation—turn a fast clock into a slow one—but designers use that phrase for several related outputs:

  1. a slower square-wave clock
  2. a one-cycle pulse that occurs every NN source cycles
  3. a periodic enable that lets selected registers update while they remain in the source clock domain

Those signals can have the same repetition rate and still behave differently. Keeping them separate prevents off-by-one errors, duty-cycle surprises, and accidental clock-domain crossings.

The Divide-by-2 Building Block

A T flip-flop with T = 1 complements its output on every active clock edge:

Qnext=Q‾Q_{next} = \overline{Q}

It takes two input edges to complete one output cycle, so:

fQ=fin2f_Q = \frac{f_{in}}{2}

A D flip-flop wired as D=Q‾D = \overline{Q} behaves the same way. Cascading ideal toggle stages gives:

fout=fin2nf_{out} = \frac{f_{in}}{2^n}

where nn is the number of divide-by-2 stages.

The same relationship appears in a binary up-counter. If Q0Q_0 is the least-significant bit, then:

Counter bitFrequencyPeriod in source cycles
Q0Q_0fin/2f_{in}/22
Q1Q_1fin/4f_{in}/44
Q2Q_2fin/8f_{in}/88
QkQ_kfin/2k+1f_{in}/2^{k+1}2k+12^{k+1}

Every bit has a 50% duty cycle after initialization, assuming the counter runs continuously.

Ripple counter or synchronous counter?

In a ripple counter, one flip-flop output clocks the next stage. Each stage changes after the previous stage’s clock-to-Q delay, so the bits do not update simultaneously. Ripple counters are compact and instructive, but decoded outputs can briefly pass through intermediate values.

In a synchronous counter, every state bit uses the same clock and combinational next-state logic decides which bits toggle. This structure is easier to analyze and integrate into a larger synchronous design.

For a learning circuit, either can demonstrate division. For production logic, identify which structure you built and verify its timing accordingly.

A 32.768 kHz-to-1 Hz Example

The familiar watch-crystal frequency is useful because:

32,768=21532{,}768 = 2^{15}

A 15-bit binary counter clocked at 32.768 kHz therefore has:

fQ14=32,768215=1 Hzf_{Q_{14}} = \frac{32{,}768}{2^{15}} = 1\ \text{Hz}

Here the indexing matters: Q0Q_0 is already divide-by-2, so the fifteenth stage is Q14Q_{14}, not Q15Q_{15}.

In DigiSim, you can reproduce the relationship at a visibly slower source rate:

  1. Connect one clock to a binary counter.
  2. Observe the clock and several counter bits on the oscilloscope.
  3. Confirm that each successive bit has twice the period of the previous bit.
  4. Count source edges rather than relying only on how quickly an output light blinks.

The mathematical division is exact when the source frequency is exact. A physical timepiece’s long-term accuracy also depends on its oscillator, loading, temperature behavior, and calibration; a counter cannot improve the accuracy of its reference.

Modulo-N Division: Define the Output First

Power-of-two division falls out of a binary counter. An arbitrary ratio needs explicit terminal-count behavior.

For one pulse every NN source cycles, use states:

0,1,2,…,N−10, 1, 2, \ldots, N-1

Then return to zero. The terminal value is N−1N-1, because there are already NN distinct count intervals in that sequence.

For N=5N=5:

Source edgeCounter before edgeCounter after edgeTerminal tick
1010
2120
3230
4340
5401

The tick repetition rate is:

ftick=finNf_{tick} = \frac{f_{in}}{N}

The classic off-by-one mistake

If a counter starts at zero and resets only after reaching NN, it may visit 0 through N—N+1N+1 states. Whether a comparator wired to reset produces NN or N+1N+1 cycles also depends on whether reset is synchronous or asynchronous and on exactly when the output pulse is sampled.

A robust design specification says:

  • which values the counter visits
  • whether terminal count is N−1N-1 or another defined value
  • whether clear/load is synchronous
  • on which edge the tick is asserted
  • whether the tick is combinational or registered

Then verify that behavior with a cycle table.

A tick is not a square wave

A terminal-count tick is typically high for one source-clock interval and low for N−1N-1. Its duty cycle is 1/N1/N, not 50%.

If you toggle another flip-flop on every terminal tick, the output needs two ticks for a full cycle:

ftoggle=fin2Nf_{toggle} = \frac{f_{in}}{2N}

To create a 50% duty-cycle divide-by-NN square wave for even NN, make the output low for N/2N/2 source cycles and high for N/2N/2. For odd NN, a perfect 50% waveform cannot be formed by changing only on one edge of the source clock; implementations may accept unequal high/low times, use both edges, or use dedicated clock-management hardware.

Prefer a Clock Enable for Synchronous Logic

Suppose a 100 MHz system needs a state update every millisecond. One option is to route a divided counter output into another register’s clock pin. Another is to keep one 100 MHz clock and assert an enable every 100,000 cycles:

if tick = 1:
    state_register loads next_state
else:
    state_register holds

The second approach keeps the registers in one clock domain. In an FPGA it also lets the tools use dedicated clock routing for the real clock and ordinary data routing for the enable.

This is a design preference, not a claim that divided clocks are forbidden. PLL outputs, clock-management blocks, and intentionally generated clocks are common. When a counter or logic cell truly creates a clock, the timing constraints must describe it as a generated clock and the target architecture must route it appropriately.

Do not assume that a divided clock is asynchronous to its source. It is normally phase-related, but the timing tool only knows that relationship if the generated clock and its source are constrained correctly.

UART Baud-Rate Generation

UART is a useful divider example because it exposes both integer quantization and receiver sampling.

For a particular Microchip USART in asynchronous normal mode, the documented relationship is:

baud=fosc16(UBRR+1)\text{baud} = \frac{f_{osc}}{16(UBRR+1)}

For a 16 MHz oscillator and a target of 9,600 bit/s:

UBRR=16,000,00016×9,600−1≈103.17UBRR = \frac{16{,}000{,}000}{16 \times 9{,}600} - 1 \approx 103.17

Choose the supported integer value 103:

actual baud=16,000,00016(103+1)≈9,615.38 bit/s\text{actual baud} = \frac{16{,}000{,}000}{16(103+1)} \approx 9{,}615.38\ \text{bit/s}

The relative error is:

9,615.38−9,6009,600×100%≈+0.16%\frac{9{,}615.38 - 9{,}600}{9{,}600} \times 100\% \approx +0.16\%

This example also shows why register formulas often contain +1: the hardware counts a defined number of clock intervals, while software stores one less than that count.

The factor of 16 is device- and mode-specific. Many asynchronous UART receivers oversample each bit, but the number of samples and decision method vary. Do not assume that every receiver takes 16 majority votes. Use the target peripheral’s datasheet.

There is also no universal UART error limit. Reliable reception depends on the combined transmitter and receiver frequency error, frame length, sampling strategy, oscillator behavior, and peripheral specification. Calculate the generated rate, include both endpoints, and follow the device limits.

Microchip documents both the baud-rate equation and why endpoint clock error affects UART tolerance.

Periodic Timers

A periodic timer uses the same modulo-N structure:

N=fsourceftickN = \frac{f_{source}}{f_{tick}}

If a 1 MHz source must produce a 100 Hz sampling enable:

N=1,000,000100=10,000N = \frac{1{,}000{,}000}{100} = 10{,}000

A counter that visits 0 through 9,999 emits one tick every 10,000 cycles.

If NN is not an integer, an integer divider cannot reproduce the target exactly. Options include:

  • choose the nearest integer and quantify the static frequency error
  • alternate between adjacent divisors so the long-term average is closer
  • use a phase accumulator
  • change the reference frequency
  • use a PLL or dedicated clock-management resource

Alternating divisors or using a phase accumulator can improve average frequency while introducing deterministic variation in individual tick intervals. Whether that is acceptable depends on the consumer.

PWM from a Counter and Comparator

Pulse-width modulation is closely related to division but has two independent parameters: period and duty cycle.

Let a counter visit 0 through P−1P-1. Define:

PWM=1when counter<DPWM = 1 \quad \text{when counter} < D

Then:

fPWM=fsourcePf_{PWM} = \frac{f_{source}}{P}

and:

duty cycle=DP\text{duty cycle} = \frac{D}{P}

For a 1 MHz source, P=1000P=1000, and D=250D=250:

  • PWM frequency = 1 kHz
  • high time = 250 source cycles
  • duty cycle = 25%

Boundary behavior should be explicit:

  • D=0D=0 produces always low
  • D=PD=P produces always high
  • changing DD in the middle of a period can produce an unexpectedly short or long pulse

Production PWM blocks often buffer a new duty value and apply it at a period boundary to avoid those mid-cycle artifacts.

Multiplexed Displays: Digit Rate vs Frame Rate

A four-digit display driver may activate one digit at a time. If the selector advances at 1 kHz:

fframe=1,0004=250 frames/sf_{frame} = \frac{1{,}000}{4} = 250\ \text{frames/s}

Each digit is refreshed once every 4 ms and is active for roughly one quarter of the time. This distinction matters because “1 kHz refresh” might refer either to the digit-selection rate or to a full four-digit frame rate.

A typical structure is:

  1. a periodic enable advances a 2-bit digit selector
  2. a decoder enables one digit
  3. a multiplexer selects that digit’s value
  4. a segment decoder drives the shared segment lines

On physical hardware, verify current limits, transistor drive, blanking time between digits, brightness at the resulting duty cycle, and the display’s electrical polarity. A logic simulation covers the sequencing, not the analog drive requirements.

Crossing Between Timing Domains

A generated or divided clock does not automatically require a two-flip-flop synchronizer. Ask first whether the source and destination clocks are:

  • the same clock with a clock enable
  • synchronously related generated clocks
  • plesiochronous clocks with similar but unbounded phase
  • fully asynchronous clocks

Related generated clocks should be declared and timed with the correct phase relationship. For asynchronous domains, the crossing structure depends on the data:

  • a two-flop synchronizer is a common choice for a single-bit level
  • a pulse may need pulse-stretching, toggle encoding, or a handshake
  • a multibit word needs a coherency-preserving protocol
  • a sustained stream often uses an asynchronous FIFO

A chain of two flip-flops is not a general-purpose solution for a data bus. The setup, hold, and metastability guide explains why the first stage can become metastable and why synchronization reduces—but never mathematically eliminates—the risk.

Reset, Decode, and Glitch Pitfalls

Combinational reset feedback

Feeding a decoded count directly into an asynchronous clear can create a narrow, implementation-dependent pulse. In a ripple counter, intermediate count values can also momentarily satisfy the decoder.

Prefer a synchronous terminal-count design when the architecture allows it:

if count == N - 1:
    count_next = 0
    tick_next  = 1
else:
    count_next = count + 1
    tick_next  = 0

If an asynchronous-clear circuit is intentional, analyze minimum pulse width, recovery/removal timing, and decoded hazards for the actual components.

Using a counter bit as ordinary data and as a clock

A counter output is logically valid in both roles, but the physical timing requirements differ. A signal used as a clock needs the target’s clock routing, clock constraints, duty-cycle, and skew analysis. Avoid casually moving it from the data network to a clock pin.

Assuming simulation frequency proves hardware timing

A functional simulator advances logical events; it does not infer the routed delay of an FPGA or ASIC. Increasing a simulated clock rate is not a substitute for static timing analysis. Use the simulator to check cycle counts and sequence behavior, and use the implementation tool to verify physical timing.

Verification Checklist

For any divider or timer:

  1. Write the exact state sequence, including reset.
  2. State whether the output is a tick, enable, or square wave.
  3. Count source edges between output events.
  4. Check whether terminal count is N−1N-1 or a hardware-specific preload value.
  5. Verify duty cycle separately from repetition frequency.
  6. Calculate the actual rate and signed percentage error.
  7. Test reset during several counter states.
  8. Test boundary values such as N=1N=1, minimum duty, and maximum duty.
  9. Decide whether the output remains data or becomes a generated clock.
  10. Constrain and analyze every real clock relationship in the implementation flow.

Build the Concept in DigiSim

Start with a T flip-flop and confirm divide-by-2. Then build a 4-bit synchronous counter and compare all bit periods on the oscilloscope. Finally, add a terminal-count comparator and create a one-cycle modulo-NN tick.

The T flip-flop guide explains the toggle primitive, counters and state machines develops counter control, and clock skew covers the physical timing consequences of distributing real clocks.

For FPGA implementation, declare counter-derived clocks when they truly clock sequential elements; AMD’s methodology guide gives a concrete generated-clock constraint example. When possible, keep slower operations on a clock enable and reserve dedicated clock networks for actual clocks.

Apply the concept in a simulator model.