AVR PORT, PIN and DDR

Register bit settings and resulting function of port pins

register bits →
pin function↓
DDRx.n PORTx.n PINx.n
tri stated input 0 0 read data bit
x = PINx.n;
pull-up input 0 1 read data bit
x = PINx.n;
output 1 write data bit
PORTx.n = x;
n/a

Bit operations: &, |, ^, >>, <<, ~

Operator Description Example Res.
& bit AND 11110000 & 0b00110001; 00110000
| bit OR 01000001 | 0b00010000; 01010001
^ bit EXLUSIVE OR (XOR) 00001001 ^ 00010000; 00000001
~ bit NOT(<one’s complement) ~11110000; 00001111
<< bit LEFT SHIFT 00000001 << 1;
00000001 << 2;
00000111 << 1;
00001110 << 2;
00000010;
00000100;
00001110;
00011100;
>> bit RIGHT SHIFT 00010000 >>1;
00010000 >>2;
11100111 >>1;
11100010 >>2;
00001000;
00000100;
01110011;
00111000;

 Bit mask

Bit number  Binary representation  Sintax  Hex value
0 0b00000001  1 0×01
1 0b00000010  (1<<1) 0×02
2 0b00000100  (1<<2) 0×04
3 0b00001000  (1<<3) 0×08
4 0b00010000  (1<<4) 0×10
5 0b00100000  (1<<5) 0×20
6 0b01000000  (1<<6) 0×40
7 0b10000000  (1<<7) 0×80

 Examples in C (GCC)

Setting a single bit (without changing any other bits)

Clearing a single bit (without changing any other bits)

Toogle a single bit (without changing any other bits)

Checking single bit (without changing any bits)

Note: This can also be used for variables, not only for IO registers.

Updated: August 18, 2014 — 6:37 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

OpenHardware.Ro © 2017 Frontier Theme