Counters, registers & state machines

Digital logic & computing · tree view

Flip-flops chained to toggle on a clock make counters (ripple vs synchronous) and shift registers; feed a register's state back through gates and you get a finite state machine — the standard way to build sequenced behaviour in hardware.

Prerequisites

flip-flop symbol
flip-flop
ttl-7400 symbol
ttl-7400
cmos-4000 symbol
cmos-4000
ripple-counter-4bit.svg
ripple-counter-4bit.svg
shift-register-siso.svg
shift-register-siso.svg
fsm-traffic-light.svg
fsm-traffic-light.svg

Note how the least significant bit (LSB) toggles between 0 and 1 for every step in the count sequence, while each succeeding bit toggles at one-half the frequency of the one before it. The most significant bit (MSB) only toggles once during the entire sixteen-step count sequence: at the transition between 7 (0111) and 8 (1000).

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 11 — Binary count sequence source

Instead of cleanly transitioning from a "0111" output to a "1000" output, the counter circuit will very quickly ripple from 0111 to 0110 to 0100 to 0000 to 1000, or from 7 to 6 to 4 to 0 and then to 8. This behavior earns the counter circuit the name of ripple counter, or asynchronous counter.

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 11 — Asynchronous counters source

A counter is simply a device that counts. Counters may be used to count operations, quantities, or periods of time. They may also be used for dividing frequencies, for addressing information in storage, or for temporary storage.

— NEETS Module 13, Module 13, ch. 3, p. 3-23 (Counters)

The total number of counts or stable states a counter can indicate is called MODULUS.

— NEETS Module 13, Module 13, ch. 3, p. 3-23 (Counters)

The only way we can build such a counter circuit from J-K flip-flops is to connect all the clock inputs together, so that each and every flip-flop receives the exact same clock pulse at the exact same time:

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 11 — Synchronous counters source

A register is a temporary storage device. Registers are used to store data, memory addresses, and operation codes.

— NEETS Module 13, Module 13, ch. 3, p. 3-31 (Registers)

A shift register is a register in which the contents may be shifted one or more places to the left or right.

— NEETS Module 13, Module 13, ch. 3, p. 3-32 (Shift registers)

SCALING means to change the magnitude of a number. Shifting binary numbers to the left increases their value, and shifting to the right decreases their value. The increase or decrease in value is based on powers of 2.

— NEETS Module 13, Module 13, ch. 3, p. 3-34 (Scaling)

A circuit whose output depends not only on the present input but also on the history of the input is called a sequential circuit.

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 11 — Finite State Machines source

The Finite State Machine is an abstract mathematical model of a sequential logic function. It has finite inputs, outputs and number of states. FSMs are implemented in real-life circuits through the use of Flip Flops

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 11 — Finite State Machines (REVIEW) source
Show alternative explanations

Ripple counters are so named because the count is like a chain reaction that ripples through the counter because of the time involved.

— NEETS Module 13, Module 13, ch. 3, p. 3-23 (Ripple counters)

High-frequency operations require that all the FFs of a counter be triggered at the same time to prevent errors. We use a SYNCHRONOUS counter for this type of operation.

— NEETS Module 13, Module 13, ch. 3, p. 3-25 (Synchronous counter)

Thus, a four stage shift register delays "data in" by four clocks to "data out". The stages in a shift register are delay stages, typically type "D" Flip-Flops or type "JK" Flip-flops.

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 12 — Introduction source

There are shift registers which will shift data either left or right.

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 12 — Universal shift register source

If the serial output of a shift register is connected to the serial input, data can be perpetually shifted around the ring as long as clock pulses are present.

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 12 — Ring counters source

A ring counter is defined as a loop of bistable devices (flip-flops) interconnected in such a manner that only one of the devices may be in a specified state at one time.

— NEETS Module 13, Module 13, ch. 3, p. 3-27 (Ring counter)

Make a note that this is a Moore Finite State Machine. Its output is a function of only its current state, not its input. That is in contrast with the Mealy Finite State Machine, where input affects the output.

— kuphaldt-linac DIGI, Vol. IV (Digital), ch. 11 — Finite State Machines source

Ripple counter — what the outputs really show during a carry

Four toggle flip-flops in a ripple (asynchronous) chain: Q0's falling edge clocks Q1, Q1's clocks Q2, and so on. Each stage reacts only after the previous one has flipped, so a carry from 7 to 8 passes through false intermediate values — exactly the sequence Kuphaldt quotes. Each row is one flip-flop propagation delay later.

To predict the outcome:

  1. Clock edge arrives; Q0 toggles first (1 → 0).
  2. Q0's 1 → 0 edge clocks Q1, which then toggles; each toggle clocks the next stage one delay later.
  3. Read the four outputs only after the last stage settles — anything seen before that is a transient, not the count.
  4. If downstream logic (a decoder, a memory pointer) samples during the ripple, it sees the false rows below; strobe it with the clock, or use a synchronous counter.
timeq3_q2_q1_q0value_seenverdict
t0 — clock edge01117correct count
t0 + 1 delay (Q0 flipped)01106false transient
t0 + 2 delays (Q1 flipped)01004false transient
t0 + 3 delays (Q2 flipped)00000false transient
t0 + 4 delays (Q3 flipped)10008correct count, settled

Modulus — predict divide-by-N from the number of stages

NEETS defines the modulus as "the total number of counts or stable states a counter can indicate." N free-running toggle stages give 2^N states, max count 2^N − 1, and an output frequency of f/2^N. Reset early (the decade counter NANDs outputs at count 10) and the modulus shrinks to whatever you reset at.

To predict the outcome:

  1. Count the stages: modulus = 2^N, max count = 2^N − 1, each stage halves the frequency again.
  2. For a decade counter, add a NAND gate that resets all stages the moment they reach 1010 (10): it counts 0–9, modulus 10, and divides by 10.
  3. To divide by any N < 2^N, decode the count N with gates into the reset line.
stagesmax_countmodulusoutput_frequency
112f / 2
378f / 8
41516f / 16
4 + NAND reset at 10 (decade)910f / 10
8255256f / 256

Synchronous counter — which bit toggles on the next edge

With every flip-flop clocked simultaneously, a stage must decide electronically whether to toggle. Kuphaldt's rule: just before a bit toggles, all lower-order bits are high — so Q1 toggles when Q0 = 1, Q2 when Q1 = Q0 = 1, and AND gates on the lower outputs enable each stage's J-K inputs. Predict the next count column-wise from the present one.

To predict the outcome:

  1. Q0 toggles on every clock edge (its J and K are tied high).
  2. For each higher bit: it toggles only if every bit below it is 1; otherwise it holds.
  3. Apply the rule to all bits at once — everything changes on the same edge, no intermediate values, no ripple.
  4. Check: the sequence 000 → 001 → … → 111 → 000 is exactly binary counting.
present_q2_q1_q0q0_togglesq1_togglesq2_togglesnext_count
000 (0)yesno (Q0 = 0)no001 (1)
001 (1)yesyes (Q0 = 1)no010 (2)
010 (2)yesno (Q0 = 0)no011 (3)
011 (3)yesyesyes (Q1 = Q0 = 1)100 (4)
100 (4)yesnono101 (5)
111 (7)yesyesyes000 (0) — wraps

Shift register — trace the data flow through four stages

A serial-in/serial-out register of four D flip-flops: on each clock the input bit enters stage D0 and every other bit moves one stage to the right. Kuphaldt: "a four stage shift register delays 'data in' by four clocks to 'data out.'" Below, the stream 1,1,0,1 (then idle 0s) is shifted in; shifting left multiplies by 2 per place, right divides — NEETS' "scaling."

To predict the outcome:

  1. Write the input stream in the order it is clocked in.
  2. At each clock: new bit → D0, old D0 → D1, old D1 → D2, old D2 → D3.
  3. The serial output shows the bit that entered four clocks earlier — a fixed delay of four clock periods.
  4. Reversing the direction (or tapping all stages in parallel) turns the same chain into serial-to-parallel conversion.
clockd3d2d1d0serial_out
0 (reset)00000
1 (in = 1)00010
2 (in = 1)00110
3 (in = 0)01100
4 (in = 1)10111
5 (in = 0)01011

FSM state-transition table — a two-road traffic light

A Moore FSM: outputs depend only on the current state. Two state bits Q1Q0 encode four states of an intersection; the only input is T (timer expired). On each clock edge, T = 1 advances the ring, T = 0 holds. Kuphaldt's design recipe: list states, assign binary codes, fill the state table, then realize next-state logic with flip-flops and gates.

To predict the outcome:

  1. List the states in cycling order: 00 NS-green → 01 NS-yellow → 10 EW-green → 11 EW-yellow → back to 00.
  2. Encode each state with the two flip-flop bits (Moore: the lights are decoded from the state alone).
  3. For each row, the input T selects the transition: T = 0 stays, T = 1 moves to the next state in the ring.
  4. Outputs follow: exactly one green (or yellow) at a time, the other road red.
state_q1_q0meaningns_lightew_lightinput_tnext_state
00north-south greenGR101
00north-south greenGR000 (hold)
01north-south yellowYR110
10east-west greenRG111
11east-west yellowRY100 — ring closes

Candidate projects — awaiting review

  • 4-bit binary counter on a breadboard: 74LS93 (or discrete JK flip-flops from 7400/4011 NANDs) clocked slowly by a 555, four LEDs reading QD..QA in binary; predict each count before the clock pulse. (https://www.allaboutcircuits.com/projects/build-a-4-bit-binary-counter-with-5x7-led-matrix/, community)
  • Divide-by-N from a 7493: wire the outputs that are 1 at N to the R0(1)/R0(2) reset inputs (QD+QB for divide-by-10) and confirm the chip clears as it reaches N — a hardware divide-by-N prediction exercise; also watch the 7 → 8 ripple transient on a scope. (https://digiwleea.wleeaf.dev/tools/build/7493-counter/, community)
  • LED chaser: 74HC595 shift register driven by a 555 astable, a single 1 walking through eight LEDs — extend by predicting the shift-in pattern that lights every third LED, then switch direction or make it a ring by feeding the last output back to serial-in. (https://www.instructables.com/8-LED-Chaser-with-74HC595-8-Bit-Shift-Register/, community)
  • Traffic-light controller FSM: implement the four-state intersection machine above with D flip-flops plus gates (or two 74LS163 counters as timers), clocked from a debounced button, LEDs as lights — the classic sequential-design lab from state diagram to wired circuit. (https://faculty.kfupm.edu.sa/COE/mimam/files/COE200experiment12.pdf, curriculum)