First thing first, lets clean up this code.
1. You declared the variable "i" twice and once in the while loop. Both are bad form.
2. There is no point in assigning a value to "i" as the for loop will do that for you.
3. The "0b00000000" format is really not well recognized and may not work in all situations. I know it looks easier as a beginner to read but really these assignments should be done in hex. For now you can get away with it but I recommend learning hex. Your computer's OS calculator should have a conversion mode for this. If not grab a free one off the net somewhere. It will make life easier.
4. Indent the blocks and try to make your brackets line up. It makes the code easier to read which when things get more complicated will help you out when you are scanning code quickly. Good coding habits start now when you are first learning. Later on it is harder to break bad habits.
5. Lots of comments! Especially if you want other people to read your code.
#include <avr/io.h>
#include <util/delay.h>
uint8_t i; // Counter varible
int main( void )
{
DDRB = 0x08; // Set PB4 (Pin 3) as output
PORTB = 0x00;
while( 1 )
{
for( i = 0; i < 10; i++ ) // Delay loop for 300ms
{
_delay_ms( 30 ); // max is 262.14 ms / F_CPU in MHz
}
PORTB ^= 1 << 4; // toggle the LED
}
return 0;
}
DDRB = 0b00001000;
Sets the direction of the port and pins. In this case this line seems to set PORTB pin 2 as an output. I'm not quite clear on the syntax of this. How do you set this to configure various pins as inputs or outputs?
That line actually sets PB4 (physical pin 3) as an output. If you want physical pin 2 you want PB3 so that is:
0b00000100;
or better yet in hex
0x04;
You will need to change the toggle line too to:
PORTB ^= 1 << 3;
or you can make life easier and do this:
PORTB ^= 1 << PB3;
Did you read the "Digital Input and Output - Pivotal Digital" article at my site ? I cover this in that article. Also the datasheet explains alot too.
BTW if you are using a newer version of GCC the "_delay_ms" no longer has the 262 restriction. You can drop 300 in and lose the loop altogether. Check the documentation that came with your GCC version. Its on your computer as it comes as part of the GCC installation.
Andrew