EE Introduction to Electrical Engineering Microprocessors and Microcontrollers with BASIC by Cappadona

VIEWS: 23 PAGES: 4

									                     EE-100 – Introduction to Electrical Engineering

                Microprocessors and Microcontrollers with BASIC Stamp USB
                                                Dr. Reyer


GOAL: Learn the hardware and software of a microprocessor and have it perform specialized tasks.

BACKGROUND: Microprocessors are found in many electronic devices and are a standard building
block for Electrical Engineers. In fact, many more microprocessors are used in EE control applications
than in personal computers! They’re found in cars (ignition system, alarm, radio, CD player, etc.),
homes (thermostat, cordless phone, TV, VCR, mp3 player, coffee pot, microwave oven, etc.) and away
from home (traffic light control, pagers, cell phones, etc.) and then are called “microcontrollers”.

GETTING ACQUAINTED: Our microprocessor system is
shown at the right. It includes the microprocessor itself, a
small board with support chips to allow programming in the
computer language “Basic”, and an experiment area for
testing. Compare with your own Basic Stamp system.

The experiment area
includes a white board with
many holes for making
connections. The holes in
rows on each side of the
center are connected, as
shown.

On the top left, the five holes
in that row are connected
together. Likewise, the five
holes in the second row are
connected. And so on.

The connections P15 – P0 are input/output (I/O) signals from the microprocessor, letting us send
information to the computer program, and letting the program control external actions. Information is
denoted by either of two voltages, +5 or 0 (indicating “true” or “false”. Sometimes these two states are
called “logic 1” and “logic 0”. Or, they could be called “on” and “off”). The Vdd connections are to the
+5 volt power supply, the Vss connections are to zero volts, or “ground”. For now, we won’t use the
Vin connections.

OUR FIRST PROGRAM – blink an LED:
Light Emitting Diodes (LEDs) are common indicators
available in various colors – red, yellow, orange,
green, blue, and even white. They’re often used to
communicate information to humans.

Our first program will have the microcontroller blink a
red LED. Wire the circuit as shown at the right, using
a red LED and a 470 ohm resistor (the value is
denoted by colored bands – yellow, violet, brown). A
resistor must always be used with an LED to limit the
current, or the LED could be ruined.
An equivalent schematic diagram is shown at the right:

After the Editor and Drivers are installed, with the Switch in position 0, plug the
power adapter into the wall outlet and to the experiment board. Move Switch to
position 1. Connect the USB cable between the experiment board and your
laptop. The green pilot light LED should light up, but your red LED may remain
turned off.

Now that the circuit is wired properly, we’ll have to write a computer program in
Basic to control the LED. Under program control, the microcontroller is able
to “ground” pin P12 (making its voltage = 0), causing current to flow from the
Vdd (+5 volt) supply, through the resistor, through the LED, and finally through the microcontroller and
back to the power supply, thus completing the circuit. It can also “unground” the pin, so no current
flows (making the P12 pin’s voltage = +5 volts. Now, with +5 on both ends of the circuit, no current will
flow because there’s no voltage “difference” to cause it).

Run the Basic Stamp Editor on your laptop, and type in the following
program (the sequence of instructions that we wish the microcontroller
to perform, when we start the program running):

• ‘ {$STAMP BS2}tells the editor which type of Basic Stamp
   board we will be using (use menu item Directive/Stamp/BS2).

• msoe:    is a label, or placeholder, that we can jump to (or goto)
   from another place in the program.

• LOW 12      causes the microcontroller to put logic level 0 on P12 (approx. 0 volts).

• PAUSE 100        causes the microcontroller to wait 100 milliseconds (1/10 second) before continuing
   the program.

• HIGH 12      causes themicroprocessor to put logic level 1 on P12 (approx. 5 volts).

• PAUSE 100       means wait 100 milliseconds.

• GOTO msoe        causes the program “flow” to revert back to the line labeled “msoe” and to start over
   at the top, and thus toggling again. All this will continue forever in an infinite loop, unless we stop it.

The program has now been typed in, but it isn’t yet running on the microcontroller. To run the
program, click on the blue arrow on the editor’s toolbar. This will send the program to the Basic
Stamp, and also start the program running.

Does the LED blink as expected (1/10 second on, followed by 1/10 second off, etc.)?

Experiment with the length of the pause, by changing the program in the editor (for example, change
pause 100 to pause 1000), and “downloading” the new program (click the blue arrow again) Try
other values. How fast can you blink the LEDs before the light appears to be ”always on”?

Pressing the small black “reset” button on the board (bottom center, beneath the pilot light) will stop
the program, if desired. Otherwise, simply downloading a new program will stop the old one and start
the new one.
DOING MORE:
Using a yellow LED and a green LED and two more 470 ohm resistors, make similar connections to
P10 and P8 on the experiment board. That is,

•   Wire a 470 ohm resistor from Vdd to row 7
    on the white board (counting from the top).
    Wire a yellow LED from that row to P10.

•   Wire a 470 ohm resistor from Vdd to row 9
    on the white board. Wire a green LED from
    that row to P8.

It should look like the photo at the right:

Now for the software. Let’s try a program that will
blink the LEDs in order. We’ll use some new ideas
(see the program listing below-right). The program
has these features:

•   comments, preceded by an apostrophe, to help
    the reader understand the program

•   the commands “high” and “low” to cause the microcontroller pin to have either a logic high or low
    level at that point in the program. Because of
    how we have the circuit wired, logic “low” will
    make the LED illuminate.

•   The PAUSE instruction causes 500 ms. Of
    delay before proceeding (1/2 second).

•   controlling pins 12, 10, and 8, because that’s
    where the red, yellow, and green LEDs are
    connected.

Does the program logic make sense? Type it in,
omitting the comments if you wish, and give it a
try. Does it work as expected? Experiment with
different values of “time”.

STANDALONE OPERATION:
Once a program is downloaded to the Basic
Stamp, the PC is no longer needed! In fact, the
serial cable can be detached, and the LEDs will still blink just fine. Also, you can even turn off the
power, and turn it back on again, and the program will start right up! This type of memory is called
“non-volatile”, and is extremely useful in devices such as a microwave oven controller, where
programming via a PC is impractical.
BE CREATIVE:
Ok, it’s time for you to design a system! Write a program to simulate a stop-and-go light (traffic
light). Here are the “software specifications”:

•   Only one LED may be on at a time
•   The sequence of illumination should be:
       Green for 5 seconds
       Yellow for 1 second
       Red for 5 seconds
       Repeat endlessly

After designing the software, type it in and test it. If it’s not working right, examine your program
carefully to find and correct the mistakes. Finally, demonstrate it to your professor.

WORKING WITH INPUTS:
Until now, your programs have used only outputs
from the microcontroller. Now we’ll connect a
pushbutton switch (like a doorbell button) to act as an
input. The program will be able to sense whether
you’re pushing the switch and take different actions,
according to your program.

Make the connections as shown in the photo:

•   a 10,000 ohm resistor (brown black orange) from
    Vdd to an empty row
•   the switch from Vss to that same row
•   a wire from P0 to that same row

Now try the following program to blink the red LED
only when the button is pressed (don’t bother to type
the comments).

Note that the line:

if in0=1 then msoe

causes the microprocessor to test P0 to see if
it has a value of logic 1 (not pressed), and if
so, jumps back to the line labeled “msoe”.
Does the program make sense? Follow the
logic and convince yourself.

CHALLENGE:
Write your own program for a simplified microwave oven controller – Have the green LED “ready
light” be on while waiting for the switch to be pressed. Press the switch to start the oven running,
denoted by the yellow LED. When a 10-second cooking interval is finished, blink the red LED twice (for
one second each time) to denote “finished” (like a beeper) and go back to “ready”. Demonstrate it to
your professor.

STILL HAVE TIME?
Invent your own control application. Try programming a solution.

								
To top