Electronics – Tuts, tips and much more …

If we hear, we forget; if we see, we remember; if we do, we understand – Confucius

AVRDude Tutorial : Burning hex files using USBasp and AVRdude

Posted by Omkar on April 15, 2009

AVRDude is excellent program for burning hex code into microcontroller. USBasp is awesome USB based programmer for the AVR. In this tutorial we will see how to use AVRdude for burning hex files into AVR microcontroller using USBasp.

AVRdude is not readily available for windows installation. Windows version of AVRdude is bundled with WinAVR compiler. Before we start : Download WinAVR from here.

Introduction:

In order to program any microcontroller you need the .HEX file. It is nothing but the machine code for the microcontroller. This file is generated by the corresponding assembler software, which converts assembly code into machine code. Assembly can be  produced by third party cross compiler software or can be handwritten.

After you finish the installation of WinAVR, AVRdude executables can be found at :
<WinAVR Installation Directory>\bin

Complete documentation of AVRdude can be found at:
<WinAVR Installation Directory>\doc\avrdude\avrdude.pdf

Connections:

- Connect the USBasp to PC.
- Connect SPI programming pins of USBasp to the AVR microcontroller. Following figure shows sample schematic diagram, if you have different AVR, then connect MOSI,MISO, SCK, RESET and GND pins of that uC to corresponding pins of USBasp.

USBaspandAVRconnections 

- Give +5V supply to the microcontroller.
- VIMP: If you are burning a fresh microcontroller, close the Slow Serial Clock jumper of USBasp. Since many brand new microcontroller are factory programmed for internal 1MHz oscillator. USBasp uses very high speed serial clock for faster programming. Thus you will have to specifically tell USBasp to use slow serial clock. This setting is done by above mentioned jumper.

NOTE: If you have uC which has internal oscillator enabled and after the programming you are not planning to change its fuse bits back to external clock setting, then you can skip the crystal.

 

Executing AVRdude:

Fortunately AVRdude is command line tool, so that you can be very sure of what you are doing with your uC Or Unfortunately AVRdude is command line tool, so you will have to spend little time to get familiar with it :)

- Open the command prompt. (Press WinKey + R. Run dialogbox will appear. Type cmd and press enter.)
fig1-commandPrompt

- Navigate to the directory where .hex file is located. For example :

> cd D:\mega16_kitCD\sample code\ex01_simple_input_output\Exe
> d:

fig2-navigate

- To burn the hex file enter following command. Consider for example name of my hex file is io.hex :

> avrdude –c usbasp –p m16 –u –U flash:w:io.hex

You should see something like this :

fig3-pgmdone

Thats all !!! It is as simple as that ! :)

Explanation for command :

avrdude –c usbasp –p m16 –u –U flash:w:io.hex

-c : Indicates the programmer type. Since we are using the USBasp programmer, argument “usbasp” is mentioned.

-p : Processor. We are using ATmega16, hence “m16”. Note ATmega16 has two variants, one is “ATmega16L” (slow speed version) and “ATmega16” normal 16MHz version. However their device signature is same and hence you will have to use “m16” as parameter for both the AVRs. This applies to all AVRs having “L” variants.

-u : Disables the default behavior of reading out the fuses three times before programming, then verifying at the end of programming that the fuses have not changed. Always use this option. Many times it happens that we forget to switch on the AVR’s +5V power supply, then at the end of programming cycle, avrdude detects inconsistent fuses and tries to reprogram them. Since there is no power supply, fuses gets programmed incorrectly and entire microcontroller gets screwed up(means becomes useless). Thus always use this option.

-U  :  memtype:op:filename[:format]

Perform a memory operation. Multiple ‘-U’ options can be specified in order to operate on multiple memories on the same command-line invocation. 

    memtype

The memtype field specifies the memory type to operate on. 

calibration   One or more bytes of RC oscillator calibration data.
eeprom       The EEPROM of the device.
efuse         The extended fuse byte.
flash          The flash ROM of the device.
fuse           The fuse byte in devices that have only a single fuse byte.
hfuse          The high fuse byte.
lfuse           The low fuse byte.
lock            The lock byte.

    op

The op field specifies what operation to perform:

r        read the specified device memory and write to the specified file
w       read the specified file and write it to the specified device memory
v       read the specified device memory and the specified file and perform a verify operation

    filename

Specify the hex file name. If file is not in current directory specify file name with appropriate path.

    format

Format need not be specified, for hex files, avrdude will automatically detect the format.

The trick to do it quickly : The Batch file :

It is extremely boring to type such a long command every time you program the uC. Therefore to avoid this you can create something called as Batch file. It is a text file which contains series of commands which will be executed by dos command processor. To create batch file follow these steps :

- Open notepad
- Type our avrdude command. i.e. copy paste following line into notepad.
avrdude –c usbasp –p m16 –u –U flash:w:io.hex
- Save the file with filename “burn.bat” and put it into the directory, which has the hex file.

Now whenever you recompile your program and want to burn it, simply double click on burn.bat. This will execute avrdude command that we have typed in it.

Possible errors:


1)couldn’t find usbasp:

found 5 busses
found 5 busses
avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc

This happens when USBasp is not connected or not recognized by the PC. Try to connect it to different USBport. Make sure that “Self programming” jumper of USBasp is open. Always disconnect AVR from USBasp, before plugging USBasp to the PC.

2)target does not answer

found 5 busses

avrdude: error: programm enable: target doesn’t answer. 1
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude done.  Thank you.

Check the connections of USBasp with uC. Check the  power supply of the uC. Check whether you have connected the crystal and decoupling capacitors. If everything is fine and still you are getting this error, then it indicates that either ur uC is dead or its fuse bits have got screwed up.

 

I hope this post will help you to get started with AVRdude. Feedback is welcome.

About these ads

22 Responses to “AVRDude Tutorial : Burning hex files using USBasp and AVRdude”

  1. hitech said

    Hi i am new to this world i am not able to program my atmega 32 for lcd programs please help this is the error

    Linking: main.elf
    avr-gcc -mmcu=atmega32 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=main.o -std=gnu99 -MMD -MP -MF .dep/main.elf.d main.o –output main.elf -Wl,-Map=main.map,–cref -lm
    main.o: In function `main':
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:12: undefined reference to `LCDInit’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:15: undefined reference to `LCDByte’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:18: undefined reference to `LCDWriteString’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:21: undefined reference to `LCDGotoXY’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:21: undefined reference to `LCDWriteString’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:26: undefined reference to `LCDGotoXY’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:26: undefined reference to `LCDWriteInt’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:27: undefined reference to `LCDGotoXY’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:27: undefined reference to `LCDWriteString’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:36: undefined reference to `LCDByte’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:40: undefined reference to `LCDWriteString’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:41: undefined reference to `LCDGotoXY’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:41: undefined reference to `LCDWriteString’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:47: undefined reference to `LCDByte’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:48: undefined reference to `LCDWriteString’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:49: undefined reference to `LCDGotoXY’
    C:\Users\AMIR\Desktop\projects\lcd interfacing/main.c:49: undefined reference to `LCDWriteString’
    make.exe: *** [main.elf] Error 1

    > Process Exit Code: 2
    > Time Taken: 00:01

  2. ganesh said

    Hello sir,

    iam using, atmega16l microcontroller and usbasp programmer
    it give this error
    ‘avrdude’ is not recognized as an internal or external command, operable program or a batch file.
    plz help me out.

  3. Saurabh said

    Hi all,
    I am trying to make USBasp since last 2 month but its not working. Its showing “Unknown device”. I have no issues regarding board and soldering. Everything I got from http://www.fischl.de/usbasp/, downloaded “usbasp.2011-05-28″, but its not working. Please help me regarding this issue. I am really got pissed off now.

    • Omkar said

      Have you programmed your mega8 AVR with the firmware and correct fuse settings ?

    • sahid said

      Hello,

      1. You shold have a correctly worked “programmer”, also a way to flash a chip. If you dont have such one, you should start on the other point. At example
      http://www.pitsch.de/stuff/mmc2iec/avrisp.htm, which works 100% with a LPT-Printer Port (on an old PCs available only) and PonyProg-software (Windows) http://www.lancos.com/prog.html

      2. If you have a programmer, you must be sure, you have installed all the needed software on your PC correctly for using existing “first” programmer

      3. @Saurabh: do you have a functioning programmer? If not, start with 1.

      The http://www.fischl.de/usbasp/ helps you to build a “second” better programmer if you have an another also “first” primitive programmer.

      I suppose, you don’t have any programmer and you try to build first one. Starts also with “1” and an old PC.

      I hope I could help you…

  4. Mayank Kr Tyagi said

    Hello sir,

    iam using, atmega16 microcontroller and usbasp programmer

    i am not able to connect my controller to pc.

    please guide me for the solution Email : mayank072@gmail.com
    i will be very thankful 2 u.

    thanks

  5. curious said

    I am using Sina Prog to program my ATmega169PV using USBasp. However, I was trying to unprogram the ‘divide clock by 8′ fuse bit and I don’t know what happened after doing that but the controller does not get detected by Sina Prog any more.
    I am getting this error:

    avrdude: warning: cannot set sck period. please check for usbasp firmware update.
    avrdude: error: programm enable: target doesn’t answer. 1
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

    avrdude done. Thank you.

    I know that the USBasp programmer is working because I tried programming another microcontroller with it and it worked.

    What should I do?

  6. sir,
    I am using atmega8l microcontroller and usbasp programmer
    while programming, it says device not responding.
    please help.

  7. [...] [...]

  8. Venkatesh said

    I really appreciate your invaluable efforts to guide newbies like me

  9. [...] – Input / OutputMake your own, ultra simple, universal AVR programmer !!Anode and Cathode of LED !AVRDude Tutorial : Burning hex files using USBasp and AVRdudeUSBasp tutorial: USBasp driver [...]

  10. Manu said

    Hai Sir,
    During burning program into Chip using extreme burner the fuse bits are set wrong by me & burned.Now i cant able to use my chip so can u please able to tel me a way to recover my chip……. please…

  11. Astonix said

    Nice post dude,
    I’ve tried to follow the “readme” file while flashing the USBasp & got this error messages :

    $ sudo avrdude -p m8 -c usbasp -P usb -U flash:w:main.hex

    avrdude: warning: cannot set sck period. please check for usbasp firmware update.
    avrdude: error: programm enable: target doesn’t answer. 1
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

    avrdude done. Thank you.

    based on ur post above, this error occur if there’s some problems with the connection between uC & USBasp.
    In this case, i just test the USBasp itself with no uController connected.

    Any advice for this?

    Thx a million,
    Aston

    • Omkar said

      That is perfectly expected. When you don’t connect your ATmega8 to the USBasp, you will get this message.

      “avrdude: error: programm enable: target doesn’t answer. ” This indicates that, USBasp hardware is not able to detect specified uC attached to it. So you have to connect your uC and then use the above mentioned command to burn the uC.

  12. [...] AVRDude Tutorial : Burning hex files using USBasp and AVRdude « Electronics – Tuts, tips and much… [...]

  13. karan said

    Use sinaprog got more options of microcontrollers

  14. Hello,
    :)

    I have recently written a GUI Software for driving USBasp compatible USB Programmers. It is much easier to use than the avrdude or avrdude-gui.

    You can download it free of cost from here

    http://extremeelectronics.co.in/avr-tutorials/gui-software-for-usbasp-based-usb-avr-programmers/

    • Mayank Gulati said

      it doesn’t have option for atmega 328 in win8 then what should i do now?

      and i need to use external oscillator and capacitor for that …

  15. [...] AVRDude Tutorial : Burning hex files using USBasp and AVRdude [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 96 other followers

%d bloggers like this: