Electronic – arduino – AVR. How to configure as output just one pin

arduinoavrc

In AVR C code, if you want to declare some port as ouput, you just have to to do this:

DDRB = 0xFF; 

But how can I do if I want to declare as ouput just one pin. For example, I want to declare has output Arduino's pin 13, which corresponds to PB5.

I know I can declare that in binary format:

DDRB = b11111111; 

But, I don't know which bit position corresponds to PB5.

Thanks in advance, I could not find this answer in Google.

Best Answer

PB5 is the 6th bit.
Just remember that the numbering is zero-based and starts with the least-significant-bit on the right, so the value you're looking for in this case is 0b00100000.
When you're setting this bit, you'll probably want to use a logical OR operation instead of a simple assignment, so:

DDRB |= 0b00100000;  

instead of

DDRB = 0b00100000;  

That way you won't inadvertently clear any of the other bits which might already be set.