Parallel Input/Output 1

advertisement
Parallel Input/Output
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
1
Parallel Input/Output Ports
•
A HCS12 device may have from 48
to 144 pins arranged in 3 to 12 I/O
Ports
– An I/O pin can be configured for
input or output
– An I/O pin usually serves multiple
functions. When it is not used as a
peripheral function, it can be used
as a general-purpose I/O pin.
From MC9S12C Family
Reference Manual
(document
ReferenceManualS12C
PUV2rev4)
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
2
Addressing I/O Ports
• When inputting or outputting to/from an I/O port, you read from or write
to the port data register
• Each I/O port register is assigned to an address in the HCS12 memory
space. For example, Port T data register is assigned to address $240:
movb
#$35,$240
; output a $35 to Port T
• A name is also assigned to each register so that we can access a register by
referring to its name:
PTT
equ
movb
$240
#$35,PTT
; this is in the “include” file
; output a $35 to Port T
• Names and addresses are defined in the “mc9s12c32.inc” file that you
include your program using the “include” statement
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
3
I/O Ports in C
• Port names are also pre-defined for C programs,
so you can just do
PTT = 0x35;
// output 35 (hex) to port T
• Definitions are in the include file “mc9s12c32.h”
• Note
– In C , port access is done with pointers, since you
can’t just do
0x240 = 0x35;
// this is invalid
– See notes on “Advanced C” for how this is done
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
4
Bi-Directional Ports
• A data direction register (DDR) for each port, controls the
direction of each port pin
•
If the DDR bit
is a 0, the pin
is input
•
Reading the
data bit (e.g.,
D0) from the
port gets the
current value
of the input
pin
•
0
1
You can write
a data bit,
but it won’t
come out to
the pin
Microcomputer Architecture and Interfacing
This circuit implements
one bit of Port T
Colorado School of Mines
Professor William Hoff
5
Bi-Directional Ports
• A data direction register (DDR) for each port, controls the
direction of each port pin
•
If the DDR bit
is a 1, the pin
is output
•
Writing the
data bit (D0)
stores the bit
into the latch
•
1
0
You can still
read out the
value of the
bit in the
latch
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
6
General purpose digital IO Ports in the
MC9S12C32
*Pins
0 and 1 are hardwired to other things
on the SSMI board (don’t use them)
Port name
# pins
Pin names
Notes
T
8
PT0..PT7
Also used for timer functions
PAD0
8 6*
PAD00..PAD07 Also used for A/D conversion
M
6
PM0..PM5
Also used for communications interfaces
(CANbus, SPI)
E
2
PE0,PE1
Also used for interrupt inputs
S
2
PS0,PS1
Also used for serial communications (so
we can’t use these unless we don’t need
communications to the PC)
• All port pins can be configured separately for digital input or output by writing
a 0 or 1 to the associated “data direction register”
• Example:
DDRT = 0xf0;
// configure bits 0:3 of Port T for
// .. input, bits 4:7 for output
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
7
(Side note on using Port PAD for digital I/O)
• Port PAD is normally used for A/D
conversion
• However, you can also use it for digital
I/O, by reading and writing to port
PTAD
• To set up:
– In register ATDDIEN, set to 1’s the bits
corresponding to the pins you want to use
as digital I/O
– Then, as with the other ports, set the data
direction register (DDRAD, in this case) to 0
for input and 1 for output
– Avoid PAD0 and PAD1 (they are hardwired
to other components on the SSMI board)
Microcomputer Architecture and Interfacing
Colorado School of Mines
This is described in detail in
the reference manual
(MC9S12C128V1.pdf) on
pages 101-102 and 242-243.
Professor William Hoff
8
Example
• Continuously read switches S0..S3,
and light LEDs L0..L3 if
corresponding input signal is high
PT7
PT6
PT5
PT4
PT3
PT2
PT1
PT0
L3
L2
L1
L0
S3
S2
S1
S0
main()
{
char c;
DDRT = 0xf0;
// configure bits 0:3 for input, bits 4:7 for output
while (1) {
c = PTT;
// get data from Port T
// We want to get bits 0:3 up into places 4:7.
c = c * 16; // Multiply by 16 to shift left 4 places.
PTT = c;
// Output to Port T bits 4:7 (doesn’t affect bits
// 0:3 since they are configured as input)
}
}
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
9
Example: Seven Segment Display
470 Ω each
PT6
PT5
PT4
PT3
PT2
PT1
PT0
• We can use 7 output pins (e.g.,
PT0:PT6) to drive the LEDs of a 7segment display
Digit
abcdefg
a
b
c
d
e
f
g
0
0x7E
1
1
1
1
1
1
0
1
0x30
0
1
1
0
0
0
0
2
0x6D
1
1
0
1
1
0
1
3
0x79
1
1
1
1
0
0
1
4
0x33
0
1
1
0
0
1
1
5
0x5B
1
0
1
1
0
1
1
6
0x5F
1
0
1
1
1
1
1
7
0x70
1
1
1
0
0
0
0
8
0x7F
1
1
1
1
1
1
1
9
0x7B
1
1
1
1
0
1
1
Microcomputer Architecture and Interfacing
Colorado School of Mines
a
b f
c
a
g
b
d
e e
c
f
d
g
common cathode
•
Professor William Hoff
What would be the code
to display the hex digit
“A”?
10
Example: Pushbutton switch
• You must use a pull-up or pull-down resistor, so that
when the switch is open, you are reading a valid voltage
logic level (as opposed to “floating”).
Pull-up
Pull-down
VDD
VDD
R
PT0
PT0
R
When the switch is pushed,
is pin PT0 is high or low?
Microcomputer Architecture and Interfacing
When the switch is pushed,
is pin PT0 is high or low?
Colorado School of Mines
Professor William Hoff
11
Example: DIP switches
• A set of pull-up resistors are needed to pull the voltage to high on one side
V
of the DIP
CC
SW DIP-8
10 KΩ
HCS12
“DIP” = dual-in-line
package
PA0
PA1
PA2
PA3
PA4
PA5
PA6
PA7
Figure 4.19 Connecting a set of eight DIP switches to Port A of the HCS 12
• To read data from switches
DDRA = 0x00;
n = PTA;
// configure port A for input
// read port A
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
Note: if you make a
mistake in this
program, you can
physically damage the
MCU! How?
12
Testing a single bit
• Sometimes you just want to test a single bit of an input Port
(e.g., Port T) and don’t care about the rest of the bits
• We will cover this more thoroughly in the next lecture, but
you will need this for Lab 3
• Example: To test if PT7 is inputting a 1 (and ignore all the
other bits of Port T):
if (PTT & 0x80 != 0)
// PT7 is a 1
:
} else {
// PT7 is a 0
:
}
Microcomputer Architecture and Interfacing
{
Colorado School of Mines
This does a bit-wise AND
of PTT and the mask
0x80. The result is zero in
every bit position except
bit 7 (which is the just the
original value of PT7)
Professor William Hoff
13
Example: Passive motion sensor
• Uses a pyroelectric sensor
which can detect levels of
infrared radiation
TR257-1
– Everything (including people)
emits some low level infrared
radiation
– The hotter something is, the
more radiation is emitted
• The sensor is actually split in
two halves
– If a person walks by, the relative
amount of radiation seen by the
two halves changes, and the
output triggers
Microcomputer Architecture and Interfacing
From: “30 Years of Passive Infrared Motion Detectors - a
Technology Review,” by Hans J. Keller, KUBE Electronics Ltd,
http://www.kube.ch/downloads/pdf/kube_irs2paper.pdf
Colorado School of Mines
Professor William Hoff
14
Example: Digital motion sensor (continued)
•
A Fresnel lens which splits up the field of
view into a set of discrete directions, or
“zones”
•
The sensor has three pins: (1) +5V power,
(2) a digital output signal, and (3) ground
When the sensor detects a moving heat
source, its output pin is pulled low for a
short period of time (~2-3 seconds)
•
•
•
Vcc
MCU
The output pin is “open collector”, so when
it is not being pulled low, it is floating
Therefore, you must use a pullup resistor
so that when the output is not being pulled
low, it is pulled up to a valid logic high
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
15
Example: Break beam sensor
• A “break beam” sensor can be constructed using a light source (e.g.,
an infrared LED) and a photodetector (e.g., a PIN diode)
– The light normally shines on the detector, which senses it
– If something passes between the source and detector, it “breaks” the
beam and the detector can sense that the light is no longer there
LED
• Problem: the detector is also sensitive to ambient light
• One solution:
– Modulate (turn on and off) the light source at a specific frequency
– Use a bandpass circuit on the detector that only passes a signal with that
frequency
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
16
Example: Break beam sensor (continued)
• TSOP 4838 IR Receiver Module
• Sensitive to IR light at 950 nm, modulated at
38 kHz
• Can be used to transmit data and do remote
control applications (e.g., turn on a TV)
from datasheet for TSOP 4838, http://www.vishay.com
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
17
Summary / Questions
• Most digital I/O pins can be either input or output.
How do you configure them for input or output?
• How many general purpose digital IO pins does our
MC9S12C32 chip have?
• When driving an LED from a digital output pin, why
should you use a resistor in series with the LED?
Microcomputer Architecture and Interfacing
Colorado School of Mines
Professor William Hoff
18
Download