I'm slowly moving from Arduino IDE to Atmel Studio to program the popular atmega328p. Since the Serial.print() Arduino function is very comfortable and useful, I'm looking for an easy way to display the values of the variables when using Atmel Studio. What is the best way in your opinion?
Of course I could add a series of LEDs to the microcontroller in order to display the values in binary or I could even connect an LCD screen, but these solutions require the use of lots of ports. How can you easily check the variables without Arduino IDE (and without using expensive external tools) ?
Setup the UART for correct parameter. Next write a byte at a time to the uart's output register, check the flag that the output register is empty and write the next byte.
The 328 datasheet contains all information including examples needed; not behind a pc to help further.
I know it is not elegant but I use communication standalone chip <-> Arduino <-> Arduino IDE Serial. Installing drivers, opening ports etc. is so intimidating for me I prefer this way. Or saving parameters in EEPROM and reading whole EEPROM with AVRDude
In Atmel Studio, you're in the world of real C, not the Arduino subset. So, you just add the line "#include <stdio.h>" and then send data to the console using printf("%d/n",myVar); type syntax. Even floats work, unlike in the Arduino environment. You'll need to set baud and other things but that's a do once type of thing.
Everything comes with a price and in the case of printf, it's code size. Hopefully you have plenty of flash space.
Sooner or later, you'll realize that debugging with print statements is slow and tedious at best and there are much better ways once you learn your way around the tools in studio. Once you discover the simulator and then add a hardware based debugger like an AVRDragon or JTAGICE II, you'll wonder how you got anything done with only print statements.
Thanks for your valuable advices, I'll study and try your solutions. There's another thing which is not perfectly clear to me: lots of people say they don't like Arduino IDE because of the simplified high level libraries. But the truth is Arduino IDE doesn't force you to use libraries (like digitalWrite, analogRead etc.), in fact it allows you to program at low level if you want, setting bits on registers and so on, so what exactly is wrong with using Arduino IDE? Of course, it forces you to maintain setup() and loop() functions but are there other bad things?
"Bad things" is relative - to other ways of achieving the same goal. I wouldn't personally call them bad things at all, for the intended audience, the Arduino environment is pretty darn good. The overall goal of simplicity is achieved at the expense of flexibility in the build process. Which, isn't a bad thing, given the goals of the project.
avr_fred:
"Bad things" is relative - to other ways of achieving the same goal. I wouldn't personally call them bad things at all, for the intended audience, the Arduino environment is pretty darn good. The overall goal of simplicity is achieved at the expense of flexibility in the build process. Which, isn't a bad thing, given the goals of the project.
Having spent more time than I like to remember in commercial development environments, having a nice, platform-specific easy to use subset is definitely a GOOD THING in my opinion!
It's like saying C is a bad thing because it lets me run the same code on all these cpu's running different opcodes!
I'm looking for an easy way to display the values of the variables when using Atmel Studio. What is the best way in your opinion?
Write a basic UART putc() function and attach it to the printf() function. (If printf() is too big, just use puts() instead.)
This usually happens in "real development" right after "blink"...
without using expensive external tools
Buy an ATmega328p "Xplained Mini" from your favorite dealer, or "Microchip Direct." (maybe wait for a free shipping deal...) They're about $10, more-or-less Arduino compatible, and include a debugging chip that works with Atmel Studio.
In Atmel Studio ... Even floats work
Hmm. Not unless you specifically include the "printf with float support" library instead of the standard printf library!
so what exactly is wrong with using Arduino IDE?
There's nothing wrong with the Arduino IDE, as far as it goes.
Some people are annoyed that it "hides" things that "real programmers ought to know."
Some people miss features from the "big IDEs", like better syntax awareness, identifier name completion, and etc. (so while some people complain that Arduino makes things too easy, other complain that it makes things harder...)
It doesn't have simulation or debugging (which are somewhat controversially useful.)
It doesn't give you much control over the build process (not that most people use that), and of course you are limitted to the boards and chips that are "supported" by someone (which is getting to be a large set of chips!)
it forces you to maintain setup() and loop() functions
Actually, it doesn't. If you put a main() in your .ino or a .cpp file, that will override the default Arduino main(), and none of setup(), loop(), or any of the arduino core code will get included unless you specifically reference it. (It will still get BUILT. See "not much control over build process.")
Did know you can program the arduino in Atmel Studio? There is an add on called visual Micro that makes that possible.
That way you can use those Serial.println statements that you like.
Making the move to Atmel studio doesn't really change what you can do though. You can go low level in the arduino IDE also and use standard C library functions.
Atmel Studio is a much better IDE IMO. I am used to the bells and whistles.
I program the Arduino in straight C since I use different micros and know C already.
Like the guys said an advantage of using and avr board instead of arduino is now you can use a debugger.
instead of print statements. I worked in commericial development and we were using 32 bit arms and all we ever did was debug using print statements LOL. But debuggers are nice, you can look at memory and see how everything is stored.
I see that alot on this forum. After messing around a bit, some guys want to see whats under the hood.
Nothing wrong with Arduino at all. Makes things easier but to understand this stuff is still a challenge.
Let me know if you need code to use printf going in atmel studio. I had to futz around with that to get it
to work. Some envirnoments set up the serial port to be the default standard output device by default.