My First AVR Circuit II: Source Code

All that is left from the Part I of this tutorial is the source code. Here it goes, a simple code with well explained comments:

// Define the internal oscillator to run at 1MHz0100000
 #define F_CPU 1000000UL
 // Include some helpful libraries.
 #include <avr/io.h>
 #include <util/delay.h>
 int main(void)
 {
 DDRB = 0xFF;    // PB0-PB7 output
 DDRD = 0xFF;    // PD0-PD6 output
 PORTB = 0x00;    // Set all PortB pins low
 PORTD = 0x00;    // Set all PortB pins low
 _delay_ms(1000);    // Wait 1 second
 while(1)
 {
 PORTB = 0x40;    // Same as 01000000. That means that we light up led 6 from port b.
 _delay_ms(500);
 PORTD = 0x40;
 _delay_ms(1000);
 PORTB = 0x00;    // Turn off all leds.
 _delay_ms(500);
 PORTD = 0x00;
 _delay_ms(1000);
 }
 return 0;
 }

You can build the code using the instructions posted on this blog (Windows, Linux). If you prefer, click here for the already compiled hex file.

Here’s how it should like after you turn it on:
[youtube http://www.youtube.com/watch?v=lzX-eIkcMW4]

This entry was posted in AVR, Electronics, Tutorial and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.