I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU...

9
I/O Ports CS-280 Dr. Mark L. Hornick 1

Transcript of I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU...

Page 1: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

I/O Ports

CS-280Dr. Mark L. Hornick

1

Page 2: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

2

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!

Page 3: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

Review: Data Memory Map

CS-280Dr. Mark L. Hornick

3

The General-purpose registers,while not SRAM, are assigned thefirst 32 addresses of the Data Memory address space.

The I/O Registers are assigned the next 64 addresses in the DataMemory address space. Note that

SREG, SPL, SPH, PORTB, DDRB, etc are all 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

Note 0x20 offsetbetween I/O andData addressesfor I/O registers

Page 4: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

4

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

Page 5: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

5

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

Page 6: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

6

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 toOUT PORTB, 0x01

But SBI affects only the single bit, while OUT sets all 8 bits

Page 7: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

7

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

Page 8: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

8

Configuring I/O Portx for digital input

1. First, use DDRx to specify which pins of Portx will function as input and output:LDI temp, 0x0fOUT DDRx, temp ;pins 0-3 output, 4-7 input

2. 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, 0xf0OUT PORTx, temp ;drive input pins high

3. 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 groundedIN temp, PINx ;read state of input pins

Page 9: I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.

CS-280Dr. Mark L. Hornick

9

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