____

Thursday, November 25, 2010

How to program an AVR Microcontroler in Ubuntu linux !




In windows , CodeVision which costs $300 is used to program AVR Microcontrollers,
but how to do it in Ubuntu for free ?

First of all we need to have a USB AVR Dragon tool.
What is AVR Dragon ?
It is a cheap tool which connects our microcontroler to a PC via USB. The AVR Dragon can program all AVRs via JTAG, HVP or ICSP.

Then we need to install two applications gcc-avr and avrdude .
to install both enter this in terminal :
sudo apt-get install gcc-avr & avrdude
What does gcc-avr do ? as you know , you can not compile a code for AVR by PC compilers , therefore we need gcc-avr which is a cross C compiler for avr.

What does Avr-dude do ? after we made machine code by gcc-avr we need an application to load the files to our microcontorler using Dragon is the program that
now that we installed both , go to the folder that your code is there and enter :

avr-gcc -g -Os -mmcu=atmega8 -c flash.c
this produces an object file called flash.o which then needs to be linked , it is obvious that if your avr is atmega16 or 32, you replace atmega8 with your avr model.

avr-gcc -g -mmcu=atmega8 -o flash.elf flash.o
now we produced a binary file called flash.elf, which is a GNU executable file. we need to change it bit more and grab some bits out of it to make the hex file

avr-objcopy -j .text -j .data -O ihex flash.elf flash.hex
congratulation now have a hex file which is suitable for putting onto the atmega8 !
now we need to hook up our AVR dragon circut and connect it to our PC , if you never hooked up a AVR dragon circuit , it can be tricky at first , read the manuals carefuly.

after connecting the AVR Dragon to USB port of the computer run this in the code folder :

sudo avrdude -p m8 -c dragon_isp -P usb -e -U flash:w:flash.hex
Thats it ! we are done .

Avrdude has many options, to learn all the options

avrdude --help
p.s : if you get errors , don't freak out , you probably need to change the speed options .

Related Posts: