Instructables
Picture of How to get started with Eclipse and AVR
Programming AVRs is fun, but sometimes the manufacturers development environments make code maintenance a chore. If your looking for a free, cross platform, high quality piece of software for programming AVRs Eclipse is a good choice.

Moving to an Integrated Development Environment (IDE), such as Eclipse is a logical step up from learning to program basic functions. By using an IDE you will be able to work easier and work with a range of tools inside the same program. you will be able to easily package and reuse your code and you will have access to advanced functions like refactoring and code analysis. For details on these benefits please have a quick skim over Step 12: Quick Tour of Cool Features.

In this instructable I will show you how to install and configure Eclipse to program AVR micro controllers (aka. Micro Controller Units or MCUs). I will also provide a quick run through uploading to your MCU and I will briefly cover some of the cool feature Eclipse has to offer.
 
Remove these adsRemove these ads by Signing Up

Step 1: Why?

Eclipse is cool. Like, really cool. Compared to something like the Arduino programming software, the Eclipse IDE is the proverbial Swiss army knife.

For example, the Arduino development environment only lets you work in one file at a time. Also if you want to rename a variable you have to do a find/replace. If you want to take some code and wrap it in a method, you have to do it by hand. Eclipse can help with these otherwise tedious tasks and more. More generally speaking, moving to a fully featured IDE like eclipse is a step up from less full featured environments.

Note: Yes, Eclipse can be used to program the Arduino - however the set up for this is slightly more complex and the specifics of using Arduino in Eclipse will not be covered here.
oululife12 days ago

Phew! It may be an old 'instructable', but sure saved me a helluvalotta time!

Trying to 'graduate' from Arduino IDE, I've tried - and rejected a few. Studio 6 (no Linux, needed at home), Code::Blocks (can't get it to pick up the xmega chip I'm using) and also AVR-Eclipse - (too complicated) until I stumbed on your article. Hooked a USBtinyISP Chinese clone to a pro Mini clone - and after a day of frustration, and a morning of 'Eureka!'s, I get it. Even on Windows 7 (Finnish edition...)

Thanks for takig the time to make this. Also for the 'comments' section where I found the last-but-one 'Gotcha!' - save always before build. (Last 'gotcha?' "Why does avrdude work only about 10 times, before the 'AVR download button does nothing?"
But that's an avrdude/eclipse issue for later....

Now at least I can get on and learn enough C++/ATxmega64A4U stuff to give my boss a finished project in a few months...

BR

Andy

silas1510 months ago

I have some question. I use CodeVisonAVR. There is wizard for ports define, setting timer etc. Is in Eclipse or AVR plugin something like this? This functionality would be very useful. I don't remember this code, because it is use once in project.

novski1 year ago
I think its this link for OSX users: http://www.obdev.at/products/crosspack/index.html
can somebody confirm?
novski novski1 year ago
i had a few issues with this package. Seams that the AVRplugin takes different paths then Crosspack installs to. I then installed MacPorts and installed it with Terminal. Its little bit more effort but then it works also with a Mac.
the full story can be found here: https://sourceforge.net/p/avr-eclipse/discussion/664382/thread/465ba881/
cheers, novski
dirtyrocks1 year ago
Step 9 is lacking! Specifically is is lacking entering a port (-P argument) such as "com9".
This Instructable lacks how to setup a programming hardware!




andy (author)  dirtyrocks1 year ago
Thanks for the comments, I have updated Step 9. I've not had a problem with the default port before but maybe some folks have. Certainly if you need to unplug/plug a lot I have seen the port hang and the number increment.

If you were hoping for a tutorial on using a particular external programmer I'm afraid I cannot cover all the many variants of programmers out there. I'd recommend googling around for your particular model of AVR programmer - normally AVRDUDE is very good - it knows about many different programmer hardwares.

If this is the case you can use the drop down in eclipse to select the appropriate entry rather than "arduino" in step nine. You may also need different configuration values, depending on programmer hardware.
ffinacio1 year ago
When I am trying to install AVR plugins, instead to load CDT Optional Features and corresponding Plugins, it is only installed AVR Eclipse Plugin. AVR Eclipse Plugin Services is not available.
Typing main.c file it is presented semantic errors for all registers and bit included in the blinky example.
I have used indigo and juno versions of eclipse .
I should like to have some comment from you.
Regards
I don't get the get the AVR cross target application project type. I have the pugin and winavr installed.
andy (author)  bassman76jazz2 years ago
Hmm, that's unexpected. What do you see listed in that view? I'd guess that perhaps the plugin did not install correctly. It half rings a bell with me - I may have had this issue before myself... If you go;

Help>Install new Software

Then select the AVR url in the "work with" drop down, you should see only avr plugins listed, are they all ticked? If so it might be a compatibility problem with the latest version of Eclipse (in which case I'd advise trying again with an older version of Eclipse).

If this doesn't help please can you let me know the version of Eclipse that you are using?

Thanks,
Andrew
I followed the directions implicitly including the beverage and biscuit. that was a nice touch..
chypsylon2 years ago
If I understood it correctly, activating the "Avrdude" setting here activates an automatic download to the avr on each build. I would recommend against setting this on, as you'd always have to have your programmer plugged in and the AVR powered on when you compile it.

Thanks for this great tutorial :)
andy (author)  chypsylon2 years ago
Good point,

Thanks for your input,
Drew
Hi, I have a really BIG problem, the thing will just not work. The code is right, all the settings are right, everything is installed, everything in ON and plugged in, etc.
but when I try to upload it, it tell me that the project has not been built, I have in auto Build, but even when I do this manually I get the samemessage. it shows no errors anf in the console it just says "Nothing to Build" and it's not creating any Hex files. my code is this;

int main(void)
{
DDRB|= (1< while (1) {
PORTB &= ~(1< delay_ms(250);
PORTB|= (1< delay_ms(250);
}
return(0);
}

on a file called "main", PLZ PLZ PLZ help me! I've searched through all the web and found nothing helpful. :)
andy (author)  Emiliano Valencia3 years ago
Hi Emihackr97, Sorry to contradict you but you have some problems with your source code.

Compile errors
I think the reason why you can't see errors in the console from the build is related to how the build is done. What this does is it first compiles your project to hex, then immediately tries to deploy it. This erases the output of the build. I think theres a setting somewhere to change this, but try the following changes to the code first.

Source
You need includes for avr/io and util/delay - you don't have these.

The code logic is very broken, for example;
DDRB|= (1< while (1) {

will break - because you are assigning the result of a "while" to DDRB - but "while" is a loop - it has no result. Furthermore, I can't see what your trying to do, but that's not how while works, and the brackets don't match - although if they did it would still not work.

I won't go through all the source, but I will provide a flashing light demo, which works on arduino. The following code is the entire contents of the main.c file;
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>

/*
* the delay routine is limited to a maximum of 250 ms / clock
*
* This method will call delay 32 times per second,
* providing a one second interval on a 20Mhz clock
*
*/
void delay (int seconds)
{
//count how many times we called delay
long i = 0;

//while we still have time to wait
while (i < ( seconds * 32 ) )
{
  //wait
  _delay_ms (250);
  //increase the counter by one
  i++;
}
}

int main(void)
{
//while ( condition ) 0 means false, all other numbers are true
while (1)
{
  /* set PORTB high */
  PORTB = 255;
  delay(1);


  /* set PORTB low */
  PORTB = 0;
  delay(1);
}

//we should not return from main
//because the AVR just keeps on executing the program
//if in doubt put a while loop at the end (we don't need it in this case)

return(0);
}



Reminders
There are a few "gotcha" type issues with eclipse;
  • Remember to save your file before you build or eclipse will ignore your unsaved changes.
  • Do not use spaces in your project name, if it has them, right click the project and rename it (you can replace the spaces with underscores). This causes an .elf error similar to what you describe.
  • The build will "fail" if you use the debug build target when your device is not plugged in. This is because debug will try to immediately upload to the AVR after a build.If you see something like;
    avrdude: ser_open(): can't open device "/dev/parport0": Permission denied
    make: *** [avrdudedummy] Error 1

    Then the build went fine, it just couldn't find your device (in this case it's looking for the wrong port, it should be using /dev/ttyUSB0). You can upload the hex whenever you want. This problem doesn't occur on the Release build target as far as I know
Summary
Try the attached - you may have another problem with set up, but just come back here and we shall see what we can do.

With regard to learning C and programming Micro controllers, there are many good resources on the web, such as https://www.mainframe.cx/~ckuethe/avr-c-tutorial/

Regards,
A.






quasipedia3 years ago
Just wishing to drop a line saying that this tutorial is really cool. I have developed for a few months with Arduino, and when I switched to AVR I felt a bit "lost". The fact of being able to use the same IDE I use for normal work helps greately!

Cool! :)
andy (author) 4 years ago
Is anybody interested in follow up instructables? Some Ideas that spring to mind are "Version Control in Eclipse" and "Arduino Development in Eclipse".


Andy


nvradeli andy4 years ago
HI -

I am new to the AVR and starting out with an Arduino. I would like to expand beyond the Arduino environment, and have downloaded the datasheet and a couple of AVR tool sets onto my MAC. However, they don't seem to have much documentation included (I guess they assume everyone already knows the system from somewhere else). I'm fairly comfortable with C programming but not the AVR specific stuff. Can you suggest a good book/manual (hard cover or e-) that goes over the AVR libraries so I can understand how use them? I have started collecting example code to try learning that way but that leaves many questions.

Thank you for any direction you can give me.

nav
andy (author)  nvradeli4 years ago
First thing to realise is that if your using the AVR Libc library then a lot of the tedious work is done for you. What AVR libc does is that it wraps up all the information that you would otherwise have to look up in the data sheet (i.e. the actual memory addresses of variables). So if you want to set all of the pins high you would simply say PORTB = 0xFF;

If you want further info just on using AVR Libc your best bet is navigating to the documentation pages from the main page (http://www.nongnu.org/avr-libc/).

Personally I learnt by getting hold of a cheap programmer (a USBTinyISP) and a ATTiny2313 and getting a blinky light example to work. From there I tried more involved  things such as serial connections, shift registers, etc. This is my preferred approach - get a working example, (perhaps in the form of a kit) and then research more complex methods. that way you are kept interested and get results fairly quickly.

If you prefer a book I can't say that I'm in a particularly good position to advise you on a good choice as I didn't really use books to learn myself (preferring online info). If your new to electronics a bit of knowledge about circuits would be very helpful, however if your using a kit this probably won't be necessary.

I think your best bet would be to start a forum topic in the answers section along the lines of "How/why does this example AVR program work?" followed by the example that you don't understand and possibly a brief description of what you are using (which AVR chip, which programmer, etc) and what you don't understand. I think discussing the examples in this context would be quite helpful.

If you post a forum topic feel free to PM me and I will try to help out, I'm fairly new to C and AVR programming (having said that I have some fairly cool things constructed already) but I'm fairly sure I'd be able to break down a code example if necessary.

Hope this was helpful,
Drew

p.s. the AVR libc manual is here and the FAQ section has saved me more than once (available here). The Answers section of instructables can be found by clicking the Answers link right at the top of this page, next to the instructables logo (of follow this link).

chaotick andy4 years ago


HI -

(If you get two replies, I apologise. Explorer is acting up)
See, I didn't even know ports are just read/written as variables. Cool. Thing is, I've been programming for a while but never cross-compiling or working with microcontrollers, so I don't really know enough to know what to ask. Thanks to your links, I now have an actual complete program, with #includes and all, to study. (so many articles out there only give you the 'interesting part' of the code, and assume you know what went around it). I'm going to try to build a program again this weekend. Thank you for your patience with me, and the good information.

RGRDS

nav
andy (author)  chaotick4 years ago
I found that learning AVR consisted of a number of single "gotcha" type problems, you may or may not be aware of the following so I'll just write everything that comes to mind.

Hardware - it's important to remember that when programming an AVR we are really dealing with very low level hardware operations.

Set up - When outputting a value we first tell the micro controller (MCU) exactly which pins are inputs and which are outputs. Then we tell the pins what value to have. When we do this we are manipulating physically disparate parts(modules) of the MCU - there is (in the chip) a physical difference between portA and portB. The Pulse Width Modulation (PWM) module is an actual module, as are the Comparitors, the Arithmatic Logic Unit (AMU) and so on.

Registers - Following from the previous point, we need a way of controlling all these components. Since we are dealing with actual hardware modules the way this is done is as follows - these modules (e.g. PWM, comparitor, portA controller etc) have some control variable - the value of which defines the behavior of the module. These "control registers" are hardwired to a particular address, so what Libc does (among other things) is gives you pointers to those fixed addresses for a given chip (different chips may or may not have different addresses for the same module - this is because different chips use different modules - the attiny45 does not have a UART module but the attiny2313 does have one, for example). Microcontrollers are all actually a number of pre-fabricated components stuck into one package.

For example, if I want to light an LED on pin 7 I have to consult the datasheet and find the "Register" to which pin 7 belongs (there are various diagrams and tables near the start of the document that tell me this) lets assume it is PORTA - bit 5. Then I jump to the section that tells me how to use PORTA. Finally in my C editor I for example set DDRB bit 5 to low (or whatever the datasheet indicates) to set pin 7 to outpout and then I tell the port that its value is by setting bit 5 of the PORTA register. (AVR chips have common modules - once you can use the port's on one AVR chances are that the technique is the same for a different AVR chip - as previously stated, microcontrollers are actually a number of pre-fabricated components stuck into one package).

Module on/off - Just to cement the previous points, when you turn on a module, say PWM, the power consumption for the chip will increase because the previously un-powered PWM module is now active (equally turning it off will reduce power consumption).

Libc - Libc gives you a lot of help with registers. If you tell Libc what chip you are targeting it will give you all of the definitions from the datasheet (if the datasheet defines bit 5 of a specific register as being called "PB5" then libc will simply make a definition of #define PB5 5). So if the datasheet says clear bit PB5 of the DDRB register to make pin 7 an output, in C you could write;

//Shift 1 n places to the left, then set that bit low
DDRB &= ~(1<<PB5);


And our hypothetical pin is an output. Note that this is hypothetical, this may or may not actually set pin 7 as output, again, it's best to consult the data sheet. Please see the attached image for a print screen of the attiny2313 data sheet highlighting the section relevant to input and output (I/O -Ports).

Hope this answers some questions,
Drew
screenshot.jpg
I don't know exactly what you mean, but take a look at this:
http://www.nongnu.org/avr-libc/user-manual/index.html

i found it when i looked for a comfortable way to convert a byte input received by Serial.read into an integer.
There's no documented function in the Arduino libraries. However there is access to the standard libraries of avr (a subset of the C standard libraries) which contain the stdlib.h library (among other things) that provides an atoi function (ASCII to integer).

On the site you will find the supported microcontrollers, library documentation, information about the toolchain, etc.

did that help?

This is big help, thank you. I can handle C-code and its regular libraries fine, but it's hard to program interrupts, or even toggle a port pin, when you don't know the name of the library to #include, let alone what's in it. This should get me moving forward again. I really appreciate the direction.

RGRDS

nav
Mr Earl andy4 years ago
Arduino Development in Eclipse sounds exciting for me as I am in fact programming with the arduino IDE at the moment. As I'm coding Java with Eclipse it would be a nice feature for me to have an one-for-all IDE. By the way: Great instructable.
andy (author)  Mr Earl4 years ago
No worries, I'll probably be able to hack it out in the next 5 days :) Andy