- The ATmega32 has four I/O ports:
- PORTA — This port is also hard-wired to the A/D converter subsystem.
- PORTB — Typically used for output.
- PORTD — Pins 0 and 1 (PD0 and PD1) are hard-wired for serial communication on the SunRom boards.
- 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:
- PORT? — address where the output port is located in memory.
- PIN? — address where the input port is located in memory.
- All ports are configured as input ports by default.
- 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
- We can configure all pins of PORTB for output by doing this:
ldi r16, 0xff
out DDRB, r16
- We can send values to the port using the OUT instruction:
out PORTB, r17 ; sends value of r17 to PORTB
- We set up a port to be an input port like this:
ldi r16, 0x00
out DDRC, r16
- We can send values to the port using the IN instruction:
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.
Mixed I/O and Masking
- 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
Pull-up Resistors for Input Ports
- 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:
out DDRA, r16 ; Configure PORTA as input on all bits
out PORTA, r16 ; Enable pull-up resistors for all input bits on PORTA
Tutorials for Other Courses
This is a mirror to be used when http://myweb.msoe.edu/taylor/, (EDU instead of US) is not available.