C7 · Lesson 29 · Designing counters
Any sequence from flip-flops, with D, T and JK inputs
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.
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?
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.
Name it
The procedure
- List the states in order and the flip-flops needed, .
- Write a table of each present state and its next state. Unused states are don't-cares.
- For each flip-flop, find the input that produces the next state (see below), and minimize it with a Karnaugh map.
- Check every unused state: where does it go? Fix any that don't lead into the cycle.
Flip-flop inputs
- D: , the next state itself.
- T (toggle): , 1 when the bit must change.
- JK: from 0, J decides (, K don't-care); from 1, K decides (, J don't-care). The many don't-cares often make JK logic the simplest.
A binary counter with T flip-flops needs , , : 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, , 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 .
Worked example
Derive the divide-by-6 counter's next-state equations with D flip-flops, using states 6 and 7 as don't-cares.
Show the solution
- The cycle. 000 → 001 → 010 → 011 → 100 → 101 → 000.
- Bit 0. toggles every time:
- Bit 1. is 1 after states 1 and 2 only:
- Bit 2. is 1 after states 3 and 4:
- 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 , and .
Worked example
Design a 3-bit binary counter with JK flip-flops.
Show the solution
- Bit 0. It toggles every clock: from 0 it goes to 1 (J = 1) and from 1 to 0 (K = 1), so .
- Bit 1. It changes only when = 1, so .
- Bit 2. It changes when = 1, so .
- 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 , and .
Worked 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?
Show the solution
- From 110. giving 101 = 5.
- From 111. The same equations give , , = 0, 0, 1, which is 100 = 4.
- Check the cycle. Both rejoin the cycle in one clock: self-starting.
- 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.
- 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
A divide-by-6 counter counts 000 to 101 and wraps. Write , using states 6 and 7 as don't-cares.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
For the same counter, write .
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
A flip-flop is at 0 and must go to 0 on the next clock.
(a)
What T input does that need?
(b)
What D input?
Check
An n-bit counter with n = 8 uses flip-flops with 0.5 ns of clock-to-Q delay.
(a)
As a ripple counter, how long does its top bit take to settle?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
(b)
What's the highest clock at which its count can be read reliably after each edge?
Check
A counter must divide a clock f by N, using flip-flops with delay t.
Step 1
How many flip-flops?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many unused states must be checked?
Step 3
If built as a ripple counter, how long would its top bit take to settle?
Check
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 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why must a counter designer check the states the counter never uses?
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
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.