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

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.

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.

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?:

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.

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.

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++.

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.

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.

"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.

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++

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.

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

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'

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

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 terms2. Compiler directives: #define, #ifdef etc - pros and cons3. Keywords like: volatile, register, const - what do they mean and how do they effect compiler output4. Defining code in .h files versus .c files - pros and cons5. 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 issues7. abstracting it via the makefile