Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.
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.
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:
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.
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.
The circuit diagram is shown below:
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:
These steps are shown in a video at the end of the tutorial.
1. Start Atmel Studio.
2. Click New Project… from the Start Page window, or click File → New → Project...
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.
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.
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.
If the program build succeeded, the program can be loaded to the AVR – described next.
You can help the Starting Electronics website by making a donation:
Any donation is much appreciated and used to pay the running costs of this website. Click the button below to make a donation.
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.
3. In the dialog box, make sure that Tool is set to AVRISP mkII and Device is set to ATtiny2313 – click the Apply button.
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.
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.
If the programming was successful, the LED connected to the circuit will start flashing on and off.
You can help the Starting Electronics website by making a donation:
Any donation is much appreciated and used to pay the running costs of this website. Click the button below to make a donation.
This video shows the above steps being performed:
Can't see the video? View on YouTube →
You may also be interested in the ATtiny2313 tutorial series.