Professional Documents
Culture Documents
15-1
Chapter 15
BASIC DIGITAL CONCEPTS and LOGIC GATES
What are digital signals?
So far, we have generally dealt with current or voltage signals which have
a continuous range of possible values. For example, a voltage might be
2.1392 volts, or 2.1393 volts, or in fact any value in between. Most
quantities which are not actually counted (i.e. they have non-integer
values) are like this, and are referred to as analog1 signals. As you have
seen, analog signals can be degraded, for example, by the addition of noise,
so that the full amount of information originally contained in them cannot
be recovered.
Digital signals, on the other hand, can have only a certain number of
possible values, and for the basic logic circuits that we will be discussing
shortly, there are only two possible values. In reality, a digital signal must
still be represented by an analog voltage, which of course may be
corrupted by noise and so on. Having two possible values then might work
something like this, allowing for noise: lets suppose the two possible
values for a voltage are zero and 1 volt. Then, if the voltage is less than 0.5
volts, assume its actual value is zero volts; if it is greater than 0.5 volts,
assume its actually 1 volt.
You might see that this could have some advantages. Some are:
CHAPTER 15
15-2
..and so on. It turns out that this is not all that restrictive, because we
can represent any amount of information by combining a number of such
signals. The common game of 20 questions is an example of this. Here,
one of a (fairly large) number of possibilities is identified by successive
yes/no answers. With each question, the number of possibilities
potentially halves. Any sequence of answers can be written down as a
string of symbols, each of which is yes/no, or, if you like, 0/1 (the latter
takes less paper..).
Each one of these smallest units of information is called a bit (for binary
digit), and we can specify the information content of a document, message,
or whatever by specifying the number of bits needed to represent it. Very
often the information is inherently numerical, and the 0/1 interpretation
of our two signal states is the most appropriate.
We are used to seeing numbers in decimal form; that is, base ten, where
the digits 0 to 9 are used. As you are probably aware, we can also
represent numbers in binary form, using only the digits 0 and 1. The
decimal integers 0 to 10 are represented in binary form as follows:
Decimal
0
1
2
3
4
5
6
7
8
9
10
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
CHAPTER 15
15-3
Lamp
Lamp
Figure 15.1 Circuits using switches to implement the AND and OR logic
functions.
This circuit has two inputs the positions of the two switches (either ON
or OFF), and one output whether the lamp is ON or OFF. Since the
switches and the lamp are all in series, the lamp will light only if both
switches are ON. Thus the lamp is ON only if switch 1 is ON and switch 2
is ON, but at no other time. We would say that this circuit performs an
AND function.
CHAPTER 15
15-4
In the right hand circuit, the situation is different. Here the lamp is ON if
switch 1 is ON or switch 2 is ON (and this includes the case when both
switches are ON). We say that this circuit thus performs an OR function.
Now it wouldnt be much use if we had to rely on flipping switches to get
results for calculations involving large numbers of binary signals. What
we need is a circuit which takes binary signals say, as voltages, and
generates outputs which are also voltages. One relatively primitive way of
doing this is with relays, which can be thought of as switches activated by
a voltage. Most cars have quite a few relays to switch relatively heavy
currents for starter motors, headlights, rear window demisters and the
like. In a relay, the voltage across a coil of wire wound around an iron rod
causes a current to flow and a magnetic field to be generated (note that
the coil is also an inductor, but its not the inductance thats of interest to
us here). The relatively strong magnetic field pulls a switch contact closed,
and this can be used to control a large current. Here is a relay version of
an AND circuit, with voltage inputs and output:
Switch contact closed
when relay is on
12V
Input A
Input B
Output
0 or +12V
Relay coils
Figure 15.2
The advantage of this circuit is that the output voltage can now be used as
an input to other circuits, so we could build quite complicated logic
circuits. For example, we might implement something like this (assuming
we have appropriate input signals from sensors):
If reactor getting too hot AND ((NOT backup safety circuit active) OR
Homer Simpson is at control panel) then sound immediate evacuation
alarm.
Of course, relays arent really a practical proposition for complicated, highspeed or compact systems. Fortunately, we can use transistors, which can
CHAPTER 15
15-5
CHAPTER 15
15-6
So far, we have met the logical AND and OR operations in our simple
circuit examples. We can describe what a gate does in terms of a truth
table, which displays the output state for all possible combinations of
input states. Although truth tables tend to use 0 or 1 to represent states,
its sometimes easier to think in terms of TRUE (=1) and FALSE (=0).
Thus for a two-input AND gate, we might say
(the output is TRUE) only if (input A is TRUE) AND (input B is TRUE)
or, that is:
(the output is 1) only if (input A is 1) AND (input B is 1)
Here are the truth tables for AND and OR gates, together with the
symbols commonly used for them:
OR
AND
A
B
A
0
0
1
1
A
B
A B
B
0
1
0
1
AB
0
0
0
1
A
0
0
1
1
A+B
B
0
1
0
1
A+B
0
1
1
1
Figure 15.3 Logic symbols for 2-input AND and OR gates and truth tables
for these functions.
There are some other types of gates, the most simple being a circuit which
performs the NOT logical function, and this gate is called an inverter. This
gate has only one input and one output, and changes a 0 at the input to a
1 at the output, and vice versa. An inverter is also said to complement or
negate the input signal that is, the complement of 0 is 1, and vice versa.
The logic symbol and truth table for the inverter are shown at the left of
the diagram below:
CHAPTER 15
XOR
NOT
A
B
A
0
1
15-7
1
0
A
0
0
1
1
AB
B
0
1
0
1
AB
0
1
1
0
Figure 15.4 Logic symbols for the inverter (NOT gate) and XOR gate and
truth tables for these functions.
The right-hand side of this diagram shows another common function the
exclusive-OR, or XOR function. This is the same as an OR function, except
when both inputs are high, and in this case the output is low. Another way
of looking at the 2-input XOR function is that it gives a high output if the
two inputs are different.
The AND and OR functions may be combined with an inverter (the NOT
function) to give NAND and NOR functions. These are the same as AND
and OR except that the outputs are inverted. Real NAND and NOR gates
are more common than AND and OR because they can be made more
simply (and can operate a little faster). The symbols and truth tables are
shown below. Notice the little bubbles on the gate outputs which indicate
negation (inversion).
CHAPTER 15
NOR
NAND
A
B
A
0
0
1
1
A
B
A B
B
0
1
0
1
15-8
A
0
0
1
1
A B
1
1
1
0
A+B
B
0
1
0
1
A + B
1
0
0
0
Figure 15.5 Logic symbols for 2-input NAND (NOT AND) and NOR
(NOT OR) gates and truth tables for these functions.
Boolean algebra
Real-world applications, particularly those involving some sort of
computation, often involve many interconnected logic gates. Many of you
will do the unit ELEC274 (Logic Design), which looks at designing digital
systems. When we get beyond a system with just a couple of gates, it
becomes difficult to design using just intuition.
One of the tools that digital designers use is boolean algebra (named after
the nineteenth-century English mathematician, George Boole). Its a way
of representing and manipulating logic signals and functions, and enables
us, for example, to select the most economical combination of gates to
carry out some function. The various logic functions correspond to
operators (analogous to addition, multiplication etc.) which act on logic
signals. They are shown in the following table, where A and B represent
two logic signals (or variables, just like normal algebra), each of which
may have the value 0 or 1:
Operation
NOT
Symbol
(bar)
AND
OR
XOR
(dot)
+ (plus sign)
Example
A
AB
A+B
AB
Meaning
NOT A (complement,
inversion or negation)
A AND B
A OR B
A XOR B
CHAPTER 15
15-9
Although the symbols for AND and OR might look like multiplication and
addition (and they do have similarities) they operate differently. The
result of any one of these operations is always just 0 or 1 (that is, one bits
worth, just like any input).
Using this notation lets us use some rules for manipulating expressions.
For example, we find that
(A B) + (A C) = A (B + C),
so that the following two logic circuits are equivalent, although the second
uses one less gate:
B
(A B) + (A C)
A
C
B
C
A (B + C)
A
Figure 15.6 Two logic circuits which implement the same function.
Notice that this is like a similar rule in normal algebra, and there are
other rules which are also similar. Although we wont take the idea of
boolean algebra any further than this, it at least gives you a taste of the
possibilities.
CHAPTER 15
15-10
B
0
1
0
1
A+B
0
1
1
1
C = A + B
1
0
0
0
0
0
1
1
0
1
0
1
A
1
1
0
0
A B
0
1
0
0
C = A B
1
0
1
1
CHAPTER 15
15-11
A B
B
Figure 15.7 Using logic gates to implement the function C = A B .
Number A
0
0
Output is 1 only if
numbers are equal
0
0
Number B
CHAPTER 15
15-12
For the two binary numbers being compared in the diagram above (1010
and 1011), the first three bits are identical, so that the outputs of the first
three XOR gates are 0. The last bit is different, but it only takes one input
of the NOR gate to be 1 to make its output zero.
The circuit can easily be expanded to handle larger numbers of bits, and
might serve as a useful building block in a larger digital system, where,
for example, a notification might be required when a counter reaches a
certain value.
CHAPTER 16
16-1
Chapter 16
FLIP FLOPS, REGISTERS and COUNTERS
B
Figure 16.1 Cross-coupled inverters form a basic flip-flop.
What does it do? Do the signals present have reasonable values? Well, lets
suppose for a start that outputs A and B can only have values 0 or 1.
Then, lets assume that A = 1. Is this OK? The output A goes into the
lower inverter and is complemented, so that B must be 0. Output B is fed
to the upper inverter, producing an output of 1 at A. This is the same as
our original assumption, and tells us that everything is consistent; the
circuit can have this state. Furthermore, there is no reason for the state to
change. It is stable.
What if we had chosen A = 0 instead? Following the same chain of
reasoning, we could argue that this is also a stable state (but obviously
the only other one). Thus the circuit has two stable states; it is referred to
as a bistable circuit, or a flip-flop. Note also that B = A.
Another way of looking at it is to say that the circuit has a memory. If it
is somehow forced into one state, it will stay there. So, in principle, we
have a simple way of saving binary information (at least as long as the
power is not turned off!). The only problem we have is that its not clear
how to make the flip-flop flip or flop on demand.
CHAPTER 16
16-2
A
0 volts
(logical 0)
A
Figure 16.2 Adding a switch enables the flip-flop to be forced into
one state or the other.
In practice we might not simply connect outputs to 0 volts like this, as the
gates might be damaged; we would include resistors in appropriate places.
A version of this technique is often used to de-bounce switches, such as
pushbuttons on a panel connected to logic circuits. It is normal for switch
contacts to chatter as they close, causing multiple changes of signal
level. The de-bouncing scheme makes sure that only one change occurs, at
the time the switch first makes contact.
CHAPTER 16
R Q
16-3
Figure 16.3 A flip-flop formed from two NOR gates makes an R-S
flip-flop which may be set and reset via the two inputs R and S.
Heres how it works:
If R = S = 0 then the two NOR gates just function as inverters for the
signals fed between the two gates. (To see this, look at the truth table
for a NOR gate with one input set to 0. The output is then just always
the complement of the other input.) Thus the circuit behaves exactly
as the simple pair of inverters that is, it just stays in whichever state
it finds itself, and acts as a one-bit memory.
If R = 1 and S = 0 (that is, we make the reset input high), then output
Q is forced to 0. (To see why, look at the NOR truth table again.
Making any input 1 forces the output to 0). Q (= Q) is thus forced to
1, and the flip-flop is reset. It doesnt matter how many times this is
then repeated; the circuit will still stay reset.
Note: Q is taken to be the normal output of the flip-flop ( Q is
always just its complement). Making Q = 1 is said to be setting
the flip-flop, while making Q = 0 is resetting it. Set and reset are
often also called preset and clear.
If R = 0 and S = 1 (that is, we make the set input high), then output Q
is forced to 0 (its just the reverse of the previous case the circuit is
symmetric). Q is thus forced to 1, and the flip-flop is set.
If R = 1 and S = 1 at the same time, we have a problem. Why? Because
the outputs of both gates will be forced low. Then, when R and S are
both returned to 0, which state will the circuit choose? (Note that Q =
Q = 0 is not a stable state if R = S = 0). The answer is we dont
know! It all depends on whether R or S went back to 0 first. For this
reason, the combination R = S = 1 is not allowed.
CHAPTER 16
16-4
R Q
Figure 16.4 Another R-S flip-flop, this time using NAND gates.
We wont work through the logic of it, but the R-S input signals used here
have to be the complement of those used in the previous (NOR) case.
That is, the set and reset signals (labelled S and R ) are normally 1 in
remember mode, and are taken to 0 to set or reset the circuit. Here the
state S = R = 0 is not allowed.
Note that these inverted sense input signals are denoted by bars over
the S and R (hence becoming S and R ) and by the little bubbles at the S
and R inputs on the flip-flop symbol.
CHAPTER 16
CLK
K
Q n +1
Qn
Clock
16-5
1
____
n
Figure 16.5 The J-K flip-flop and its truth table. (Note that the R
and S inputs are not provided on all J-K flip-flops.)
Heres how the J-K flip-flop works:
A change can only occur when the clock rises (that is, changes from 0
to 1). Nothing can happen at any other time (for example, when the
clock falls). The flip-flop is said to be rising-edge-triggered. (It is also
possible to get J-K flip-flops which are falling-edge-triggered.)
What happens depends on the values of J and K at the instant the
clock rises.
The third column of the truth table is just to remind you that the
values of J and K are only important at the moment the clock rises.
The fourth column indicates the state which Q goes to after the clock
rises, for a particular set of values of J and K. The notation looks a bit
strange, but heres what it means: Qn+1 means the state after the clock
rises, while Qn means the state before. So the four entries in column 4
for Qn+1 mean:
Qn:
0:
1:
Qn :
CHAPTER 16
16-6
as toggling. Note that this behaviour did not occur with the
R-S flip-flop.
The J-K truth table is actually not too difficult to remember. The rules
are:
(1) Things only happen when the clock rises.
(2) If J = K = 0 then nothing happens.
(3) If J = K = 1 then the flip-flop toggles
(4) If J K then Q goes to the value that J has.
The J-K flip-flop has some advantages over the R-S type. First, because
changes are controlled by the clock, the operation of many circuits can be
synchronised. Second, the behaviour is defined for all values of J and K
(remember that R = S = 1 was not allowed with the R-S flip-flop). Third,
the toggling behaviour when J = K = 1 allows us to design circuits which
do some neat things.
There are many useful circuits we can build by connecting J-K flip-flops
together. Two of the most important are shift registers and counters. Lets
look at some of these.
Shift registers
In digital parlance, a register is simply a fancy name for a memory circuit
to hold one chunk of information consisting of a number of bits. A shift
register has the ability to shift all the bits of the binary number
contained in it one place to the left (or right, or either, depending on its
design), for each tick of a clock.
Its exactly like having a full row of seats at the cinema, each seating a girl
(1) or boy (0). Some newcomer wants to sit at the end, so everybody shifts
along by one at the same time to accommodate them. Of course, somebody
also gets dropped off the other end, since there are only a fixed number of
seats. So the situation might look like this:
Before shift:
After shift:
1 1010101100110100
(new girl)
1101010110011010 0
(boy lost off end)
The figure below shows a single stage of a shift register using a J-K flipflop. J is set to the new value to be shifted in, while K is set to its
complement (this is easy to arrange with an inverter if necessary). When
the clock rises, the J and K values are transferred to Q and Q respectively
CHAPTER 16
16-7
(refer back to the J-K truth table to see why). The old values of Q and Q
are lost from this flip-flop, but are shifted to the following stage at the
same time.
A
CLK
K
J1
Q1
CLK
A
K1
J2
Q2
CLK
Q1
K2
J3
Q3
to next
stages
CLK
Q2
K3
Q3
Clock
CHAPTER 16
Data
16-8
Clock
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
Figure 16.8
Timing diagram for serial-to-parallel conversion
using an 8-bit shift register. Q1 to Q8 are all initially 0, but after
8 clock cycles are set to the values of the incoming serial data.
At this point all the bits of the signal are available simultaneously at the
flip-flop outputs that is, in parallel, rather than one at a time. Since the
serial data stream is converted into parallel form, the shift register thus
acts as a serial-to-parallel converter. (In practice, a little more circuitry
would be required to inform the eventual receiver of the data that a new
output was available after every 8 bits.)
This might be useful, for example, if you wanted to test whether
consecutive bits of the serial data stream were equal to some particular
pattern (you would also need a little extra circuitry), or if they had to be
sent to another device which required them all at the same time. For
example, printers used with personal computers used to accept their data
this way, one byte (8 bits) at a time, via the computers parallel port.
A shift register can also be used to perform parallel-to-serial conversion,
as illustrated in the next timing diagram. Here the idea is that the shift
register is loaded with the new data, 8 bits at a time. These 8 bits are
then shifted to the right one bit at a time, appearing at Q8. After 8 clock
cycles, the next 8 bits are loaded, and so on.
CHAPTER 16
16-9
Clock
Q1
Q2
Q3
Q4
Q5
Q6
Q7
Q8
CHAPTER 16
Decimal
0
1
2
3
4
5
6
7
Binary
0000
0001
0010
0011
0100
0101
0110
0111
Decimal
8
9
10
11
12
13
14
15
16-10
Binary
1000
1001
1010
1011
1100
1101
1110
1111
1
Clock
Clock
CLK
1
CHAPTER 16
16-11
flip-flop. The output of the second flip-flop will now change at one-quarter
the rate of the clock.
J1
Q1
J2
CLK
K1
Q2
CLK
Q1
K2
Q2
Clock
Clock
Q1
Q2
01
10
11
00
01
10
Q2Q1 00
Number
CHAPTER 16
16-12
whole circuit has only 4 different possible states. It certainly cant take
longer than 4 clock cycles to return!
Taken together as a binary number, the pair {Q2Q1} cycles through the
values 00, 01, 10, 11, (decimal 0, 1, 2, 3,). That is, we have
constructed a 2-bit binary counter.
Initial state
After 1 clock rise
After 2 clock rises
After 3 clock rises
After 4 clock rises
After 5 clock rises
After 6 clock rises
and so on.
J1
K1
Q1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
0
1
0
1
0
CLK2
(= Q1 )
1
0
1
0
1
0
1
CLK2
rose?
Yes
Yes
Yes
J2
K2
Q2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
1
1
0
0
1
We start by filling in the values of Q1 and Q2 for the initial state (shown
in bold). We then work our way down the table row by row, filling in the
values of Q1 and Q2 in the current row using the values of Q, J and K for
each flip-flop from the previous row, together with our extra column which
shows whether the clock for flip-flop 2 has risen in the current row
compared to the previous row. Notice that we have to work out Q1 before
Q2 in each row, since the output from flip-flop 1 forms the clock for flipflop 2.
The 2-bit counter circuit can be easily extended by just stringing more
stages together, as shown in the 4-bit example below. In this case, the
outputs Q1, Q2, Q3... have been labelled A, B, C; this shorter notation is
sometimes used for labelling stages in shift registers or counters.
CHAPTER 16
16-13
Outputs
1
J1
CLK
1
Clock
K1
J2
CLK
A
K2
J1
CLK
B
K1
J2
CLK
C
K2
A few final points are worth mentioning about binary counters like these:
Be careful about the order of the bits (that is, left to right or vice
versa). The least-significant bit (LSB) of a binary number is the one
that changes most often when counting, and this will correspond to the
left-hand flip-flop in the diagram above, not the right-hand one, since
the clock input is at the left.
If too many stages are strung together, this type of counter does have a
problem, which may be quite serious for some applications. This is
because the clock is only connected to one stage (the left-hand flip-flop
in the examples above).
The clock signal to the second flip-flop (that is, from the output of the
first flip-flop) has a slight additional delay, since it takes a small but
finite amount of time for the first flip-flop to change state. When
added together in a large counter, these delays mean that it may take
quite a while for a new state to ripple through and settle down.
Often, a much better idea is to design a synchronous (rather than our
asynchronous) counter, where the same clock is connected to all
stages, which then change state together. Although slightly more
complicated, this is the way that most counters are designed. We will
briefly revisit this idea later.
CHAPTER 16
16-14
Example 16.2: Draw the timing diagram for the 4-bit binary counter
shown in the diagram above. How many different states does it have?
What happens if it starts in a state where A, B, C and D are not all 0?
Answer: The timing diagram looks like this:
Clock
Q1
Q2
Q3
Q4
To aid in lining up the waveforms, the vertical dotted lines show the
times when the (main) clock rises. Notice that since the clock input for the
second flip-flop is taken from the A (= Q) output of the first flip-flop, this
will rise when A ( = Q) falls, and so on for the other flip-flops. So each flipflop changes state (toggles) when the output of the previous one to the left
falls. These transitions are indicated on the timing diagram with falling
arrows.
By looking at all the possible combinations of Q1 to Q4, we can see that
there are 16 different possible states for this simple type of counter, which
basically counts through all possible states. As we might expect, the
number of states is simply 2N, where N is the number of bits (or flip-flops)
in the counter.
To see what happens if the counter starts in a state where all of Q1
through Q4 are not zero, observe that a state depends only on the
previous state. So the counter cycles through the same states, only with
a different starting point, and after 2N clock cycles is back where it
started.