The mystery is revealed if we Read The Friendly Manual (Atmega328P datasheet):
Here we can see that the bit masks are officially named DDB7
and so on from Atmel. (Arguably it would make more sense to name them DDRB7
but they didn't do that.) Always use the same bit mask names as stated in the manual. As we can see both DDB7
and PORTB7
will always be 0x80.
The reason why we use bit names is to avoid magic numbers. DDRB |= 0x80u;
is less readable, as is DDRB |= (1u << 7);
.
DDRB |= (1u << PORTB7);
would just be confusing.
Although in practice you probably wouldn't be using these bit mask names directly, but those #defined
for the hardware peripheral. In production code we might write:
DDR_SPI = (1u << DD_MOSI) |
(0u << DD_MISO) |
(1u << DD_SCK) ;
Where (0u << DD_MISO)
is 0
and is therefore only there as self-documenting code, stating that you intend for MISO to be an input.
As for what "DDR" stands for, on "Motorola-flavoured" MCUs like AVR, it means Data Direction Register. Motorola always named GPIO port registers as PORTX
/DDRX
and AVR picked it up from there.
DDRB
means the whole register,DDB0
means Bit 0 of registerDDRB
\$\endgroup\$