loading

Getting started with ubuntu and the AVR dragon

Picture of Getting started with ubuntu and the AVR dragon
Here's what i did to get started using the AVR Dragon to program ATMEGA8 microcontrollers using ubuntu (This is aimed at beginners, I myself am also being a beginner, so any improvements from more experienced users are greatly appreciated).

After finally switching my laptop and computer over to ubuntu permanently, i found no software that would support my PIC microcontroller programmer and after a few vauge and useless attempts to reverse engineer it (i'm really not that good at electronics, i wake up in the night wondering why i'm not doing a degree to become a tree surgeon), i finally thought "sod it" and splashed out a bit of my student loan on an AVR Dragon after seeing there was an open source c compiler for AVR microcontrollers and supporting programming software, also there's some absolutely wicked projects people have done with them.

It then promptly got discarded in my box of bits for a couple of months (i like to think it was laying dormant in its lair......its a dragon, get the pun?)

so anyways, here's how i got it working, this will probably work with most flavours of ubuntu that use synaptic (you can probably do it on any version of linux if you compile it from source and whatnot, i like synaptic because its easy) and its more a collection of information from various sources, but should hopefully get you up and running!

I realise there are other instructables for getting started with avr microcontrollers, but i couldn't find anything that applied to my situation, so i hope this is at least a bit of help to somebody.
 
Remove these adsRemove these ads by Signing Up

Step 1: Getting the software

Picture of getting the software
in this instructable i wont be using any IDE (integrated development environment, think netbeans or visual basic) or make files (i don't understand them, maybe one day i will), so its mainly gonna be done via the command line and the text editor of your choice.

there are 2 main pieces of software i'll be using

gcc-avr
this is the gnu c cross compiler for avr microcntrollers. A cross compiler is needed because we're compiling code for a microcontroller on an architecture (personal computer) that cannot run the compiled code.
This will take the code you write in c and convert it into a "machine readable" format (loads of 1's and 0's) which can then be put onto a microcontroller.

avrdude
This is what puts the code onto the microcontroller, it connects with the avr dragon and transfers the Hex file (the compiled code) onto the microcontroller.

to install these pieces of software go:
system -> administration -> Synaptic Package Manager
search for gcc-avr and then avrdude and mark them both for installation, then install! simple.

if you want to use the command line you could try
sudo apt-get install gcc-avr
sudo apt-get install avrdude

Step 2: Compiling the code with gcc-avr

Picture of compiling the code with gcc-avr
before we build any hardware well get the code all nice and compiled and ready to go.
i'll be using some c code from this website:
http://www.nongnu.org/avr-libc/user-manual/group__demo__project.html
its under a beer-ware license so i guess i'm good to include it here.
also needed is iocompat.h which just makes sure the code is compatible with the microcontroller we'll be using. ( an ATMEGA8-16PU, although i reckon most of the atmega8 range should be ok)
so go ahead and download the code from below and put the flash.c and the iocompat.h into the same folder.

now for the compiling!

open a terminal and navigate to where the code is downloaded too. then run the following commands:

1.) avr-gcc -g -Os -mmcu=atmega8 -c flash.c

this produces an object file called flash.o which then needs to be linked using:

2.) avr-gcc -g -mmcu=atmega8 -o flash.elf flash.o

this has now produced a binary file called flash.elf, which is a GNU executable file. we gotta mess with it a bit more and grab some bits out of it to make the hex file

3.) avr-objcopy -j .text -j .data -O ihex flash.elf flash.hex

we now have a hex file which is suitable for putting onto the atmega8, we'll be coming back to this in a bit.
(for a more detailed description of what each of the commands and options do see here:
http://www.nongnu.org/avr-libc/user-manual/group__demo__project.html )

lets get the hardware sorted!

Step 3: A bit about the dragon

Picture of A bit about the dragon
IMAG0020.JPG
The avr dragon is a pretty mean bit of kit, it supports loads of different kinds of way to program avr microntrollers, it even has JTAG! (note the added enthusiasm to mask the fact i don't actually know what JTAG is, from what i've heard it sounds fun, i'll hopefully get round to learning about it someday.)

Of worthy note is it has in-circuit debugging, a similar system for PIC microcontrollers will set you back near £100 (the MPLAB ICD2, we use them alot at uni), i bought my dragon from http://www.rapidonline.com for around £34, so i was rather chuffed.

There is a large area on the board for you to add your own headers and ZIF socket's ect. i had none of these to hand so i kept it simple and used ISP to program my avr.
(for a brilliant instructable on populating the empty bits see this )

so to use the ISP thingy we need an ISP cable!
forward to the next step!

Step 4: Making the ISP cable

Picture of Making the ISP cable
IMAG0022.JPG
IMAG0021.JPG
This quite curiously was one of the hardest steps i found in getting myself up and running. the main reason for this is lack of documentation.
Apparently most of the documentation for the dragon comes bundled with avr studio (the official windows only IDE from atmel) which, quite honestly, sucks (not the IDE, i cant really say, i've never used it, but the fact you cant get the documentation).
i've been told if you want a copy of the documentation you have to grab a copy from an installed version of avr studio, which means either having a friend with it installed or *shudder* switching to windows for a bit.
But even then its in a weird windows proprietary format so you have to find some software to open it (some does exist for linux).

shame on you atmel, whatever happened to PDF's?

But anyway, i didn't know any of this so i spent many a few minuets searching the atmel website trying to find the pin names for the ISP header on the Dragon. eventually i stumbled across them on a random website (i'm sorry i forget which) and here they are:

pin 1 MISO
pin 2 VIG
pin 3 SCK
pin 4 MOSI
pin 5 RST
pin 6 GND

i used some turned-pin sockets to make a connector by cutting some off a strip and soldering some ribbon cable onto the back. (the images below give a better impression)

we now have a cable to connect to the microcontroller from the Dragon and a compiled hex file to put onto it. step on to the next step!

Step 5: Building the circuit

Picture of Building the circuit
IMAG0021.JPG
Now its time to build up the circuit!, follow the schematic below and everything should be hunkydory. (i apologise if its a bit scruffy, i was doing power ballad fists to david bowie while drawing it, if there's any confusion the circuit diagram from http://www.nongnu.org/avr-libc/user-manual/group__demo__project.html is fairly close, but we don't need the crystal as the avr is running on its internal 1MHZ oscillator)

if you grab a copy of the datasheet for the microcontroller things may be a bit clearer.
Make sure the LED and resistor are connected to the pin labelled OC1 or OC1A.

Check lots of times to make sure all the ISP pins go to the right place on the microcontroller, if you do it wrong you may risk damaging the Dragon (or slaying it) or hurting the microcontroller, neither of which you really want to happen.

now you have all that set-up it should look something similar to the second picture (hopefully allot less scruffy).

now that done lets get on with dumping the hex file onto this badboy.

Step 6: Writing the hex file

Picture of Writing the hex file
Remember the hex file made in step 2? now its time to transfer it to the microcontroller.To do this i'll be using avrdude.

Avrdude is a fantastic bit of open source software that supports loads and loads of different AVR programmers.

Firstly connect your avr dragon to your computer via usb, then check one more time that all the isp connections are correct then plug your isp cable into the avr dragon board (checking lots and lots that its the right way around, labelling it in some way would probably be a good idea).

Now that everything is hooked up open up a terminal and type the following command:

sudo avrdude -p m8 -c dragon_isp -P usb -e -U flash:w:flash.hex
(n.b the sudo is required because you need root privileges to access the usb port)

Here's the breakdown of the options:

-p m8
tells avr dude it an atmega8 we are trying to program.
-c dragon_isp
tells it we are using the avr dragon and its isp programming mode
-P usb
tells it that the dragon is connected to usb (defaults to parallel port usually??)
-e
erases the microcontroller prior to putting the hex file on it.
-U flash:w:flash.hex
this is the meat and two veg (or qourn if your veggie) of the operation, it tells ' avrdude to write the hex file to the AVR's memory

note: if this fails and returns "invalid device signature" or something similar try the following:
sudo avrdude -p m8 -c dragon_isp -B 10 -P usb -e -U flash:w:flash.hex
the -B 10 tells the dragon to program it a bit slower, i had some issues with this.

Step 7: All done!

Picture of All done!
Plug in the power to the microcontroller and you should have a pretty pulse width modulated flashy LED thingy! (hopefully not as messy as the one below).

Here are some good links:

www.avrfreaks.net
The forums are great, and seem very accommodating to newbs if you ask nice and polite.
http://www.ladyada.net/library/avrdevtut/index.html
A brilliant tutorial, pretty much makes this obsolete, but i only found it while writing this.
http://www.nongnu.org/avr-libc/user-manual/group__demo__project.html
Great beginners tutorial for using avr-gcc
http://www.nongnu.org/avrdude/user-manual/avrdude_4.html#SEC4
Pretty much all you need to know about avrdude
www.rapidonline.com
Where i get most of my bits and pieces from (i think its probably UK only)

http://www.instructables.com/id/Ghetto-Programming%3a-Getting-started-with-AVR-micro/
http://www.instructables.com/id/AVR-mini-board-with-additional-boards/
http://www.instructables.com/id/Ghetto-Development-Environment/
various other good instructables made by people clever than i.
(just search for AVR and theres lots of good stuff)

hope this is of some use to somebody.

*tips top hat*
}{itch
vamsi danda8 months ago

what if there are multiple source files.

Thanks, very nice.

OuZo6 years ago
that code only compiles with avr-libc installed! sudo apt-get install avr-libc maybe you could edit this in on step 1
Iskariote OuZo10 months ago

Thanks for pointing this out!

collard416 years ago
wouldnt it be alot easier to use windows (like normal people) and use AVR studio 4. after all it was desgned for that.

anyway JTAG is like an emulator for PIC but it is in the chip. you connect the corresponding ports on the chip and then enable the JTAG in the programming and then using AVR studio you can view the states of the file, working and EEPROM registers as it advances through the program. this would help with debugging.

and also, dont you find Assembly code easier to write in, maybe its because i learnt it in assembly code.

if you want to learn more about AVR or PIC programming buy these books, they are fantastic, if you are interested you can learn it in a week! here are the links to amazon AVR and for PIC

Note: Written by same author. PIC book written when he was 16 and AVR book was written while at University at the age of 19
>"wouldnt it be alot easier to use windows (like normal people) "

Is that a troll?

You do realize this community seems strongly open source, and you just don't get that in a Windows community. Windows doesn't even do anything until you spend a whole lot more buying shareware.

Besides, not all of us are so rich that we like buying new scanners and printers every time a new version of the OS comes out (can't tell you how many times I had to do that before I said enough was enough, no more Windows,).

Really? I've never had that problem, most companies will release new driver software for the new OS within a few weeks unless that hardware you're talking about was 10-12 years old and thus outside of operating standards ususally

gmoon7 years ago
It's nice to know that avrdude now works with USB in Linux (I've got a Dragon just sitting around, next to an unused STK500...) But I normally just use my $5 parport DAPA cable on a breadboard with Xubuntu. (We don't need no sinkin' dedicated programming boards...)
pfred2 gmoon4 years ago
Yeah USB is icky for hacking!
}{itch (author)  gmoon7 years ago
On reflection parport was probably the way to go. i didnt know so much about AVR's when i got the dragon (had mainly been using PIC's). kinda of glad i got it now though, means i can use it on my laptop (doesn't have a parallel port). but still, it took a bit of effort to get it up and running.
gmoon }{itch7 years ago
That's a crucial point: my current desktop doesn't have a parport, either. And yours is the first demo (explicit, with photos) I've seen of using the ISP from the Dragon on an external board, AND making it work with Linux. So good job.
}{itch (author)  gmoon7 years ago
cheers :) whatever happened to the good old days of the parallel port? i remember writing Qbasic code on an IBM 100 to control a homemade drum plotter through the parallel port......golden times.
ssokolow }{itch7 years ago
Unless you're using a specialized kernel driver, you should probably be able to do whatever you need with a USB-to-parport adaptor. I haven't needed one, but I did notice that there's at least one driver for them on Linux while I was doing the usual `make menuconfig`.
geeklord6 years ago
Is this + this an easy way to get into AVRs?
}{itch (author)  geeklord6 years ago
That looks like a good start. The learning curve can be quite steep when first getting into micro-controller development but don't be discouraged! its well worth getting the basic principles down because you can build on them rapidly to produce some pretty awesome projects. If you need any further help feel free to PM me (I'll help as much as i can but can't promise to know everything) or check out the forums at avrfreaks.com (they are a fantastic bunch of people with a great wealth of knowledge and are generally fantastic at helping new people). Best of luck!
geeklord }{itch6 years ago
Thanks! I think I'm going to go with the USB programmer from atmel off of digikey for $35 instead of hunting for a computer with a parallel port. Are there any good AVR coding tutorials that you know of? Maybe one set up like the Arduino website's reference page, that one is handy.
pfred2 geeklord4 years ago
You can get really good parallel port cards for about $15. I personally wouldn't use a built in parallel port on a machine if I was connecting prototype electronics circuits I'd made to it anyways. $15 parallel port cards are easier to replace than computers! Though knock on wood I haven't blown one up yet myself.

http://img407.imageshack.us/img407/1168/stepbench.jpg

The gray sub D cable is plugged into one in the picture. Then signals from my home brewed buffer are running one of the worst loads out there, an inductive stepper motor controller running at a couple of amps.

So as they say, it works here!

http://www.youtube.com/watch?v=cgbeyNNBZ68
Kemo0o5 years ago
Good stuff .. But I see that running codevision under Wine is more easier BTW I'am going to give it a try
jonathanjo6 years ago
Thanks so much for the -B 10 note: I had exactly this problem, and couldn't work out what was going wrong.

This was the error message -- though the device signature would come back differently each time:

avrdude: Device signature = 0x3f00ff
avrdude: Expected signature for ATMEGA168 is 1E 94 06
Double check chip, or use -F to override this check.

NB: the wrong signature would always be runs of 1s and 0s, never completely random.

-B 10 fixed it straightaway.

Thanks again.
}{itch (author)  jonathanjo6 years ago
yhea, that -B 10 got me stumped for quite some time before i asked the kind people over at avrfreaks.com cheers for the vid! thats a great demo of what PWM does!
jonathanjo6 years ago
hi itch thanks for such clear help. attached is a video of the oscilloscope trace of the pulse-width changing. thanks again.
jaem7 years ago
For AVRs, the KontrollerLab (http://www.cadmaniac.org/projectMain.php?projectName=kontrollerlab ) IDE is quite nice. It's KDE-based, and comes packaged as i386 RPMs and DEBs.
}{itch, if you still have PICs lying around like me, check out http://home.pacbell.net/theposts/picmicro/. There's programmer software there for the PICKit2, which is the cheapest "official" USB PIC programmer available from Microchip. Unfortunately, development has stalled, and the latest firmware for the programmer is only supported by the latest alpha of the software. It might be worth a try,though.
jaem jaem7 years ago
darn - missed the comment further down.
UltraMagnus7 years ago
There is also KontrollerLab, which is a GUI program for AVR
LinuxH4x0r7 years ago
Nice. I don't think i'll be doing pic anytime soon
Dang, looks difficult, but nice job.
}{itch (author)  GorillazMiko7 years ago
cheers :) its really not so hard once you've gone through it. and well worth it too be able to begin programming microcontrollers (although there probably are simpler ways.... like using windows, but i'm stubborn and like to stick to linux :D )