5.2. AVR programming using Microchip Studio

5.2.1. Faster programming with F5 in Microchip Studio

If you have an Orangutan or 3pi Robot or wish to use the Pololu AVR C/C++ Library for some other reason, we recommend following the Pololu AVR Programming Quick Start Guide instead of this tutorial.

The following tutorial covers the steps needed to program AVRs in Windows using Microchip Studio (formerly known as Atmel Studio) and a Pololu USB AVR Programmer v2.x. Microchip Studio is a free integrated development environment (IDE) provided by Microchip for Microsoft Windows systems.

You will need to:

  • Download and install Microchip Studio by following the instructions on Microchip’s website. This tutorial was written for Microchip Studio 7.0.
  • Install the programmer’s drivers on your computer. See Section 4.1 for instructions.

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

  1. Open Microchip Studio and click New Project. In the New Project dialog, select GCC 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 Atmel Studio 6.

  1. In the Device Selection window, select the device name of your specific AVR. Click OK to create the project.

The Device Selection dialog of Atmel Studio 6.

  1. Remove the template code that was automatically placed in BlinkLED.c and replace it with the code below:
#define F_CPU 20000000    // AVR clock frequency in Hz, used by util/delay.h
#include <avr/io.h>
#include <util/delay.h>

int main() {
	DDRD |= (1<<1);        // set LED pin PD1 to output
	while (1) {
		PORTD |= (1<<1);   // drive PD1 high
		_delay_ms(100);    // delay 100 ms
		PORTD &= ~(1<<1);  // drive PD1 low
		_delay_ms(900);    // delay 900 ms
	}
}

Note: The value of F_CPU should be the clock frequency of your AVR in units of Hz, so if your AVR is not running at 20 MHz you will need to change that line. If you do not make this change, the timing of _delay_ms() will be off, but the LED will still blink.

  1. Click the Build Solution button on the toolbar (or press Ctrl+Shift+B) to compile the code.

Building a project with Atmel Studio 6.

  1. Make sure your programmer is connected to your computer via USB and then select Add target… from the Tools menu. Select STK500 as the tool. 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, run the Pololu USB AVR Programmer v2 Configuration Utility and look in the upper left corner or see Section 4.5, which has more information about determining serial port names. This step can be skipped if you have done it before.

The “Add target” dialog box in Atmel Studio 6.

  1. In the Tools menu, select Device Programming.
  2. This will bring up the Device Programming dialog. For the Tool, select the STK500 that you added earlier. Select the same device you selected earlier. If your device is not available in the list, we recommend upgrading to Microchip Studio 7.0 or later. For the Interface, select ISP. Click Apply.

Selecting a programmer, device, and interface in the Device Programming dialog of Atmel Studio 6.

If you get an error when you click “Apply”, then it means that Microchip Studio is having trouble communicating with the programmer. You should try unplugging the programmer from everything and then plug it back in. You should also make sure you have selected the right COM port. If that does not help, see the Windows USB driver troubleshooting advice in Section 4.1.

  1. 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. If your device does not have an ISP header, see Section 5.1 for more information about connecting the programmer. You can test the connection by clicking the Read button next to the Device Signature box. This sends a command to the target AVR asking for its signature. If everything works correctly, you should see a number in hex notation appear in the Device Signature box. If you get an error about the signature being wrong, you might have selected the wrong device. If you get a warning that says “Read voltage … is outside selected device’s operating range” then double check to make sure that your device is powered. For more help getting your connection working, see Section 5.6.

Reading the device signature of an AVR in Atmel Studio 6.

  1. Now it is time to program your target device. Select the Memories section on the left. The Flash box should contain the path to the ELF file that was generated when you built your program. If it does not, 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 “Debug\<project name>.elf”. Click the Program button in the Flash box.

The Memories section of the Device Programming dialog in Atmel Studio 6.

As your programmer programs the AVR, you should see all four 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 Section 5.6 for help identifying and fixing it.

Related Products

Pololu USB AVR Programmer v2
Pololu USB AVR Programmer v2.1
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors