Filipe Chagas Ferraz

Understanding Flip-Flop Circuits

Blog Article Digital Electronics 09-Sep-2018
This is an English translation of an article originally written in Portuguese and published on Medium. Read the original article in Portuguese →
Logic circuit of a 4-bit counter with T flip-flops
Logic circuit of a 4-bit counter with T flip-flops

Flip-flops are, basically, digital memory circuits. Each flip-flop corresponds to a 1-bit memory.
There are several types of flip-flops, each one with its applications. Each type of flip-flop has a set of properties for which we must design a circuit. This means that, for each type of flip-flop, there is a digital circuit different from the circuits of the other types. These various different circuits, however, have similarities.

Here we will analyze 4 types of flip-flops: SR, D, JK and T.

Logic circuit of a JK flip-flop
Logic circuit of a JK flip-flop

Look at the logic circuit of a JK flip-flop in the image above. Confusing, don't you think? At first glance it really seems quite complex, but if we analyze its anatomy part by part, everything will become simpler.

SR (set-reset) latch

Everything starts with a small and simple set-reset latch circuit.

Simulation of a set-reset logic circuit
Simulation of a set-reset logic circuit. (The top button is the set input, the bottom button is the reset input, the top lamp is the Q output and the bottom lamp is the Q’ output)

This small circuit is the basis of all flip-flops. It has 2 inputs (called set and reset) and 2 outputs (called Q and Q’). When the set input (or simply S) goes from logic level 0 to 1, the circuit “memorizes” logic level 1 on the Q output and logic level 0 on the Q’ output. Likewise, when the reset input (or simply R) goes from logic level 0 to 1, the circuit “memorizes” logic level 1 on the Q’ output and logic level 0 on the Q output. This means that, if the outputs of the circuit are Q = 1 and Q’ = 0, they remain in this state until the R input goes from logic level 0 to 1. Likewise, if the outputs of the circuit are Q = 0 and Q’ = 1, they remain in this state until the S input goes from logic level 0 to 1.
While the logic level of both inputs is 0, the state of the outputs Q and Q’ does not change, until one of the two inputs goes to logic level 1.
Note that the outputs Q and Q’ always have complementary logic levels: if Q = 1, then Q’ = 0; if Q = 0, then Q’ = 1.

However, if both inputs are at logic level 1, a logic error will occur that will cause the circuit not to behave according to this rule.

Simulation of a logic error in a set-reset latch circuit
Simulation of a logic error in a set-reset latch circuit. (The button is connected to both inputs. The top lamp is the Q output and the bottom lamp is the Q’ output)

A solution to this problem will be discussed later, but before that let's continue analyzing the set-reset circuits.

SR (set-reset) flip-flop

In the applications of flip-flops, the state changes of the circuit need to happen in sync with the pulses of a clock.

The simplest way to add a clock to a set-reset circuit is to replace the NOT gates of the S and R inputs with two NAND gates. Each of these NAND gates must have two inputs: one for the respective input (S or R) of the circuit, and another for the clock.

Set-reset circuit with clock
Set-reset circuit with clock. (The top button is the set input, the bottom button is the reset input, the top lamp is the Q output and the bottom lamp is the Q’ output)

This circuit has a flaw: the state of the outputs is susceptible to changes while the clock is at logic level 1, and we do not want that. We want a circuit whose outputs are only susceptible to state changes at the exact moment the logic level of the clock goes from 0 to 1 (a moment we will call the “rising edge”). In other words, the outputs Q and Q’ cannot change state at any moment other than a rising edge of the clock.

Behavior of the inputs and outputs of an SR flip-flop
Chart showing the behavior of the inputs and outputs of an SR flip-flop. The Q and Q’ charts in red are the charts of the ideal behavior.

For that, it is necessary to combine two clocked set-reset circuits in a master-slave configuration. The Q and Q’ outputs of the master circuit must be connected, respectively, to the S and R inputs of the slave circuit. The clock must be inverted with a NOT gate at the input of the master circuit, and in the slave circuit, the clock must enter without inversion or with two inversions in a row.

Master-slave set-reset circuit
Master-slave set-reset circuit (inputs and outputs indicated)
Simplified master-slave set-reset circuit
Simplified master-slave set-reset circuit

Note that, now, the outputs of the circuit only change state on the rising edge of the clock (the exact moment the logic level of the clock goes from 0 to 1).

The clock signal that enters the master circuit is the inverse of the clock signal that enters the slave circuit. The master circuit is susceptible to state changes while the logic level of the clock is 0, and the slave circuit is susceptible to state changes while the logic level of the clock is 1.

The master circuit works as a kind of filter in this configuration. The inputs of the slave circuit only receive the signals obtained from the inputs of the master circuit while the clock is at logic level 0, and the state change on the outputs of the slave circuit only happens when the clock is at logic level 1.

Roughly speaking, while the clock is at logic level 0, the input signals of the circuit are memorized in the master circuit. At the moment the logic level of the clock goes from 0 to 1 (rising edge), the outputs of the slave circuit are updated according to the state memorized in the master circuit. After the rising edge, while the clock is at logic level 1, no state change happens in the circuit.

Asynchronous preset and clear inputs

We have already managed to design an SR flip-flop circuit with two synchronous inputs (S and R), but, for the next types of flip-flops, we also need two asynchronous inputs, which we will call preset and clear (or simply PRE and CLR). These two inputs have functions similar to the S and R inputs. The difference is that the PRE and CLR inputs can change the state of the outputs of the flip-flop at any moment, regardless of the state of the clock.

PRE and CLR inputs in an SR flip-flop
Demonstration of how the PRE and CLR inputs work in an SR flip-flop

To add the PRE and CLR inputs to our flip-flop, we will need to add an input to each NAND gate of the circuit. Once this is done, we need to connect the PRE and CLR inputs, each one inverted by a NOT gate, to the NAND gates as shown in the following animations:

Connecting the PRE and CLR inputs to the master circuit
Connecting the PRE and CLR inputs (each one inverted by a NOT gate) to the NAND gates of the master circuit

The way the PRE and CLR inputs are connected to the NAND gates is the same in the master and slave circuits:

Connecting the PRE and CLR inputs to the master and slave circuits
Connecting the PRE and CLR inputs (each one inverted by a NOT gate) to the NAND gates of the master and slave circuits

JK flip-flops

To fix the logic error problem of SR flip-flops when both inputs are at logic level 1, there are JK flip-flops, which are similar to SR ones, with one difference: when both inputs (J and K) are at logic level 1, instead of a logic error occurring, the outputs Q and Q’ have their states inverted.

How a JK flip-flop works
Demonstration of how a JK flip-flop works

This circuit can be designed based on the SR flip-flop circuit we built earlier. Just connect the Q’ output of the circuit to the NAND gate of the S input and the Q output to the NAND gate of the R input. We can disconnect the PRE and CLR inputs from these two NAND gates to make room for the new connections.

Designing a JK flip-flop from an SR flip-flop
Demonstration of the design of a JK flip-flop from the circuit of an SR flip-flop. (Ignore the labels indicating the connections of the CLR and PRE inputs to the NAND gates, I forgot to remove them before taking the screenshot)

T and D flip-flops

The other two types of flip-flops are based on JK flip-flops. We do not even need to modify the circuit we have already built, we just need to add a few things.

T (toggle) flip-flops have clock, T, PRE and CLR inputs, and Q and Q’ outputs. These are, basically, flip-flops in which the state of the outputs is toggled on the rising edge of the clock when the T input is at logic level 1.

To design a T flip-flop, just join the J and K inputs of a JK flip-flop and turn them into a single T input.

Designing a T flip-flop from a JK flip-flop
Design of a T flip-flop from a JK flip-flop

D (data) flip-flops have clock, D, PRE and CLR inputs, and Q and Q’ outputs. These are, basically, flip-flops in which the state of the Q output is set equal to the state of the D input on the rising edges of the clock.

To design a D flip-flop, just connect the D input to the J input of the JK flip-flop and the D input, negated by a NOT gate, to the K input of the JK flip-flop.

Designing a D flip-flop from a JK flip-flop
Design of a D flip-flop from a JK flip-flop