Bare Metal Micro

Eclipse for AVR Development

5. Build the Project

Now that we have a project created and configured we are ready to add our source code and build the project.
  1. Select the project in the Project Explorer view.
  2. From the main menu, choose File -> New -> Source File.
  3. Set the Source file to main.c.
  4. Select <None> for the Template.
  5. Press Finish
  6. Add the following code to main.c. We will be using it as a basic "Hello, World!" program.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <avr/io.h>
    #include <util/delay.h>
    
    int main(void)
    {
        // The LED is on Port B, Pin 5
        // Setup LED pin as an output
        DDRB |= _BV(5);
    
        // Loop forever
        while (1)
        {
            // Turn LED on for 500ms
            PORTB |= _BV(5);
            _delay_ms(500);
    
            // Turn LED off for 500ms
            PORTB &= _BV(5) ^ 0xFF;
            _delay_ms(500);
        }
    }
    
  7. From the main menu, choose Project -> Build Project.
  8. Eclipse will build your project and when it is done you should see output similar to this in the CDT Build Console view:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    12:12:01 **** Rebuild of configuration Debug for project Blink ****
    Info: Internal Builder is used for build
    avr-gcc -DF_CPU=16000000UL -Os -g3 -Wall -c -fmessage-length=0 -ffunction-sections -fdata-sections -mmcu=atmega328p -o main.o ../main.c 
    avr-gcc -mmcu=atmega328p -Xlinker -Map=Blink.map -Xlinker --gc-sections -o Blink main.o 
    avr-size Blink 
       text    data     bss     dec     hex filename
        176       0       0     176      b0 Blink
    avr-objcopy -O ihex -R .eeprom Blink Blink.hex 
    
    12:12:02 Build Finished (took 355ms)
    
  9. Before moving on, make sure that there are no errors generated during the build. If you do encounter errors, make sure that the source code is correct, make any necessary changes, and build again.