Page 1 of 1

Ateml AVR GPIO operations quick guide

Posted: Fri Apr 13, 2012 12:05 am
by Shane
All port B pins as output (Direction register)

Code: Select all

DDRB = 0xff;
All port pins set to 1 (ON)

Code: Select all

PORTB = 0xff;
Set port D pin 2 as output

Code: Select all

DDRB |= 1 << DDB2;

Set port D pin 2 as input

Code: Select all

DDRB &= ~(1 << DDB2);

Set port B pin 2 to 1 (ON)

Code: Select all

PORTB |= _BV(PB2);
OR

Code: Select all

PORTB |= 1 << 2;
OR

Code: Select all

PORTB |= 1 << PINB2;

Set port B pin 2 to 0 (OFF)

Code: Select all

PORTB &= ~_BV(PB2);
OR

Code: Select all

PORTB &= ~(1 << 2);
OR

Code: Select all

PORTB &= ~1 << PINB2;
Check if port D pin 3 equal to 1 (ON)

Code: Select all

if (PIND & (1<<PIND3))
{
...
}


Check if port D pin 3 equal to 0 (OFF

Code: Select all

if (!(PIND & (1<<PIND3)))
{
...
}