Synchronous Counter Design from Flip-Flops

Clock every flip-flop together and design its toggle logic. Then build a counter for any sequence that no unused state can trap.

You Will Be Able To

  • Build a synchronous binary counter from T flip-flops and an AND chain.
  • Design a counter for any sequence from a state table, an excitation table and K-maps.
  • Check where every unused state goes, and fix a counter that locks up.
  • Explain why every bit of a synchronous counter changes at the same moment.
Lesson
G6.4
Time
30 min
Runs on
Your browser
Check
5 questions

The ripple counter of G6.3 shows false counts because its flip-flops switch one after another. Give them all the same clock and they switch together. Then each one needs logic that says when to toggle.

A 3-Bit Synchronous Counter, 011 to 100
A 3-Bit Synchronous Counter, 011 to 100n0 · NOTd0=0DQf0 · DFFq0=1x1 · XORd1=1DQf1 · DFFq1=0a2 · ANDt2=0x2 · XORd2=1DQf2 · DFFq2=1clk=0
clk=0
n0 · NOTq0=1 → d0=0
f0 · DFFd0=0, clk=0 → q0=1
x1 · XORq1=0, q0=1 → d1=1
f1 · DFFd1=1, clk=0 → q1=0
a2 · ANDq1=0, q0=1 → t2=0
x2 · XORq2=1, t2=0 → d2=1
f2 · DFFd2=1, clk=0 → q2=1
Read the connections
GateKindInputsOutput
n0NOTq0d0
f0DFFd0, clkq0
x1XORq1, q0d1
f1DFFd1, clkq1
a2ANDq1, q0t2
x2XORq2, t2d2
f2DFFd2, clkq2

Outputs: q2, q1, q0. MUX inputs: d0, d1, select. Latch/flip-flop inputs: data, enable/clock. Unknown values are marked x; high impedance is folded into x in this model.

Gate delay 16. Settled after 16 gate delays from t = 0.

Output values at each gate delay012345678910111213141516q200000111111111111q111111000000000000q011111000000001111
Scroll horizontally ↔
Every computed frame
Timeq2q1q0
0011
1011
2011
3011
4011
5100
6100
7100
8100
9100
10100
11100
12100
13101
14101
15101
16101

  1. Step 3 · Count 3. q = 011. T2 = q1 q0 = 1, so every D input already holds the next count, 100.
  2. Step 5 · One edge, one delay. All three flip-flops load at the same edge. The outputs jump from 011 to 100 together: no false count.
  3. Step 6 · Logic settles inside. The next-state logic now works out 101. d2 dips for one delay, but no flip-flop is sampling it.
  4. Step 13 · Count 5. The next edge loads 101. Only q0 changes.

Unit-delay model. A T flip-flop is drawn as a DFF with D = Q xor T. Compare with the ripple counter of G6.3.

The Synchronous Binary Counter

Count in binary and watch each bit. Bit 0 flips every time. Bit 1 flips when bit 0 is 1. Bit k flips when every bit below it is 1.

BitToggles whenT input
q0alwaysT0 = 1
q1q0 = 1T1 = q0
q2q0 = q1 = 1T2 = q0 q1
qkall lower bits are 1Tk = q0 q1 … q(k-1)

Each T input is the AND of the bits below it, so the gates form an AND chain.

Every flip-flop gets the same clock. At the edge each one either toggles or holds, all at once. The lab builds this counter from T flip-flop cells and one AND gate.

A Counter for Any Sequence: The State Table

A counter can step through any sequence. Take 1, 3, 2, 6, 4 and back to 1, with three T flip-flops. List each state and its next state.

Designing a Counter for Any SequenceFour steps, left to right. The last step can send you back to the K-maps.
  • Neutral
  • Step
  • Structure
Designing a counter for any sequenceFour steps, left to right. The last step can send you back to the K-maps. neutral State table present → next neutral Excitation T = Q xor Q+ neutral K-maps one per input step Check unused every state enters? locks up
  1. neutral State tablepresent → next
    • Excitation
  2. neutral ExcitationT = Q xor Q+
    • K-maps
  3. neutral K-mapsone per input
    • Check unused
  4. step Check unusedevery state enters?
    • locks up → K-maps
  1. State table

    One row per state of the sequence; the unused states start as don't-cares.

  2. Excitation

    For each flip-flop, the input that makes each change happen.

  3. K-maps

    Minimize each input over the don't-cares.

  4. Check

    Work out where each unused state goes. If one never enters the sequence, pick another cover or give up a don't-care.

Read the diagram as text
  • State table: present → next
  • Excitation: T = Q xor Q+
  • K-maps: one per input
  • Check unused: every state enters?
  • State table → Excitation
  • Excitation → K-maps
  • K-maps → Check unused
  • Check unused → K-maps: locks up

The last step is the one most often skipped, and the one that finds lock-up.

Present q2 q1 q0Next q2 q1 q0T2 T1 T0 = Q xor Q+
001011010
011010001
010110100
110100010
100001101
000unusedX X X
101unusedX X X
111unusedX X X

For a T flip-flop the excitation is T = Q xor Q+: 1 where the bit must change. The unused states 0, 5 and 7 start as don't-cares.

With D flip-flops you would skip the last column: D is the next state itself. With JK, each bit gets two columns from the JK excitation table.

K-Maps for the T Inputs

Each T column is a function of q2 q1 q0 with three don't-cares. Minimize each one with a K-map (G3.1). Each has two minimum covers of the same size.

Input1 in statesMinimum covers (2 terms, 4 literals each)
T22, 4q1'q0' + q2'q0' or q2'q0' + q2q1'
T11, 6q1'q0 + q2q1 or q2'q1' + q2q1
T03, 4q1'q0' + q1q0 or q1q0 + q2q1'

Computed with an exact minimizer over the don't-cares 0, 5 and 7.

Pick one cover for each input and the design is done. Or is it? The pick also decides what the unused states do.

Unused States and Lock-Up

A counter can land in an unused state at power-up or after a noise hit. It must find its way back into the sequence. A counter that stays out forever has locked up.

First pick

T2 = q1'q0' + q2'q0'
T1 = q2'q1' + q2q1
T0 = q1'q0' + q1q0

Unused states: 0 → 7, 5 → 5, 7 → 4. State 5 goes to itself: the counter locks up.

Fixed: the other T0 cover

T2 = q1'q0' + q2'q0'
T1 = q2'q1' + q2q1
T0 = q1q0 + q2q1'

Unused states: 0 → 6, 5 → 4, 7 → 4. Every one enters the sequence at the next edge.

Both picks cost the same. Only the check of the unused states tells them apart. When no minimum cover works, give up a don't-care: write the next state you want for that unused state.

The Fixed Counter, Unused States Included
The Fixed Counter, Unused States Included0101010101010101S1001count 1S3011count 3S2010count 2S6110count 6S4100count 4S0000unused 0S5101unused 5S7111unused 7
  1. count 1 · S1001
    • ↓0 → S1
    • ↓1 → S3
  2. count 3 · S3011
    • ↓0 → S3
    • ↓1 → S2
  3. count 2 · S2010
    • ↓0 → S2
    • ↓1 → S6
  4. count 6 · S6110
    • ↓0 → S6
    • ↓1 → S4
  5. count 4 · S4100
    • ↓0 → S4
    • ↓1 → S1
  6. unused 0 · S0000
    • ↓0 → S0
    • ↓1 → S6
  7. unused 5 · S5101
    • ↓0 → S5
    • ↓1 → S4
  8. unused 7 · S7111
    • ↓0 → S7
    • ↓1 → S4
StateInputNextOutput in state
S10S1001
S11S3001
S30S3011
S31S2011
S20S2010
S21S6010
S60S6110
S61S4110
S40S4100
S41S1100
S00S0000
S01S6000
S50S5101
S51S4101
S70S7111
S71S4111

Current state: S4 · Output: 100

Sampled input, state and output waveformStepState0S51S42S13S34S25S66S4en—111111q21100011q10001110q01011000
Scroll horizontally ↔
Sampled waveform: Moore output after each edge; Mealy output on the taken arc.
Step0123456
Input—111111
StateS5S4S1S3S2S6S4
Output101100001011010110100

  1. Step 1. From the unused state 5 the first edge goes to 4.
  2. Step 6. After that the counter stays in its loop of five states.

en = 1 is a clock edge with the counter running; en = 0 holds. The run starts in unused state 5.

Why the Bits Change Together

In the lab every changed bit moves 1 ns after the edge, the clock-to-Q delay of one cell. The next-state logic works between edges and has a whole period to settle.

The cost is the AND chain. For a wide counter it grows long, which is why G6.5 looks at lookahead enables.

Asked in interviews
  1. How do you design a synchronous counter with T flip-flops?

    Each bit toggles when every lower bit is 1, so T0 = 1 and Tk is the AND of q0 to q(k-1). All flip-flops share the clock.

  2. What is lock-up in a counter?

    The counter enters an unused state and never returns to its sequence. Check the next state of every unused state after minimizing, and fix the don't-cares if one loops.

  3. Why do we use the excitation table to design a counter?

    We know each change of state we want and need the inputs that cause it. The excitation table gives those inputs, with don't-cares that simplify the logic.

Try It

Each line is one edge. The count goes 001 to 111 and wraps to 000, and every changed bit moved 1 ns after its edge.

Even 011 to 100, where all three bits flip, moves in one step. Compare with the 6 ns ripple of G6.3.

Change t2 to come from q[1] alone and run again: the count goes wrong after 010.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: q=001 (every changed bit moved 1 ns after the edge) q=010 (every changed bit moved 1 ns after the edge) q=011 (every changed bit moved 1 ns after the edge) q=100 (every changed bit moved 1 ns after the edge) q=101 (every changed bit moved 1 ns after the edge) q=110 (every changed bit moved 1 ns after the edge) q=111 (every changed bit moved 1 ns after the edge) q=000 (every changed bit moved 1 ns after the edge) q=001 (every changed bit moved 1 ns after the edge)
Read included file: cells.sv
`timescale 1ns/1ns
// cells.sv: the cells of this lab (read-only).
// TFF: toggles 1 ns after a rising clk edge when t = 1; clr clears it at once.
module TFF  (output reg q, input wire t, input wire clk, input wire clr);
  always @(posedge clk or posedge clr)
    if (clr)    q <= #1 1'b0;
    else if (t) q <= #1 ~q;
endmodule
module AND2 (output wire y, input wire a, input wire b); and #1 (y, a, b); endmodule
Read included file: tb.sv
// tb.sv: nine rising edges; after each, q and how long after the edge its bits moved (read-only).
`timescale 1ns/1ns
module tb;
  logic clk = 0, clr = 0;
  wire  [2:0] q;
  time  t_edge, t_change;
  count3 dut (.clk(clk), .clr(clr), .q(q));
  always @(q) t_change = $time;
  initial begin
    #1 clr = 1; #1 clr = 0;               // q = 000
    repeat (9) begin
      #8 clk = 1; t_edge = $time;
      #2 clk = 0;
      $display("q=%b  (every changed bit moved %0d ns after the edge)", q, t_change - t_edge);
    end
  end
endmodule

Check Yourself

ConceptIn a 5-bit synchronous binary counter built from T flip-flops, what is T4?
ConceptA T flip-flop must go from Q = 1 to Q+ = 1. What must T be?
ConceptAfter minimizing a counter with don't-cares, an unused state goes to another unused state, which goes back to the first. What is wrong?
Predict the outputThe lesson's first pick of covers starts in the unused state 5. The program prints the state after each of four edges. Predict the exact line.
module tb;
  logic       clk = 0;
  logic [2:0] q;
  logic [2:0] t;
  string      s = "";
  // The first pick: T2 = q1'q0' + q2'q0'; T1 = q2'q1' + q2q1; T0 = q1'q0' + q1q0
  assign t[2] = (~q[1] & ~q[0]) | (~q[2] & ~q[0]);
  assign t[1] = (~q[2] & ~q[1]) | (q[2] & q[1]);
  assign t[0] = (~q[1] & ~q[0]) | (q[1] & q[0]);
  always @(posedge clk) q <= q ^ t;              // three T flip-flops
  initial begin
    q = 3'd5;                                    // start in an unused state
    repeat (4) begin #1 clk = 1; #1 clk = 0; s = {s, $sformatf("%0d", q)}; end
    $display("%s", s);
  end
endmodule
ConceptA synchronous 3-bit counter and a ripple 3-bit counter use flip-flops with a 2 ns clock-to-Q. How long after the edge does 011 become 100 in each?

Common Mistakes

Wrong Leave the unused states as don't-cares and never check them.

The don't-cares decide the next state of the unused states. In the worked design one choice made 5 go to 5 forever.

Fix After minimizing, work out the next state of every unused state. Change a cover if one loops.

Wrong Clock the higher stages from the lower outputs, as in a ripple counter.

The bits then change one after another and the count passes through false values (G6.3).

Fix Give every flip-flop the system clock and compute when each one toggles.

Wrong Make bit k toggle when only bit k-1 is 1.

A binary count carries into bit k only when every lower bit is 1. With T3 = q0 a 4-bit counter jumps from 0001 to 1010.

Fix T for bit k is the AND of all the bits below it.

Wrong Read the characteristic table when designing the input logic.

It answers "what will Q become?" The design question is "which input gives this change?"

Fix Use the excitation table: for a T flip-flop, T = Q xor Q+ (G5.7).

Remember

  • A synchronous counter clocks every flip-flop together; all changed bits move at one moment.
  • In a binary counter bit k toggles when every lower bit is 1: Tk = q0 q1 … q(k-1).
  • Any sequence: state table, excitation table (T = Q xor Q+), then a K-map per input.
  • Don't-cares decide where unused states go: check every one, and fix a cover that locks up.

Practice

Report a problem with this page