C728 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 28 · Registers, counters and clocks

Flip-flops in rows, stepping together

Put n flip-flops side by side on one clock and you have an n-bit register: a word of memory that updates all at once. Feed a register's output through an adder back to its input and it counts. Feed each flip-flop into the next and it shifts. Registers, counters and shift registers are how digital circuits keep time, divide frequencies, convert between serial and parallel data, and hold every value a processor works with.

Play

  • A 3-bit counter: each clock edge adds 1 to the number held in Q2 Q1 Q0. Hold R at 1 to reset it.
  • Read the count off the timing diagram. Compare the frequencies of CLK, Q0, Q1 and Q2.
  • After 7, what comes next?

Predict

A 3-bit counter is clocked at 8 MHz. What frequency appears on its top bit, Q2?

Name it

Sequential building blocks

Register. n D flip-flops sharing a clock (and often an enable and a reset). On each edge all n bits load at once.

Binary counter. A register plus an incrementer: Q ← Q + 1. Bit k toggles when all lower bits are 1:

Q0←Q¯0Q1←Q1⊕Q0Q2←Q2⊕Q1Q0

An n-bit counter counts 0 to 2n−1, then wraps to 0. Bit k has frequency fclk/2k+1.

Modulo-N counter. Reset to 0 when the count reaches N − 1: a divide-by-N, for clock dividers that aren't powers of 2.

Shift register. Each flip-flop's input is the previous one's output, so on every edge the bits move one place along. Used for serial links (one bit per clock in, a whole word out), delay lines and pseudo-random sequences.

Clocks. Every flip-flop in a synchronous design shares one clock, generated by a crystal oscillator and often multiplied up by a phase-locked loop. The clock must reach all flip-flops at nearly the same moment; the difference, clock skew, eats into the timing budget of lesson 19. The counter above shows that budget at work: its top bit's next value passes through three gates (an AND, an XOR and the reset multiplexer), so with a flip-flop's own delay it needs four ticks. Its clock period is 8 ticks; at 4, the top bit would be sampled before its logic settled and the count would go wrong.

Worked example

A 4-bit binary counter runs from a 16 MHz clock. At what frequency does its top bit toggle?

Show the solution
  1. Clocks per cycle of the top bit. Bit 3 completes one cycle every 24 = 16 clocks.
  2. Divide. 16MHz16=1MHz Each bit divides the frequency by two.

Answer 1 MHz.

Worked example

Design a decimal (modulo-10) counter. How many flip-flops, and when does it reset?

Show the solution
  1. Count the flip-flops. Four, since 24≥10.
  2. When it resets. It counts 0 to 9 and returns to 0 on the clock after 9 (1001).
  3. Its output. Its output toggles at a tenth of the clock rate.

Answer four flip-flops, returning to 0 on the clock after 9 (1001).

Worked example

A 4-bit Johnson counter (a shift register whose inverted last bit feeds its input) starts at 0000. List its states. How many are there, and what's it good for?

Show the solution
  1. List the states. Each clock shifts right and feeds back the complement of the last bit: 0000, 1000, 1100, 1110, 1111, 0111, 0011, 0001, then 0000 again.
  2. Count them. Eight states from four flip-flops, 2n in general, against 16 for a binary counter.
  3. Decoding. In return, only one bit changes per step, so each state can be decoded by a two-input gate without glitches.
  4. Dividing. The counter divides its clock by 8 with a square wave on every bit.

Answer eight states; it gives glitch-free decoding with two-input gates and divides its clock by 8 with a square wave on every bit.

Check

What's the highest value a 3-bit binary counter reaches before wrapping to 0?

Check

A binary counter runs from an 8 MHz clock. What's the frequency of bit Q0?

Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.

Check

How many divide-by-2 stages turn a 1048576 Hz crystal into 1 Hz?

Check

A 4-bit shift register holds 0110 and shifts left 1 time(s), with 0s shifted in at the right. What does it hold?

Check

A modulo-3 counter runs from 10 MHz. How often does it wrap around?

Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.

Check

An n-flip-flop register with n = 4 runs from a 10 MHz clock.

(a)

How many states does a Johnson counter made from it have?

(b)

At what frequency does a binary counter's top bit toggle?

Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.

Check

A modulo-N counter runs from a clock of frequency f.

Step 1

How many flip-flops does it need?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does each bit of a binary counter run at half the frequency of the bit below it?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.