Counters (Up/Down/Ring)

A counter is a sequential circuit that steps through a fixed sequence of states, one per clock edge. Counters count events, divide frequencies, generate timing signals and address memories. You have already built a 2-bit counter from T flip-flops, and in this lesson you will extend the idea to up counters, down counters, counters with any modulus and the special ring and Johnson counters built from shift registers.

You should know flip-flops, shift registers and the difference between synchronous and asynchronous counters.

Learning objectives

After this lesson you will be able to:

  • Find the number of flip-flops needed for a given modulus.
  • Describe the sequence of a mod-N counter, including a mod-6 counter.
  • Trace an up/down counter.
  • Write the sequences of a ring counter and a Johnson counter.
  • Choose between the counter types for a given job.

Up and down counters

An n-bit binary up counter counts from 0 to 2^n - 1 and then wraps to 0, so it has 2^n states. A down counter counts the other way, from 7 down to 0 and then wraps to 7 for a 3-bit counter. An up/down counter has a control input that selects the direction. In a synchronous design with T flip-flops, an up counter toggles a bit when all lower bits are 1, and a down counter toggles a bit when all lower bits are 0.

Mod-N counters

The modulus of a counter is the number of different states in its cycle. A mod-N counter needs the smallest number n of flip-flops such that 2^n is at least N. A mod-6 counter needs 3 flip-flops, because 2^2 = 4 is too small and 2^3 = 8 is enough. A mod-10 counter (a decade counter) needs 4 flip-flops, and a mod-12 counter also needs 4.

One way to build a counter with a modulus that is not a power of 2 is to let a binary counter run and clear it as soon as it reaches the first unwanted state. The other way, which is cleaner in a synchronous design, is to design the next-state logic so that the count jumps from N - 1 straight to 0.

Example 1: a mod-6 counter

The counter counts 0 to 5 and then returns to 0.

Clock edge Count (decimal) Q2 Q1 Q0
0 0 000
1 1 001
2 2 010
3 3 011
4 4 100
5 5 101
6 6 (briefly) 110, cleared at once to 000

Result: the counter has six stable states. With the clear method, the counter reaches 110 for a very short moment before the clear signal resets it, and that moment is why a synchronous design that jumps directly from 101 to 000 is preferred.

Ring and Johnson counters

A ring counter is a shift register whose last output feeds its first input. It is loaded with a single 1 and the 1 circulates around the ring, so each state has exactly one flip-flop at 1. An n-bit ring counter has only n states, but each state needs no decoding: the outputs are already one-hot.

A Johnson counter (twisted ring counter) feeds the inverted last output back to the first input. An n-bit Johnson counter has 2n states, twice as many as a ring counter with the same flip-flops, and each state differs from the next by just one bit.

Example 2: ring against Johnson

Both counters have four flip-flops, Q3 Q2 Q1 Q0, and shift right. The ring counter starts at 1000 and the Johnson counter starts at 0000.

State number Ring counter (4 bits) Johnson counter (4 bits)
0 1000 0000
1 0100 1000
2 0010 1100
3 0001 1110
4 - 1111
5 - 0111
6 - 0011
7 - 0001

Result: the ring counter repeats after 4 states and the Johnson counter after 8. The Johnson counter needs the same four flip-flops but gives twice as many states, and any state can be recognised by looking at only two adjacent bits.

Example 3: an up/down counter

A 3-bit up/down counter starts at 010 (decimal 2) and receives the direction signals below at five clock edges.

Edge Direction Before After
1 Down 010 001
2 Down 001 000
3 Down 000 111
4 Up 111 000
5 Up 000 001

Result: the counter counts down 010, 001, 000 and then wraps to 111, then counts up again through 000 to 001. The wrap-around in both directions is a property of the modulus: after the largest state comes 000, and before 000 comes the largest state.

Common mistakes

  • Calculating the number of flip-flops wrongly. You need 2^n at least N, so a mod-6 counter needs 3 flip-flops, not 2.
  • Forgetting the wrap-around. A down counter goes from 000 to 111, and an up counter goes from 111 to 000.
  • Mixing up ring and Johnson counters. The ring counter feeds back Q, and the Johnson counter feeds back Q'.
  • Starting a ring counter at 0000. It needs a single 1 to circulate, otherwise it never leaves the all-zero state.
  • Trusting a cleared counter's brief unwanted state. An asynchronous clear shows the state N for a moment, which may cause glitches.

Practice problems

Check your logic with the truth table generator.

  1. How many flip-flops does a mod-12 counter need?

    Answer: 4, because 2^3 = 8 is too small and 2^4 = 16 is enough.

  2. How many states does a 5-bit Johnson counter have?

    Answer: 10 states (2 x 5).

  3. How many states does a 6-bit ring counter have, and how many flip-flops does it need?

    Answer: 6 states, and it needs 6 flip-flops.

  4. A mod-10 counter is at 9. What is the next count?

    Answer: 0, because it wraps after the highest state of its cycle.

Frequently asked questions

What is the difference between a ring counter and a Johnson counter? A ring counter feeds the last output straight back and has n states for n flip-flops. A Johnson counter feeds back the inverted output and has 2n states, so it is more efficient, though its states need a little decoding.

Why use a ring counter if it needs more flip-flops? Its outputs are already one-hot, so each state can be used directly as a control signal without any decoding gates. It is common in sequencers that switch on one step at a time.

How do I build a mod-N counter? Choose n flip-flops with 2^n at least N, then design the next-state logic so that the count after N - 1 is 0. This can be done by clearing the counter at N, or better, by designing the next-state equations directly.

Try designing next-state equations with the Boolean calculator.

Next lesson: Mealy vs Moore Machines