PhysicsElectronics › Flip-flops, counters and clocks

Flip-flops, counters and clocks

A D-type flip-flop copies its data input to its output on each rising clock edge and holds it between edges, storing one bit. Wire a row of them to toggle and every stage halves the frequency, so the chain counts in binary. An astable supplies the clock that steps it.

Digital signal processing, part 2 of 2. Part 1 is Combinational logic.

Builds on Combinational logic and The ideal op-amp and the comparator.

IN THIS TOPIC

  • Describe the D-type flip-flop as a one-bit memory, and use counters that halve the frequency at every stage.
  • Reset a chain early to count modulo n, and tell BCD, up/down and Johnson counters apart.
  • Read the pulse width, frequency, mark-to-space ratio and duty cycle off an astable's output.

COMMON MISCONCEPTION

An astable's duty cycle is its high time divided by its low time.

That ratio, t₁/t₂, is the mark-to-space ratio. The duty cycle is the high time as a fraction of the whole period, t₁/(t₁ + t₂), quoted as a percentage: a 3:1 mark-to-space is a 75% duty cycle, not 300%.

Memory, and counting by halving

Gates have no memory; release a switch and the output reverts immediately. Sequential circuits add the missing ingredient with the D-type flip-flop, a one-bit memory. It has a data input D, a clock input, and an output Q. On each rising clock edge it copies whatever is on D to Q, then holds Q frozen until the next edge, ignoring D completely in between. Registers and other small fast storage are rows of these, one per bit; bulk memory uses denser cells instead, a capacitor per bit in DRAM and charge-trapping transistors in flash.

One connection turns that one-bit memory into a frequency divider. Connect the flip-flop's inverted output back to its own D input and every clock edge loads the opposite of what Q held, so Q toggles at each edge. Two clock cycles pass before Q completes one full cycle of its own, so Q runs at half the clock frequency, a clean square wave. Feed that stage's inverted output onward as the clock of a second stage and the halving repeats. For n stages,

fout=fin2nf_{out} = \frac{f_{in}}{2^{n}}NOT ON THE AQA DATA SHEET: LEARN IT
Above, a clock feeds two divide-by-two blocks in series, the frequency labelled four thousand and ninety-six hertz, then two thousand and forty-eight, then one thousand and twenty-four. Below, three aligned square waves: the clock, then the first stage's output changing state at every clock cycle and so half its frequency, then the second stage's output at a quarter.
FIG. 1A clock and two toggling flip-flop stages. The first output changes state at every clock cycle's end, completing one cycle for every two of the clock; the second halves the frequency again, to a quarter of the original.

Read all the Q outputs at once, last stage first, and the same chain is a binary counter. The outputs step through 00, 01, 10, 11 and onward, one count per clock pulse, rolling over after 2n2^{n} states. Counting and frequency division are the same circuit described two ways.

WORKED EXAMPLE

The watch that counts out one second

A quartz watch crystal oscillates at 32 768 Hz. How many toggling stages does the watch need to derive its once-per-second tick?

32 768 is 2152^{15}, so each of 15 halvings takes the frequency down one power of two, ending at exactly 1 Hz.

That number is designed backwards. Crystal makers chose 32 768 Hz precisely because a whole number of halvings lands on one second.

INDEPENDENT PRACTICE

Designing the divider

A timing circuit is clocked at 4096 Hz and must drive a display at 16 Hz. How many flip-flop stages are needed, and how many distinct states does that chain pass through as a counter?

Show the working

The division needed is 4096/16 = 256 = 282^{8}, so 8 stages.

An 8-stage counter runs through 282^{8} = 256 states, 0 to 255, before repeating.

Keep the two answers apart. Frequency out is f/2nf/2^{n}; the number of states counted is 2n2^{n}.

Counters that stop where you want them to

A plain chain always runs to 2n2^{n} and rolls over, and that is rarely the number anybody wants. Wire the outputs that are high at the unwanted count into a NAND gate, and take its output to the reset input of every stage. The instant that count appears the gate clears the whole chain, so the count never persists there and the chain starts again from zero instead. Detect ten, 1010, and the chain counts 0000 up to 1001 and resets: a modulo-10, or decade, counter. Any modulus is available by choosing which outputs feed the gate.

The decade is the useful one, because a decimal digit has exactly ten states. Four bits holding 0000 to 1001 and nothing above is binary-coded decimal, and a BCD counter is a decade counter read as one decimal digit. Cascade them, the last output of each decade clocking the next, and the units decade rolls over once per ten pulses while the tens decade rolls over once per hundred, so four of them drive a display reading 0 to 9999 with no arithmetic in between. A frequency meter is that row of BCD counters behind a gate that lets pulses through for exactly one second.

Counting backwards needs one extra connection per stage. Each flip-flop copies D on a rising edge, so a stage clocked from the previous stage's inverted output steps the count up, and the same stage clocked from Q instead steps it down. An up/down counter brings both routes to a pair of gates in every stage and lets a single direction input choose between them, which is what a lift controller requires when it has to track how many floors are left in either direction.

A Johnson counter gives up the ripple altogether. Put n flip-flops in a row, all clocked together, each output feeding the next one's D input so that a pattern shifts one place along the row per pulse, and then bring the last stage's inverted output back to the first stage's D. From 0000 the row fills with ones from the left, 1000, 1100, 1110, 1111, and the inverted feedback then fills it with zeros the same way, 0111, 0011, 0001, 0000. Four stages therefore cycle through eight states rather than sixteen, and n stages through 2n.

Using only half the available states has two consequences. Only one output changes at each step, so the outputs never pass through a wrong combination on their way to the right one as a ripple chain does, and any single state can be recognised by a two-input gate rather than an n-input one. Where a ripple counter is asked only for a number, a Johnson counter is asked for clean, glitch-free timing signals in a fixed order.

Two ladders of binary codes side by side, each generated by working the circuit through. On the left a 4-bit ripple counter runs through all sixteen states from 0000 to 1111, and the step ringed in coral, 0111 to 1000, changes all four bits at once; because the stages of a ripple chain toggle one after another it shows 0110, then 0100, then 0000 on the way, codes nobody asked for. On the right a 4-stage Johnson counter runs through eight states only, 0000, 1000, 1100, 1110, 1111, 0111, 0011, 0001 and back to the start, with exactly one digit coloured in each row because exactly one bit changes at every step, the wrap included. Half the states are given up, and what comes back is ordering with no wrong code in between, which is why timing signals are taken from a Johnson counter and a number from a ripple counter.
FIG. 2The two sequences side by side, each worked through from its own circuit. The 4-bit chain runs to sixteen states, and the step from 0111 to 1000 changes all four bits at once; because a ripple chain's stages toggle one after another it shows 0110, then 0100, then 0000 on the way, codes that do not belong to its count. The Johnson counter gives up half the states and gets ordering in return: one bit changes at each step, the wrap included.

GUIDED PRACTICE

Choosing the chain

A circuit must count from 0 to 11 and repeat. How many flip-flops does it need, and which count must the reset gate detect? Then state how many states a 5-stage Johnson counter passes through.

Show the working

Twelve states need at least twelve, and 232^{3} = 8 is too few, so 4 flip-flops, which offer sixteen.

The chain must be cleared as soon as it reaches twelve, so the gate detects 1100, watching the 8s and 4s outputs. The counter then shows 0000 to 1011, which is 0 to 11.

A Johnson counter gives 2n states, so five stages give 10 states, not 252^{5} = 32.

Count the states rather than the top number. A modulo-12 counter reaches 11, so the count the reset gate detects is one more than the highest count displayed.

The astable, which supplies the clock

Every counter above waits on a clock, and something has to make one. A comparator will, once it is given a memory of its own. Feed a fraction of the output back to the non-inverting input, which is positive feedback, and the circuit holds whichever rail it has reached until the other input is dragged past the threshold that fraction sets. Now charge a capacitor towards the output through a resistor and take the inverting input from it. The capacitor climbs, crosses the upper threshold, and the output flips; it then falls towards the other rail, crosses the lower threshold, and the output flips back. Neither state is stable, so it never stops. That is an astable.

All the timing lives in the charging. Each interval lasts a fixed multiple of the RC of whichever path is charging the capacitor during it, the multiple set by where the thresholds sit, so doubling that resistance doubles that interval and touches nothing else. Give the two directions separate paths, a pair of resistors each with a diode that lets current through one way only, and the high time and the low time become independent of each other.

Three numbers describe the output, and questions ask for them by name. The pulse width is the length of the high part on its own, t1t_{1}. The period is t1+t2t_{1} + t_{2} and the frequency is its reciprocal. The mark-to-space ratio is the high time divided by the low time, t1/t2t_{1}/t_{2}, quoted as a ratio. The duty cycle is the high time as a fraction of the whole period, t1/(t1+t2)t_{1}/(t_{1} + t_{2}), quoted as a percentage. Equal intervals give a square wave, mark-to-space 1:1 at a duty cycle of 50%.

A single cyan square wave runs left to right, its high parts three times as long as its low parts. Dashed vertical lines mark off the first complete cycle; above it a cyan arrow spans the long high time t one, 1.5 ms, and an amber arrow the short low time t two, 0.5 ms. A black arrow below spans the whole 2.0 ms period, labelled with the 500 Hz frequency it gives, and a line underneath reads off the 3:1 mark-to-space ratio and the 75% duty cycle.
FIG. 3An astable output held high for 1.5 ms and then low for 0.5 ms. The pulse width t₁ and the low time t₂ are marked, a bracket below spans the 2.0 ms period and gives the 500 Hz frequency it stands for, and a line reads off the 3:1 mark-to-space ratio and the 75% duty cycle from the same two times.

WORKED EXAMPLE

Reading an astable's output

An astable's output is high for 1.5 ms and low for 0.50 ms. Give the pulse width, the frequency, the mark-to-space ratio and the duty cycle. The resistance in the path that sets the low time is then doubled: give the new duty cycle and frequency.

The pulse width is the high time alone, 1.5 ms. The period is 1.5 + 0.50 = 2.0 ms, so f = 1/(2.0 × 10−3) = 500 Hz.

Mark-to-space = 1.5/0.50 = 3.0, written 3:1. Duty cycle = 1.5/2.0 = 0.75, or 75%.

Doubling that resistance doubles the low time to 1.0 ms, so the period becomes 2.5 ms. Duty cycle = 1.5/2.5 = 60% and f = 1/(2.5 × 10−3) = 400 Hz.

Changing one interval changes the frequency as well as the ratio. Only altering both paths together keeps the frequency where it was.

A counter responds to any of those settings, since it acts on an edge, provided the pulse stays wide enough and its edges sharp enough to meet the counter's own timing requirements; make a pulse too narrow or too slow-edged and even an edge-triggered input misses it.

For a load switched by a MOSFET the duty cycle is what matters. The duty cycle is the fraction of the time the channel is open, so it fixes the average power the load receives: modelling the lamp as a constant resistance, one fed at 20% duty from a 12 V supply is being given a fifth of the power it would take at 100% (a real filament's resistance shifts with temperature, so the proportion belongs to the model), while the switch itself stays cool because it is either fully on or fully off.

ASSESSMENT FOCUS

  • The D-type in one line: on the clock's rising edge it copies D to Q, and holds Q between edges. The qualitative description is what is required.
  • Counter arithmetic: frequency divides by 2n2^{n}, the number of states is 2n2^{n}. Decide which the question wants before reaching for the calculator.
  • A modulo-n counter is a chain plus a gate that spots n and resets it, so it displays 0 to n − 1. A decade counter is the modulo-10 case and one BCD digit. A Johnson counter of n stages has 2n states, not 2n2^{n}.
  • Astable numbers are three separate quantities. Pulse width is the high time by itself, mark-to-space is high over low, duty cycle is high over the period as a percentage. Write which one you are giving.

CHECK YOURSELF

A 4-stage binary counter is clocked at 96 Hz. Find the frequency at its final output, and state how many distinct states it passes through. The chain is then wired to reset the instant it reaches 1010: state the counter this makes and the range of counts it now shows. The astable supplying the 96 Hz clock is high for 3.0 ms and low for 7.4 ms: give its pulse width, mark-to-space ratio and duty cycle.

Show a hint

Each stage halves the frequency, and a chain of n stages has 2 to the n states. A reset gate firing at ten leaves the counts below it. Keep the astable's three numbers apart.

Show the answer

Four halvings divide by 242^{4} = 16, so the final output runs at 96/16 = 6 Hz.

A free-running 4-stage chain passes through 242^{4} = 16 states, 0000 up to 1111, before rolling over.

Resetting the instant 1010 (ten) appears makes a modulo-10, or decade, counter: it now shows 0000 up to 1001, that is 0 to 9, then returns to zero.

The pulse width is the high time alone, 3.0 ms, and the mark-to-space ratio is 3.0/7.4 = 0.41, about 1:2.5.

The period is 3.0 + 7.4 = 10.4 ms, which matches the 96 Hz clock, so the duty cycle is 3.0/10.4 = 29%.

A D-type flip-flop copies its input to its output on each rising clock edge and holds it between edges, so it stores one bit.

Every toggling stage halves the frequency, so n stages divide by 2 to the n, and a gate that resets the chain early counts modulo n.

An astable's mark-to-space ratio is its high time over its low time; its duty cycle is its high time over the whole period.

10 questions on this topicAnswer them one at a time and mark yourself against the mark scheme.Practise this topic

Or read them with their mark schemes on the combinational logic questions page.

5 flashcards on this topicDefinitions, off-sheet equations and a spot-the-error card, scheduled by spaced repetition in your browser.Revise with flashcards

CHECK YOUR PROGRESS

Rate how confident you feel with each objective for this lesson. Ratings are saved in this browser, on this device, unless you sign in.

  • Describe the D-type flip-flop as a one-bit memory, and use counters that halve the frequency at every stage.
  • Reset a chain early to count modulo n, and tell BCD, up/down and Johnson counters apart.
  • Read the pulse width, frequency, mark-to-space ratio and duty cycle off an astable's output.

Open the full revision checklist to track your progress across the whole unit.