Project

General

Profile

General Port Usage

All AVR's have I/O pins grouped into ports. To control pins, you must read and write values to the following registers:
DDRx
PORTx
PINx
Note: Registers are labelled by a letter, usually A, B, C, or D. Replace the x in the register name with the port, eg. DDRx for port A is DDRA.

First, set the pins as inputs or outputs in the DDRx register. 0 is input, 1 is output.
ex: Set pins PA0, PA3, and PA7 as outputs:
DDRA = 0b10001001;
or using macros in AVR Studio:
DDRA = (1<<PA7)|(1<<PA3)|(1<<PA0);

To set an pin designated as output to high or low, set the PORTx register. 0 is LOW, 1 is high.
ex: continuing from above, set PA0 and PA3 to high, PA7 to low:
PORTA = 0b00001001;
A better method:
PORTA |= (1<<PA0)|(1<<PA3);
PORTA &= ~(1<<PA7);

Setting an input pin to high may enable a pullup resistor on the input on certain AVR's, such as the Atmega328.

To read an input pin, read the PINx register.
ex: If PA2 was designated as input in DDRA, then the state of PA2 is:
uint8_t PA2_state = (PINA>>PA2)&0x1; /* PA2 is bit 2 in PINA, and with 1 to get only state of PA2 */

Note: Other port functions may override or require certain port behavior.