Starting AVR 8-bit Development

Created on: 7 September 2012

This tutorial looks at the tools needed to start development (C programming) on 8-bit AVR microcontrollers and shows how to write a C program and load it to an AVR microcontroller.

Development is done using the free Atmel Studio running on a Windows platform. A simple flashing LED circuit will be built using an AVR microcontroller.

Hardware Requirements

A USB programmer is required in order to load a program into an AVR microcontroller. The AVRISP mkII (ATAVRISP2) from Microchip (formerly from Atmel) is used in this tutorial. There are AVRISP 2 or AVRISP mkII compatible USB programmers available.

An ATtiny2313 microcontroller breadboard circuit will be used as the test circuit. This microcontroller will be powered by 5V – the 5V from a PC power supply will be used.

An LED and resistor will be required to build the circuit. Single strand, single core wire will be needed to connect the microcontroller to the AVR programmer.

Tutorial parts list:

  • Electronic breadboard
  • Single core single stranded wire
  • 5V power supply
  • AVRISP mkII programmer from Atmel
  • ATtiny2313 microcontroller, DIP (ATtiny2313-20PU)
  • 10k resistor, 1/4W, 5%
  • 470 ohm resistor, 1/4W, 5%
  • LED, through hole mounting
  • 100n capacitor

Software Requirements

Download Microchip Studio from the Microchip website, or find an older version of Atmel Studio in the Microchip archives. Atmel Studio 6.0 is used in the tutorial and loaded onto Windows 7.

Install the Software and AVRISP Programmer

After downloading Atmel Studio, install it by running the downloaded executable file. After software installation, plug the AVRISP mkII device into a spare USB port on the PC. The drivers for the AVRISP mkII should automatically be installed.

Building the Circuit

The circuit diagram is shown below:

ATtiny2313 blink circuit diagram
ATtiny2313 Blink Circuit Diagram - click for a bigger image

Build the circuit on a breadboard and use the connections from the ISP header in the circuit diagram to connect the AVRISP mkII to the ATtiny2313 using single strand wire.

This photo shows the result:

ATtiny2313 breadboard circuit
ATtiny2313 Breadboard Circuit - click for a bigger image

Using Atmel Studio to Write the Program

1. Start Atmel Studio.

2. Click New Project… from the Start Page window, or click File → New → Project...

Starting a new project in Atmel Studio
Starting a New Project in Atmel Studio - click for a bigger image

3. Choose the location to save the project to and choose a project name. The project for this tutorial will be named attiny2313_blink. Be sure to fill this name into the Name field and not the Solution name field.

Saving the project in Atmel Studio
Saving the Project in Atmel Studio - click for a bigger image

4. In the next dialog box that pops up, select the device. Use the Device Family drop-down box to select tinyAVR, 8-bit and then select the device in the table below – ATtiny2313.

Selecting the device
Selecting the Device - click for a bigger image

After selecting the device, the project will be created and a C file will be opened that contains the main() function.

5. Write the program.

The code is shown below, copy it and paste it into the C file that is open in Atmel Studio. Save the file.

#include <avr/io.h>

void Delay(void);

int main(void)
{
    DDRB |= 1 << PB0;
    while(1)
    {
        PORTB &= ~(1 << PB0);
        Delay();
        PORTB |= 1 << PB0;
        Delay();
    }
}

void Delay(void)
{
    volatile unsigned int del = 16000;
	
    while(del--);
}

6. Build the project by clicking Build → Build attiny2313_blink on the top menu bar.

Building the project
Building the Project - click for a bigger image

If the program build succeeded, the program can be loaded to the AVR – described next.

Programming the Device

1. Make sure that the AVRISP mkII is plugged into the USB port.

2. On the top menu bar, click Tools → Device Programming to open the Device Programming dialog box.

Opening the Device Programming dialog box
Opening the Device Programming Dialog Box - click for a bigger image

3. In the dialog box, make sure that Tool is set to AVRISP mkII and Device is set to ATtiny2313 – click the Apply button.

Device Programming settings
Device Programming Settings - click for a bigger image

4. Test that the programmer can connect to the target.

Switch the power to the target on (the 5V supply to the ATtiny2313). In the Device Programming dialog box, click the Read button under Target Voltage – a voltage should be read and displayed. Click the Read button under Device signature – a device signature should be read and displayed.

Reading Circuit and Device Parameters
Reading Circuit and Device Parameters - click for a bigger image

5. Program the device.

Click Memories in the left pane of the Device Programming dialog box.

Under Flash, open the attiny2313_blink.hex file by clicking the [] button and then navigating to the Debug directory of the project.

Click the Program button to load the program to the ATtiny2313 device.

Programming the AVR device
Programming the AVR Device - click for a bigger image

If the programming was successful, the LED connected to the circuit will start flashing on and off.

You may also be interested in the ATtiny2313 tutorial series.