MICROCOMPUTER INTERFACING

advertisement
MICROCOMPUTER INTERFACING
INTRODUCTION
Since its introduction in the mid seventies, the microcomputer has become an important tool which has
impacted on all aspects of our society. Probably the most popular impression of this device is one that
includes wordprocessing, data manipulation, graphics processing, communications, simulation and game
playing. Just as important, but maybe not as visible to the average person, is the role of the microcomputer
and its main constituent part, the microprocessor, in external sensing and control of other devices. Examples
include space shuttles, airliners, ships, modern buildings, automobiles, medical devices, consumer electronics
and appliances, artificial limbs, smart toys and military hardware; the list is growing exponentially.
The purpose of this module is to investigate the use of the microcomputer in data acquisition and
control. This topic is divided into four logical parts which include digital input and output and
analog input and output. To facilitate; this study, the student will use hands on activities as a focus for
each of the four components. These activities will use the Quick Basic language and several dedicated
software applications to interact with the hardware. A prior knowledge of basic electronics would be an
asset but is not essential to the completion of the activities and projects.
Analog vs Digital
Before you begin an investigation of the microcomputer's potential for interfacing to the external world we
will define a few of the terms that are crucial to understanding the basics of this module.
Volt - Defined as the unit of electrical potential energy difference. The power supply of a typical
microcomputer delivers both 5 and 12 volts of direct current (DC).
Digital - Based on discrete, definite values.
Analog - Continuously and smoothly varying
The circuits used in a microcomputer consist of thousands of switches that turn on and off at very high
speeds. For the most part, the switches can only be in two states, either on or off. This corresponds to
voltage levels of +5 volts and 0 volts and of course are represented by the binary number system's digits of 1
and 0. This is the reason that such computers are called digital because their switching can only exist in
either of the two states. Analog is the opposite. It refers to electrical signals that can take any value (of
voltage), can vary continuously and can have positive or negative polarity. A modern microcomputer cannot
handle analog signals WITHOUT SPECIAL CIRCUITS that convert voltages into digital "words". One
method of illustrating this difference is to graph voltage as a function of time. The first example shows a
simple circuit consisting of a 5 volt supply, a switch and a light bulb. As the switch is turned on and off the
Page 1
INTRODUCTION
resulting graph depicts a square wave that changes between the two discrete values of 0 and +5 volts. (This
is the graph of an ideal switch, an actual switch "bounces" when closed and does not provide a clean square
wave graph. Special devices called debouncing circuits are used to clean up the signal generated by a
manual switch such as a keyboard key.)
The example used to illustrate the analog signal is an actual signal generated by a magnetic microphone.
Page 2
Note how the voltage changes in both magnitude and polarity (+ and -) with time.
INTERFACE CARDS
Most microcomputers are already configured to communicate with the outside through printer, serial
(communications) and even game ports. In your investigations of computer interfacing, you will be using
several special interface "cards" which are plugged into the expansion port of a standard PC. One such
card, called the Advanced Interface Board from Sunset Labs, can be used for all four I/O (input/output)
functions listed previously. This circuit board contains three main circuit chips. The 8255
Programmable Peripheral Interface (PPI), used for Digital I/O, an Analog to Digital
Converter (ADC) for analog input and a Digital to Analog Converter (DAC) for analog output.
This card has two output ports that connects these functions for use in external circuits. These ports, 1 and 2,
accept polarized (one way) plugs which are connected by ribbon cables to 16 pin DIP (Dual Inline Package)
connectors which can be plugged into circuit boards (bread boards). Most of the activities will be carried out
on these bread boards. The ports also provide power in the form of 5 and 12 volts DC. The functions of this
card and other interface devices will be explained in greater detail in subsequent sections.
UNIT 1: DIGITAL INPUT
The digital input function enables the computer, through appropriate software, to detect whether an input
port is in a high state (+5 volts) or a low state (0 volts). This can be as simple as sensing a mechanical switch
closing or a beam of light being blocked and detected by some type of light sensor. You will be using the
Advanced Interface board and in particular the 8255 PPI chip. This chip contains four, 8 bit registers which
control the digital I/O functions.. A register is defined as a storage place for binary digits or bits.
Remember that a binary digit or bit can only be a 0 or 1 which corresponds to the two voltage levels, 0 and 5
V. The following block diagram shows the components of the 8255 chip that are of interest.
Page 3
UNIT 1 - DIGITAL INPUT
You
will
be
using
the
I/O
ports
A and
B to
detect
ON/O
FF
condi
tions. Port C is used for other functions and won't be accessed. The Data Direction Register is a control
register that determines whether an I/O port is to be used for input or output.
Each of the registers is assigned a unique I/O address. By using this address we can communicate directly
with the chip. Since each register has a capacity of 8 bits, the I/O ports have been designated PA0, PA1.....
PA7 (port A - line 0, port A - line 1 etc.) , PB0, PB1......PB7 and PC0, PC1....PC7. As configured, only
four lines for ports A and B are available from the ribbon cable. (A connection can be made on the card to
make all eight lines available). Port C is used for on board functions and will not be used in the activities.
Let's have a closer look at an I/O Port and how it can be used to detect the on/off states of a switch. The
following diagram is a representation of I/O Port A. Each of the 8 I/O lines are shown, with line PA0 on the
Page 4
UNIT 1 - DIGITAL INPUT
right.
As stated previously, the I/O ports are 8 bit registers. Each bit reflects the state of the corresponding I/O line.
For example, suppose line PA0 is connected to a 5 volt source as in the next diagram.
The Port A register would contain the bit pattern 00000001. Remember, 5 volts is interpreted by the
computer as the binary digit 1. The I/O line is said to be in a high state. You might recognize the 8 bit
pattern as a
byte. Let's
stop for a
moment and
review the
binary system.
Each digit
(bit) of the
byte has a
place value.
The bit on the far right is called the least significant bit or LSB . It has a place value of 20 which is
equal to a decimal value of 1. The bit on the far left is called the most significant bit or MSB. It has a
place value of 27 or 128 decimal. Starting with the MSB the place values are
27
or
26
25
24
23
22
21
20
Page 5
UNIT 1 - DIGITAL INPUT
128
64
32
16
8
4
2
1
Here are two more examples:
The bit pattern
here would be
00000100.
Which
corresponds to
a value of 4
(decimal).
The bit pattern
for this
example is
00001010
which
corresponds to
10 (decimal).
Up to now we have not mentioned the role of the Data Direction Register (DDR). Port A (and Port B) are
I/O ports which means that they have a dual function. Whether a port is used as an input or an output is
determined by placing a particular bit pattern in the DDR. You also need to know how to find out what the
Page 6
UNIT 1 - DIGITAL INPUT
contents of a particular I/O register is. That is, to be able to detect when a switch has been closed and 5 volts
applied to a particular I/O line. One procedure is to use Quick Basic, a high level language which, by now,
you probably have some familiarity with. To read the I/O ports and to set the Data Direction Register
requires that you know the I/O address of these registers. The advanced interface board has fixed
addresses for these locations which are as follows:
Data Direction Register
819
I/O Port A
816
I/O Port B
817
These are decimal numbers, although hexadecimal equivalents can also be used. You will need to do a
number of activities to place this theory into practise.
ACTIVITY 1
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
1 kS resistor, ¼ watt
Momentary switch
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #1 of the AIB board. Connect the other
components according to the next illustration. A schematic diagram is also shown for your information.
When you have connected the circuit, have it checked by your teacher before you turn on the computer. The
circuit is connected to the power supply and damage could occur if it is not configured correctly.
Page 7
UNIT 1 - DIGITAL INPUT
Activity 1: Bread board set-up
When your set-up has been
checked, turn on the
computer, run Quick Basic
and enter the edit screen.
Type in the following basic program:
Activity 1: Schematic diagram
OUT 819, 154
DO WHILE INKEY$ <> CHR$(13)
N = INP(816)
IF (N AND 1) = 1 THEN
PRINT "SWITCH IS CLOSED"
ELSE
PRINT "SWITCH IS OPEN"
ENDIF
LOOP
Check the program and then run it. Press the switch several times.
What do you observe? Press the enter key to stop the program,
Page 8
UNIT 1 - DIGITAL INPUT
PROGRAM COMMENTS
This is a fairly simple program but it contains several important concepts crucial to your understanding of
interfacing. We will look at this program in detail. In subsequent activities, comments will be made on new
concepts.
OUT 819, 154
As previously mentioned, 819 is the address for the Data Direction
register. The OUT statement places a byte in a particular address. In
this case the decimal number 154 (1001110 binary) sets I/O Port A as an
input port. The numbers that can be used to configure the DDR are1:
DDR
Contents
PORT A
PORT B
154
INPUT
INPUT
152
INPUT
OUTPUT
138
OUTPUT
INPUT
136
OUTPUT
OUTPUT
DO WHILE....
Sets up a loop that will execute until the enter key is pressed. (Chr$(13) is the
character code for a carriage return, i. e. the enter key.)
N = INP(816)
The INP statement "looks at" or returns the contents of a particular address. In
this example the address 816 is I/O PORT A. When you press the switch you
are placing 5 volts on I/O line PA0 which forces the line "high". This means a
binary 1 is placed in the PORT A register in this position. In this example we are
assigning the contents of I/O PORT A to the simple variable N.
IF (N AND 1) = 1 THEN
Since we are only interested in what happens on I/O line 0 we need to
mask out what is happening on the other I/O lines of Port A. (In a
subsequent activity, you will connect two switches to different I/O
lines.) This is the purpose of the AND, which is called a Boolean
operator.
When you use the AND operation on two numbers (bytes) certain rules
apply. This table shows the possible results of the AND operation on
two digits, A and B.
A
B
RESULT
0
0
0
1
0
0
0
1
0
1
Check the Sunset Labs manual for further information on possible configurations for the DDR.
Page 9
UNIT 1 - DIGITAL INPUT
1
1
1
As you can see from the table, the only time the result is 1 is when
both digits are 1. How does this apply to our example? Since no other
switches are connected, the contents of I/O Port A can only be
00000000 (switch open) or 000000001 (switch closed). If we AND
the contents of I/O Port A with the number 1 (00000001 binary) there
can only be two results:
00000000
00000001
------------00000000
<----Port A contents (switch open)
<----Number which is "Anded" (1)
00000001
00000001
------------00000001
<----Port A contents (switch closed)
<----Number which is "Anded" (1)
<----Result (0 = switch open)
<----Result (1 = switch closed)
Therefore this line checks the contents of the Port A register to see if the
switch has been closed. If you are a little confused at this point, don't
worry. You are going to do several more activities which should make
this procedure routine. The rest of this program is straight forward and
should not require further comment.
ACTIVITY 2
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
1 kS resistor, ¼ watt
Momentary switch
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #1 of the AIB board. Connect the other
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer.
Page 10
UNIT 1 - DIGITAL INPUT
This activity is
identical to Activity 1,
except that you will
connect the switch to
I/O line 2 (PA2).
Activity 2: Schematic diagram
When your set-up has been checked, turn on the computer, run Quick Basic and enter the edit screen. Type
in the following basic program:
Activity 2: Bread board setup
OUT 819, 154
DO WHILE INKEY$ <> CHR$(13)
Page 11
UNIT 1 - DIGITAL INPUT
N = INP(816)
IF (N AND 4) = 4 THEN
PRINT "SWITCH IS CLOSED"
ELSE
PRINT "SWITCH IS OPEN"
ENDIF
LOOP
Check the program and then run it. Press the switch several times.
What do you observe? Press the enter key to stop the program,
PROGRAM COMMENTS
The only difference in this program is that we are checking for a switch closure on I/O line 2 of Port A
(PA2). In fact, this is the third I/O line as the numbering starts at 0. Lets investigate the third line in the
program.
IF (N AND 4) = 4
The difference is that we are using the AND operator with the decimal number 4
which is 00000100 binary. Remember you have connected the switch to I/O line
2 (PA2). When the switch is pressed the bit pattern 00000100 is placed in the
PORT A register. To review the function of the AND operator consider the
following.
00000000
00000100
------------00000000
<----Port A contents (switch open)
<----Number which is "Anded" (4)
00000100
00000100
------------00000100
<----Port A contents (switch closed)
<----Number which is "Anded" (4)
<----Result (0 = switch open)
<----Result (4 = switch closed)
YOUR TURN
Modify the circuit and the program so that a switch connected to I/O line 1 of PORT A (ie. PA1) is detected
by the program. Again make sure the computer is turned off and your circuit is double checked before you
proceed.
Questions:
1.
What number did you AND with N (INP(816)) to detect the switch closing.
2.
Use eight bit binary numbers to show how the AND operator works for your example.
ACTIVITY 3
Page 12
UNIT 1 - DIGITAL INPUT
Materials:
Advanced
Interface
Board
(installed)
Microcompu
ter
Ribbon
cable with
connectors
Activity 3: Bread board setup
Bread board
21 kS resistor, ¼ watt
2 - momentary switch
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #1 of the AIB board. Connect the other
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer.
This activity is a combination of Activity 1 and Activity 2. You will connect two switches and use an
appropriate program to detect the individual switch closures.
Page 13
UNIT 1 - DIGITAL INPUT
When your set-up has been checked, turn on the computer, run Quick Basic and enter the edit screen. Type
in the following basic program:
819, 154
OUT
DO
WHILE
INKEY$
<>
CHR$(13
)
N=
INP(816)
(N AND 1) = 1 THEN
IF
Activity 3: Schematic diagram
PRINT "SWITCH A IS CLOSED"
ELSEIF (N AND 4) = 4 THEN
PRINT "SWITCH B IS CLOSED"
ELSE
PRINT "SWITCHES ARE OPEN"
END IF
LOOP
Check the program and then run it. Press each switch several times.
Page 14
UNIT 1 - DIGITAL INPUT
What do you observe? Press the enter key to stop the program,
PROGRAM COMMENTS
This is a combination of the first two programs. It is important that the brackets are used in the IF and
ELSEIF lines or the decision cannot be made correctly. Try adding these lines to each part of the program
shown. These new lines activate the computer's built in speaker and provide audio feedback of a switch
closure.
IF (N AND 1) = 1 THEN
PRINT "SWITCH A IS CLOSED"
BEEP : BEEP
ELSEIF (N AND 4) = 4 THEN
PRINT "SWITCH B IS CLOSED"
BEEP : FOR I = 1 TO 500 : NEXT I : BEEP
YOUR TURN
1.
Modify the circuit and the program so that the switches are connected to I/O line 1 of PORT A
(PA1) and I/O line 3 (PA3) and their individual closure is detected by the program. Again make
sure the computer is turned off and your circuit is double checked before you proceed.
2.
Modify the program in #1 or the original program so that the program reports "BOTH
SWITCHES CLOSED" if both switches are pressed together. (This is not hard to do, but
you should know the IF...ELSEIF....ELSE block structure. Check Page 12 of the
"Programming in Basic" section of the Microsoft Quick Basic Manual for some information on this
structure.
Page 15
UNIT 1 - DIGITAL INPUT
ACTIVITY 4
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
bread board
R1 - 1 kS resistor, ¼ watt
R2 - 330 S resistor, ¼ watt
Infrared LED
NPN photo transistor
jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #1 of the AIB board. Connect the other
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer.
NOTE: The Phototransistor has a plastic tab closest to the emitter lead. For this type of transistor, the
emitter is connected to the ground (0 volts). If the photo transistor is reversed it will not operate properly.
The Infrared LED has a flat on the circular base. This is closest to the cathode, which is also connected to
ground. If it is reversed it also will not operate. If you are not sure, connect a visible red LED to make sure
the circuit is working (you can't see the infrared light from the IR LED). You can then replace it with the IR
LED as the phototransistor is not very sensitive to the visible red light. The 330 S resistor limits the current
in the LED.
Activity 4: Bread board set-up
Page 16
UNIT 1 - DIGITAL INPUT
When your set-up has been
Activity 4: Schematic diagram
checked, turn on the
computer, run Quick Basic
and enter the edit screen. Type in the following basic program:
OUT 819, 154
DO WHILE INKEY$ <> CHR$(13)
N = INP(816)
IF (N AND 1) = 1 THEN
PRINT "BEAM IS UNBLOCKED"
ELSE
PRINT "BEAM IS BLOCKED"
BEEP : BEEP
ENDIF
LOOP
Check the program and run it. This is an identical activity to activity #1. The photo transistor is being used
as an ON/OFF switch which turns on when enough light falls on its photo sensitive surface. This type of
transistor is most sensitive to infrared light, You are using an infrared light emitting diode simply as a light
source. When the program starts, it should report "beam is unblocked" ( if you have not placed
anything in the beam path). If you don't get this message, check the program, make sure the LED is pointed
at the top of the photo transistor and check to see that the components are connected properly.
If you get the correct message then place something between the photo transistor and the LED. The message
should change to "beam is blocked" and you should hear a beep. This arrangement is used in science
labs and in sports events to determine when the beam has been broken.
YOUR TURN
Page 17
UNIT 1 - DIGITAL INPUT
1.
Modify the circuit and the program so that the photo transistor is connected to I/O line 1 of PORT A
(PA1). Again make sure the computer is turned off and your circuit is double checked before you
proceed.
2.
Physically move the photo transistor (or the LED) to see what kind of range you can get. Make
sure the electrical connections are correct. A strong flash light or a small laser can also be used as a
light source.
Page 18
UNIT 1 - DIGITAL INPUT
PROJECTS
1.
Simulate a computerized security system that protects two doors and two windows in a building.
Use switches for three of the openings and the photo transistor for the fourth. Make sure your
program can tell the security guard which of the windows or doors has been opened.
2.
Design a program and configure the hardware to count the number of times the light beam is
broken. The running total should be displayed on the screen. Use the photo transistor and the
infrared diode. You don't have to make it large enough for people, just simulate it on the bread
board.
Page 19
UNIT 2: DIGITAL OUTPUT
In this unit you will investigate how a microcomputer can be programmed and configured to control external
devices. You will use the Advanced Interface Board which, as you have previously seen, has two 8 bit I/O
PORTS. As configured, only four I/O lines per port are connected to the ribbon cable and are available at the
16 pin DIP header plug. Although either or both ports can be used to control external devices, you will use
I/O Port B for the activities.
The 8255 PPI chip, which contains the I/O Ports, can be programmed to place +5 volts, on any or all of the
I/O lines. However, it cannot supply enough current to drive any of the devices you will be using.
Think of the analogy of trying to start a car with 9 flash light cells connected in series. The potential, 12
volts, is the same as a car battery but the cells lack the large current capacity (really the energy) that is
required to turn over the engine. Therefore, in most of the activities, you will use the I/O lines to control
some other device which can deliver the current necessary from the computer's power supply or a battery to
run LEDs, motors, lights, etc.
The 7406 TTL chip is a hex inverter buffer/driver. That's quite a mouthful, so we had better explain what all
this means. The 7406 integrated circuit belongs to one of the oldest group of chips, the 7400 series. If you
look inside any computer, you will see chips from this family. TTL means Transistor Transistor Logic. Hex
means there are 6 separate devices in the chip. A buffer/driver is a device that isolates one device form
another and provides enough current to drive the circuit. In this case the I/O line is buffered from a light or
motor. An inverter is any device that inverts the input. That is, if the input to any of the six inverters is at a
high level, ie. 5 volts, the output will be low, ie 0 volts. The reverse is also true. A low input causes the
output to go high. A diagram for this chip follows.
Page 20
UNIT 2 - DIGITAL OUTPUT
ACTIVITY 5
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
330 S resistor, ¼ watt
Red LED
7406 hex inverter
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #2 of the AIB board. Connect the other
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer.
NOTE: The red LED has a flat on the circular base. This is closest to the cathode, which is connected to
ground. If it is reversed it will not operate. The 7406 chip has a dot that identifies pin #1. Make sure it is in
the right place on the bread board.
Activity 5: Bread board setup
When your setup has been
checked, turn
on the
Activity 5: Schematic diagram
Page 21
UNIT 2 - DIGITAL OUTPUT
computer. Note: the LED should light. If it does not, recheck your circuit. Run Quick Basic and enter the
edit screen. Type in the following basic program:
OUT 819, 136
OUT 817, 0
Run the program. The LED should go out.
PROGRAM COMMENTS
This very short program tests to see if your interface is correctly wired. The first out statement places the
decimal number 136 in the Data Direction Register (also called the Control Register). Check the table in
Activity 1. This value causes I/O PORTS A and B to be used as OUTPUTS. The second line places a 0
(00000000 binary) in PORT A, which causes the LED to go out.
If your circuit is working, type in the following programs. Each is an example of different techniques to
control an external device, in this case the LED.
OUT 819, 136
X = 300
Y = 300
DO WHILE INKEY$ <> CHR$(13)
OUT 817, 1
FOR J = 1 TO X : NEXT J
OUT 817, 0
FOR J = 1 TO Y : NEXT J
LOOP
Run this program. You should now have a miniature lighthouse.
Note: The line OUT 817,1 (00000001 binary) places a 1 on line 0 (PB0) of I/O PORT B. This causes the
7406 chip to turn the LED on.
What is the function of the FOR...NEXT loops? What will happen if you make changes to the values of the
variables X and Y? Change them and see. If you know how, try replacing X and Y with random numbers.
The next two programs use keyboard keys to turn the LED on and off. Which one is better?
OUT 819, 136
CLS
PRINT "Press the A key to turn light on"
PRINT "Press the B key to turn light off"
Page 22
UNIT 2 - DIGITAL OUTPUT
PRINT
PRINT "Press the ENTER key to end"
DO WHILE INKEY$ <> CHR$(13)
IF INKEY$ = "A" THEN
OUT 817, 1
ELSEIF INKEY$ = "B" THEN
OUT 817, 0
END IF
LOOP
Did your program work. If it didn't, you may not have the cap lock on! The program is looking for a
capital A and B. Try modifying for lower case or investigate the UCASE$ function.
OUT 819, 136
CLS
PRINT "Press F1 to turn light on"
PRINT "Press F2 to turn light off"
PRINT
PRINT "Press the ENTER key to end"
DO WHILE INKEY$ <> CHR$(13)
ON KEY(1) GOSUB LIGHTON
ON KEY(2) GOSUB LIGHTOFF
KEY(1) ON
KEY(2) ON
LOOP
END
LIGHTON:
OUT 817, 1
RETURN
LIGHTOFF:
OUT 817, 0
RETURN
The KEY statement allows access to the keyboard keys. Did you leave out the colon (:) in the subroutine
labels? You can try other keys as well as combinations of keys eg. CTRL O. See page 235 to 239 in the
"Programming in Basic" section of the Quick Basic manual.
Page 23
UNIT 2 - DIGITAL OUTPUT
ACTIVITY 6
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
2 -330 S resistor, ¼ watt
2 - Red LED
7406 hex inverter
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #2 of the AIB board. Connect the other
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer.
NOTE: The red LED has a flat on the circular base. This is closest to the cathode, which is connected to
ground. If it is reversed it will not operate. The 7406 chip has a dot that identifies pin #1. Make sure it is in
the right place on the bread board. This is a similar activity to #5 except you will be using 2 LEDS. Take
note of how the I/O PORT is accessed to control the second LED.
Page 24
UNIT 2 - DIGITAL OUTPUT
Activity 6: Bread board setup
Activity 6: Schematic diagram
When your
set-up has been checked, turn on the computer. Note: Both LEDs should light. If they do not, recheck your
circuit. Run Quick Basic and enter the edit screen. Type in the following basic program. (Note: If you have
saved the second program from Activity 5, you can open it and make a few changes.)
OUT 819, 136
X = 300
Y = 300
DO WHILE INKEY$ <> CHR$(13)
OUT 817, 1
FOR J = 1 TO X : NEXT J
OUT 817, 0
FOR J = 1 TO Y : NEXT J
Page 25
UNIT 2 - DIGITAL OUTPUT
OUT 817, 4
FOR J = 1 TO X : NEXT J
OUT 817, 0
FOR J = 1 TO Y : NEXT J
LOOP
PROGRAM COMMENTS
The line, OUT 817, 4 is really the only difference in this activity. The decimal number 4 is 00000100
binary. This number forces the third I/O line of Port B (PB2) to go to a high level and turns on the second
LED.
YOUR TURN
1.
Using a red, a green and a yellow LED as well as the Quick Basic program, simulate the operation
of a traffic light. Make sure the LEDs are on in the correct sequence and for the approximate times.
You might want to use input statements to enter the sizes of the timing loops.
Because you are increasing the number of parts on the bread board, be extra careful that all
connections are correct and that you have used the appropriate current limiting resistors with the
LEDs. Have someone double check the circuit before applying power.
2.
Page 26
Many traffic lights have a blinking arrow for a left turn which comes on before the light turns
green. Add a second green LED to the first circuit and simulate this light. Make it rapidly blink as
in the real thing. Again, observe the precautions listed above before turning on the computer.
UNIT 2 - DIGITAL OUTPUT
Up to now you have been using the computer and interface card to operate a low current device, the LED.
With the addition of a few parts you can control devices that require larger current such as motors,
incandescent light bulbs, heaters, electromagnets.
In activity #5 you were introduced to the 7406 hex inverter. One of its functions was to switch enough
current to operate the LED. The basic building block that comprises the 7406 and in fact all electronic
devices is the transistor. You have already used a photo transistor (activity #4 ) as a switch which is
activated by light. A common transistor is activated by a small current. In the next few activities you will
use a power transistor to control the current to several external devices. The following is a diagram of a
silicon, NPN power transistor, the TIP29A, made by Texas Instruments.
The transistor has 3
terminals which as
you can see are
labelled base,
collector and emitter.
Transistors come in a
variety of packages
both large and small.
This package is called a TO220. The metal tab with the hole is electrically connected to the collector. This
tab can be bolted to a piece of finned aluminum called a heat sink which carries away heat and allows the
transistor to conduct more current without destroying itself. Be careful when using transistors as they can
burn out very quickly if you incorrectly connect them in a circuit. (ie. If you short the collector and emitter
together when connect to a power supply.) The TIP 29A can conduct 1 ampere of current continuously.
Let's look at how this transistor is configured in a simple circuit. we will begin by looking at a simple circuit
consisting of a small motor, the kind that powers small toys, a switch and a couple of flashlight cells. If the
Page 27
UNIT 2 - DIGITAL OUTPUT
switch is closed,
current will flow
and the motor will
turn.
Now let's replace
the simple switch with the TIP 29A power transistor.
This is not the full
circuit as there is
no connection to
control the
transistor which in
turn controls the current to the motor. The motor would be off if this circuit were connected. Note that the
emitter is connected to the negative terminal of the battery and the motor is connected to the collector.
This is the typical configuration for an NPN transistor being used as a simple switch. The next diagram
shows the connections to the I/O Port needed to switch the transistor.
Page 28
UNIT 2 - DIGITAL OUTPUT
In the previous diagram, the transistor's base was not connected. Here the base is connected to one of the I/O
Lines. The emitter is connected to the computer's ground pin to complete the control circuit. The transistor
is switched on when a positive potential (+5 volts) is placed on the base. This is done by placing a 1 on the
appropriate place of the I/O Port register which forces the corresponding line to go high.
The next set of activities will make use of the current switching property of the transistor.
ACTIVITY 7
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
NPN Power transistor, TIP 29A
small DC motor
Battery holder
2 C cells
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #2 of the AIB board. Connect the other
Page 29
UNIT 2 - DIGITAL OUTPUT
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer.
Activity 7: Bread board setup
If the motor
runs when
you turn on
the
computer,
immediatel
y turn it off
and check
Page 30
Activity 7: Schematic diagram
UNIT 2 - DIGITAL OUTPUT
your connections.
You will use a simple program to test your circuit. If you have saved your programs from previous
activities, you can load and modify the third program from activity 5. If you haven't, type in and run the
following:
OUT 819, 136
CLS
PRINT "Press F1 to turn motor on"
PRINT "Press F2 to turn motor off"
PRINT
PRINT "Press the ENTER key to end"
DO WHILE INKEY$ <> CHR$(13)
ON KEY(1) GOSUB MOTORON
ON KEY(2) GOSUB MOTOROFF
KEY(1) ON
KEY(2) ON
LOOP
END
MOTORON:
OUT 817, 1
RETURN
MOTOROFF:
OUT 817, 0
RETURN
When you have tested the motor control, press the enter key.
The comments for the programs in activity 5 apply to this activity. You have used a different device to
control the current to the external device, but the programming of the I/O Port is the same.
Page 31
UNIT 2 - DIGITAL OUTPUT
You have just used a simple circuit to switch current from an external power supply, in this case, a battery.
The transistor used there is limited to about 1 ampere of current and a potential of 100 volts DC. What if you
needed to control a more powerful device such as an electric heater, a furnace or a large AC motor? The
device most widely used to switch high current is called an electromagnetic relay. It is also the oldest
of the devices you will connect to the computer, dating back to the last century. In fact the first electro
mechanical computer, invented in the 1930's, used the relay as its active component. You may have already
studied the operation of this device in a general science or physics course. We will summarize its operation
briefly for those who haven't. Consider the following illustration:
When current
flows through
the
electromagnet
, the moving
arm is
attracted and
the contact points close. The contact points make up a switch which can be used to control an external
circuit. When the current stops, the return spring opens the contact points and therefore breaks whatever
external circuit to which the relay is connected. Most modern relays are sealed, many in an inert
environment of nitrogen gas. You will use a small breadboard mounted relay to simulate the control of large
devices.
You will also be using another device called an opto isolator. In activity
#4 you set up a photo transistor and an LED to form a control device with which the computer could sense
the blocking and unblocking of a light beam. An opto isolator consists of both these components placed in a
6 pin, DIP package which looks very much like an integrated circuit chip. What is the opto islolator used
for? It has several functions. The name opto isolator translates to optically isolated. This means that you
can electrically isolate one part of a circuit from another. The control signals from one part of the circuit are
coupled to another part optically, that is by light. (This device is also called an opto coupler.) This is a safety
feature, in that, if you connect something incorrectly in the circuit, the computer itself is isolated from the
mistake and will not be damaged. It also isolates the computer from any electrical "noise" that may be
generated in the external circuit. This noise can cause false signals to disrupt the computer's operation.
There is another reason for your use of the opto coupler in the next activity. The 7406 chip can only handle
30 milliamperes (.030 ampere) of current. If you are going to use a relay whose electromagnet (called a
Page 32
UNIT 2 - DIGITAL OUTPUT
coil) requires more than this current, then you must use some other device to supply it. You could use a
transistor as you did in the last activity, but for the other reasons already given, you will use the isolator. The
NPN photo transistor in the device can safely handle 150 ma (milliamperes) which is more than adequate for
the relay. The relay will be used to control a small incandescent light but you can also connect a motor, etc.
The next illustration shows the internal parts of the opto isolator. Note which pins connect to the LED and
the photo transistor.
Now, lets put all this
together in the next
activity.
Page 33
UNIT 2 - DIGITAL OUTPUT
ACTIVITY 8
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
330 S resistor, ¼ watt
7406 hex inverter
Opto isolator (NTE 3044)
Relay (5v) Omron #G2VN-234P-US
3 volt lamp
Battery holder, 2 C cells
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect the DIP plug to the bread board and plug into Port #2 of the AIB board. Connect the other
components according to the next illustration. Again, have your teacher check the circuit before you turn on
the computer. This circuit is the most detailed to date. Work slowly and double check each connection.
Activity 8: Bread board setup
Page 34
UNIT 2 - DIGITAL OUTPUT
Activity 8: Schematic diagram
You
can
use the same program as in the previous activity to control the light. Modify it slightly so that it will display
"Print F1 to turn light on" etc. Try replacing the light with the motor. When the you activate the circuit
listen for a faint clicking sound as the points of the relay open and close.
Questions
1.
Trace the operation of the external circuit from the I/O line to the light.
2.
Why is part of the schematic diagram shaded?
3.
If you were to use this circuit to control a large motor, for example a conveyer belt in a factory,
what would you have to change in the circuit shown above? Explain why.
Page 35
UNIT 2 - DIGITAL OUTPUT
PUTTING IT ALL TOGETHER
For the last activity you will combine what you have learned in Unit 1, Digital Input and this unit, Digital
Output. One of the I/O ports, Port A will become an input port and Port B, will be the output port.
You are going to construct an interface circuit that uses the photo sensor to turn on a motor. You can think
of this as a simulation of an automatic door opener, a control to start a conveyer belt when a the beam is
broken or maybe a security device that raises the drawbridge to prevent a thief from leaving the castle. (or
whatever!!)
ACTIVITY 9
Materials:
Advanced Interface Board (installed)
Microcomputer
2 - Ribbon cable with connectors
Bread board
R1 - 1k S resistor, ¼ watt
R2 - 330 S resistor, ¼ watt
7406 hex inverter
NPN transistor TIP29A
NPN Photo transistor
Infrared diode
Motor - 3 volt
Battery holder, 2 C cells
Jumper wires
Make sure the computer is turned off before you make any connections.
Connect one of the DIP plugs into the left side of the bread board (see next diagram) and plug into Port #1
of the AIB board. Plug the other DIP plug into the right side of the bread board and plug into Port #2 of
the AIB board. Connect the other components according to the diagram. Again, have your teacher check
the circuit before you turn on the computer.
Page 36
UNIT 2 - DIGITAL OUTPUT
Activity 9: Bread board setup
Activity 9: Schematic diagram
Type the
following program.
Page 37
UNIT 2 - DIGITAL OUTPUT
OUT 819, 152
OUT 817, 0
CLS
PRINT SPC(10)"Press enter key to exit program"
DO UNTIL INKEY$ = CHR$(13)
N = INP (816)
IF (N AND 1) = 0 THEN GOSUB TURNON
END IF
LOOP
END
TURNON:
PRINT SPC(10) "Press the spacebar to turn motor off"
OUT 817, 1
DO UNTIL INKEY$ = CHR$(32) : LOOP
OUT 817, 0
RETURN
PROGRAM COMMENTS
OUT 819, 152
This line places 152 in the data direction register. If you check the table
in activity #1, 152 sets I/O PORT A as an input port and I/O PORT B as
an output port.
Note also that both I/O addresses 816 and 817 are used which correspond to the two ports.
If your circuit is connected properly (and your program is working) the motor should start when you break
the infrared beam. If the motor starts without the beam being interrupted, the LED may not be squarely
facing the photo transistor.
Page 38
UNIT 2 - DIGITAL OUTPUT
YOUR TURN
1.
Modify the program so that the motor is on only when the beam is blocked.
2.
Make a modification to do the opposite, that is the motor is only off if the beam is blocked.
3.
Modify the program so that the motor turns on only after the beam is blocked twice in a row. That
is the motor remains off after the first pass through the beam and activates the second time the beam
is blocked. (You may have to install time delays to allow the computer to count the number of
times the beam is blocked.
4.
CHALLENGE: Can you think of a way to make the motor run in reverse as well as forward.
Please show your circuit ideas to your teacher before you proceed.
Page 39
UNIT 2 - DIGITAL OUTPUT
ACTIVITY 10 (OPTIONAL)
Materials:
Advanced Interface Board (installed)
Microcomputer
Ribbon cable with connectors
Bread board
R1 - 100 k S resistor, ¼ watt
R2 - 50 S resistor, 10 watt
7406 hex inverter
SAA1042 Stepper motor driver
Stepper Motor (Bipolar) 12 v
12 v DC power supply (eg. 2 - 6v lantern batteries in series)
Jumper wires
BACKGROUND:
In previous activities you used a small DC motor which was configured to run under computer control. This
type of motor usually runs at a set, high speed for a given voltage. The stepper motor is different in that it
can be controlled to turn in small increments or steps either forward or in reverse. This kind of motor forms
the basis of the very accurate drive components found in printers, disk drives, computer controlled industrial
machines, robots and aircraft control. The bipolar stepper motor has four electromagnets or coils and one
permanent magnet that is connected to the motor shaft. The permanent magnet can move in a clockwise or
counter clockwise steps when the electro magnets are turned on in order. Diagram. 1 shows part of the
sequence.
The stepper motor can be connected to the computer through many types of interface circuits. The simplest
configuration, however, is to use a special integrated circuit (ic) that is designed for this purpose. The
SAA1042 found in your kit, can be used to drive a small 12 V (or less) with very few external parts added.
This chip can control the 4 electromagnets or coils in the motor. The computer is used to send pulses to pin 7
of this chip. Each pulse causes the stepper motor to make one step. The faster the pulses arrive, the faster
the motor steps. The computer can generate a pulse by turning on an I/O Line and then quickly turning it
off. By placing a low 0 (0 V) or a high 1 (5 V) on pin 10, the motor can be made to turn clockwise or
counter clockwise. Diagram 2 is an illustration of what happens in the circuit. A 7406 hex inverter is used to
make sure the on-off pulses are "clean" and cause no confusion for the SAA1042 chip.
Page 40
UNIT 2 - DIGITAL OUTPUT
Make sure the computer is turned off before you make any connections.
Connect the DIP plug into the left side of the bread board (see next diagram) and plug into Port #2 of the
AIB board. Connect the other components according to the diagram. Again, have your teacher check the
circuit.
The two breadboard illustrations that follow are for the same setup. The first diagram does not show the
motor hookup. The second diagram is an enlarged drawing of the right side of the bread board.
Page 41
UNIT 2 - DIGITAL OUTPUT
Activity 10: Bread board setup
Page 42
UNIT 2 - DIGITAL OUTPUT
Type the following program.
OUT 819, 152
CLS
PRINT SPC (10) "Press ENTER to stop program"
PRINT
PRINT SPC (10) "Press F1 to run motor forward"
PRINT
PRINT SPC (10) "Press F2 to run motor backwards"
DO WHILE INKEY$ <> CHR$(13)
ON KEY(1) GOSUB FORWARD
ON KEY(2) GOSUB BACKWARD
KEY(1) ON
KEY(2) ON
OUT 817, X
OUT 817, Y
FOR J = 1 TO 200 : NEXT J
LOOP
END
FORWARD:
Page 43
UNIT 2 - DIGITAL OUTPUT
X=1
Y=0
RETURN
BACKWARD:
X=3
Y=2
RETURN
Run the program and verify that the motor is turning. If any components on the bread board get very hot,
check the circuit diagram.
PROGRAM COMMENTS
OUT 817, X & OUT 817, Y
When X is 1 and Y is 0, I/O Line 0 (PB0) will be rapidly turned
on and off producing a pulse to drive the stepper motor.
Because 1 = 00000001 binary and of course 0 = 00000000, I/O
Line 1 (PB1) is kept low (ie. 0). This line is connected to the
forward/reverse pin of the stepper motor controller chip and
therefore the motor turns in the forward direction only.
However when X = 3 and Y = 2 the motor will reverse.
Consider 3 = 00000011 binary which means both PB0 and
PB1 are on. 2 = 00000010 binary, which means that PB0 is off
(this produces the pulse) but PB1 is still on, so the motor turns
in the opposite direction.
FOR J = 1 TO 200 : NEXT J
This is a simple timing loop that inserts a delay between one
pulse and the next. If the pulses are sent too fast to the stepper
motor, it will not keep pace and will stall. The value 200 is
arbitrary and can be changed.
YOUR TURN
1.
2.
Try changing the value 200 in the timing loop to run the motor slower or faster. What is the
smallest value that will still run the motor?
What would the program look like if you use PB2 and PB3 instead of PB0 and PB1 to control the
stepper motor controller chip.?
Try this modified program to control the motor speed as well.
OUT 819, 152
CLS
T = 200
PRINT SPC (10) "Press ENTER to stop program"
PRINT
PRINT SPC (10) "Press F1 to run motor forward"
PRINT
PRINT SPC (10) "Press F2 to run motor backwards"
Page 44
UNIT 2 - DIGITAL OUTPUT
PRINT
PRINT SPC (10) "Press F3 to run motor faster"
PRINT
PRINT SPC (10) "Press F4 to run motor slower"
DO WHILE INKEY$ <> CHR$(13)
ON KEY(1) GOSUB FORWARD
ON KEY(2) GOSUB BACKWARD
ON KEY(3) GOSUB FASTER
ON KEY(4) GOSUB SLOWER
KEY(1) ON
KEY(2) ON
KEY(3) ON
KEY(4) ON
OUT 817, X
OUT 817, Y
FOR J = 1 TO T : NEXT J
LOOP
END
FORWARD:
X=1
Y=0
RETURN
BACKWARD:
X=3
Y=2
RETURN
FASTER:
IF T > 10 THEN
T = T - 20
END IF
RETURN
SLOWER:
T = T + 20
RETURN
YOUR TURN
1.
Explain why the FASTER: subroutine has an IF clause.
2.
Modify your program so that some indicator of speed is printed on the screen.
Page 45
UNIT 2 - DIGITAL OUTPUT
3.
Create a program that will allow the user to input the number of steps to turn the motor in either
direction. The motor should then turn the required number of steps and stop.
4.
Modify the program in #3 to allow the user to input the turn angle in degrees. Hint: How many
steps does your stepper motor need to make a full turn 3600. (Dividing 3600 by the number of steps
gives the resolution of your motor in degrees. That is the finest step it is capable of. This is one
specification you need to know if you are buying a motor for a particular application.
PROJECTS
The stepper motor is an excellent start point for project work. Robot arms, smart conveyer belts, positioning
tables, sun trackers, and a variety of other devices can be controlled. See your teacher for help in identifying
some applications.
Page 46
Download