I/O Ports

advertisement
I/O Ports
CS-280
Dr. Mark L. Hornick
1
Ports are channels from the CPU
to external hardware and software

Atmega32 has:



4 Digital I/O ports,
1 Analog-to-Digital port,
1 Serial port,…

The number of ports depends on processor type

Each port has a specific fixed address in the I/O
address space:


0x00 through 0x3F (0-63)
This address space is separate and distinct from
the Program and Data address spaces!
CS-280
Dr. Mark L. Hornick
2
Review: Data Memory Map
The General-purpose registers,
while not SRAM, are assigned the
first 32 addresses of the
Data Memory address space.
The I/O Registers are assigned
the next 64 addresses in the Data
Memory address space. Note that
SREG, SPL, SPH, PORTB,
DDRB, etc are all I/O registers
Note 0x20 offset
between I/O and
Data addresses
for I/O registers
Note that all I/O Registers thus have two addresses:
one address in the I/O address space and a corresponding
address in the Data Memory address space!
OUT 0x18, R20
; same as OUT PORTB, R20
is equivalent to:
STS 0x38, R20
; alternate: STS PORTB+0x20, R20
CS-280
Dr. Mark L. Hornick
3
Atmega32 has four digital I/O ports

PortA, PortB, PortC, PortD
 each have 8 pins (Px0 – Px7)

Each of these ports is associated with
three special-purpose I/O registers,
each with their own addresses:




DDRx
PORTx
PINx
DDRB, the I/O register (at I/O address
0x17) controls whether the pins on PortB
function as input or output:
 DDRB - Data Direction Register B

Each pin of PortB can be configured
independently via DDRB

Writing 0xff to DDRB configures all pins
of PortB to behave as output CS-280
Dr. Mark L. Hornick
4
Writing to a Port

The OUT instruction is used to write a value from a Register
to a port
 OUT PORTB, r25
writes the value of r25 to the PORTB output register (at I/O
address 0x18)
 Each bit in the 8-bit value corresponds to a hw pin of the port
 Only the pins configured as output will actually be written

Be sure to .INCLUDE “m32def.inc”, (which .DEF’s
PORTB=0x18):

The SBI instruction is used to set a single bit within a port
 SBI PORTB, 0
sets the value of bit 0 to 1

The CBI instruction is used to reset a single bit within a port
 CBI PORTB, 0
sets the value of bit 0 to 0
CS-280
Dr. Mark L. Hornick
5
PortB (0x18) is hard-wired to
the on-board LEDs
Setting a bit (to logical 1) turns
the LED on
SBI PORTB, 0 ;set bit 0


The associated pin must be
configured as an output
Note this is similar to
OUT PORTB, 0x01
But SBI affects only the single bit,
while OUT sets all 8 bits
CS-280
Dr. Mark L. Hornick
6
The digital I/O ports can also be
configured to act as INPUT ports
The I/O register DDRB controls whether
the pins on PortB function as input or
output:

Each pin of PortB can be configured
independently via DDRB

Writing 0x00 to DDRB configures all
pins of PortB to behave as input

Writing 0x0f configures pins 0-3 of
PortB as output and pins 4-7 as input
CS-280
Dr. Mark L. Hornick
7
Configuring I/O Portx for digital input
1.
2.
3.
First, use DDRx to specify which pins of Portx will
function as input and output:
LDI temp, 0x0f
OUT DDRx, temp ;pins 0-3 output, 4-7 input
Next, drive the input pins high (logical 1).
If you forget to do this, the pins will assume random
voltages (e.g. from static electricity) unless an external
voltage is deliberately applied!
LDI temp, 0xf0
OUT PORTx, temp ;drive input pins high
Finally, you can read the current state of the input pins as
the voltages are applied (+5v = logical 1, 0v = logical 0) or
as the pin is grounded
IN temp, PINx ;read state of input pins
CS-280
Dr. Mark L. Hornick
8
Read from a PINx, not PORTx!

The IN instruction is used to read a value
from the input pins on a port configured for
input:


IN R0, PINx
; read input pins
reads the value of the PORTx INput latch into R0
PINx [the input latch associated with Portx] can
only be read, not written

Unlike PORTx and DDRx, which can be both read and
written
CS-280
Dr. Mark L. Hornick
9
Download