Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 57 ยท Registers and Counters

Asynchronous and Synchronous Counters

Build ripple (asynchronous) and synchronous counters from T flip-flops, make up/down and modulo-N counters, and see clock division in live Python waveforms.

A counter is a register that steps through a fixed sequence of states each time its clock ticks. Most often the sequence is plain binary counting: 000, 001, 010, 011, and so on. Counters are everywhere: they divide a fast clock into slow ones, count events, generate addresses for memory, make timers, and step through the stages of a controller.

A counter is nothing more than flip-flops plus a little wiring. This lesson builds the two classic styles. The asynchronous (ripple) counter is the cheapest: each flip-flop is clocked by the one before it. The synchronous counter costs a few more gates but clocks every flip-flop together, which makes it faster and glitch-free. You should already know the T flip-flop (it toggles when T is 1) and the idea of a clock edge.

The key idea: binary counting is toggling

Watch the bits of a binary count and look for a pattern:

Count Q2 Q1 Q0 What changed
0 000
1 001 Q0 toggled
2 010 Q0 and Q1 toggled
3 011 Q0 toggled
4 100 Q0, Q1 and Q2 toggled

Bit 0 toggles on every tick. Bit 1 toggles on every second tick, exactly when Q0 was 1 and is about to fall. Bit 2 toggles when both Q0 and Q1 were 1. In general, bit k toggles when all lower bits are 1. A T flip-flop is the perfect part for this job: wire T high to toggle always, or wire T to "all lower bits are 1" to toggle conditionally.

Two ways to build a counter

Ripple Each stage clocks the next one.
Synchronous One shared clock for every stage.
Trade-off Ripple is small. Synchronous is fast.
1 / 2

Asynchronous (ripple) counters

In a ripple counter only the first flip-flop sees the real clock. Every T input is tied to 1, so each stage toggles whenever its own clock input has the right edge. The clock input of stage k+1 is the output of stage k. Using flip-flops that trigger on the falling edge, stage k+1 toggles each time Q of stage k falls from 1 to 0, which is exactly the carry moment of binary counting.

flowchart LR
    CLK["CLK"] --> F0["FF0<br/>T = 1"]
    F0 -->|"Q0"| F1["FF1<br/>T = 1"]
    F1 -->|"Q1"| F2["FF2<br/>T = 1"]
    F2 -->|"Q2"| OUT["divide by 8"]

Each output feeds the clock of the next stage, so the change ripples along the chain.

The price of this simplicity is delay. Stage 2 cannot react until stage 1 has changed, and stage 1 cannot react until stage 0 has changed. When the count goes from 0111 to 1000 the change must ripple through four flip-flops one after the other. For a short moment the outputs show nonsense values. The simulation below follows the ripple one stage at a time and prints every state the outputs pass through.

Python A ripple counter, one stage at a time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On every odd tick the count settles at once, and on most even ticks there are only one or two wrong states. The worst case is the rollover from all ones, where every stage has to change in turn. If you use a ripple counter's outputs as inputs to other logic, those transient wrong values can trigger false actions. That is the main reason designers avoid ripple outputs inside synchronous systems.

The clock period of a ripple counter must be longer than the whole ripple, which is roughly the number of stages times the delay of one flip-flop. Faster clocks make the counter miscount, not just look wrong.

Counters as clock dividers

Look at any single stage of a ripple counter. It toggles once for every two edges of its input, so its output is a square wave at half the input frequency. Chain n stages and stage k runs at f / 2^(k+1). Three flip-flops give outputs at f/2, f/4 and f/8. This is why a counter is also called a frequency divider, and why a quartz watch can start from a 32,768 Hz crystal and use 15 flip-flops to reach exactly one pulse per second (2 to the 15th power is 32,768).

Synchronous counters

In a synchronous counter every flip-flop shares the same clock, so all changes happen together at the same edge. What changes is the T input of each stage. We saw that bit k toggles when all lower bits are 1, so:

  • T0 = 1
  • T1 = Q0
  • T2 = Q0 AND Q1
  • T3 = Q0 AND Q1 AND Q2

At each edge a stage with T = 1 toggles and a stage with T = 0 holds. There is no ripple. The only delay is one flip-flop plus the AND gates that form T, and the AND gates settle before the next edge. Outputs change at the same time, so there are no transient wrong counts.

Counting down and up/down

To count down, bit k toggles when all lower bits are 0 (a borrow, as in subtraction). The T equations use the inverted outputs: T1 = NOT Q0, T2 = NOT Q0 AND NOT Q1. An up/down counter has a control input UP. Each T input is chosen by a 2:1 multiplexer, or written as Tk = UP * (carry) + NOT UP * (borrow).

Counting with a limit: modulo-N

A modulo-N counter counts 0 to N-1 and then returns to 0. N does not need to be a power of two. A 4-bit counter that stops at 9 is a decade counter (modulo 10, or BCD counter), natural for decimal displays. A synchronous design adds logic that detects the last state (for example 1001 for N = 10) and forces the next state to 0 on the following clock edge instead of 1010.

An asynchronous design does it differently: it lets the counter reach N, detects that state with a gate and clears all flip-flops through their asynchronous reset pins. State N exists for only a few nanoseconds, so it is a glitch, and every stage has to be cleared fast enough. The approach is cheap but fragile.

Python Synchronous, up/down and modulo-N counters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The decade counter above never shows 10 to 15, and the mod-6 counter never shows 6 or 7, even though the flip-flops could hold them. Those extra states are unused states. A well-built counter must be sure it recovers if noise or a power-up accident puts it in one of them. The sync design above does: from an unused state the next step wraps back into the valid cycle. We will come back to this problem in State Machine Patterns and Pitfalls.

Speed: ripple versus synchronous

Let tff be the clock-to-output delay of a flip-flop, tand the delay of an AND gate and tsu the setup time of a flip-flop. For an n-bit counter:

  • Ripple: the last output settles after about n * tff.
  • Synchronous with a chain of ANDs: the clock period must cover tff + (n - 2) * tand + tsu. With wide AND gates or a tree, it is even shorter.

The synchronous period grows slowly with n because AND gates are faster than flip-flops, while the ripple period grows with every stage. The calculator below uses sample numbers (10 ns for a flip-flop, 5 ns per AND gate, 3 ns setup) and then draws the waveforms of a 3-bit counter so that you can see the clock division.

Python Maximum speed and waveforms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the plot, Q0 flips every clock, Q1 every second clock, Q2 every fourth clock. Each is a square wave with exactly half the frequency of the one before, which is the divide-by-2 chain from earlier.

Registers versus counters, and the register-transfer view

A counter is a close relative of a register: both are groups of flip-flops with a shared clock. What differs is what decides the next value.

Register Shift register Counter
Stores any value that is loaded a bit pattern that moves a number that steps through a sequence
Next value comes from the parallel data inputs (or holds) the neighbouring bit and a serial input its own present value (plus 1, or the next in a pattern)
Typical use holding data, operands, results serial-parallel conversion, delays counting events, dividing clocks, sequencing
Next-state logic just a multiplexer (load or hold) wires from bit to bit an incrementer or a custom logic function

In other words, a counter is a register whose input is computed from its own output. Real designs rarely use a bare counter: they use one register with several micro-operations chosen by control inputs: clear, parallel load, increment, shift, hold. The program below models such a register. A priority order settles what happens when two controls are active at once, in this case "load beats increment".

Python One register, many micro-operations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same hardware acts as a plain register (load, hold), as a counter (increment, including the wrap-around from 15 to 0) and as a shift register (shift left). This is how the counters and shift registers in a processor are built: not as separate chips but as one register with a small set of control signals.

Register-transfer level (RTL) design

The state machine lessons designed circuits at the level of flip-flops and states. As systems grow, that becomes unmanageable, so designers move up one level of abstraction and describe a system as registers plus the transfers between them. This is the register-transfer level (RTL), and it is the level at which Verilog and VHDL designs are written (see hardware description languages). A register-transfer statement has this shape:

T1: R1 โ† R1 + R2, R3 โ† 0

It means "when control signal T1 is active, at the next clock edge load R1 with the sum of R1 and R2, and clear R3". The arrow is a parallel transfer, and all the right-hand sides use the values from before the edge, the same idea as the non-blocking assignment <=. An RTL design has two cooperating parts:

  • The datapath: the registers, adders, multiplexers and buses that perform the micro-operations.
  • The control unit: a finite state machine that raises the right control signals in the right order.

Conventional sequential design starts from a state diagram and ends at flip-flop equations. RTL design starts from "which registers, and which transfers, does the algorithm need?", and only afterwards designs the control. The example computes n + (n-1) + ... + 1 with two registers ACC and CNT, and a four-state control unit.

Python An RTL algorithm: datapath and control

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trace shows the control unit stepping through S1 and S2 while the datapath does its work: in S2 both transfers (ACC โ† ACC + CNT and CNT โ† CNT - 1) happen in the same clock, using the old values. The result 10 appears after 2n + 2 cycles. This tiny machine is a one-instruction processor, and the tiny CPU lesson shows the same structure at larger scale: a datapath of registers and an ALU, steered by a control unit.

Choosing a style

Use a ripple counter when the speed is low, the outputs are not fed into other logic, and cost or power matters (clock dividers are the classic case). Use a synchronous counter everywhere else, especially when the count is read by other circuits. In an FPGA or an integrated design, synchronous counters are almost always the default: a clean single clock domain is much easier to analyze, which we will push further in Timing, Clock Skew and Metastability.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a binary up counter, when does bit 3 toggle?

    A bit toggles when all lower bits are 1, because that is when the addition carries into it. For bit 3 that means Q0, Q1 and Q2 are all 1.
  2. 2. Why can a ripple counter show wrong values briefly?

    The clock of each stage is the output of the previous one, so the change travels along the chain. Until it reaches the end, the outputs hold a mixture of old and new bits.
  3. 3. A 4-bit counter is made to count only 0 to 9. What is it called?

    Counting through ten states and then returning to 0 is modulo 10, also known as a decade or BCD counter.
  4. 4. The input clock is 64 kHz. What frequency does the output of the third flip-flop in a ripple chain have?

    Each stage divides by 2: 64 kHz becomes 32, then 16, then 8 kHz after the third stage.
  5. 5. What is the main advantage of a synchronous counter over a ripple counter?

    All stages share one clock, so the delay is one flip-flop plus the T-input logic instead of one flip-flop per stage. The price is the extra AND gates.

Summary

  • A counter is a register fed from its own output; one register with load, increment, clear and shift controls covers register, counter and shift-register roles, and RTL designs pair such a datapath with a control state machine.
  • Binary counting is toggling: bit k toggles when all lower bits are 1 (up) or 0 (down), so T flip-flops are the natural building block.
  • A ripple counter clocks each stage from the previous output: it is tiny but slow, and its outputs glitch while the change ripples through.
  • A synchronous counter shares one clock and uses T0 = 1, T1 = Q0, T2 = Q0 AND Q1, and so on, so all bits change together.
  • Down counting uses the inverted outputs (borrow); an up/down counter selects between the two.
  • A modulo-N counter (decade counter for N = 10) detects its last state and wraps; unused states must lead back into the valid cycle.
  • Each counter stage divides its input frequency by two, which makes counters clock dividers.

Next: Finite State Machines: Moore and Mealy.