soggybag wrote:
It appears the attiny13 has one port PORTB. PORTB connects to each of the 6 pins PB0 - PB5.
DDRB is the Data Direction Register. Setting this sets the port and the pins as outputs or inputs. My main confusion is how to set the each invidual pin as an input or output?
The register is represented as a 6 bit binary word, for example "001001" Each bit position in the word corresponds to a pin, and the 0 and 1 values correspond to the pin being set to input (0), or output (1).
The least significant bit in the word is pin 0.
soggybag wrote:
I was looking at a block of code:
Code: Select all
DDRB |= ( 1 << 2 ); // Set LED1 as output, pin #?
DDRB |= ( 1 << 0 ); // Set LED2 as output, pin #?
I added "pin #?", I was wondering which pins this would apply to.
That code is startlingly opaque if you aren't familiar with bit shift operators (which I am not - I've been using this as a learning exercise too).
<< is the left shift operator.
Quote: "A simple way to think of the left-shift operator is that it multiplies the left operand by 2 raised to the right operand power."
So.. 1 << 2 == 4
4 is the same as 000100 .. which would set PB2 to be an output.
1<<0 is 1, which would affect PB0
In both these cases, the |= operator is doing a bitwise OR between the current contents of DDRB and the argument on the right. So if DDRB started out containing 000000, it would become 000100 and then 000101 through these two operations.