View
233
Download
0
Category
Preview:
Citation preview
7/28/2019 Microcontrollers Work
1/17
Microcontrollers are hidden inside a
urprising number of products these days. If
our microwave oven has
nLEDorLCDscreen and a keypad, it
ontains a microcontroller. All modern
utomobiles contain at least one
microcontroller, and can have as many as
x or seven: Theengineis controlled by a
microcontroller, as are theanti-lock brakes,
hecruise controland so on. Any device that
as a remote control almost certainly
ontains a microcontroller:TVs,VCRsand
igh-end stereo systems all fall into this
ategory. NiceSLRanddigital cameras,cell
hones,camcorders,answering
machines,laser printers,telephones(the
nes with caller ID, 20-number memory,
tc.), pagers, and feature-
adenrefrigerators,
ishwashers,washersanddryers(the ones
with displays and keypads)... You get the
dea. Basically, any product or device that
nteracts with its user has a microcontroller
uried inside.
n this article, we will look at microcontrollers
o that you can understand what they are
nd how they work. Then we will go onetep further and discuss how you can start
working with microcontrollers yourself -- we
will create a digital clock with a
microcontroller! We will also build a digital
hermometer. In the process, you will learn
n awful lot about how microcontrollers are
sed in commercial products.
What is a Microcontroller?
A microcontroller is a computer. All
computers -- whether we are talking abo
personaldesktop computeror a
largemainframe computeror a
microcontroller -- have several things in
common:
All computers have aCPU(central
processing unit) that executes programs
you are sitting at a desktop computer rig
now reading this article, the CPU in that
machine is executing a program that
implements the Web browser that is
displaying this page.
The CPU loads the program from
somewhere. On your desktop machine,
browser program is loaded from thehard
disk.
The computer has someRAM(random-access memory) where it can store
"variables."
And the computer has some input and
output devices so it can talk to people. O
your desktop machine,
thekeyboardandmouseare input devic
and themonitorandprinterare outputdevices. A hard disk is an I/O device -- it
handles both input and output.
The desktop computer you are using is a
"general purpose computer" that can run
any of thousands of programs.
Microcontrollers are "special purpose
http://electronics.howstuffworks.com/led.htmhttp://electronics.howstuffworks.com/led.htmhttp://electronics.howstuffworks.com/led.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://auto.howstuffworks.com/engine.htmhttp://auto.howstuffworks.com/engine.htmhttp://auto.howstuffworks.com/engine.htmhttp://auto.howstuffworks.com/auto-parts/brakes/brake-types/anti-lock-brake.htmhttp://auto.howstuffworks.com/auto-parts/brakes/brake-types/anti-lock-brake.htmhttp://auto.howstuffworks.com/auto-parts/brakes/brake-types/anti-lock-brake.htmhttp://auto.howstuffworks.com/cruise-control.htmhttp://auto.howstuffworks.com/cruise-control.htmhttp://auto.howstuffworks.com/cruise-control.htmhttp://electronics.howstuffworks.com/tv.htmhttp://electronics.howstuffworks.com/tv.htmhttp://electronics.howstuffworks.com/tv.htmhttp://electronics.howstuffworks.com/vcr.htmhttp://electronics.howstuffworks.com/vcr.htmhttp://electronics.howstuffworks.com/vcr.htmhttp://electronics.howstuffworks.com/camera.htmhttp://electronics.howstuffworks.com/camera.htmhttp://electronics.howstuffworks.com/camera.htmhttp://electronics.howstuffworks.com/cameras-photography/digital/digital-camera.htmhttp://electronics.howstuffworks.com/cameras-photography/digital/digital-camera.htmhttp://electronics.howstuffworks.com/cell-phone.htmhttp://electronics.howstuffworks.com/cell-phone.htmhttp://electronics.howstuffworks.com/cell-phone.htmhttp://electronics.howstuffworks.com/cell-phone.htmhttp://electronics.howstuffworks.com/camcorder.htmhttp://electronics.howstuffworks.com/camcorder.htmhttp://electronics.howstuffworks.com/camcorder.htmhttp://electronics.howstuffworks.com/question21.htmhttp://electronics.howstuffworks.com/question21.htmhttp://electronics.howstuffworks.com/question21.htmhttp://electronics.howstuffworks.com/question21.htmhttp://computer.howstuffworks.com/laser-printer.htmhttp://computer.howstuffworks.com/laser-printer.htmhttp://computer.howstuffworks.com/laser-printer.htmhttp://electronics.howstuffworks.com/telephone.htmhttp://electronics.howstuffworks.com/telephone.htmhttp://electronics.howstuffworks.com/telephone.htmhttp://home.howstuffworks.com/refrigerator.htmhttp://home.howstuffworks.com/refrigerator.htmhttp://home.howstuffworks.com/refrigerator.htmhttp://home.howstuffworks.com/washer.htmhttp://home.howstuffworks.com/washer.htmhttp://home.howstuffworks.com/dryer.htmhttp://home.howstuffworks.com/dryer.htmhttp://home.howstuffworks.com/dryer.htmhttp://computer.howstuffworks.com/pc.htmhttp://computer.howstuffworks.com/pc.htmhttp://computer.howstuffworks.com/10-types-of-computers.htmhttp://computer.howstuffworks.com/10-types-of-computers.htmhttp://computer.howstuffworks.com/10-types-of-computers.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://computer.howstuffworks.com/hard-disk.htmhttp://computer.howstuffworks.com/hard-disk.htmhttp://computer.howstuffworks.com/hard-disk.htmhttp://computer.howstuffworks.com/hard-disk.htmhttp://computer.howstuffworks.com/ram.htmhttp://computer.howstuffworks.com/ram.htmhttp://computer.howstuffworks.com/ram.htmhttp://computer.howstuffworks.com/keyboard.htmhttp://computer.howstuffworks.com/keyboard.htmhttp://computer.howstuffworks.com/keyboard.htmhttp://computer.howstuffworks.com/mouse.htmhttp://computer.howstuffworks.com/mouse.htmhttp://computer.howstuffworks.com/mouse.htmhttp://computer.howstuffworks.com/monitor.htmhttp://computer.howstuffworks.com/monitor.htmhttp://computer.howstuffworks.com/monitor.htmhttp://computer.howstuffworks.com/inkjet-printer.htmhttp://computer.howstuffworks.com/inkjet-printer.htmhttp://computer.howstuffworks.com/inkjet-printer.htmhttp://computer.howstuffworks.com/inkjet-printer.htmhttp://computer.howstuffworks.com/monitor.htmhttp://computer.howstuffworks.com/mouse.htmhttp://computer.howstuffworks.com/keyboard.htmhttp://computer.howstuffworks.com/ram.htmhttp://computer.howstuffworks.com/hard-disk.htmhttp://computer.howstuffworks.com/hard-disk.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://computer.howstuffworks.com/10-types-of-computers.htmhttp://computer.howstuffworks.com/pc.htmhttp://home.howstuffworks.com/dryer.htmhttp://home.howstuffworks.com/washer.htmhttp://home.howstuffworks.com/refrigerator.htmhttp://electronics.howstuffworks.com/telephone.htmhttp://computer.howstuffworks.com/laser-printer.htmhttp://electronics.howstuffworks.com/question21.htmhttp://electronics.howstuffworks.com/question21.htmhttp://electronics.howstuffworks.com/camcorder.htmhttp://electronics.howstuffworks.com/cell-phone.htmhttp://electronics.howstuffworks.com/cell-phone.htmhttp://electronics.howstuffworks.com/cameras-photography/digital/digital-camera.htmhttp://electronics.howstuffworks.com/camera.htmhttp://electronics.howstuffworks.com/vcr.htmhttp://electronics.howstuffworks.com/tv.htmhttp://auto.howstuffworks.com/cruise-control.htmhttp://auto.howstuffworks.com/auto-parts/brakes/brake-types/anti-lock-brake.htmhttp://auto.howstuffworks.com/engine.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/led.htm7/28/2019 Microcontrollers Work
2/17
omputers." Microcontrollers do one thing
well. There are a number of other common
haracteristics that define microcontrollers.
a computer matches a majority of these
haracteristics, then you can call it a
microcontroller":
Microcontrollers are "embedded" inside
ome other device (often a consumer
roduct) so that they can control the
eatures or actions of the product. Another
ame for a microcontroller, therefore, is
embedded controller."
Microcontrollers are dedicated to one tasknd run one specific program. The program
s stored inROM(read-only memory) and
enerally does not change.
Microcontrollers are often low-power
evices. A desktop computer is almost
lways plugged into a wall socket and might
onsume 50 watts of electricity. A battery-perated microcontroller might consume 50
milliwatts.
A microcontroller has a dedicated input
evice and often (but not always) has a
mall LED or LCD display for output. A
microcontroller also takes input from the
evice it is controlling and controls the
evice by sending signals to different
omponents in the device. For example, the
microcontroller inside a TV takes input from
heremote controland displays output on
he TV screen. The controller controls the
hannel selector, thespeakersystem and
ertain adjustments on the picture tube
electronics such as tint and brightness.
Theengine controllerin a car takes inpu
from sensors such as the oxygen and kn
sensors and controls things like fuel mix
spark plug timing. Amicrowave
ovencontroller takes input from a keypa
displays output on an LCD display and
controls arelaythat turns the microwave
generator on and off.
A microcontroller is often small and low
cost. The components are chosen to
minimize size and to be as inexpensive
possible. A microcontroller is often, but not
always, ruggedized in some way. The
microcontroller controlling a car's engine
example, has to work in temperature
extremes that a normal computer genera
cannot handle. A car's microcontroller in
Alaska has to work fine in -30 degree F C) weather, while the same microcontro
in Nevada might be operating at 120
degrees F (49 C). When you add the he
naturally generated by theengine, the
temperature can go as high as 150 or 18
degrees F (65-80 C) in the engine
compartment. On the other hand, amicrocontroller embedded inside a VCR
hasn't been ruggedized at all.
The actual processorused to implemen
microcontroller can vary widely. For
example, the cell phone shown onInside
Digital Cell Phonecontains aZ-80
http://computer.howstuffworks.com/rom.htmhttp://computer.howstuffworks.com/rom.htmhttp://computer.howstuffworks.com/rom.htmhttp://electronics.howstuffworks.com/inside-rc.htmhttp://electronics.howstuffworks.com/inside-rc.htmhttp://electronics.howstuffworks.com/inside-rc.htmhttp://electronics.howstuffworks.com/speaker.htmhttp://electronics.howstuffworks.com/speaker.htmhttp://electronics.howstuffworks.com/speaker.htmhttp://auto.howstuffworks.com/under-the-hood/trends-innovations/car-computer1.htmhttp://auto.howstuffworks.com/under-the-hood/trends-innovations/car-computer1.htmhttp://auto.howstuffworks.com/under-the-hood/trends-innovations/car-computer1.htmhttp://home.howstuffworks.com/microwave.htmhttp://home.howstuffworks.com/microwave.htmhttp://home.howstuffworks.com/microwave.htmhttp://home.howstuffworks.com/microwave.htmhttp://electronics.howstuffworks.com/relay.htmhttp://electronics.howstuffworks.com/relay.htmhttp://electronics.howstuffworks.com/relay.htmhttp://auto.howstuffworks.com/engine.htmhttp://auto.howstuffworks.com/engine.htmhttp://auto.howstuffworks.com/engine.htmhttp://electronics.howstuffworks.com/inside-cell-phone.htmhttp://electronics.howstuffworks.com/inside-cell-phone.htmhttp://electronics.howstuffworks.com/inside-cell-phone.htmhttp://electronics.howstuffworks.com/inside-cell-phone.htmhttp://www.geocities.com/SiliconValley/Peaks/3938/z80arki.htmhttp://www.geocities.com/SiliconValley/Peaks/3938/z80arki.htmhttp://www.geocities.com/SiliconValley/Peaks/3938/z80arki.htmhttp://electronics.howstuffworks.com/inside-cell-phone.htmhttp://electronics.howstuffworks.com/inside-cell-phone.htmhttp://auto.howstuffworks.com/engine.htmhttp://electronics.howstuffworks.com/relay.htmhttp://home.howstuffworks.com/microwave.htmhttp://home.howstuffworks.com/microwave.htmhttp://auto.howstuffworks.com/under-the-hood/trends-innovations/car-computer1.htmhttp://electronics.howstuffworks.com/speaker.htmhttp://electronics.howstuffworks.com/inside-rc.htmhttp://computer.howstuffworks.com/rom.htm7/28/2019 Microcontrollers Work
3/17
rocessor. The Z-80 is an 8-
itmicroprocessordeveloped in the 1970s
nd originally used in home computers of
he time. The Garmin GPS shown inHow
GPS ReceiversWork contains a low-power
ersion of the Intel 80386, I am told. The
0386 was originally used in desktop
omputers.
n many products, such as microwave
vens, the demand on the CPU is fairly low
nd price is an important consideration. In
hese cases, manufacturers turn
o dedicated microcontroller chips --
hips that were originally designed to be
ow-cost, small, low-power, embedded
CPUs. The Motorola 6811 andIntel 8051are
oth good examples of such chips. There is
lso a line of popular controllers called "PIC
microcontrollers" created by a company
alledMicrochip. By today's standards,hese CPUs are incredibly minimalistic; but
hey are extremely inexpensive when
urchased in large quantities and can often
meet the needs of a device's designer with
ust one chip.
A typical low-end microcontroller chip mightave 1,000bytesof ROM and 20 bytes of
RAM on the chip, along with eight I/0 pins.
n large quantities, the cost of these chips
an sometimes be just pennies. You
ertainly are never going to run Microsoft
Word on such a chip -- Microsoft Word
equires perhaps 30 megabytes of RAM and
a processor that can run millions of
instructions per second. But then, you do
need Microsoft Word to control a microw
oven, either. With a microcontroller, you
have one specific task you are trying to
accomplish, and low-cost, low-power
performance is what is important.
Using Microcontrollers
InHow Electronic Gates Work, you learn
about 7400-series TTL devices, as well
where to buy them and how to assemble
them. What you found is that it can often
take many gates to implement simple
devices. For example, in thedigital clock
article, the clock we designed might con
15 or 20 chips. One of the big advantage
a microcontroller is that software -- a sm
program you write and execute on the
controller -- can take the place of many
gates. In this article, therefore, we will us
microcontroller to create a digital clock. T
is going to be a rather expensive digital
clock (almost $200!), but in the process
will accumulate everything you need to p
with microcontrollers for years to come.
Even if you don't actually create this digi
clock, you will learn a great deal by read
about it.
The microcontroller we will use here is a
special-purpose device designed to mak
life as simple as possible. The device is
called a "BASIC Stamp" and is created b
http://www.geocities.com/SiliconValley/Peaks/3938/z80arki.htmhttp://www.geocities.com/SiliconValley/Peaks/3938/z80arki.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://electronics.howstuffworks.com/gadgets/travel/gps.htmhttp://electronics.howstuffworks.com/gadgets/travel/gps.htmhttp://electronics.howstuffworks.com/gadgets/travel/gps.htmhttp://electronics.howstuffworks.com/gadgets/travel/gps.htmhttp://www.cs.ucr.edu/~dalton/i8051/http://www.cs.ucr.edu/~dalton/i8051/http://www.microchip.com/http://www.microchip.com/http://www.microchip.com/http://computer.howstuffworks.com/bytes.htmhttp://computer.howstuffworks.com/bytes.htmhttp://computer.howstuffworks.com/bytes.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://computer.howstuffworks.com/bytes.htmhttp://www.microchip.com/http://www.cs.ucr.edu/~dalton/i8051/http://electronics.howstuffworks.com/gadgets/travel/gps.htmhttp://electronics.howstuffworks.com/gadgets/travel/gps.htmhttp://computer.howstuffworks.com/microprocessor.htmhttp://www.geocities.com/SiliconValley/Peaks/3938/z80arki.htm7/28/2019 Microcontrollers Work
4/17
ompany calledParallax. A BASIC Stamp is
PIC microcontroller that has been
ustomized to understand the BASIC
rogramming language. The use of the
ASIC language makes it extremely easy to
reate software for the controller. The
microcontroller chip can be purchased on a
mall carrier board that accepts a 9-
oltbattery, and you can program it by
lugging it into one of the ports on your
esktop computer. It is unlikely that any
manufacturer would use a BASIC Stamp in
n actual production device -- Stamps are
xpensive and slow (relatively speaking).
owever, it is quite common to use Stamps
or prototyping or for one-off demo products
ecause they are so incredibly easy to set
p and use.
hey are called "Stamps," by the way,
ecause they are about as big as a postagetamp.
he specific BASIC Stamp we will be using
n this article is called the "BASIC Stamp
Revision D".
he BASIC Stamp Revision D is a BS-1
mounted on carrier board with a 9-volt
attery holder, a power regulator, a
onnection for a programming cable, header
ins for the I/O lines and a small prototyping
rea. You could buy a BS-1 chip and wire
he other components in on a breadboard.
he Revision D simply makes life easier.
You can see from the previous table tha
you aren't going to be doing anything ex
with a BASIC stamp. The 75-line limit (th
256 bytes ofEEPROMcan hold a BASIC
program about 75 lines long) for the BS-
fairly constraining. However, you can cre
some pretty neat stuff, and the fact that
Stamp is so small and battery operated
means that it can go almost anywhere.
Programming the BASIC Stamp
You program a BASIC Stamp using
the BASIC programming language. If y
already know BASIC, then you will find t
the BASIC used in a Stamp is
straightforward but a little stripped-down
you don't know BASIC, but you do know
another language likeC, Pascal orJava
then picking up BASIC will be trivial. If yo
have never programmed before, you
probably want to golearn programming
desktop machine first. Here is a quick
rundown on the instructions available in
Stamp BASIC. (For complete
documentation, go toParallax: BASIC St
Documentation.)
Standard BASIC instructions:
for...next - normal looping statement
gosub - go to a subroutine
goto - goto a label in the program (e.g. -
"label:")
if...then - normal if/then decision
http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://electronics.howstuffworks.com/everyday-tech/battery.htmhttp://electronics.howstuffworks.com/everyday-tech/battery.htmhttp://electronics.howstuffworks.com/everyday-tech/battery.htmhttp://computer.howstuffworks.com/rom5.htmhttp://computer.howstuffworks.com/rom5.htmhttp://computer.howstuffworks.com/rom5.htmhttp://computer.howstuffworks.com/c.htmhttp://computer.howstuffworks.com/c.htmhttp://computer.howstuffworks.com/c.htmhttp://computer.howstuffworks.com/program.htmhttp://computer.howstuffworks.com/program.htmhttp://computer.howstuffworks.com/program.htmhttp://computer.howstuffworks.com/program.htmhttp://computer.howstuffworks.com/program.htmhttp://www.parallax.com/html_pages/downloads/basicstamps/documentation_basic_stamp.asphttp://www.parallax.com/html_pages/downloads/basicstamps/documentation_basic_stamp.asphttp://www.parallax.com/html_pages/downloads/basicstamps/documentation_basic_stamp.asphttp://www.parallax.com/html_pages/downloads/basicstamps/documentation_basic_stamp.asphttp://www.parallax.com/html_pages/downloads/basicstamps/documentation_basic_stamp.asphttp://computer.howstuffworks.com/program.htmhttp://computer.howstuffworks.com/program.htmhttp://computer.howstuffworks.com/c.htmhttp://computer.howstuffworks.com/rom5.htmhttp://electronics.howstuffworks.com/everyday-tech/battery.htmhttp://www.parallaxinc.com/7/28/2019 Microcontrollers Work
5/17
et - assignment (optional)
eturn - return from a subroutine
nd - end the program and sleep
nstructions having to do with I/O pins:
utton - read a button on an input pin, withebounce and auto-repeat
igh - set an I/O pin high
nput - set the direction of an I/O pin to input
ow - set an I/O pin low
utput - set the direction of an I/O pin to
utput
ot - read a potentiometer on an I/O pin
ulsin - read the duration of a pulse coming
n on an input pin
ulsout - send a pulse of a specific duration
ut on an output pin
wm - perform pulse width modulation on anutput pin
everse - reverse the direction of an I/O pin
erin - read serial data on an input pin
erout - write serial data on an output pin
ound - send a sound of a specific
equency to an output pinoggle - toggle the bit on an output pin
nstructions specific to the BASIC
tamp:
ranch - read a branching table
ebug - send a debugging string to the
onsole on the desktop computer
eeprom - download a program to EEPRO
lookdown - return the index of a value in
list
lookup - array lookup using an index
nap - sleep for a short time
pause - delay for the specified time
random - pick a random number
read - read a value from EEPROM
sleep - power down for the specified tim
write - write data to EEPROM
Operations:
+ - addition
- - subtraction
* - multiplication (low-word)
** - multiplication (high-word)
/ - division
// - mod
max - return maximum of 2 values
min - return minimum of 2 values
& - AND
| - OR
^ - XOR &/ - NAND
|/ - NOR
^/ - XNOR
If statement logic:
=
7/28/2019 Microcontrollers Work
6/17
>
=
=
AND
OR
VariablesAll variables in the BS-1 have pre-defined
ames (which you can substitute with
ames of your own). Remember that there
re only 14 bytes of RAM available, so
ariables are precious. Here are the
tandard names:
w0, w1, w2...w6 - 16-bit word variables0, b1, b2...b13 - 8-bit byte variables
it0, bit1, bit2...bit15 - 1-bit bit variables
ecause there are only 14 bytes of memory,
w0 and b0/b1 are the same locations in
RAM, and w1 and b2/b3 are the same, and
o on. Also, bit0 through bit15 reside in w0and therefore b0/b1 as well).
O pins
You can see that 14 of the instructions in
he BS-1 have to do with the I/O pins. The
eason for this emphasis is the fact that the
O pins are the only way for the BASIC
Stamp to talk to the world. There are eig
pins on the BS-1 (numbered 0 to 7) and
pins on the BS-2 (numbered 0 to 15).
The pins are bi-directional, meaning th
you can read input values on them or se
output values to them. The easiest way
send a value to a pin is to use
the HIGH orLOW functions. The statem
high 3 sends a 1 (+5 volts) out on pin 3.
LOW sends a 0 (Ground). Pin 3 was cho
arbitrarily here -- you can send bits out o
any pin from 0 to 7.
There are a number of interesting I/O pin
instructions. For example, POT reads th
setting on a potentiometer (variable resis
if you wire it up with acapacitoras the P
instruction expects. The PWM instructio
sends out pulse-width modulated signals
Instructions like these can make it a loteasier to attach controls and motors to t
Stamp. See thedocumentationfor the
language for details. Also, a book like Sc
Edward's Programming and Customizing
the BASIC Stamp Computercan be
extremely helpful because of the examp
projects it contains.
http://electronics.howstuffworks.com/capacitor.htmhttp://electronics.howstuffworks.com/capacitor.htmhttp://electronics.howstuffworks.com/capacitor.htmhttp://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://electronics.howstuffworks.com/capacitor.htm7/28/2019 Microcontrollers Work
7/17
Playing with a BASIC Stamp
you would like to play with a BASIC
tamp, it's very easy to get started. What
ou need is a desktop computer and
BASIC Stamp starter kit. The starter kit
ncludes the Stamp, a programming cable
nd an application that you run on your
esktop computer to download BASIC
rograms into the Stamp.
You can get a starter kit either
omParallax(the manufacturer) or from a
upplier likeJameco(who should be familiaro you from theelectronic gatesanddigital
lockarticles). From Parallax, you can order
he BASIC Stamp D Starter Kit (part number
7202), or from Jameco you can order part
umber 140089. You will receive the Stamp
pictured below), a programming cable,
oftware and instructions. The kit is $79om both suppliers. Occasionally, Parallax
uns a special called "We've Bagged the
asics" that also includes Scott
dward's Programming and Customizing
he BASIC Stamp Computer].
ooking up the Stamp is easy. You connect
into theparallel portof your PC. Then you
un a DOS application to edit your BASIC
rogram and download it to the Stamp.
o run the program in this editor, you hit
ALT-R. The editor application checks the
ASIC program and then sends it down the
wire to the EEPROM on the Stamp. The
Stamp then executes the program. In th
case, the program produces a square w
on I/O pin 3. If you hook up a logic probe
LED to pin 3 (see theelectronic gates
articlefor details), you will see the LED f
on and off twice per second (it changes
state every 250 milliseconds because of
PAUSE commands). This program woul
run for several weeks off of a 9-volt batte
You could save power by shortening the
time that the LED is on (perhaps it is on
50 milliseconds and off for 450
milliseconds), and also by using the NAP
instruction instead of PAUSE.
http://www.parallax.com/http://www.parallax.com/http://www.parallax.com/http://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://computer.howstuffworks.com/parallel-port.htmhttp://computer.howstuffworks.com/parallel-port.htmhttp://computer.howstuffworks.com/parallel-port.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://computer.howstuffworks.com/parallel-port.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/digital-electronics.htmhttp://www.jameco.com/http://www.parallax.com/7/28/2019 Microcontrollers Work
8/17
Creating a Really Expensive
Digital Clock
pending $79 to flash anLEDmay seem
xtravagant to you. What you would
robably like to do is create something
seful with your BASIC stamp. By spending
bout $100 more you can create a really
ice digital clock! This may
eemextremelyextravagant, until you
ealize that the parts are reusable in a
ariety of other projects that you may want
o build later.
et's say that we would like to use the I/O
ins on the BASIC Stamp to display numeric
alues. In thedigital clock article, we saw
ow to interface to a 7-segment LED display
sing a 7447 chip. 7447s would work just as
well with the BASIC Stamp. You could wire
our of the I/O pins straight into a 7447 and
asily display a number between 0 and 9.
ince the BS-1 Stamp has eight I/O pins, it
s easy to drive two 7447s directly like this.
For a clock, we need a minimum of four
digits. To drive four 7447s with eight I/O
pins, we have to be slightly more creativ
The following diagram shows you one
approach:
In this diagram, the eight I/O lines from t
Stamp enter from the left. This approach
uses four lines that run to all four 7447s
Then the other four lines from the Stamp
activate the 7447s in sequence ("E" on t
chips means "Enable" -- on a 7447, that
would be the blanking input on pin 5). To
make this arrangement work, the BASIC
program in the Stamp would output the f
digit on the four data lines and activate t
first 7447 by toggling its E pin with the fi
control line. Then it would send out the
value for the second digit and activate th
second 7447, sequencing through all fou
the 7447s like this repeatedly. By wiringthings slightly differently, you could actu
do this with only one 7447. By using a
74154 demultiplexer chip and some driv
you could drive up to 16 digits using this
approach.
This is, in fact, a standard way to controLED displays. For example, if you have a
old LED calculator, turn it on and shake
while watching the display. You will actu
be able to see that only one digit is ever
illuminated at once. The approach is
called multiplexing the display.
http://electronics.howstuffworks.com/led.htmhttp://electronics.howstuffworks.com/led.htmhttp://electronics.howstuffworks.com/led.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/digital-clock.htmhttp://electronics.howstuffworks.com/led.htm7/28/2019 Microcontrollers Work
9/17
While this approach works fine for clocks
nd calculators, it has two important
roblems:
EDs consume a lot of power.
-segment LEDs can only display numeric
alues.
An alternative approach is to use anLCD
creen. As it turns out, LCDs are widely
vailable and can be easily hooked to a
tamp. For example, the two-line by 16-
haracter alphanumeric display shown
elow is available from bothJameco(partumber 150990) andParallax(part number
7910). A typical display is shown here,
mounted on a breadboard for easier
nterfacing:
his sort of LCD has several advantages:
he display can be driven by a single I/O
in. The display contains logic that lets a
tamp communicate with it serially, so only
ne I/O pin is needed. In addition, the
EROUT command in Stamp BASIC
andles serial communication easily, so
alking to the display is simple.
The LCD can display alphanumeric text:
letters, numbers and even custom
characters.
The LCD consumes very little power -- o
3 milliamps.
The only problem is that one of these
displays costs $59. Obviously, you woul
not embed one of these in atoaster oven
you were designing a toaster oven,
however, you would likely prototype with
one of these displays and then create
custom chips and software to drive muc
cheaper LCDs in the final product.
To drive a display like this, you simply
supply it with +5 volts and ground (the
Stamp supplies both from the 9-volt batt
and then hook one of the I/O pins from t
Stamp to the display's input line. The
easiest way I have found to connect theStamp's I/O pins to a device like an LCD
to use a wire-wrap tool (Jamecopart
number 34577) and 30-gauge wire wrap
wire (Jameco part number 22541 is typic
That way, no soldering is involved and th
connections are compact and reliable.
The following BASIC program will cause
BASIC Stamp to behave like a clock and
output the time on the LCD (assuming th
LCD is connected to I/O pin 0 on the
Stamp):
pause 1000 'wa
for LCD display to boot
http://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/lcd.htmhttp://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://home.howstuffworks.com/toaster.htmhttp://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://home.howstuffworks.com/toaster.htmhttp://www.parallaxinc.com/http://www.jameco.com/http://electronics.howstuffworks.com/lcd.htmhttp://electronics.howstuffworks.com/lcd.htm7/28/2019 Microcontrollers Work
10/17
erout 0, n2400, (254, 1)clear the display
erout 0, n2400, ("time:")Paint "time:" on the display
'preset beforeoading program
0 = 0seconds
1 = 27minutes
2 = 6hours
gain:
0 = b0 + 1increment seconds
f b0 < 60 then minutes
b0 = 0 'ifeconds=60
b1 = b1 + 1 'hen increment minutes
inutes:
f b1 < 60 then hours
b1 = 0 'ifinutes=60
b2 = b2 + 1 'hen increment hours
ours:
f b2 < 13 then show
b2 = 1 'ifhours=13 reset to 1
show:
serout 0, n2400, (254, 135)
'position cursor on display,
'then display time
serout 0, n2400, (#b2, ":", #b":", #b0, " ")
pause 950
'pause 950 milliseconds
goto again'repeat
In this program, the SEROUT command
send data to the LCD. The sequence (25
1) clears the LCD (254 is the escape
character and 1 is the command to clear
screen). The sequence (254, 135) positi
the cursor. The other two SEROUT
commands simply send text strings to th
display.
This approach will create a reasonably
accurate clock. By tweaking the PAUSE
statement you can get the accuracy to
within a few seconds a day. Obviously, i
real clock you would like to wire up a pu
button or two to make setting it easier --
this program, you preset the time before
download the program to the Stamp.
7/28/2019 Microcontrollers Work
11/17
While this approach is simple and works, it
s not incredibly accurate. If you want better
ccuracy, one good approach would be to
wire a real-time clock chip up to your
tamp. Then, every second or so, you can
ead the time from the chip and display it. A
eal-time clock chip uses aquartz crystalto
ive it excellent accuracy. Clock chips also
sually contain date information and
andleleap yearcorrection automatically.
One easy way to interface a real-time clock
o a stamp is to use a component called
he Pocket Watch B.
he Pocket Watch B is available from
othJameco(part number 145630)ndParallax(part number 27962). This part
s about as big as a quarter and contains the
lock chip, crystal and a serial interface so
hat only one I/O pin is necessary to
ommunicate with it. This component costs
bout $30 -- again, not something you want
to embed in a toaster oven, but easy to p
with when constructing prototypes
Building a Digital Thermomete
Now that you understand a little bit abou
your Stamp and the LCD, we can add
another component and create a digital
thermometer. To create a thermometer,
will use a chip called the DS1620. This c
contains: A temperature-sensing device
An analog-to-digital (A/D) converterfo
the temperature-sensing device
A shift registerto read the data out of t
A/D converter
A little EEPROM (electrically erasable
programmable read-only memory) to
remember settings
The DS1620 has two modes: In one mod
it acts as a stand-alone thermostat chip,
in the other mode you hook it up to a
computer and use it as athermometer. T
EEPROM remembers the current mode
well as the set temperatures for the
thermostat.
Hooking up the DS1620 to the Stamp is
very easy. The DS1620 comes in an 8-p
chip. Supply +5 volts from the Stamp to
8 of the DS1620. Supply ground to pin 4
http://electronics.howstuffworks.com/gadgets/clocks-watches/quartz-watch1.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/quartz-watch1.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/quartz-watch1.htmhttp://science.howstuffworks.com/science-vs-myth/everyday-myths/time5.htmhttp://science.howstuffworks.com/science-vs-myth/everyday-myths/time5.htmhttp://science.howstuffworks.com/science-vs-myth/everyday-myths/time5.htmhttp://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://home.howstuffworks.com/therm.htmhttp://home.howstuffworks.com/therm.htmhttp://home.howstuffworks.com/therm.htmhttp://www.parallaxinc.com/http://www.jameco.com/http://science.howstuffworks.com/science-vs-myth/everyday-myths/time5.htmhttp://electronics.howstuffworks.com/gadgets/clocks-watches/quartz-watch1.htm7/28/2019 Microcontrollers Work
12/17
he DS1620. You then use three I/O pins
om the Stamp to drive three pins on the
DS1620:
in 1 on the DS1620 is the data pin. You
ead and write data bits on this pin.
in 2 on the DS1620 is the clock pin. Youock data in and out of the shift register
with this pin.
in 3 on the DS1620 is the reset/select pin.
You set pin 3 high to select the chip and
ommunicate with it.
or this example code, it is assumed that:
he data pin goes to I/O pin 2 on the Stamp.
he clock pin goes to I/O pin 1 on the
tamp.
he reset/select pin goes to I/O pin 0 on the
tamp.
he completed wiring looks like this:
You can get a DS1620 either
omJameco(part number 146456)
rParallax(part number 27917) in an
application kit" that includes the chip, the
apacitor, some good documentation and
ample code. Or you can buy the chip on itswn fromJameco(part number 114382). I
would suggest getting the application kit the
rst time you try using the DS1620 because
he documentation is very useful.
You can assemble the DS1620 in the
rototype area of the Stamp carrier board or
on a separate breadboard. Once you ha
assembled it, hook your LCD display up
I/O pin 3 of the Stamp, and then load an
run the following program:
symbol RST = 0 ' select/reset
line on 1620
symbol CLK = 1 ' clock line foshift registers on 1620
symbol DQ = 2 ' data line on1620
symbol DQ_PIN = pin2 ' pinrepresentation for DQ
symbol LCD = 3 ' data line forLCD
begin:
low RST ' deselect the 162unless talking to it
high CLK ' clock pin on 162should default high
pause 1000 ' wait for thethermometer and LCD to boot
setup:
high RST ' select the1620
b0 = $0C ' $0c is the1620 command byte
' saying"Write Config"
http://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.parallaxinc.com/http://www.jameco.com/7/28/2019 Microcontrollers Work
13/17
osub shift_out ' send it to the620
0 = %10 ' %10 is the620 command byte
' to sethermometer mode
osub shift_out ' send it to the620
ow RST ' deselect the620
ause 50 ' delay 50ms forEPROM
tart_convert:
0 = $EE ' $EE is the620 command byte
' to startonversions
igh RST ' select the620
osub shift_out ' send it to the620
ow RST ' deselect the620
This is the main loop
- reads and displaysemperature every second
ain_loop:
high RST ' select the1620
b0 = $AA ' $AA is the1620 command byte
' forreading temperature
gosub shift_out ' send it tothe 1620
gosub shift_in ' read thetemperature
' from the1620
low RST ' deselect tDS1620.
gosub display ' display thtemp in degrees C
pause 1000 ' wait asecond
goto main_loop
' The shift_out subroutine senwhatever is in
' the b0 byte to the 1620
shift_out:
output DQ ' set the pin to
' outputmode
for b2 = 1 to 8
7/28/2019 Microcontrollers Work
14/17
low CLK ' prepare tolock the bit
' into620
DQ_PIN = bit0 ' Send theata bit
high CLK ' latch datait into 1620
b0 = b0/2 ' shift allits right
' towardit 0
ext
eturn
The shift_in subroutine gets a-bit
temperature from the 1620
hift_in:
nput DQ ' set the DQin to
' inputode
0 = 0 ' clear w0
or b5 = 1 to 9
w0 = w0/2 ' shiftnput right.
low CLK ' ask 1620or next bit
bit8 = DQ_PIN ' read thebit
high CLK ' toggleclock pin
next
return
' Displays the temperature indegrees C
display:
if bit8 = 0 then pos ' ifbit8=1
'then temp is negative
b0 = b0 &/ b0 'invert b0 by NANDing it
'
with itself
b0 = b0 + 1
pos:
serout LCD, n2400, (254, 1) clear the LCD
serout LCD, n2400, ("Temp = ")display "Temp="
on the display
bit9 = bit0 save the half degree
b0 = b0 / 2
convert to degrees
7/28/2019 Microcontrollers Work
15/17
f bit8 = 1 then neg 'ee if temp is negative
serout LCD, n2400, (#b0) 'isplay positive temp
goto half
eg:
serout LCD, n2400, ("-", #b0)'isplay negative temp
alf:
if bit9 = 0 then even
serout LCD, n2400, (".5 C")display the half degree
goto done
ven:
serout LCD, n2400, (".0 C")display the half degree
one:
eturn
you run this program, you will find that it
isplays the centigrade temperature with an
ccuracy of one-half degree.
he DS1620 measures temperatures inentigrade half-degrees. It returns the
emperature in a 9-bit 2s-complement
umber with a range of -110 to 250 F (-55 to
25 C). You divide the number you receive
y 2 to get the actual temperature. 2s-
omplement binary numbers are a
onvenient way to represent negative
values. The following list shows the valu
for a 4-bit 2s-complement number:
0111 : 7
0110 : 6
0101 : 5
0100 : 4
0011 : 3
0010 : 2
0001 : 1
0000 : 0
1111 : -1
1110 : -2
1101 : -3
1100 : -4
1011 : -5
1010 : -6
1001 : -7
1000 : -8
You can see that instead of the 4 bitsrepresenting values from 0 to 15, the 4 b
in a 2s-complement number represent th
values -8 to 7. You can look at the left-m
bit to determine if the number is negative
positive. If the number is negative, you c
invert the bits and add 1 to get the positi
representation of the number.
7/28/2019 Microcontrollers Work
16/17
ere's what goes on with the digital
hermometer program shown here:
uses the symbol keyword to set up
everal constants that make the program
ightly easier to read (and also make it
asy for you to move the chip to different I/Oins on the Stamp).
sets the CLK and RST pins on the
DS1620 to their expected values.
writes a command byte to the EEPROM
n the DS1620 to tell the chip to operate in
hermometer mode." Because the mode is
tored in EEPROM, you only have to do it
nce, so you could technically take this
ection of the code out of the program after
ou run the program once (to save program
pace).
he program sends the command $EE ("$"
means "hexadecimal number" -- $EE is 238
n decimal) to tell the thermometer to start
p its conversion process.
he program then enters a loop. Every
econd, it sends a command to the DS1620
elling the DS1620 to return the current
emperature, and then it reads the 9-bit
alue that the DS1620 returns into the w0ariable. The Stamp sends and receives
ata 1 bit at a time by toggling the CLK line
n the DS1620. Remember that the w0 (16-
it) variable overlays the b0/b1 (8-bit)
ariables, which overlay the bit0/bit1/.../bit15
1-bit) variables, so when you insert a bit
om the DS1620 into bit 8 and divide w0 by
2, what you are doing is shifting each bit
the right to store the 9-bit temperature fr
the DS1620 into w0. Once the temperat
has been saved in w0, the display
subroutine determines whether the num
is positive or negative and displays it
appropriately on the LCD as a centigrad
temperature. The conversion from degre
C to degrees F is:
dF = dC * 9/5 + 32
At this point, we have succeeded in crea
an extremely expensive thermometer. W
might you do with it? Here's one idea. Le
say you work for a drug company and yo
are shipping expensive drugs across the
country that MUST remain at a certain
temperature the entire way or the drugs
spoil. What you can do with a Stamp is
create a data logging thermometer.
BothJameco(part number 143811)andParallax(part number 27960) sell a
device called the "RAM Pack module." It
contains a low-power 8-kilobyte (or
optionally 32-kilobyte) RAM chip with a
serial interface. You could add this
component (or something similar) to you
Stamp and write code that savestemperature readings to the RAM every
minute. You could then slip your Stamp
the drug shipment, and at the other end
the trip retrieve the Stamp. The RAM
module would contain the temperature
history of the entire trip and you would k
whether or not the drugs ever thawed ou
http://www.jameco.com/http://www.jameco.com/http://www.jameco.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.parallaxinc.com/http://www.jameco.com/7/28/2019 Microcontrollers Work
17/17
here are all kinds of neat, useful devices
ke this that you can build with a Stamp now
hat you know how microcontrollers work!
or more information on microcontrollers
nd related topics, check out the links on
he next page.
Recommended