Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 57 ยท Registers and Counters

Counter Design: Arbitrary Sequences, Lock-Out and Modulus

Design a counter for any state sequence with excitation tables and K-map minimization, using D or JK flip-flops, then test for lock-out and make it self-starting.

Counters usually count in plain binary, and ring and Johnson counters follow the shifting patterns of a register. But a design problem can ask for any sequence at all: a stepper motor that needs the pattern 0, 2, 3, 6, 5, then repeat; a lamp that cycles through six colors; a controller that needs exactly 11 steps. Such a circuit is a small finite state machine with no inputs, and you can design it with a method that works for every sequence.

This lesson shows the excitation-table method for an arbitrary counter, using both D and JK flip-flops. It then turns to the most important safety check, lock-out: a counter with unused states can start there at power-up and never find its way back to the legal sequence. You will let a small program do the Boolean minimization, so you can focus on the method.

The design recipe

Every counter design follows the same five steps:

  1. Write down the desired sequence of states and decide how many flip-flops you need: ceil(log2 N) for N states.
  2. List each state, its next state, and the flip-flop inputs that cause that step (the excitation).
  3. Mark the unused states as don't-cares, because they never occur in normal operation.
  4. Simplify each flip-flop input with a K-map (or any other method).
  5. Check the unused states: where would the counter go if it ever started there?

Step 2 depends on the flip-flop type, and that is the only difference between a D design and a JK design.

Excitation tables

An excitation table answers the opposite question from a truth table. A truth table says "given these inputs, what is the next state?". An excitation table says "I want to go from this state Q to that state Q+; what must I feed in?"

Q Q+ D J K T
0 0 0 0 x 0
0 1 1 1 x 1
1 0 0 x 1 1
1 1 1 x 0 0

The x entries mean "either value works". From a 0 you can reach 0 with J = 0 and K being anything, because J = 0, K = 1 resets and J = 0, K = 0 holds. Those free choices make JK designs smaller: they add don't-cares to the K-maps. The D flip-flop has no free choices (D simply equals the next state bit), so its equations are the next-state equations. The T flip-flop is also simple: T = Q xor Q+.

An example: the sequence 0, 2, 3, 6, 5

Here is a 3-bit counter that must walk through the five states 0, 2, 3, 6, 5 and wrap back to 0. It needs three flip-flops. The unused states are 1, 4 and 7.

State (Q2 Q1 Q0) Next state
0 (000) 2 (010)
2 (010) 3 (011)
3 (011) 6 (110)
6 (110) 5 (101)
5 (101) 0 (000)
1, 4, 7 don't care

With D flip-flops, each D equals the next-state bit. The don't-cares stand for the three unused states. With JK flip-flops, each state transition gives a J and a K from the excitation table, and the unused states are don't-cares for both.

flowchart LR
    S0["0"] --> S2["2"] --> S3["3"] --> S6["6"] --> S5["5"] --> S0
    U1["1 (unused)"] -.-> S2
    U4["4 (unused)"] -.-> U4
    U7["7 (unused)"] -.-> U4

The solid ring is the wanted cycle. The dashed arrows show where a D design with don't-cares sends the unused states, which is a trap.

The program below does all of it: it minimizes each equation with a brute-force search for the smallest sum of products, designs the counter with D and with JK flip-flops, simulates both, and checks the unused states.

Python Designing an arbitrary counter and checking lock-out

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.

Read the output carefully, because it contains the most important lesson of the whole design.

  • The D design is cheap, with seven literals in total, and its main cycle is correct. But the lock-out check reports that states 4 and 7 are locked out: state 4 maps to itself (D0 = 0, D1 = 0, D2 = 1), and 7 leads to 4. A counter that powers up in 4, or is pushed there by noise, stays there forever. It looks like the counter is dead, even though every legal state works.
  • The JK design is even smaller, with five literals, and by luck of the particular don't-care choices it recovers from every unused state. Nothing in the method guarantees that. A different minimization could trap it.

Lock-out and how to fix it

Lock-out means an unused state leads into a cycle of unused states, never returning to the legal sequence. A counter that has this flaw can fail in the field after a power glitch, a bad clock edge or a faulty reset.

You can choose between three fixes:

  1. Self-starting design. Do not treat unused states as free choices. Assign each one a next state inside the legal sequence (for example 0), which is what the last part of the program does. The logic grows: the program's result needs 12 literals instead of 7. Alternatively, keep the don't-cares, check the minimized result and choose another minimization, or change one don't-care into a specific next state until all unused states recover.
  2. Reset or preset. Add a reset that loads state 0 at power-up. This guarantees the start but does not protect against noise later.
  3. Check by analysis. For small counters, always trace every unused state through the final equations, as the program does. For large counters, use a simulator or formal tool.

Ring and Johnson counters have the same problem: a ring counter with two 1s in it keeps both forever. The usual fix is feedback logic that injects a 1 only when all the other stages are 0, which corrects an illegal pattern within a few clocks.

Modulus and the number of flip-flops

A modulo-N counter has N states in its cycle. It needs ceil(log2 N) flip-flops and leaves 2^k - N states unused. The sequence does not have to be the binary count. Besides the design method above, two shortcuts exist:

  • Truncated binary count (as in Asynchronous and Synchronous Counters): count in binary up to N - 1, then jump to 0. The design is a special case of the method above, with SEQ = range(N).
  • Asynchronous clear: detect state N with a gate and use it to reset all flip-flops. It is cheap, but state N exists briefly as a glitch, so the circuit is fragile.

The output frequency of a modulo-N counter's carry output is the clock frequency divided by N, for any N. This is how a 1 MHz clock becomes 1 Hz with a chain of decade (mod-10) counters, six of them in a row.

Python Any modulus from a recipe

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.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a JK flip-flop need to go from Q = 1 to Q+ = 1?

    To stay at 1 the flip-flop must not reset, so K = 0 and J can be anything (1 sets it again, 0 holds).
  2. 2. How many flip-flops does a modulo-12 counter need, and how many states are unused?

    ceil(log2 12) = 4 flip-flops give 16 codes, 12 of which are used, so 4 are unused.
  3. 3. What is lock-out?

    Lock-out is a property of the next-state function on the unused states: they form a cycle outside the intended sequence.
  4. 4. Why are the D equations of an arbitrary counter simply the next-state bits?

    Q+ = D for a D flip-flop, so the excitation table is the next-state table itself.
  5. 5. A counter design with don't-cares for unused states passes the main-cycle test. What must you still do?

    Minimization may send an unused state into a loop of unused states. The only safe approach is to check every unused state.

Summary

  • To design a counter for any sequence: choose ceil(log2 N) flip-flops, write the next state of each state, convert transitions to flip-flop inputs with the excitation table, mark unused states as don't-cares, simplify and verify.
  • For a D flip-flop the input is the next-state bit. For JK, each transition gives a J and a K with don't-cares, which often means smaller logic.
  • Unused states are free for minimization, but the result is arbitrary there, and may lock the counter into a loop of unused states.
  • Always test every unused state. Fix lock-out with a self-starting design, extra feedback or a reset.
  • A modulo-N counter divides the clock by N, and chains of mod-N counters build seconds, minutes and other counting displays.

Next, move on to full state machines in Finite State Machines: Moore and Mealy.