Society of Robots - Robot Forum

Software => Software => Topic started by: MarkBrown on August 30, 2008, 05:40:43 PM

Title: C++ and AVR Studio
Post by: MarkBrown on August 30, 2008, 05:40:43 PM
What programming languages can I use with AVR Studio, or to program AVR chips?  I would really like to use C++ for the classes and try...catch exception handling but I only see info on C.  I tried searching the forums for a simular quesiton, but the search only returns posts with the letter C in it, which is allot of posts.

Thank you,

Mark
Title: Re: C++ and AVR Studio
Post by: Webbot on August 30, 2008, 05:49:42 PM
I've done a tutorial on it !!  http://www.societyofrobots.com/member_tutorials/node/108 (http://www.societyofrobots.com/member_tutorials/node/108)

I've added quite a lot of new stuff into the library recently - so I need to get round to uploading a new version.

I just build the code using a makefile from the command line.

Remember C++ still has restrictions - you cannot use the 'new' operator so you have to create all of your instances at the beginning.
Title: Re: C++ and AVR Studio
Post by: MarkBrown on August 30, 2008, 06:02:40 PM
So if I understand this correctly, I can use C++ to program my AVR chips, I just can't use AVR Studio to compile it right?  I will have to compile it via command line.
Title: Re: C++ and AVR Studio
Post by: izua on August 30, 2008, 08:43:29 PM
no dude, AVR is an IDE. even if you compile it via the command line, you will use the same compiler.
unless, you use another compiler which can instantiate in-code.

as a sidenote, C++ is bad for MCUs unless you have a ton of space, speed and ram.
Title: Re: C++ and AVR Studio
Post by: MarkBrown on August 30, 2008, 09:07:01 PM
So classes, inheritance, and object oriented programming is a bit to much for these chips? 

I understand what you are saying about AVR Studio just being an IDE, I was just hoping that there was a flag I could set when trying to compile through command line that AVR Studio doesn't set.

Thanks,

Mark
Title: Re: C++ and AVR Studio
Post by: izua on August 31, 2008, 11:47:32 AM
IMO, yes, I think it's too much for an 8 bit avr.
Well, why would such an important option (C++ compatibility) would be disabled by default?

However, there are also 32 bit avrs, which run linux kernels. Try those, you can login into them, run vim (on them), and compile on them a filethat you wrote in vim.
Title: Re: C++ and AVR Studio
Post by: MarkBrown on August 31, 2008, 01:46:43 PM
Since I am very stubborn, i decided to do some more research.  I found out that if the compiler is switched from avr-gcc.exe to avr-c++.exe, AVR Studio will compile the C++ code.  I am working on making some changes to my Ping code and plan to test the difference between my C and C++ code.

Thanks,

Mark
Title: Re: C++ and AVR Studio
Post by: sotu on August 31, 2008, 03:12:50 PM
What about dev-c++? Any1 who can reccomend it, or is there any better compilers for c++?

And for you who know a bit about C++ code language, is this code suppose to show a result on the screen after compiling and running it, or is it ment to be showed on a mini LCD screen on a microcontroller?:

#include <iostream>
using namespace std;

int main ()
{
    cout << "Hallo";
return 0;
}
Title: Re: C++ and AVR Studio
Post by: MarkBrown on August 31, 2008, 03:44:44 PM
What about dev-c++? Any1 who can reccomend it, or is there any better compilers for c++?

Dev-C++ is a great compiler.  The only one I tend to like better is Microsoft's Visual C++ Express.  There is alot of great things included in it, as well as some of the best code completion found in any compiler I have used.  The reason I am not using Dev-C++ though is that as Admin has said in a different post, it is used to make applications for computers, not for microcontrollers.

Quote
And for you who know a bit about C++ code language, is this code suppose to show a result on the screen after compiling and running it, or is it ment to be showed on a mini LCD screen on a microcontroller?:

#include <iostream>
using namespace std;

int main ()
{
    cout << "Hallo";
return 0;
}
Your code will show Hallo in a command window, although depending on the compiler it could show and hide faster than you can see the results.  It is possible to get the results to a mini LCD screed as there are functions for doing this in the AVRLib files.  You could also send this to a terminal window on your computer using UART if it is setup, although you would use rprintf() instead of cout.

Mark
Title: Re: C++ and AVR Studio
Post by: Webbot on August 31, 2008, 04:24:46 PM
So if I understand this correctly, I can use C++ to program my AVR chips, I just can't use AVR Studio to compile it right?  I will have to compile it via command line.
Correct

So with the avr compiler and Notepad you have everything you need to create C++ code.

When it comes to writing the code then you may prefer an IDE that can do all the usual colour coding, class hierarchies etc etc. To help make coding quicker, easier, more visual. Note that the IDE you choose doesn't then necessarily have to be able to compile the code - you can still do that via a makefile. IMHO this is better anyway - as you can give your code to someone else and they can compile it - rather than having to require the same IDE as you.

Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Unlike the Java runtime, C++ doesn't require any runtime interpreter so the code can be just as tight as it is in C - if not better. And the benefits of encapsulation etc make your code robust and re-usable.

I do ALL of my AVR software projects in C++


Title: Re: C++ and AVR Studio
Post by: MarkBrown on August 31, 2008, 05:08:37 PM
So if I understand this correctly, I can use C++ to program my AVR chips, I just can't use AVR Studio to compile it right?  I will have to compile it via command line.
Correct

So with the avr compiler and Notepad you have everything you need to create C++ code.

In case you are interested in actually using and compiling with AVR Studio, it is possible.  If you go into AVR Studio->Project Options->Custom Options, you can uncheck the "Use WinAVR" option, which defaults to avr-gcc.exe, and specify your own compiler, like avr-c++.exe.

(http://lh5.ggpht.com/marksbrown/SLsjnUc0C0I/AAAAAAAABao/vWzDkfkGdwU/s400/AVR%20Studio%20Options.JPG)

Quote
Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Unlike the Java runtime, C++ doesn't require any runtime interpreter so the code can be just as tight as it is in C - if not better. And the benefits of encapsulation etc make your code robust and re-usable.

I do ALL of my AVR software projects in C++

I was very happy to read this.  There are so many benifits I can see to using C++ over C with this, your example of sensors is a good example.  So for now I will continue to try to do this in C++.

Thank you,

Mark
Title: Re: C++ and AVR Studio
Post by: sotu on September 01, 2008, 07:06:16 AM

Your code will show Hallo in a command window, although depending on the compiler it could show and hide faster than you can see the results. 



This is exactly what happens.

I can see a small black window popping up for less then a second, so fast i cant read what it says. Any way to fix this ?
Title: Re: C++ and AVR Studio
Post by: sotu on September 01, 2008, 03:35:46 PM
never mind  ;) just found out about the: system ("pause") ;  ;D
Title: Re: C++ and AVR Studio
Post by: MadMax on September 01, 2008, 04:08:35 PM
Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Right, to a certain point. For example, in the library you showed me, you're handling it a little ineffecient. When reading through it I saw you created a LED class with a toggle function. This means you store the current state in a variable. Sometimes you don't need this variable to store whether the pin is high or low. I agree with you that 1 bit is a very small value, and it won't affect the memory usage a lot. But it still affects memory. I would create a base LED class, and a derived ToggableLed class.

Not attacking you, just giving you ideas for your library ;)
Title: Re: C++ and AVR Studio
Post by: Webbot on September 01, 2008, 05:16:33 PM
Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Right, to a certain point. For example, in the library you showed me, you're handling it a little ineffecient. When reading through it I saw you created a LED class with a toggle function. This means you store the current state in a variable. Sometimes you don't need this variable to store whether the pin is high or low. I agree with you that 1 bit is a very small value, and it won't affect the memory usage a lot. But it still affects memory. I would create a base LED class, and a derived ToggableLed class.

Not attacking you, just giving you ideas for your library ;)

Suggest you re-do your research. The 'toggle' function for the LED class is inherited from the OUT (ie outpin pin class) it just does a bit-wise exclusive-or with the current output value. Just like the C avrlib library does. So requires 'ZERO' memory.
Title: Re: C++ and AVR Studio
Post by: brijesh on September 04, 2008, 08:39:59 PM
Here is related info.

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus

"When programming C++ in space- and runtime-sensitive environments like microcontrollers, extra care should be taken to avoid unwanted side effects of the C++ calling conventions like implied copy constructors that could be called upon function invocation etc. These things could easily add up into a considerable amount of time and program memory wasted. Thus, casual inspection of the generated assembler code (using the -S compiler option) seems to be warranted."

If the program is so big and complicated that OOP's methodology provides significant and real benefits, then probably 8-bit microcontroller is not the right solution.
Title: Re: C++ and AVR Studio
Post by: JesseWelling on September 05, 2008, 12:33:38 AM
If the program is so big and complicated that OOP's methodology provides significant and real benefits, then probably 8-bit microcontroller is not the right solution.

That's a +5 insightful. But you might want to also consider that OOP is easier to grok and refactor. So using OOP in C is a Good Thing™.

Also, on many 16/32 bit processors (ala ARM and ST10) C is still widely used even though C++ would be more ideal. Due to the OS and Application being compiled into one binary, I havn't seen many ARM or ST10 projects use C++  :-\
Title: Re: C++ and AVR Studio
Post by: Webbot on September 05, 2008, 11:19:33 AM
Thus, casual inspection of the generated assembler code (using the -S compiler option) seems to be warranted. (http://Thus, casual inspection of the generated assembler code (using the -S compiler option) seems to be warranted.)

Yeah - I would recommend doing it with C as well. Compilers generate code to do what we've asked them to do. Sometimes we aren't very good at optimizing what 'we' tell the compiler to do. Hence GIGO - garbage in garbage out.
Title: Re: C++ and AVR Studio
Post by: brijesh on September 05, 2008, 05:49:19 PM
So using OOP in C is a Good Thing™.

Agree 100%. The main concepts of data abstraction/hiding and programming to "interfaces" is valid no matter what methodology one uses.
Title: Re: C++ and AVR Studio
Post by: MadMax on September 06, 2008, 11:50:42 AM
Suggest you re-do your research. The 'toggle' function for the LED class is inherited from the OUT (ie outpin pin class) it just does a bit-wise exclusive-or with the current output value. Just like the C avrlib library does. So requires 'ZERO' memory.

To be honest, I didn't do any research, I'm sorry... I will shut up and RTFM in the future ;)
Title: Re: C++ and AVR Studio
Post by: Admin on September 06, 2008, 03:19:13 PM
Quote
Compilers generate code to do what we've asked them to do. Sometimes we aren't very good at optimizing what 'we' tell the compiler to do.
Anyone volunteering to write a tutorial for this? I made a post somewhere like a year ago requesting one for this. I'm not a professional programmer, more ad hoc programmer you may say . . . 'it works? ok, finished'

I'm sure 95% of the people on this forum are the same . . .
Title: Re: C++ and AVR Studio
Post by: Webbot on September 06, 2008, 06:45:16 PM
Admin: do you mean a tutorial on programming from the ground up? Optimization etc?
Yeah - I could be up for it
Title: Re: C++ and AVR Studio
Post by: Admin on September 06, 2008, 06:55:24 PM
Not a tutorial on programming (there are a million out there), but a tutorial on optimizing code.

Basically a tutorial that will make my code optimal like your code :P
Title: Re: C++ and AVR Studio
Post by: Ro-Bot-X on September 06, 2008, 07:18:29 PM
Admin: do you mean a tutorial on programming from the ground up? Optimization etc?
Yeah - I could be up for it

Ohh, I would love such a tutorial!
Title: Re: C++ and AVR Studio
Post by: Webbot on September 10, 2008, 06:08:52 PM
Not a tutorial on programming (there are a million out there), but a tutorial on optimizing code.

Basically a tutorial that will make my code optimal like your code :P

Ok - have been thinking about topics. So the broad brush is as follows:-

1. Optimisation - turned either: off, for speed, for size - and what it does in general terms
2. Compiler directives: #define, #ifdef etc - pros and cons
3. Keywords like: volatile, register, const - what do they mean and how do they effect compiler output
4. Defining code in .h files versus .c files - pros and cons
5. How to reduce your code by using structures without having to go to C++. Packing structures.
6. The dangers of multi-threading / interrupts - how to avoid issues
7. abstracting it via the makefile

Anyone got any comments / things to add?
Title: Re: C++ and AVR Studio
Post by: Ro-Bot-X on September 10, 2008, 09:56:47 PM
Yes, the use of pointers.
Title: Re: C++ and AVR Studio
Post by: stan on September 10, 2008, 11:09:30 PM
now the stupid question can the Axon handle c++ ;D
Title: Re: C++ and AVR Studio
Post by: Webbot on September 10, 2008, 11:20:51 PM
Yes, the use of pointers.
Ok thanks - sort of had that in my list.
Title: Re: C++ and AVR Studio
Post by: Webbot on September 10, 2008, 11:23:56 PM
now the stupid question can the Axon handle c++ ;D

The hardware doesn't matter - its whether the compiler can do it. certainly the avr gnu compiler can do it for AtMega8/168 - so can't see why the controller on the Axon would be different.
Title: Re: C++ and AVR Studio
Post by: stan on September 11, 2008, 12:16:37 AM
the only reason I ask I found a c++ book in a library book store for $2 written in 2006.... courtesy of our local college kids....

Title: Re: C++ and AVR Studio
Post by: Admin on September 11, 2008, 08:16:31 PM
Quote
1. Optimisation - turned either: off, for speed, for size - and what it does in general terms
2. Compiler directives: #define, #ifdef etc - pros and cons
3. Keywords like: volatile, register, const - what do they mean and how do they effect compiler output
4. Defining code in .h files versus .c files - pros and cons
5. How to reduce your code by using structures without having to go to C++. Packing structures.
6. The dangers of multi-threading / interrupts - how to avoid issues
7. abstracting it via the makefile

Webbot, thats exactly what I meant :)

I have a barely functional understanding of each of those points, so I can immediately apply what I learn from your tutorial! I vote for all of them :P

Also, tips for reducing RAM usage. I always run out :-\
And how does one multi-thread on an mcu?
Title: Re: C++ and AVR Studio
Post by: Webbot on September 12, 2008, 02:59:48 PM
Quote
1. Optimisation - turned either: off, for speed, for size - and what it does in general terms
2. Compiler directives: #define, #ifdef etc - pros and cons
3. Keywords like: volatile, register, const - what do they mean and how do they effect compiler output
4. Defining code in .h files versus .c files - pros and cons
5. How to reduce your code by using structures without having to go to C++. Packing structures.
6. The dangers of multi-threading / interrupts - how to avoid issues
7. abstracting it via the makefile

Webbot, thats exactly what I meant :)

I have a barely functional understanding of each of those points, so I can immediately apply what I learn from your tutorial! I vote for all of them :P

Also, tips for reducing RAM usage. I always run out :-\
And how does one multi-thread on an mcu?

Ok - looks like we have a basis of a tutorial then. Not quite as daunting as 'How do I program' - but still a fairly large topic - so its gonna take me a while...
Will add a new post once complete. Over and out.