dimanche 11 octobre 2009

Creating a Really Expensive Digital Clock using microcontroller







First, Playing with a BASIC Stamp

If you would like to play with a BASIC Stamp, it's very easy
to get started. What you need is a desktop computer and
a BASIC Stamp starter kit. The starter kit includes the
Stamp, a programming cable and an application that you run on
your desktop computer to download BASIC programs into the
Stamp.

You can get a starter kit either from Parallax (the
manufacturer) or from a supplier like Jameco. From Parallax,
you can order the BASIC Stamp D Starter Kit (part number
27202), or from Jameco you can order part number 140089. You
will receive the Stamp, a programming cable, software and
instructions. The kit is $79 from both suppliers.
Occasionally, Parallax runs a special called "We've Bagged
the Basics" that also includes Scott Edward's Programming
and Customizing the BASIC Stamp Computer.

Hooking up the Stamp is easy. You connect it into the
parallel port of your PC. Then you run a DOS application to
edit your BASIC program and download it to the Stamp. Here is
a screenshot of a typical editor (in this case, the one from
Scott Edward's book).

To run the program in this editor, you hit ALT-R. The editor
application checks the BASIC program and then sends it down
the wire to the EEPROM on the Stamp. The Stamp then executes
the program. In this case, the program produces a square wave
on I/O pin 3. If you hook up a logic probe or LED to pin 3,
you will see the LED flash on and off twice per second (it
changes state every 250 milliseconds because of the PAUSE
commands). This program would run for several weeks off of
a 9-volt battery. You could save power by shortening the time
that the LED is on (perhaps it is on for 50 milliseconds and
off for 450 milliseconds), and also by using the NAP
instruction instead of PAUSE.

Now, Creating a Really Expensive Digital Clock

Spending $79 to flash an LED may seem extravagant to you.
What you would probably like to do is create something useful
with your BASIC stamp. By spending about $100 more you can
create a really nice digital clock! This may seem extremely
extravagant, until you realize that the parts are reusable in
a variety of other projects that you may want to build later.

Let's say that we would like to use the I/O pins on the BASIC
Stamp to display numeric values. 7447s would work just as well
with the BASIC Stamp. You could wire four of the I/O pins
straight into a 7447 and easily display a number between 0
and 9. Since the BS-1 Stamp has eight I/O pins, it is 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
creative. The diagram shows you one approach.

In this diagram, the eight I/O lines from the 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 the 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 first digit on the four data lines and activate
the first 7447 by toggling its E pin with the first control
line. Then it would send out the value for the second digit
and activate the second 7447, sequencing through all four of
the 7447s like this repeatedly. By wiring things slightly
differently, you could actually do this with only one 7447.
By using a 74154 demultiplexer chip and some drivers, you
could drive up to 16 digits using this approach.

This is, in fact, a standard way to control LED displays. For
example, if you have an old LED calculator, turn it on and
shake it while watching the display. You will actually be
able to see that only one digit is ever illuminated at once.
The approach is called multiplexing the display.

While this approach works fine for clocks and calculators, it
has two important problems:

* LEDs consume a lot of power.
* 7-segment LEDs can only display numeric values.

An alternative approach is to use an LCD screen. As it turns
out, LCDs are widely available and can be easily hooked to
a Stamp. For example, the two-line by 16-character
alphanumeric display shown below is available from both
Jameco (part number 150990) and Parallax (part number 27910).
A typical display is shown here, mounted on a breadboard for
easier interfacing.

This sort of LCD has several advantages:

* The display can be driven by a single I/O pin. The
display contains logic that lets a Stamp communicate with it
serially, so only one I/O pin is needed. In addition, the
SEROUT command in Stamp BASIC handles serial communication
easily, so talking to the display is simple.
* The LCD can display alphanumeric text: letters, numbers
and even custom characters.
* The LCD consumes very little power -- only 3 milliamps.

The only problem is that one of these displays costs $59.
Obviously, you would not embed one of these in a toaster oven.
If 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 much 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
battery) and then hook one of the I/O pins from the Stamp to
the display's input line. The easiest way I have found to
connect the Stamp's I/O pins to a device like an LCD is to
use a wire-wrap tool (Jameco part number 34577) and 30-gauge
wire wrap wire (Jameco part number 22541 is typical). That
way, no soldering is involved and the connections are
compact and reliable.

The following BASIC program will cause a BASIC Stamp to
behave like a clock and output the time on the LCD (assuming
the LCD is connected to I/O pin 0 on the Stamp):

pause 1000 'wait for LCD display to boot
serout 0, n2400, (254, 1) 'clear the display
serout 0, n2400, ("time:") 'Paint "time:" on the display
'preset before loading program
b0 = 0 'seconds
b1 = 27 'minutes
b2 = 6 'hours

again:
b0 = b0 + 1 'increment seconds
if b0 < 60 then minutes
b0 = 0 'if seconds=60
b1 = b1 + 1 ' then increment minutes
minutes:
if b1 < 60 then hours
b1 = 0 'if minutes=60
b2 = b2 + 1 ' then increment hours
hours:
if b2 < 13 then show
b2 = 1 'if hours=13 reset to 1

show:
serout 0, n2400, (254, 135) 'position cursor on display,
'then display time
serout 0, n2400, (#b2, ":", #b1, ":", #b0, " ")
pause 950 'pause 950 milliseconds
goto again 'repeat

In this program, the SEROUT commands send data to the LCD.
The sequence (254, 1) clears the LCD (254 is the escape
character and 1 is the command to clear the screen). The
sequence (254, 135) positions the cursor. The other two
SEROUT commands simply send text strings to the 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, in a real clock you
would like to wire up a push-button or two to make setting it
easier -- in this program, you preset the time before you
download the program to the Stamp.

While this approach is simple and works, it is not incredibly
accurate. If you want better accuracy, one good approach
would be to wire a real-time clock chip up to your Stamp.
Then, every second or so, you can read the time from the chip
and display it. A real-time clock chip uses a quartz crystal
to give it excellent accuracy. Clock chips also usually
contain date information and handle leap year correction
automatically.

One easy way to interface a real-time clock to a stamp is to
use a component called the Pocket Watch B.

The Pocket Watch B is available from both Jameco (part number
145630) and Parallax (part number 27962). This part is about
as big as a quarter and contains the clock chip, crystal and
a serial interface so that only one I/O pin is necessary to
communicate with it. This component costs about $30 -- again,
not something you want to embed in a toaster oven, but easy
to play with when constructing prototypes.

Aucun commentaire:

Enregistrer un commentaire