EXCELLENT CIRCUITS WITH EXCEL
Using Spreadsheets for Digital Circuits
BY PETER STARK
NUTS & VOLTS
Nuts & Volts Magazine recently had two articles
about linear-feedback shift registers, which are shift
registers with some of their outputs fed back to the
input for generating cyclic redundancy check (CRC)
bits or random numbers. Short of actually building
these (and other digital) circuits, it’s sometimes hard to
check that they do what they claim to do. Here is an
interesting and simple technique for simulating these
circuits on a computer using a spreadsheet program.
It doesn’t have to be an expensive or powerful
spreadsheet program. For the examples below, I will use
Excel, but sometimes old versions of Lotus 123 or
shareware programs like As-Easy-As work even better.
Figure 1 shows the basic idea. Looking at an inverter
— such as a 7404 — in the TTL manual, you’ll often see the
inputs and outputs labelled as In and Out, or perhaps A
and B. But simply label them A1 and B1, and right away,
you get the idea: Just use cell A1 in the spreadsheet for
the input and cell B1 for the output. There’s nothing
magic about these two cells, as you could use any two
cells in the spreadsheet. Now all you have to do is put the
right formula into cell B1, so it’s always
the opposite of A1, and you’re done.
FIGURE 1. A simple inverter.
For the inverter in Figure 1, if the input is a 0, then
the output is a 1, and if the input is 1, then the output is
0 (you have to use 0 and 1, because it won’t work if you
try to use H and L or True and False instead). There are
two ways to do this. The simplest is to write this as the
Output = 1 - Input
In other words, in the spreadsheet, cell B1 should be
equal to one minus whatever is in cell A1: 1 minus 0 is
1, whereas 1 minus 1 is 0. The formula then becomes:
B1 = 1 - A1
In Excel, you’d type =1-A1 into cell B1 (with the
equal sign); whereas, in most other spreadsheets, it
would be just 1-A1. Make sure to check the exact format
for whichever spreadsheet you are using. Another way
is to use the spreadsheet’s IF function, like this:
B1 = IF(A1 = 0, 1, 0)
The parentheses contain three arguments: a test,
what to do if the test is correct, and what to do if it isn’t.
In this case, it says, “if A1=0, then make this cell equal
to 1, otherwise make it 0.” For obvious reasons, I prefer
to use the “one
Figure 2 shows
some other simple
gates, but let’s do
them one at a time.
FIGURE 2. Simple digital gates.
The AND Gate
For the AND gate at the top left
corner of Figure 2, the truth table
(which describes how it works) is
shown in Table 1, from which we can
see that the output is just the
product of the two inputs — a simple
multiplication. So, the formula to put
into cell C1 is A1 x B1.
Some spreadsheets actually have functions for doing
simple logical operations. Excel insists on using words
like TRUE and FALSE, so that is not very useful to us, but
123 and others use ones and zeroes. The corresponding
syntax for this formula in 123 and some others would be
+A1#AND#B1. Don’t bother — A1 x B1 is much simpler.
The NAND Gate
In the NAND gate at the top right corner of Figure
2, the output is similar to that of the AND, but inverted.
C1 = 1 - (A1 x B1)
should just about do it, since the “1-” part does the
inversion just as in the inverter.
The OR Gate
The truth table for the OR is
shown in Table 2. The easiest way to
implement this is with an IF, like this:
C1 = IF(A1 + B1 = 0, 0, 1)
which says that if the two inputs add
up to zero (which means they are both zero), make the
output a 0, otherwise make it a 1.
The NOR Gate
The NOR is just the opposite of an OR gate (Table