Digital Electronics II: Microcontrollers
crocontrollers, powerful single-chip computers that can be
programmed to perform almost any digital task.
Introduction
Lets suppose you want to build something more complex than the digital circuits we studied in
the previous experiment. For example, you might have a Wheatstone bridge and a null detector
that you want to hook up to a computer so you can automatically balance the bridge and record
the null point, maybe with an adjustable excitation voltage for the bridge. Or, say you want to
build a tiny battery powered data transmitter to feed to a dolphin and find out how his body
temperature varies while hes swimming around in the ocean. Since its hard to transmit signals
through a dolphin, the transmitter should store the temperature data until it is excreted by the
dolphin and floats to the surface where your receiver can detect the signal.
These and many other applications require complex digital circuitry. Any digital system,
including a programmable computer, can be built by combining the gates and flip-flops we
discussed in the previous experiment. But it is hardly practical to build something like a
computer by connecting together a bunch of TTL chips, since you could easily require thousands
or even millions of gates. There are much better ways to get the job done. In fact, if you find
yourself using more than a few discrete logic gates in a circuit you are probably making a
mistake.
As a scientist you should generally try to avoid building anything digital. The first example
above could be handled by a PC with data acquisition cards and a power supply with a computer
interface. For the potentiometer of the bridge you could use a digital potentiometer or a stepper
motor driving an analog pot, both of which can be connected to a computer with commercial
hardware. However, for the second example you would probably have to build everything from
scratch.
There are three general approaches to building complex digital circuits: application specific
integrated circuits (ASICs), programmable logic devices (PLDs), and microcontrollers. ASIC is
a general term for any complex integrated circuit that is devoted to a very specific task.
Examples are chips designed for cell phones, TV sets, or controllers for liquid crystal displays.
Generally, ASICs are designed to serve large commercial markets, and so you wont find
anything that will be much help for the two applications mentioned above, or for most scientific
Experiment #10
10.2
Fall 2005
applications. But if there is an ASIC that does what you need to do, or that can be adapted to
your task, you should certainly use it. If you have a really large project with at least a million
dollars to spend you could make a custom ASIC that does just what you want. ASIC
manufacturers have many ready-to-go design elements (called standard cells), including
complete computer processors, digital filters, counters and timers, memories and so forth, so you
can generally avoid having to do much gate-level design work. In many cases it is possible to
include analog circuitry as well, such as output drivers or ADCs. ASICs (and also
programmable logic) are commonly used in high energy physics experiments. Check this link
www.hep.anl.gov/elec_support/ for several examples.
Assuming there is no ASIC that does what you need and you cant afford a custom ASIC, you
might consider programmable logic devices. These are chips containing many logic gates, and in
most cases also flip-flops, that can be programmed (usually once) to interconnect the gates and
flip-flops in a way that performs a custom function (see H&H 8.15, 8.27). A single PLD can
replace a large pile of TTL chips, and the smaller PLDs containing a few thousand gates are
inexpensive and relatively easy to program. The largest PLDs can contain 300,000 gates or
more, and the programming effort can become a major project.
By far the most common approach to complex digital design is to use a microcontroller, a single
chip programmable computer. Compared to programmable logic, microcontrollers process data
more slowly but with much greater flexibility and power, and they are generally cheaper and
more power efficient. They are available in incredible variety, from 8-pin versions costing less
than a dollar to devices with processing power rivaling that of a PC. A given device may offer
only digital input and outputs pins (I/O pins), or may include analog comparators or even ADCs.
Modern versions usually contain their own program and data memory, and some have
programmable non-volatile memory so they can remember what they were doing before the
power went out. There are many specialized varieties of microcontroller, including DSP chips
(digital signal processor) that can implement fast digital filters and Fourier transforms, and
devices that include high-level digital interfaces, such as Ethernet or USB. To get an idea of the
variety available, have a look at some of the manufacturers web sites (Microchip at
www.microchip.com; Motorola at www.freescale.com; Rabbit Semi at
www.rabbitsemiconductor.com).
In this experiment, we will take a look at the 14-pin Microchip PIC16F676. It is a member of
the Microchip PIC x14 processor family, with 1024 words of 14-bit wide program memory and
64 bytes of 8 bit data memory. The data and program memories are completely separate, making
this a Harvard architecture processor. Program memory uses the latest flash technology, so
Experiment #10
10.3
Fall 2005
you can program it over and over again as you debug your hardware and code, and it is non-
volatile, so the chip wont forget its programming when you turn the power off. Programming
can be done without taking the chip out of your circuit via a serial digital interface that
Microchip calls ICSP (In-Circuit Serial Programming). Besides program and data memory, there
are 128 bytes of non-volatile data memory that can be programmed by the processor to
remember the state of your system when power is off. Nearly every pin of the chip can be
programmed in multiple ways for various uses, and up to 12 of the 14 pins can be used as bi-
directional digital I/O. There is an internal variable frequency clock and two separate
timer/counter/scalers. For dealing with analog signals there is a comparator with a
programmable voltage reference, and to top it all off, a 10-bit analog-to-digital converter with an
8-channel analog multiplexer, so you can digitize 8 separate analog signals. Amazingly, you can
have all this for just over one dollar per chip, about the same cost as the discrete 2N4416A JFET
we used in Experiment 8.
Heres the bad news: All this power, typical of modern microcontrollers, comes at the price of
complexity. Have a look at the data sheet for this chip (on our web site or at
www.microchip.com). It is 129 pages long, and many aspects of the chip are only summarized
there. To get the full story you have to read the PICmicro Mid-Range MCU Family Reference
Manual, which runs to 688 pages! Despite this, we will show you that its not too hard to get to
know the PIC, thanks to some excellent tutorial info available from Microchip, and the excellent
(and free) programming software they provide. These resources, and much else that you can find
on the web, are the main reason we have chosen the PIC for this course.
A FEW WORDS ABOUT OBSELESENCE IN ELECTRONICS
Although we tend to think of electronics as a rapidly developing area, almost all of the analog
parts used in circuits today have been around for a long time. The 2N3904 and 2N4416
transistors date from the 1960s, and the LF356 op-amp dates from 1975. Since the second
edition of Horowitz and Hill (1989) the main change in analog electronics has been that
packages have gotten smaller. The 0.1-inch pin spacing of DIP packages (which is great for
learning and prototyping) has been largely replaced by 0.05-inch pitch and smaller surface mount
packages. (When mounted on a printed circuit board, surface mount packages dont require you
to drill holes through the board for the leads to go through. If you get stuck prototyping with
these smaller packages, try not to go below 0.05-inch pitch, unless you want to work under a
microscope). If a new edition of H&H were to come out today, Chapters 1-7 would hardly have
to be changed, except for the various tables containing real part numbers, such as Table 4.1 on
op-amps. Many of those part numbers have changed as manufactures come and go and
Experiment #10
10.4
Fall 2005
improvements are made, but most of the kinds of parts listed are still available. In any case,
tables like this are unnecessary today, since you can easily visit the manufacturer web sites to see
whats currently available.
In digital electronics change has been more rapid, as anyone who buys PCs knows. Not only
have packages become smaller, but much more functionality is now included within a single
package, and maximum clock speeds have continuously increased. There is nothing obsolete
about the concepts described in the digital chapters of H&H, but today no one would build a
microprocessor system like the one shown in Fig. 11.10, if you could even get the parts to do it.
All of the functionality shown in Fig. 11.10 and more is now available on a single chip.
If you start looking for parts on manufacturers web sites and then try to buy what you find, you
will discover that not all of those parts are really