Overview
Readings Brown & Vranesic §7.9, 7.11 · Floyd (Digital Fundamentals) counters · week 9
A counter is a group of flip-flops whose outputs step through a fixed sequence of binary values, one step per clock pulse. It’s one of the most important uses of flip-flops, since a counter counts pulses, divides a frequency, and generates timing sequences. This chapter picks up from Ch 7, where T and D flip-flops were introduced. It covers asynchronous (ripple) counters, which are simple but slow, then synchronous counters, where every flip-flop shares one clock. It finishes with extra features (parallel load and output carry) and two special counters: the BCD counter and the Johnson counter.
Notes
Counting with flip-flops
Take two negative edge-triggered J-K flip-flops, both with (so each one toggles on every active edge), and start them RESET. Flip-flop A is clocked by . Flip-flop B is clocked by .
- A toggles on every falling clock edge, so changes once per clock pulse.
- B toggles each time makes a HIGH-to-LOW transition, so changes once every two clock pulses.
Taking as the least significant bit, the pair steps through and then recycles to . That’s a 2-bit binary counter, and the sequence repeats every 4 clock pulses.
| After clock pulse | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| 0 | 1 | 0 | 1 | 0 | |
| 0 | 0 | 1 | 1 | 0 | |
| Count | 0 | 1 | 2 | 3 | 0 |
This is the key idea of the whole chapter: each stage divides the frequency of the stage before it by 2. So runs at , at , and in general an -bit counter’s most significant output runs at .
Example 7.13: How many flip-flops?
How many flip-flops are needed to produce a binary sequence for the decimal numbers 0 through 15? For 0 through 99?
Show answerHide answer
- flip-flops can hold different values, so we need the smallest with (number of states).
- 0 to 15 is values, and , so 4 flip-flops.
- 0 to 99 is values, and , so 7 flip-flops.
A counter with distinct states is a modulo- counter. An -bit binary counter is modulo-.
Asynchronous (ripple) up-counter
Figure 7.3: A 3-bit asynchronous up-counter made of positive edge-triggered T flip-flops. Each stage is clocked by the output of the stage before it.
The version in Brown & Vranesic (Figure 7.20) uses positive edge-triggered T flip-flops:
- Every input is tied to 1, so a flip-flop toggles on each positive edge of its own clock input.
- The first flip-flop is clocked by the primary input.
- The other two are clocked by the output of the stage in front of them. When that stage goes , rises, which is a positive edge. So a stage toggles each time the previous stage falls.
Reading as the count, it goes . That’s a modulo-8 up-counter.
The circuit is called asynchronous or a ripple counter, because only the first stage is synchronized to the clock. Each later stage changes after the one before it has changed, so the change ripples along, like a carry in a ripple-carry adder (Ch 5). Every flip-flop adds its own propagation delay.
Example 7.14: The transient states of a ripple counter
A 3-bit ripple up-counter has . What happens at the next clock pulse?
Show answerHide answer
The count should go (), and that needs all three flip-flops to toggle. They don’t toggle together:
| Time | Why | |
|---|---|---|
| Before the edge | 011 | stable count = 3 |
| After 1 flip-flop delay | 010 | toggled ; the others haven’t reacted yet |
| After 2 delays | 000 | the falling made toggle |
| After 3 delays | 100 | the falling made toggle (stable count = 4) |
For a short time the counter shows and , values that aren’t part of the counting sequence. If something decodes the count right then, it’ll see a glitch. In an -bit ripple counter the settling time grows with , which is the main reason ripple counters are only used where speed doesn’t matter.
Try it: press the clock pulse on the ripple counter and step it through to see the transient values. Then switch to the synchronous counter, where every stage changes on the same edge, and use to pause the count.
Example 7.15: Waveforms of a 3-bit counter
Three negative edge-triggered J-K flip-flops () are cascaded as in the first figure ( clocks B, clocks C). Find the binary sequence.
Show answerHide answer
Each output changes on the falling edge of the signal that clocks it:
| After clock pulse | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| (MSB) | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |
| 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | |
| (LSB) | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
| Count | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 0 |
The sequence is and then back to . is , is , is .
Asynchronous down-counter
A tiny change to the up-counter gives a down-counter. In Brown & Vranesic Figure 7.21 the clock input of each later stage is driven by the output of the stage before it, instead of by . Now a stage toggles when the previous one rises (instead of when it falls), and the count goes
A counter that can do both is an up/down counter.
Example 7.16: Up or down?
A 3-bit ripple counter has . What’s the count after the next pulse if it is (a) the up-counter, (b) the down-counter?
Show answerHide answer
- (a) . Only the first stage toggles. goes , which makes fall, and that isn’t the edge the next stage waits for.
- (b) . goes , which is the rising edge the next stage waits for, so toggles . Then rising makes toggle . All three bits end up 1, which is “0 minus 1” wrapping around to 7.
Synchronous counters
To avoid the ripple, give every flip-flop the same clock. Then all the flip-flops that need to change do so after one propagation delay from the same edge. The question is how each flip-flop knows when to change. Look at the count of a 3-bit up-counter:
| Count | |||
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 0 |
| 3 | 0 | 1 | 1 |
| 4 | 1 | 0 | 0 |
| 5 | 1 | 0 | 1 |
| 6 | 1 | 1 | 0 |
| 7 | 1 | 1 | 1 |
- changes on every clock cycle.
- changes only when .
- changes only when .
In general, in an up-counter a given flip-flop changes state only when all the flip-flops before it are 1.
Synchronous counter with T flip-flops
A T flip-flop toggles when and holds when , so each input is just the “all lower bits are 1” condition:
A 4-bit version (Brown & Vranesic Figure 7.22) uses a chain of AND gates, where each gate feeds the next stage and also the next gate. That doesn’t slow down the response in terms of the flip-flops, since they all still change after one propagation delay from the same positive edge. But a change in may have to pass through several AND gates before it reaches the top stage, and that has to finish within one clock period (minus the flip-flop setup time).
Example 7.17: T inputs in a 4-bit synchronous counter
The counter holds . Find and the next count. Repeat for .
Show answerHide answer
| Count | Toggling | Next count | ||||
|---|---|---|---|---|---|---|
| 0101 | 1 | 1 | 0 | 0 | 0110 | |
| 0111 | 1 | 1 | 1 | 1 | all four | 1000 |
At every lower bit is 1, so all four flip-flops toggle at once, with no ripple. This is the same style change that gave the glitch in Example 7.14.
Synchronous counter with D flip-flops
A D flip-flop has no toggle mode, so to toggle we feed it , and to hold we feed it . An XOR gate does both: gives when (hold) and when (toggle). That’s the same trick as the T flip-flop built from a D flip-flop in Ch 7. So (Brown & Vranesic Figure 7.24, 4 bits with an input):
The AND-gate chain creates the toggle condition for each stage. When every XOR passes straight back, so the counter holds.
Example 7.18: D-flip-flop counter with Enable
A 4-bit counter of this type is at . Find the next count with and with .
Show answerHide answer
- : , , , . Next count 1000.
- : every AND output is 0, so . The count stays 0111.
Timing limit
The slowest path in the counter runs from a flip-flop output, through the AND chain, through an XOR, and into the farthest input. For the chain in Figure 7.24 with bits, the clock period has to satisfy
where is the clock-to- delay, and are the gate delays and is the flip-flop setup time. So a synchronous counter is much faster than a ripple counter, which needs flip-flop delays after the clock edge, but its speed still drops slowly as gets bigger.
Output carry and cascading
The 4-bit counter has one more AND gate that produces an output carry:
It’s 1 only when counting is enabled and the counter is at its maximum count (1111 = 15), so it’s about to roll over to 0. Its job is cascading: connect it to the of a second 4-bit counter and you get an 8-bit counter.
Example 7.19: Cascading two 4-bit counters
Two 4-bit counters make an 8-bit counter. The carry out of the lower one drives the of the upper one. Show what happens from 14 to 16.
Show answerHide answer
| Lower 4 bits | Output carry | Upper 4 bits | Whole 8-bit count |
|---|---|---|---|
| 1110 | 0 | 0000 | 14 |
| 1111 | 1 | 0000 | 15 |
| 0000 | 0 | 0001 | 16 |
When the lower counter hits 1111, its carry goes HIGH and enables the upper counter. At the next clock edge the lower counter rolls over to 0000 and the upper one counts up by 1. The upper counter ticks once every 16 pulses.
Counters with parallel load
Normally a counter starts at 0. To start at any other value, the counter needs inputs through which an initial count can be loaded. Brown & Vranesic Figure 7.25 does this by putting a 2-to-1 multiplexer in front of every input:
- Input 0 of each MUX is the normal counting logic (the XOR output).
- Input 1 is the external data bit .
- The select line is a control input, .
| Mode | What happens at the next rising edge | |
|---|---|---|
| 0 | Count | counts up (if ) |
| 1 | Parallel load |
With the counting logic is bypassed completely. With the circuit is the normal counter of Figure 7.24 (the equations above still apply).
The main parts are four edge-triggered D flip-flops (they share one clock, so they update together), four 2-to-1 MUXes (the steering switches), the XOR gates (toggle logic) and the AND chain (the carry that moves from the least to the most significant bit).
Example 7.20: Load, then count
A 4-bit counter with parallel load has and data inputs . for the first clock edge, then . Find after each of the first 5 edges.
Show answerHide answer
| Edge | after | Note | |
|---|---|---|---|
| 1 | 1 | 1010 | loaded (10) |
| 2 | 0 | 1011 | count |
| 3 | 0 | 1100 | count |
| 4 | 0 | 1101 | count |
| 5 | 0 | 1110 | count |
Loading is also how you get a counter that doesn’t use the full states, because the counter can be sent back to a chosen start value before it runs out.
BCD counter
A BCD (binary-coded decimal) counter counts to and then goes back to . A plain 4-bit counter would keep going to 15, so the trick is to use parallel load to cut the count short:
- The data inputs are wired permanently to 0000.
- An AND gate watches and . The count 9 is , which is the first count with , so the gate goes HIGH exactly when the digit reaches 9.
- That signal goes to (through an OR gate, so that an external can also force a load).
- At the next clock edge the counter loads instead of counting to 10.
Brown & Vranesic Figure 7.28 puts two of them together for a two-digit BCD counter (00 to 99):
- Ones digit (): is permanently 1, so it counts every clock pulse. It wraps from 9 back to 0 using the method above.
- Tens digit (): its is driven by the ones digit’s ”= 9” AND gate. So it’s frozen almost all the time, and only counts when the ones digit is at 9, which is exactly when the ones digit is about to wrap. The ones digit goes and the tens digit goes up by 1 at the same edge.
- At 99: a second AND gate checks that both digits are 9, and it loads the tens digit with 0000 too, so the display goes .
- Clear: a signal goes to the pins of both digits, and the next edge forces the display to .
Example 7.21: Two-digit BCD counter
Show the count going from 28 to 31, and from 98 to the next valid count.
Show answerHide answer
| Edge | Ones digit | Tens digit | What happened |
|---|---|---|---|
| start | 8 (1000) | 2 (0010) | tens frozen, ones |
| 1 | 9 (1001) | 2 | ones counts; AND gate now HIGH |
| 2 | 0 (0000) | 3 (0011) | ones loads 0; tens is enabled, so it counts |
| 3 | 1 (0001) | 3 | tens frozen again |
From 98: the ones digit goes to 9 (99). Both digits are 9, so both are loaded with 0000 on the next edge, giving 00. The count never shows 100 and never shows an invalid BCD digit like 1010.
Johnson counter
A Johnson counter (also called a twisted-ring or switch-tail ring counter) is a shift register where the inverted output of the last flip-flop is fed back to the data input of the first one:
An ordinary ring counter would feed back instead. The inversion makes the register fill up with 1s one position at a time, and then empty with 0s one position at a time. All flip-flops are reset to 0 at the start.
Example 7.22: A 4-bit Johnson counter
Starting from , list the states.
Show answerHide answer
| Edge | ||
|---|---|---|
| start | – | 0000 |
| 1 | 1 | 0001 |
| 2 | 1 | 0011 |
| 3 | 1 | 0111 |
| 4 | 1 | 1111 |
| 5 | 0 | 1110 |
| 6 | 0 | 1100 |
| 7 | 0 | 1000 |
| 8 | 0 | 0000 |
- flip-flops give different states (compared to for a binary counter), then the sequence repeats.
- Only one bit changes on each clock edge.
- Because of that, each state can be decoded with just a 2-input AND gate (look at two neighbouring bits), and there are no glitches from several bits changing at once.
Johnson counters are used as hardware frequency dividers (prescalers), and as sequence generators, for example to drive a stepper motor.
Key formulas
Flip-flops needed for states
- : number of states (the modulus); : number of flip-flops
Frequency division
- : frequency of the output of stage (stage 0 is the LSB)
Synchronous up-counter, T flip-flops
Synchronous up-counter, D flip-flops with enable
Output carry (4 bits)
Counter with parallel load
- : the external data bit for stage
Johnson counter
Common mistakes
- Thinking a ripple counter has the right count the instant after the clock edge. It passes through transient counts while the change ripples through the stages.
- Mixing up which output clocks the next stage. With positive edge-triggered flip-flops, gives an up-counter and gives a down-counter.
- Using the wrong toggle condition in a synchronous counter. Stage toggles when all lower bits are 1, not just the bit right below it (, not ).
- Forgetting the XOR in a D flip-flop counter. A D flip-flop can’t toggle on its own, so .
- Forgetting that makes the counter hold, and that the output carry also needs .
- Ignoring the timing limit of the AND chain. The whole chain has to settle within one clock period minus the setup time.
- Loading the wrong value in a BCD counter. The data inputs are wired to 0000, and the load fires at 9, so the counter never shows 10.
- Feeding (not ) back in a Johnson counter, which gives a plain ring counter instead.
- Saying an -bit Johnson counter has states. It has .