Suppose a number of IO port registers should get the value 0xff assigned. Conveniently, it is implemented like this:

DDRB = DDRD = 0xff;

According to the rules of the C language, this causes 0xff to be assigned to DDRD, then DDRD is read back, and the value is assigned to DDRB. The compiler stands no chance to optimize the readback away, as an IO port register is declared "volatile". Thus, chaining that kind of IO port assignments would better be avoided, using explicit assignments instead:

DDRB = 0xff;
DDRD = 0xff;

Even worse ist this, e. g. on an ATmega1281:


The same happens as outlined above. However, when reading back register DDRG, this register only implements 6 out of the 8 bits, so the two topmost (unimplemented) bits read back as 0! Consequently, all remaining DDRx registers get assigned the value 0x3f, which does not match the intention of the developer in any way.

Back to FAQ Index.