I/O Ports

  • The ATmega32 has four I/O ports:
    • PORTA
    • PORTB
    • PORTC
    • PORTD
  • Each port is 8 bits wide.
  • Each port has a data direction register associated with it (DDR? where ? is AD).
  • 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 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

Output

  • We set up a port to be an output port like 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

Input

  • 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

Enabling 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:
       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
cs280/ioports.txt · Last modified: 2009/06/03 11:22 (external edit)
 

This website is not owned or managed by the Milwaukee School of Engineering.

© 2003-2010 Dr. Christopher C. Taylor, et. al. • Office: L-343 • Phone: 277-7339 • npǝ˙ǝosɯ@ɹolʎɐʇ • -> RSS <-