Friday, March 19, 2010

AVR Digital I/O Registers

Each port pin consists of three register bits: DDxn, PORTxn, PINxn.
DDxn bits are accessed at the DDRx I/O address.
PORTxn bits are at the PORTx I/O address.
PINxn bits at the PIN I/O address.

Note:
Each instance of 'x' should be replaced with the port identification letter (A,B,C,D, ect.)
Each instance of 'n' should be replaced with a pin number (0..7)

DDRx is the Data Direction Register. This register sets the data direction of individual pins of port x. A logic level of '1' sets a port pin as output. A logic level of '0' sets a port pin as input.

ex:
DDRB=0b00000001 // Set direction of port B pin 0 as output, all others input
DDRD=0b00100010 // Set direction of port D pin 5 and pin 1 as output, all others input

PORTx is the Data Register. If you have set any pins on the port as output using the DDRx register you can now control them with this register. A logic level of '1' makes a port pin high. A log level of '0' makes a port pin low.

ex:
PORTB=0b00000100 // Make port B pin 2 high, all others low
PORTA=0b00000101 // Make port A pin 2 and pin 0 high, all others low

PINx is the Input Pin Address Register. If you have set any pins on the port as input using the DDRx register you can now read the status of them with this register.

ex: Assuming a switch is interfaced to Port A Pin 0 with a pull up resistor...
if (PINA & 0b00000001) {
    // Switch interfaced to Port A Pin 0 is open
}
else {
    // Switch interfaced to Port A Pin 0 is closed
}

No comments:

Post a Comment