The ATmega32 has four I/O ports:
Each port is 8 bits wide.
Each port has a data direction register associated with it (DDR? where ? is A – D).
The data direction register configures each bit of the corresponding port as either an input or output bit.
A zero bit in the DDR makes the corresponding bit on the port act as an input bit.
A one bit in the DDR makes the corresponding bit on the port act as an output bit.
We write to DDR? and PORT? using the OUT instruction.
Each port has two labels:
All ports default to be input ports.
To make all bits of a PORTB be output bits, we can do the following:
ser r16 ; Sets all the bits in r16 to 1 (r16 = 255)
out DDRB, r16
ldi r16, 0xff
out DDRB, r16
out PORTB, r17 ; sends value of r17 to PORTB
ldi r16, 0x00
out DDRC, r16
in PINC, r17 ; sends value of PINC to r17
When using the port as an input port we
must use the
PIN? label instead of the
PORT? label.
Each bit of a port can be configured independently to be an input or output pin.
We can make the most significant four bits of PORTA to be input bits and the least significant four bits of PORTA to be output bits:
ldi r16, 0b00001111
out DDRA, r16
When we send output to PORTA we only really want to send four bits.
We can using masking operations to clear the most significant four bits.
Suppose R16 contains the value we wish to send out to PORTA
andi r16, 0x0f ; masking operation that clears 4 most sign. bits
out PORTA, r16
The ATmega32 has built-in pull-up resistors.
The pull-up resistors make the input pins high (instead of floating) if they are not hooked up to anything.
To enable the pull-up resistors for a port, do the following:
clr r16
out DDRA, r16 ; Configure PORTA as input on all bits
ser r16
out PORTA, r16 ; Enable pull-up resistors for all input bits on PORTA