---
title: Designing counters
heading: Any sequence from flip-flops, with D, T and JK inputs
eyebrow: C7 · Lesson 29 · Designing counters
xp: 40
---

::: hook
A binary counter counts 0, 1, 2, and so on. Real designs want other sequences: divide by 6 for a clock, count in Gray code for a crossing between clock domains, step a motor through a pattern. Any sequence is a little state machine with no inputs, and designing one is mechanical: list the transitions, write the next-state logic from a table, and check what happens if the counter wakes up in a state you didn't plan for.

::: play
- This counter counts 0 to 5 and starts over: a divide-by-6. Its three flip-flops follow the next-state equations below.
- Watch the waveforms: Q2 is high for two counts of six, so its frequency is a sixth of the clock's.
- The two unused codes, 6 and 7, never appear. The third example below shows what would happen if they did.
:::
:::

```sim logic
input R
output Q0, Q1, Q2
clock CLK 8
always @(posedge CLK) begin
  if (R) Q0 <= 0; else Q0 <= ~Q0;
  if (R) Q1 <= 0; else Q1 <= ~Q2 & (Q1 ^ Q0);
  if (R) Q2 <= 0; else Q2 <= Q1 & Q0 | Q2 & ~Q0;
end
wave: CLK, Q0, Q1, Q2
time: 160
```

::: predict
## A divide-by-6 counter uses three flip-flops, so codes 6 and 7 are never meant to occur. If noise at power-up leaves it in state 7, what should a careful design guarantee?

- [ ] Nothing: state 7 can't happen
- [ ] That it stays in 7 until reset
- [x] That it returns to the 0 to 5 cycle within a clock or two by itself
- [ ] That it counts 7, 8, 9 and wraps

::: reveal
That it finds its way back. A counter that can lock up in an unused state, cycling 6, 7, 6, 7 forever, fails silently until someone resets it. The designer either checks that every unused state leads into the main cycle (a **self-starting** counter) or forces it there with reset logic. The equations above send 6 to 5 and 7 to 4: back in the cycle after one clock.
:::
:::

::: nameit
## The procedure

1. List the states in order and the flip-flops needed, $\lceil\log_2(\text{states})\rceil$.
2. Write a table of each present state and its next state. Unused states are don't-cares.
3. For each flip-flop, find the input that produces the next state (see below), and minimize it with a Karnaugh map.
4. Check every unused state: where does it go? Fix any that don't lead into the cycle.

## Flip-flop inputs

- **D:** $D = Q^+$, the next state itself.
- **T** (toggle): $T = Q \oplus Q^+$, 1 when the bit must change.
- **JK:** from 0, J decides ($J = Q^+$, K don't-care); from 1, K decides ($K = \overline{Q^+}$, J don't-care). The many don't-cares often make JK logic the simplest.

A binary counter with T flip-flops needs $T_0 = 1$, $T_1 = Q_0$, $T_2 = Q_1Q_0$: each bit toggles when all the bits below it are 1.

## Synchronous against ripple

A **ripple counter** clocks each flip-flop from the one before: no logic, but the top bit settles only after every stage's delay, $n\,t_{cq}$, and the outputs pass through false values on the way. A **synchronous** counter clocks every flip-flop together, so all its outputs change within one $t_{cq}$.
:::

::: example
## Derive the divide-by-6 counter's next-state equations with D flip-flops, using states 6 and 7 as don't-cares.

::: solution
1. **The cycle.** 000 → 001 → 010 → 011 → 100 → 101 → 000.
2. **Bit 0.** $Q_0$ toggles every time:
   $$Q_0^+ = \bar Q_0$$
3. **Bit 1.** $Q_1^+$ is 1 after states 1 and 2 only:
   $$Q_1^+ = \bar Q_2(Q_1 \oplus Q_0)$$
4. **Bit 2.** $Q_2^+$ is 1 after states 3 and 4:
   $$Q_2^+ = Q_1Q_0 + Q_2\bar Q_0$$
5. **The don't-cares.** Each is as simple as the don't-cares allow; in the map, cells 6 and 7 were free to be either.

**Answer:** $Q_0^+ = \bar Q_0$, $Q_1^+ = \bar Q_2(Q_1 \oplus Q_0)$ and $Q_2^+ = Q_1Q_0 + Q_2\bar Q_0$.
:::
:::

::: example
## Design a 3-bit binary counter with JK flip-flops.

::: solution
1. **Bit 0.** It toggles every clock: from 0 it goes to 1 (J = 1) and from 1 to 0 (K = 1), so $J_0 = K_0 = 1$.
2. **Bit 1.** It changes only when $Q_0$ = 1, so $J_1 = K_1 = Q_0$.
3. **Bit 2.** It changes when $Q_1Q_0$ = 1, so $J_2 = K_2 = Q_1Q_0$.
4. **Compare with the T counter.** A JK flip-flop with J = K is a T flip-flop, so this is the T-counter's logic again.

**Answer:** $J_0 = K_0 = 1$, $J_1 = K_1 = Q_0$ and $J_2 = K_2 = Q_1Q_0$.
:::
:::

::: example
## Where do the divide-by-6 counter's unused states go? And how long does an 8-bit ripple counter take to settle, with 1 ns per flip-flop?

::: solution
1. **From 110.**
   $$\begin{aligned} Q_0^+ &= 1 \\ Q_1^+ &= \bar1 \cdot (1 \oplus 0) \\ &= 0 \\ Q_2^+ &= 1 \cdot 0 + 1 \cdot 1 \\ &= 1 \end{aligned}$$
   giving 101 = 5.
2. **From 111.** The same equations give $Q_0^+$, $Q_1^+$, $Q_2^+$ = 0, 0, 1, which is 100 = 4.
3. **Check the cycle.** Both rejoin the cycle in one clock: self-starting.
4. **The ripple counter.** Its top bit changes 8 ns after the clock edge when a carry ripples through all eight stages, and in between the count passes through wrong values.
5. **A synchronous counter.** A synchronous 8-bit counter settles in one flip-flop delay, 1 ns, plus its logic, and never shows a wrong value at its outputs after that.

**Answer:** 110 goes to 101 and 111 to 100, so the counter is self-starting; the 8-bit ripple counter takes 8 ns to settle.
:::
:::

::: check
kind: boolean
vars: Q2, Q1, Q0
answer: ~Q2 & (Q1 ^ Q0)
ops: and, or, not, xor
dontcare: 6, 7

## A divide-by-6 counter counts 000 to 101 and wraps. Write $Q_1^+$, using states 6 and 7 as don't-cares.

::: right
Right: $\bar Q_2(Q_1 \oplus Q_0)$, or any equivalent on the six used states.
:::

::: misses
- [any] Not yet. $Q_1^+$ is 1 only after states 1 and 2.
:::
:::

::: check
kind: boolean
vars: Q2, Q1, Q0
answer: Q1 & Q0 | Q2 & ~Q0
ops: and, or, not
dontcare: 6, 7

## For the same counter, write $Q_2^+$.

::: right
Right: $Q_1Q_0 + Q_2\bar Q_0$.
:::

::: misses
- [any] Not yet. $Q_2^+$ is 1 only after states 3 and 4.
:::
:::

::: check
kind: parts
given: q = 0 | 1; n = 0 | 1

## A flip-flop is at {q} and must go to {n} on the next clock.

::: part
answer: q + n - 2*q*n
tolerance: 0

### What T input does that need?

::: misses
- [any] $T = Q \oplus Q^+$.
:::
:::

::: part
answer: n
tolerance: 0

### What D input?

::: misses
- [any] $D = Q^+$.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 8 | 16; t = 0.5 | 1 | 2

## An n-bit counter with n = {n} uses flip-flops with {t} ns of clock-to-Q delay.

::: part
answer: n*t
unit: ns
tolerance: 0.5%

### As a ripple counter, how long does its top bit take to settle?

::: misses
- [t] Each stage waits for the one before it.
- [any] $nt_{cq}$.
:::
:::

::: part
answer: 1000/(n*t)
unit: MHz
tolerance: 0.5%

### What's the highest clock at which its count can be read reliably after each edge?

::: misses
- [any] $\frac{1}{nt_{cq}}$.
:::
:::
:::

::: check
kind: derivation
vars: N = 2..100000, f = 1000..1000000000, t = 0.0000000001..0.00000001

## A counter must divide a clock f by N, using flip-flops with delay t.

::: step
answer: ceil(log(N)/log(2))

### How many flip-flops?
:::

::: step
answer: 2^ceil(log(N)/log(2)) - N

### How many unused states must be checked?
:::

::: step
answer: ceil(log(N)/log(2))*t

### If built as a ripple counter, how long would its top bit take to settle?
:::

::: right
Right. Every unused state needs a path back into the cycle.
:::
:::

::: check
kind: python

## Write `next_state(q)` for the divide-by-6 counter (q from 0 to 7, bits Q2 Q1 Q0) using the equations from the first example, and `settles(q)` returning how many clocks it takes from state q to reach a state in 0 to 5 (0 if it's already there).

```python starter
def next_state(q):
    return (q + 1) % 8

def settles(q):
    return 0

print([next_state(q) for q in range(8)])
```

```python solution
def next_state(q):
    q2, q1, q0 = (q >> 2) & 1, (q >> 1) & 1, q & 1
    n0 = 1 - q0
    n1 = (1 - q2) & (q1 ^ q0)
    n2 = (q1 & q0) | (q2 & (1 - q0))
    return n2 * 4 + n1 * 2 + n0

def settles(q):
    n = 0
    while q > 5:
        q = next_state(q)
        n += 1
    return n

print([next_state(q) for q in range(8)])
```

```python tests
assert [next_state(q) for q in range(6)] == [1, 2, 3, 4, 5, 0], "The main cycle counts 0 to 5."
assert next_state(6) == 5 and next_state(7) == 4, "The unused states rejoin the cycle."
assert settles(6) == 1 and settles(7) == 1 and settles(3) == 0, "Self-starting in one clock."
```

::: right
Right. Checking the unused states is a few lines of code and catches a lock-up before it ships.
:::
:::

::: explain
## Why must a counter designer check the states the counter never uses?

::: model
A counter with n flip-flops has two to the n possible states, but a counter that divides by a number that isn't a power of two uses only some of them. At power-up, or after a glitch, the flip-flops can land in any state, including an unused one. The next-state logic was simplified by treating the unused states as don't-cares, so whatever it does there was never chosen deliberately: it might lead back into the counting cycle, or it might trap the counter in a loop of unused states forever. Checking each unused state's successor, and fixing any that don't lead back, makes the counter recover by itself.
:::
:::

::: practice

::: check
given: n = 5 | 10 | 12 | 60
answer: 2^ceil(log(n)/log(2)) - n
tolerance: 0

## A divide-by-{n} counter. How many unused states?

::: misses
- [any] $2^{\lceil\log_2n\rceil} - n$.
:::
:::

::: check
given: n = 6 | 12 | 24; t = 0.8 | 3
answer: n*t
unit: ns
tolerance: 0.5%

## {n}-bit ripple counter, {t} ns per stage. Settling time?

::: misses
- [any] $nt_{cq}$.
:::
:::
:::
