Hi there,
Same as my comment but as suggested, now posted as a solution (so that this stops showing as an "Unanswered question":
"PORTD OrEquals 1 left shifted the number of times of the value of PORTD1" i.e. PORTD = PORTD | (1<<PORTD1);
X << Y left shifts X, Y times e.g. 1 << 5 lefts shifts 1, 5 times giving 00010000b i.e. 32. Put some examples into Wolfram Alpha and you'll get the idea.
Hope this helps,
Ed
Edit: To clear up the confusion of how this is related to "// drive PD1 high":
Regardless of what micro-controller hardware you are using, the terminology is the same. If you have an output pin e.g. PORTD1, making the output voltage on that pin +ve is called "driving it high" i.e. if you drew a graph of the voltage of that pin, the line would go high. In code, to drive the output voltage high means setting the pin's output to 1. Driving a pin low means making it go to 0 voltage i.e. setting the output to 0. We use the word "drive" because the micro-controller actually forces the pin voltage to that level rather than just allowing the pin to reach that level of it's own accord (not the best explanation but hopefully simple enough).
So the statement:
PORTD |= (1<<PORTD1);
(As pointed out in a comment on my answer) uses a bit mask to turn on the bit which is PORTD1 positions from the left. Since PORTD1 actually refers to the second bit in the PORTD register, it will either be 0 or 1. Thus the statement really equates to either of the following:
PORTD = PORTD | (1<<0);
which will always result in:
PORTD = PORTD | 1
(Noteably: PORTD refers to all the bits in PORTD where as PORTD1 refers to just the first bit. Thus the value of PORTD is: 00000000b if all bits are set to 0 and there are 8 bits in PORTD).
or it results in:
PORTD = PORTD | (1<<1);
. However, since it was the second bit that we tested for in the first place, it will have no effect on the PORTD output. (Since the code basically means: if PORTD1 is 1, set PORTD1 to 1 thus no change.)
It seems like a pretty odd piece of code to be using therefore. So I would suggest the OP replaces it with something a little simpler (unless I have missed something???):
if(PORTD1 == 0)
{
PORTD |= 1;
}
Hope this clears up some of the confusion (or creates some new, interesting questions),
Ed
Improved version of code (now that I have seen the original article):
int main() {
DDRD |= (1<<DDD1); PORTD = 0b00000000; while (1) {
PORTD |= 0b00000010;
_delay_ms(500);
PORTD &= 0b11111101;
_delay_ms(500);
}
}
This could probably be made more compact using "~" operator (the name has slipped my mind) but for simplicity sake I have simply used to bit masks to toggle the second bit of PORTD.
The original code, I think, was trying to be too clever with toggling based on whether PORTD1 was already on or not. It significantly overcomplicated the code...especially for a beginners' tutorial. Or even for practical application really...