Draft preview. Lessons are being written and reviewed.

Digital Design / Module 5: Counters and frequency dividers

28. Ring, Johnson and Gray counters

Pick the right count sequence.

Lecture coming soon

See it move

The idea

Not every counter needs to count in binary. Different sequences trade flops against simplicity and how many bits change at each step.

TypeStates from N flopsBits changing per stepDecoding
Binary2^NUp to NNeeds gates
RingN2One flop per state
Johnson2N1Two inputs per state
Gray2^N1Needs gates

Ring counter

A with its end fed back to its start, holding a single 1. It is by nature, so each flop directly marks a state. It suits sequencers, where each step enables one block.

always_ff @(posedge clk)
  if (rst) q <= 4'b0001;
  else     q <= {q[2:0], q[3]};

Reset must load exactly one 1. All zeros gets stuck at zero forever.

Johnson counter

The same chain, feeding back the inverted end. It fills with ones, then with zeros, giving twice the states with the same flops, and only one bit changes per step.

always_ff @(posedge clk)
  if (rst) q <= 4'b0000;
  else     q <= {q[2:0], ~q[3]};

A 4-bit Johnson counter has eight states: 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000.

Illegal cycles

A 4-bit Johnson counter uses 8 of the 16 possible values. The other 8 form a separate loop. If a bit upset lands the counter in that loop, it stays there, and ring counters have the same issue. Robust designs add logic to detect illegal values and recover.

Common mistake

Choosing a Gray counter because it has fewer glitches, without asking whether anything will ever sample it asynchronously. Gray's main benefit is safe crossing between . Inside a single domain, plain binary is usually simpler.

Key ideas

  • N-flop ring has N states and is one-hot.
  • N-flop Johnson has 2N states with one-bit changes.
  • Gray has 2^N states with one-bit changes.

Your exercise

Write a 4-bit Johnson counter and list states.

Find the bug

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

What goes wrong: From reset, this Johnson counter stays at 0000 forever. It should go through eight states.

Interview questions

coreStates in a 4-bit Johnson?

basicYou need a 10-state sequence. How many flops does a ring counter, a Johnson counter and a binary counter need?

basicList the states of a 4-bit Johnson counter that starts at 0000, and say how many bits change at each step.

coreIn the 4-bit Johnson sequence 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000, which two bits uniquely identify the state 0011?

coreThis ring counter never produces any output pulses. What is wrong?

always @(posedge clk)
  if (rst) q <= 4'b0000;
  else     q <= {q[2:0], q[3]};

coreWhy does it matter that only one bit changes per step when a counter value is read by logic in another clock domain?

coreWhy might a designer choose a ring counter to sequence four steps rather than a 2-bit binary counter plus a decoder?

advancedA bit upset puts a 4-bit Johnson counter into the state 0010. What happens next?

q <= {q[2:0], ~q[3]};

advancedHow would you make a Johnson counter recover by itself from an illegal state?

Practise these 9 questions with animations