Program AVR Microcontroller with USBTinyISP and Eclipse IDE

In this post I will detail out how we can program an AVR microcontroller, specifically ATTiny13a, with USBTinyISP and Eclipse IDE in Linux environment. I am using Ubuntu 16.04 LTS. I hope it will work in other distributions with little or no modifications.

ATTiny13a SSU mounted on adapter plate
ATTiny13a SSU mounted on adapter plate

Components Required

  1. (Obviously) an AVR Microcontroller
  2. USBTinyISP
  3. 6 jumper wires to connect the programmer with microcontroller
  4. USB cable to connect the programmer to computer

Setting Up IDE and Toolchain

We need following software to be installed on our system.

  1. AVR GCC toolchain
  2. avrdude
  3. Eclipse

AVR GCC Toolchain

You can download AVR GCC toolchain from Microchip site http://www.microchip.com/avr-support/avr-and-arm-toolchains-(c-compilers). I downloaded AVR 8-bit Toolchain 3.5.4 – Linux 64-bit. Extract it to a convenient location in your computer.

avrdude

In Ubuntu, we can install avrdude program from package manager.

sudo apt-get install avrdude

Or you can download source from http://download.savannah.gnu.org/releases/avrdude/ and build it yourself.

Eclipse IDE

Download and install latest Eclipse IDE. You can download Eclipse IDE for C/C++ Developers from Eclipse Download page.

Open Eclipse. Open Help -> Eclipse Marketplace…. In search tab enter “AVR” and hit Go button. You will get AVR Eclipse Plugin. Install it.

Configure AVR Paths

Go to Window -> Preferences to open Preferences window. Select AVR -> Paths. Now configure AVR-GCC path. You need to provide path for bin directory of AVR-GCC toolchain. Please see the image below to see how I configured it.

Eclipse preferences to configure AVR paths
Eclipse preferences to configure AVR paths

Now hit Apply button to save the changes.

GNU Make and AVRDude paths will be auto-detected. Otherwise you may need to manually configure them as needed. It is not necessary to configure AVR Header Files and Atmel Part Description Files paths. However it will not allow to go to other sections in Preferences window without filling AVR Header Files path, in that case you can just close Preferences window.

Configure AVRDude

Go to Window -> Preferences to open Preferences window. Select AVR -> AVRDude. Click on Add… button to open Edit AVRDude Programmer Configuration New Configuration window.

Fill Configuration name and Description fields similar how I shown in image below. Select USBtiny simple USB programmer, http://www.ladyada.net/make/usbtinyisp/ within Programmer hardware (-c) list in left side.

Eclipse preferences to configure AVRDude
Eclipse preferences to configure AVRDude

Click OK button then Apply and Close button to save the changes.

(More about AVR Plugin path management can be read at http://avr-eclipse.sourceforge.net/user%20manual/concepts/avr_path_management.html)

Configure udev rules for USBTinyISP

Enter following command in terminal:

sudo gedit /etc/udev/rules.d/99-USBtiny

Paste following code and save the file.

SUBSYSTEM=="usb", ATTR{idVendor}=="1781", ATTR{idProduct}=="0c9f", GROUP="adm", MODE="0666"

Now restart udev service to make it effective:

sudo service udev restart

Create Simple Program to Blink LED in ATTiny13a

Within Project Explorer, click right button of mouse and select New -> Project… to open New Project window. Select C Project under C/C++. Click Next.

Fill project name as “Blink”. Then Select Empty Project under AVR Cross Target Application as shown in image below.

Select AVR project type
Select AVR project type

Uncheck Debug option since we are not discussing debugging here. Click Next.

Select project build configurations
Select project build configurations

Select ATtiny13 as MCU Type and leave 1000000 as MCU Frequency (Hz).

Choose target hardware
Choose target hardware

I selected ATtiny13 instead of ATtiny13a because AVRDude was complaining it as not a valid part. Need to figure out to make AVRDude to accept ATtiny13a.

I chose 1000000 (1MHz) frequency because I am using ATtiny13a without any external clock. You would need to choose appropriate value if you are using external clock.

Now create new C source file called main.c and paste the program code given below. I used it to blink the LED connected at Port B pin 4.

#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>

int main(void)
{
    const int msecsDelayPost = 1000;

    // Set up Port B pin 4 mode to output
    DDRB = 1<<DDB4;

    // Set up Port B data to be all low
    PORTB = 0;

    while (1) {
        // Toggle Port B pin 4 output state
        PORTB ^= 1<<PB4;

        // Pause a little while
        _delay_ms (msecsDelayPost);
    }

    return 0;
}

Now build the project. You should see something similar given below in Console window if everything is configured correctly.

17:42:07 **** Build of configuration Release for project Blink ****
make all
Building file: ../main.c
Invoking: AVR Compiler
avr-gcc -Wall -Os -fpack-struct -fshort-enums -ffunction-sections -fdata-sections -std=gnu99 -funsigned-char -funsigned-bitfields -mmcu=attiny13 -DF_CPU=1000000UL -MMD -MP -MF"main.d" -MT"main.o" -c -o "main.o" "../main.c"
Finished building: ../main.c

Building target: Blink.elf
Invoking: AVR C Linker
avr-gcc -Wl,-Map,Blink.map -mmcu=attiny13 -o "Blink.elf"  ./main.o   
Finished building target: Blink.elf

Invoking: AVR Create Extended Listing
avr-objdump -h -S Blink.elf  >"Blink.lss"
Finished building: Blink.lss

Create Flash image (ihex format)
avr-objcopy -R .eeprom -R .fuse -R .lock -R .signature -O ihex Blink.elf  "Blink.hex"
Finished building: Blink.hex

Create eeprom image (ihex format)
avr-objcopy -j .eeprom --no-change-warnings --change-section-lma .eeprom=0 -O ihex Blink.elf  "Blink.eep"
Finished building: Blink.eep

Invoking: Print Size
avr-size --format=avr --mcu=attiny13 Blink.elf
AVR Memory Usage
----------------
Device: attiny13

Program:      72 bytes (7.0% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


Finished building: sizedummy


17:42:07 Build Finished (took 166ms)

Configure Project to use USBTinyISP

Right click on project within Project Explorer to open Properties for Blink. Select AVR -> AVRDude. Under Programmer tab select USBTinyISP as Programmer configuration. Hit Apply and Close button.

Select programmer to upload project to microcontroller
Select programmer to upload project to the microcontroller

Upload Program to Microcontroller

Connect USBTinyISP with the microcontroller. I am using ATTiny13a 8 pin package with pin configuration as shown below.

ATTiny13a 8 pin package pin configuration
ATTiny13a 8 pin package pin configuration

Connecting USBTinyISP with AVR microcontroller is straightforward.

USBTinyISPATTiny13a
GND → GND (pin 4)
VCC → VCC (pin 8)
RESET → RESET (pin 1)
SCK → SCK (pin 7)
MISO → MISO (pin 6)
MOSI → MOSI (pin 5)

Now click menu item AVR -> Upload Project to Target Device to flash the program to MCU. If everything went good then you should see output like given below.

Launching /usr/bin/avrdude -pt13 -cusbtiny -Uflash:w:Blink.hex:a
Output:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9007 (probably t13)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "Blink.hex"
avrdude: input file Blink.hex auto detected as Intel Hex
avrdude: writing flash (72 bytes):

Writing | ################################################## | 100% 0.16s

avrdude: 72 bytes of flash written
avrdude: verifying flash memory against Blink.hex:
avrdude: load data flash data from input file Blink.hex:
avrdude: input file Blink.hex auto detected as Intel Hex
avrdude: input file Blink.hex contains 72 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.36s

avrdude: verifying ...
avrdude: 72 bytes of flash verified

avrdude done.  Thank you.

avrdude finished

Hope it helps!


Comments

2 responses to “Program AVR Microcontroller with USBTinyISP and Eclipse IDE”

  1. Jesus G. Palomo Avatar
    Jesus G. Palomo

    Excellent post! Very complete! Actually I try to find information about to how can is possible to configure the fuse bits with eclipse. Could you have some info about it? Kinds regards!

    1. Junaid P V Avatar
      Junaid P V

      I have to set up Eclipse again and try it. I had reinstalled my Ubuntu.
      BTW, I see we have “Fuses” tab in AVRDude configuration for the project. Please have look on it.

Leave a Reply

Your email address will not be published. Required fields are marked *