110 likes | 402 Views
I/O Ports. 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:
E N D
I/O Ports CS-280 Dr. Mark L. Hornick
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
Review: Data Memory Map 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 0x20 offsetbetween I/O and Data addressesfor 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, R20is equivalent to: STS 0x38, R20; alternate: STS PORTB+0x20, R20 CS-280 Dr. Mark L. Hornick
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
Writing to a Port • The OUT instruction is used to write a value from a Register to a port • OUT PORTB, r25writes 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
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
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
Configuring I/O Portx for digital input • 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 • 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 • 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 CS-280 Dr. Mark L. Hornick
Read from a PINx, not PORTx! • The IN instruction is used to read a value from the input pinson a port configured for input: • IN R0, PINx ; read input pinsreads the value of the PORTxINput 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