I got a code example which says like this:
DDRA = (1<<DDA7) | (1<<DDA6) | (1<<DDA5) | (1<<DDA4) |
(1<<DDA3) | (1<<DDA2) | (1<<DDA1) | (1<<DDA0);
What does that code means? What is the function of DDA
? And is "<<” a bitwise ?
I got a code example which says like this:
DDRA = (1<<DDA7) | (1<<DDA6) | (1<<DDA5) | (1<<DDA4) |
(1<<DDA3) | (1<<DDA2) | (1<<DDA1) | (1<<DDA0);
What does that code means? What is the function of DDA
? And is "<<” a bitwise ?
The constants
DDA0
etc. refer to bit positions in the DDRA register. This register determines which of the eight pins on port A are input and which are outputs. A1
in a bit position means output pin, and a0
(default) means it's an input pin.with
DDA0
defined as zero means, "put a 1 into this register after bitwise-shifting it zero positions to the left." So yes, bitwise operators. In other words, make the least significant bit a 1. The others shift a one various positions to the left, making them 2,4,8, etc.The net result of this statement is that all bit positions are ORed together, resulting in a value of 11111111 or 0xFF, which means that the entire PORTA port is set to be output.
It's worth noting that this entire statement could be simplified by simply using:
Even though this is shorter, there is no code size penalty because the compiler collapses the long expression on the right to 0xFF anyway. You'll see both styles of programming ports as you explore more.
From the ATmega documentation: