Ateml AVR GPIO operations quick guide

Microcontroller Topics
Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

Ateml AVR GPIO operations quick guide

Post by Shane » Fri Apr 13, 2012 12:05 am

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)))
{
...
} 
Post Reply

Return to “Microcontrollers”