Author Topic: C++ and AVR Studio  (Read 67492 times)

0 Members and 1 Guest are viewing this topic.

Offline MarkBrownTopic starter

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
    • Mark's Projects
C++ and AVR Studio
« 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
It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #1 on: August 30, 2008, 05:49:42 PM »
I've done a tutorial on it !!  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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline MarkBrownTopic starter

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
    • Mark's Projects
Re: C++ and AVR Studio
« Reply #2 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.
It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: C++ and AVR Studio
« Reply #3 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.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline MarkBrownTopic starter

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
    • Mark's Projects
Re: C++ and AVR Studio
« Reply #4 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
It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: C++ and AVR Studio
« Reply #5 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.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline MarkBrownTopic starter

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
    • Mark's Projects
Re: C++ and AVR Studio
« Reply #6 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
It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/

Offline sotu

  • Supreme Robot
  • *****
  • Posts: 350
  • Helpful? 0
  • ïBB - Eye Biped Bot
    • Easy Built Biped Bot
Re: C++ and AVR Studio
« Reply #7 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;
}
How to build a biped bot:

Offline MarkBrownTopic starter

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
    • Mark's Projects
Re: C++ and AVR Studio
« Reply #8 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
It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #9 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++


Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline MarkBrownTopic starter

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
    • Mark's Projects
Re: C++ and AVR Studio
« Reply #10 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.



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
« Last Edit: August 31, 2008, 05:15:53 PM by MarkBrown »
It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/

Offline sotu

  • Supreme Robot
  • *****
  • Posts: 350
  • Helpful? 0
  • ïBB - Eye Biped Bot
    • Easy Built Biped Bot
Re: C++ and AVR Studio
« Reply #11 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 ?
« Last Edit: September 01, 2008, 03:33:39 PM by sotu »
How to build a biped bot:

Offline sotu

  • Supreme Robot
  • *****
  • Posts: 350
  • Helpful? 0
  • ïBB - Eye Biped Bot
    • Easy Built Biped Bot
Re: C++ and AVR Studio
« Reply #12 on: September 01, 2008, 03:35:46 PM »
never mind  ;) just found out about the: system ("pause") ;  ;D
How to build a biped bot:

Offline MadMax

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
Re: C++ and AVR Studio
« Reply #13 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 ;)

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #14 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.
« Last Edit: September 01, 2008, 05:17:11 PM by Webbot »
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline brijesh

  • Full Member
  • ***
  • Posts: 55
  • Helpful? 0
Re: C++ and AVR Studio
« Reply #15 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.

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: C++ and AVR Studio
« Reply #16 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++  :-\

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #17 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.

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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline brijesh

  • Full Member
  • ***
  • Posts: 55
  • Helpful? 0
Re: C++ and AVR Studio
« Reply #18 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.

Offline MadMax

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
Re: C++ and AVR Studio
« Reply #19 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 ;)

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: C++ and AVR Studio
« Reply #20 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 . . .

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #21 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
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: C++ and AVR Studio
« Reply #22 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

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: C++ and AVR Studio
« Reply #23 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!
Check out the uBotino robot controller!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #24 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?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
    • Ro-Bot-X Designs
Re: C++ and AVR Studio
« Reply #25 on: September 10, 2008, 09:56:47 PM »
Yes, the use of pointers.
Check out the uBotino robot controller!

Offline stan

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
Re: C++ and AVR Studio
« Reply #26 on: September 10, 2008, 11:09:30 PM »
now the stupid question can the Axon handle c++ ;D
I have a brain tumor and whats your excuse?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #27 on: September 10, 2008, 11:20:51 PM »
Yes, the use of pointers.
Ok thanks - sort of had that in my list.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: C++ and AVR Studio
« Reply #28 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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline stan

  • Jr. Member
  • **
  • Posts: 28
  • Helpful? 0
Re: C++ and AVR Studio
« Reply #29 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....

I have a brain tumor and whats your excuse?