3.b. Programming AVRs Using AVR Studio 5

3.b.1. Adding Devices to AVR Studio 5
3.b.2. Using Advanced Features of AVR Studio 5

This tutorial covers AVR Studio 5. However, at the time of this writing, all of our tutorials and example projects for the Pololu AVR C/C++ Library require AVR Studio 4. Therefore, if you have an Orangutan or 3pi Robot, or wish to use the Pololu AVR C/C++ Library for some other reason, you probably should use AVR Studio 4 and see Section 3.c.

The following tutorial covers the steps needed to program AVRs in Windows using AVR Studio 5 and a Pololu USB AVR Programmer. AVR Studio 5 is a free integrated development environment (IDE) provided by Atmel. In this tutorial, we will write a simple program to blink an LED connected to pin PD1 of an AVR. On any of the Orangutan robot controllers and the 3pi Robot, this program will blink the red user LED. If you want to program an AVR that does not have an LED connected to pin PD1, the code in this tutorial can be modified.

You will need to:

  • Download and install AVR Studio 5 by following the instructions on Atmel’s website.
  • Install the Pololu USB AVR Programmer’s drivers on your computer. See Section 3.a for instructions.
  • Upgrade your programmer’s firmware to version 1.06 or later. See Section 9 for instructions.
  • Add the appropriate XML file to AVR Studio 5 to get it to support the AVR you wish to program, if necessary. See Section 3.b.1 for instructions.

After you have completed these prerequisites, you can create a new AVR Studio 5 project:

  1. Open AVR Studio 5 and click New Project. In the New Project dialog, select C Executable Project for the template. Enter the project name and location. In this tutorial, we will name our project “BlinkLED” and put it in the “C:\” directory, but you can choose a different name and location if you would like. Uncheck the Create directory for solution box to simplify the directory structure of your project. Click OK.

    The New Project dialog of AVR Studio 5.
  2. In the Device Selection window, select the device name of your specific AVR. For an Orangutan or 3pi Robot, this will either be ATmega48, ATmega168, ATmega328P, ATmega324PA, ATmega644P, or ATmega1284P depending on which chip your Orangutan or 3pi Robot has. Click OK to create the project.

    The Device Selection dialog of AVR Studio 5.
  3. Remove the template code that was automatically placed in BlinkLED.c and replace it with the code below:

    // F_CPU tells util/delay.h our clock frequency
    #define F_CPU 20000000UL	// Orangutan and 3pi frequency (20MHz)
    #include <avr/io.h>
    #include <util/delay.h>
    
    void delayms( uint16_t millis ) {
        while ( millis ) {
            _delay_ms( 1 );
            millis--;
        }
    }
    
    int main( void ) {
        DDRD |= 1 << DDD1;               // set LED pin PD1 to output
        while ( 1 ) {
            PORTD &= ~( 1 << PORTD1 );   // drive PD1 low
            delayms( 900 );              // delay 900 ms
            PORTD |= 1 << PORTD1;        // drive PD1 high
            delayms( 100 );              // delay 100 ms
        }
    }

    Note: You will probably want to customize this program slightly if the clock frequency of your AVR is not 20 MHz. F_CPU should be defined as the clock frequency of your AVR in units of Hz. If you do not make this change, the timing of delayms() will be off, but the LED will still blink.

  4. Change the active configuration from Debug to Release, as shown below. One of the effects of this is that AVR Studio will use the -Os compiler option, which optimizes the compiled program for size. The functions in util/delay.h require this optimization to be enabled.

    Selecting the Release configuration in AVR Studio 5.
  5. Click the Build Solution button on the toolbar (or press F7) to compile the code.

    Building a project with AVR Studio 5.

  6. Make sure your USB AVR programmer is connected to your computer via its USB A to mini-B cable and then select Add STK500… from the Tools menu. Select the COM port that has been assigned to the programmer’s programming port, and click Apply. If you are not sure which COM port to select, look in the Device Manager under the “Ports (COM & LPT)” list.. This step can be skipped if you have done it before.

    The Add STK500 dialog box in AVR Studio 5.
  7. Click the AVR Programming button on the toolbar. You can also select AVR Programming from the Tools menu.


  8. This will bring up the AVR Programming dialog. For the Tool, select the STK500 which you added earlier. Select the same device you selected earlier. If your device is not in the list, you will need to add it to the list by following the instructions in Section 3.b.1. For the Interface, select ISP. Click Apply.

    Selecting a programmer, device, and interface in the AVR Programming dialog of AVR Studio 5.

    If you got an error that says “Unable to connect to tool STK500” and you see an error message in the Output pane in the main window that says “The signature of the attached tool is AVRISP_2, which is unexpected.” then you need to upgrade your programmer’s firmware to version 1.06 or later (see Section 9). If you get a different error, see Troubleshooting (Section 8) for help identifying and fixing the problem.

    If AVR Studio brings up a dialog box instructing you to upgrade (or downgrade) your programmer’s firmware, you can click OK to ignore the message and continue. To prevent this dialog from appearing in the future, use the Configuration Utility (Section 3.e) to change the programmer’s hardware and software version numbers.

  9. If you have not done so already, connect the programmer to the target device using the 6-pin ISP cable. Make sure the cable is oriented so that pin 1 on the connector lines up with pin 1 on your target device, and that the target device is powered on. You can test the connection by clicking the Read button next to the Device ID box. This sends a command to the target AVR asking for its device ID (also known as the signature). If everything works correctly, you should see three numbers in hex notation appear in the Device ID box. If you get a “Device signature mismatch” error, you probably have the wrong device selected. If reading the signature fails entirely, please see Troubleshooting (Section 8) for help getting your connection working.

    Reading the device ID (also known as the signature) of an AVR in AVR Studio 5.
  10. Now it is time to program your target device. Select the Memories section on the left. In the Flash box, enter the path to the HEX file that was generated when you built your program. You can browse for this using the “…” button to the right of the text box. If you navigate to your project’s folder, you should find it as “Release\<project name>.hex”. Click the Program button in the Flash box.

    The Memories section of the AVR Programming dialog in AVR Studio 5.

    As your USB AVR Programmer programs the AVR, you should see all three LEDs flicker and you should see the following text appear at the bottom of the window:

    Erasing device... OK
    Programming Flash...OK
    Verifying Flash...OK

If there were no problems, the LED connected to PD1 of your AVR should now be flashing! Note that if you are trying this on a 3pi robot and you have not yet soldered in the optional through-hole LEDs, the flashing LED will be on the bottom of the robot. If there was a problem, please see Troubleshooting (Section 8) for help identifying and fixing it.

Productivity tip: The AVR Programming dialog in AVR Studio 5 is modal, which means you must close it after you are done programming in order to go back to editing your source code. Then when you are done editing the source code and want to program again, it takes 4 clicks to open up the dialog and initiate the programming process. To get around this, you can run a second copy of AVR Studio 5 and launch the AVR Programming dialog from there. This allows you to leave the AVR Programming dialog open all the time and initiate programming with a single click.