Switching from Arduino to straight AVR-C

I run arduino on Linux and have various robotics projects. Some of them are complicated. I'm kind of new at programming so I'm trying to understand the difference between Arduino and just plain old C. Arduino is the only language I "know".

The only problem I have with Arduino is that I feel like it's somewhat limiting if you try to get more elaborate than blinkenled programs and stuff. Some of my projects have been around for months/years and have been forked and versions held back. Some of them are over a thousand lines long. My major problem is that I'm forced to use the Arduino program to write code, and I would rather use a better text editor. Once my programs get to the hundreds of lines long, working with the Arduino program becomes really tedious compared to my favorite text editor. Additionally, as far as I can tell there's no way to use a versioning system or anything with Arduino code, so I'm left with a primitive system of re-naming files manually and sorting them by "last modified" date and so on.

It seems like I have three options going forward
-Continue using Arduino and deal with it
-Continue writing Arduino code, but somehow figure out how to not have to use the Arduino program itself
-Switch to just writing C

Sticking with Arduino
Pros:
--I don't have to do anything
--can use code from Arduino community (I don't do that much)
--can use Arduino hardware and FTDI cable and so on
Cons:
--I get annoyed and bogged down with the whole IDE paradigm

Writing Arduino code without Arduino
Pros:
--I keep my old code
--I can use my favorite text editor
--I can use any content versioning system I want
Cons:
--I don't know if this is possible
--How do I upload programs?

Switching to Plain Old C
Pros:
--sky is limited only by avr-c. Total freedom.
Cons:
--I have to abandon my old code or port it
--I don't get to use Arduino libraries (not a deal breaker since most aren't that good but I like LiquidCrystal in particular)
Questions:
--Can I still use Arduino hardware like the Arduino Pro Mini? Is writing C substantially 'harder' than writing Arduino? What features does Arduino language actually have compared to C? It doesn't have anything substantially different like garbage collection or anything right? And the syntax is pretty much same as C, right? The only thing Arduino language has going for it is some libraries and a convenient uploady thing. And the fact that it's guaranteed to work wit certain Atmel chips and serial hardware.

limiting.... try to get more elaborate than blinkenled programs and stuff.

I somewhat disagree. You are not really missing much from the whole GCC world... there is just a nice blanket of insulation with the Arduino.

So:

A) Your code can be ported.
B) You can find equivalent libraries in most cases for AVRGCC already
C) SmileyMicro's did create a way to migrate Arduino APPS to AVRGCC already.
D) You can certainly still use your Arduino board without the bootloader and code it with straight AVRGCC. If you use Smiley's method, you can even still use PIN assignments (but to be honest... sometimes it's nice to twiddle with the port pins are they were meant to be by ATMEL.
E) The coding is basically the same, except that some of the more obscure/obtuse/"annoying to newbs" parts of C are kept well hidden.

EXAMPLE:

#include <avr/io.h>
#include <avr/delay.h>

int main (void)
{
DDRB = 0xff; //sets all bits of port B for output
DDRA = 0xfe; //sets one bits of portA to be an input
while(1)
{
PORTB= PINA;


}
return 1;

Don't think that doesn't happen with Arduino... It does... you just don't see it.

My advice... Think of AVRGCC as a next step if you are willing to "take a next step" and you also can come up with some real limitations you can't escape... (and I can't think of one right now... mmmm OK, maybe you need to code a routine in ASM for timing purposes and need to know what's going on at all times... )

And by all means... Hang Out at AVR-FREAKS forum.

You can use an external editor to edit the code, while still using the IDE to compile/link/upload.

For the lcd code this is one great lib to use with plain Avr C:
http://homepage.hispeed.ch/peterfleury/avr-lcd44780.html

About the programming and using the AvrStudio just see my blog, I will try to post a new tutorial each week.

You can use an external editor to edit the code, while still using the IDE to compile/link/upload.

Oh, I've tried editing the text files with an external editor, but I gave it up because it didn't have proper syntax highlighting. And I don't want to have to start up the Arduino program to compile the code, I want to compile it with the commandline like God intended.

D) You can certainly still use your Arduino board without the bootloader and code it with straight AVRGCC. If you use Smiley's method, you can even still use PIN assignments (but to be honest... sometimes it's nice to twiddle with the port pins are they were meant to be by ATMEL.

Yeah, I would have to relearn the pin numbers and stuff, but I'm willing to do that. Actually I think the whole Arduino pin numbering scheme what with the "analog pins" and "digital pins" is completely wack (you have to use a different pin number for the same pin, depending on whether it's pretending to be an "analog pin" or "digital pin" at the moment, not to mention the attachInterrupt() uses numbers that are different than the actual pin numbers.).

I somewhat disagree. You are not really missing much from the whole GCC world... there is just a nice blanket of insulation with the Arduino.

I'm fine with the blanket, it's just that IDE, man...maybe the Windows version is better or something, but the Arduino IDE is just not doing it for me.

A) Your code can be ported.

Cool! How? It can't be ported if it uses Arduino libraries, right? The only thing I really use is LiquidCrystal but I do use delay(), micros(), and so on.

You can certainly still use your Arduino board without the bootloader and code it with straight AVRGCC

Can I still use the FTDI cable to program it? I was worried that the necessary pins to program the chips weren't brought out on all Arduino boards. I use a lot of Arduino Pro Minis and Boarduinos, with the FTDI cable and pin header.

Think of AVRGCC as a next step if you are willing to "take a next step" and you also can come up with some real limitations you can't escape... (and I can't think of one right now...)

Only a couple things; I usually want to change PWM frequency, and I've been cooking up multi-encoder systems where polling all the various systems can be very complicated, and it almost seems like I would need multithreading to get anything done, but I read that you can have a a timer-overflow interrupt that can basically, outside of main loop execution, interrupt your main code every X amount of time and run some ISR, and that's almost kind of what I need, but I don't know how to do that in Arduino.

I nearly ALWAYS write my first draft of code using CONTEXT (http://www.contexteditor.org/) and I have an Arduno Highligher... basically tweaked "C" highlighter.

the whole Arduino pin numbering scheme what with the "analog pins" and "digital pins" is completely wack

Not if you take into account that they were trying to invent a better, cheaper Parallax STAMP.

Can I still use the FTDI cable to program it?

There are non-arduino bootladers, yes, There are dozens of bootloader options available at AVRFREAKS, for example. It's not unique to Arduino... but I much prefer NATIVE ICSP programming using an external programmer. But that's me.

RE:

Only a couple things...

Nearly all those things can be done with Arduino with some tweaks.

I wish I had a link to Smiley's Arduino (TAW/TCW) stuff. Sorry, it's not handy.

I'm confused as **** with this post.

First you say "Arduino is the only language I "know"." And later on you say that programs should be compiled through the command line. If you only know Arduino, how do you know that compiling through the command line is better ?
How do you even know C is better?

Now my two cents worth about it:

I didn't even started to learn the Arduino language... I knew C, I programmed AVRs before, so to me it really was limiting. I had seen the other side and the grass looked greener there.

Than being said, the main difference is that it is far easier to create libraries and separate functions in files with GCC than Arduino.

It is perfectly possible to upload without arduino and the best is that you can even use the arduino libraries in AVR-GCC. When you install arduino, everything you need is installed (except maybe the nice text editor). AVR-GCC is there, the makefiles used to compile programs are there, avr-dude is there to upload the software and the upload commands are in the makefile as well.
I notice that you need to manual reset the board everytime you want to download a new program, but that's not a big problem. :slight_smile:

Your code can be ported, it's gonna take some work, but it can be done.

So it's really about wanting to do it. I must, however, warn you that if you don't know microcontrollers (registers, prescalers, IOs, timers, etc, etc...) coding in C is gonna be a lot more difficult than Arduino.
In Arduino you never have to use any of the registers to accomplish something, while in C you must know what each register does and how it does. So it's not just a question about coding in a different way, you also need to know the hardware to effectively use C and achieve your "total freedom".

Give it a try, weigh the pros and cons again and decide.

I don't know your background, but I can guarantee you that learning all the stuff I said above is like driving a car. You learn once with one model and you can then drive any model you like.

I learned microcontrollers with a PIC, then used 8051 and jumped to AVR a few years ago and although PIC is very different in the way the registers are used, configuring a timer or ports is based on the same principle. So it's really an investment you put in towards microcontroller knowledge.

I'm trying not to pick sides...

Let me make one thing clear though.

I'm familiar with C. Heck, I have Microsoft C 1.04 that came on floppies, but that's another story. I've been messing with AVRGCC longer than the Arduino... I know how to work work both and I'm not a stranger to AVR STUDIO either. Heck, I even use BASCOM AVR sometimes.

So why do I prefer Arduino? Because it is all about rapid development and turning an idea into reality... fast. IDE... who cares... so it's not Visual Studio quality... I always look at it like "you get what you pay for" and this was free... so why complain. If it bugs me I can pursue fixing it. This is all open source... so better than Microsoft or Embarcadero or whoever... we can fix or at least try influence things with some hope of success.

CAN you move from Arduino to AVRGCC pretty easily? Sure. But you have to want to... that's how I see it.

You can compile Arduino sketches from the command-line and
download them using Make.

You can also program the boards in GCC using the ICSP using
gcc and make. Depending on how you do this you may overwrite
the bootloader but you can always put it back.

Most of time I use EMACS (which has the syntax highlighting), gcc
and make. I find programming in C is no more difficult than
Arduino.

(* jcl *)

http://www.wiblocks.com

I'm not a software guru at all, but I find most arguments about using the arduino IDE Vs using 'real C' a little strained at best and inaccurate at worst. I would think the only real valid argument one could make against the Arduino IDE is not liking it's built in text editor, but while I haven't tried it I'm pretty sure the IDE will let you utilize an external editior if you wish? I don't like the serial monitor all that much, but found I can use brey terminal program that I can leave opened on the desktop and attach and detach from the serial port at my whim.

Other then that I think that some don't understand that the arduino IDE just comes with lots of predefined C/C++ functions and libraries that one is free to use or not and one is free to include any of the library functions that AVR gcc offers. What is restricting about that? The IDE can be easily modified via configuration text files to even not use the bootloader on the avr chip, but rather use five different supported hardware ISP programmers, one of which is just a plain arduino board with a preloaded sketch.

What technically can one not do that the underlining gcc compiler will allow to do under some other ide or using direct command line control?

I feel that some of the more experienced programmers out there, coming to arduino for the first time, just must not appreciate, to us that came with no prior C/C++ knowledge, how easy and quick the learning curve is to be able to actually accomplish useful projects that the Arduino project and platform allows. They did a great job bring the C/C++ language and AVR programming to people outside the high priesthood. :wink:

I think sometimes the high priests protest too much?

Lefty

Because it is all about rapid development and turning an idea into reality... fast.

This says it all. And it is the main idea behind Arduino, proof of that is the reset when you connect the serial port. In any normal application, that wouldn't be there. But this is for prototyping and making something fast or without extended knowledge.
If you feel like you can take on C, go for it. Just keep in mind what I said previously.

Arduino can be useful to test hardware too... :slight_smile:

Ok, so while we are in preach mode: (evil grin) I say that anyone who thinks that they are NOT learning "valid" C language while using the Arduino is just off-base.

True: What you are not learning with Arduino is all the "NOT_EXACTLY C" pre-preocessor preamble mumbo-jumbo nor are you worrying about source of much geekness... the dreaded MAKEFILE. But that's actually kind of nice for a novice not to have to deal with. It would certainly scare so many off that Arduino would not BE what it is today... which is first and foremost a "teaching" tool and secondly a creativity "enablement" device.

If you want or need that kind of extra control head on over to AVRGCC, you have my permission to leave the comfort of the "bridge" and join Laforge down in warp engine engineering room where all the hard stuff gets done the hard way... OK, I might have said that just to be geeky...

I may have been misunderstood... I didn't say it wasn't C... but it is quite different than C.
For one, is the C++ alike way of organizaing functions in objects and methods.
There are no pointers (in any example, although it is perfectly possible to use them).

Using Arduino you're also bypassing the hardware configuration registers... which is a big part of the microcontroller functions and will have to be dealt with if you use C. Although it is not a C like feature or lack of, it is something that comes with it.

Isn't this supposed to be Geeks 'r us?

bypassing hardware configuration registers

To be honest... I think you will find many here who disagree with that...

To be honest... I think you will find many here who disagree with that...

Yes, many or few, but still disagree, nonetheless.

Using Arduino you're also bypassing the hardware configuration registers... which is a big part of the microcontroller functions and will have to be dealt with if you use C.

I disagree, nothing is bypassed. The arduino core does setup and maintain all the registers required to support the functions they provide that utilize them. It's not bypassed or hidden, they are all in the core source files (open source remember?) and one is free to study or modify them if they wish.

Just this weekend I posted a question about how one might address the analog A/D mux registers so that one could read the voltage value of the build-in bandgap reference. Coding Badly answered in a few mins, said that interested him and he would get back to me, and in a few hours later posted the steps I would need so I was able to write the function:

int getBandgap(void)
    {
     const long InternalReferenceVoltage = 1050L;  // Adust this value to your specific internal BG voltage x1000
        // REFS1 REFS0          --> 0 1, AVcc internal ref.
        // MUX3 MUX2 MUX1 MUX0  --> 1110 1.1V (VBG)
     ADMUX = (0<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0);
        // Start a conversion  
     ADCSRA |= _BV( ADSC );
        // Wait for it to complete
     while( ( (ADCSRA & (1<<ADSC)) != 0 ) );
        // Scale the value
     int results = (((InternalReferenceVoltage * 1023L) / ADC) + 5L) / 10L;
     return results;
    }

Wa-la, real C, using real avr register control. Using the Arduino IDE didn't prevent me from doing it. It's a rather obscure task, so wasn't included in the standard arduino core for the analogRead() function, but certainly something that was accomplished without having to leave the 'safety' of the Arduino IDE.

The key is I can read the Arduino reference documents and see what features it performs 'behind the curtains'. and if I understand that, what am I giving up to allow the arduino to continue doing for me?

I can't go into my backyard right now and start a fire using a flint and rock, but I understand the concept and if I really had a need to perform such a task I'm pretty sure I could accomplish it. It's just not worth my time as I have a nice IDE like feature called a match I can use that saves me time and effort, and makes me feel happy I'm not a caveman. :wink:

Lefty

If your main reason for feeling limited is the difficulty of editing large files, then try Arduino with Eclipse.

Eclipse is open source and must be about the most used integrated development environment in the world. Its built for big files.

I have not tried using Eclipse with Arduino, but I have heard its possible.

Ok... my point didn't come accross.

Have you tried the other way around?

write something like

setup(){}

outside main() in C and still work?

True, you can use all the registers to configure the Arduino chip in Arduino, but do you? I was trying to point out that once the Arduino IDE is left, you really need to know which register does what. And what about dependencies? I mean, you can configure the chip your own way, but you decide to use one of the Arduino objects that interfere with a timer or a pin... what then?

I'm not dissing Arduino, I'm just trying to point out the differences between them to the average user. That's it.

Ok, you are right. They made it too easy...

So maybe with a little explanation.... some progress can be made.

Here is a place to start: (I sorted of hinted at this earlier)

Joe really has figured this all out for us.

http://smileymicros.com/blog/2010/10/03/42/

And for even more fun... check out his articles in NUTS&VOLTS Magazine... or check out one of his downloads: http://smileymicros.com/download/Workshop%2010%20Source%20Code.zip for some interesting reading.

It was the objective of the thread...

It is true that it's really easy to get one and start writing something to turn our idea into reality. And Arduino serves its purpose brilliantly, but if someone wants to use C, then they need to be aware of everything that is behind the curtains in Arduino... because even the most basic things in arduino have some knowledge behind. The setup() and loop() functions, setting a pin, etc, etc...

And I'll confess, although not liking the language it is my prime choice to troubleshoot known hardware connected to the arduino. I mean, there it is, already implemented and tested by thousands of people... so every time I get some new hardware connected to the board, I pick up an example and run it to make sure everything's good with the hardware and if some problems appear with the C software, it is definitely a software problem. :slight_smile:

Does the title imply that there's a gay AVR-C?