Writing in C

Hello all,

I'm about to buy a Freeduino SB to learn more about embedded programming; I'm in mech eng, so I don't really get much exposure to circuits or microchips outside of class. It uses an ATmega328 which I'd love to use for my own custom applications like motor encoder feedback loops and speed differentials for tank steering.

From what I've read and heard, Arduino has its own IDE and programming language that rides upon another layer of abstraction below it. This intermediate layer translates Arduino code into something the microcontroller understands (whether it be to C or directly to bytecode, can't remember).

Is it possible to bypass that entirely and take full advantage of the 328's capabilities by writing in C? I really like how the board is plug-n-play, but eventually I will want to move on from the Arduino language. In terms of programming experience, I've done a little C++ and looked at some bits of C code that my friend writes from time to time. I'll need to be using C later on when I get a job, too.

This is in part a learning experiment and part a way for me to bring some skills to my university's robotics club, as I'm thinking of starting a project team for an autonomous rover racing competition next year that we're hosting.

Thanks,

G-bo

Sure, you can program in pure C.

From what I've read and heard, Arduino has its own IDE and programming language that rides upon another layer of abstraction below it. This intermediate layer translates Arduino code into something the microcontroller understands (whether it be to C or directly to bytecode, can't remember).

No, you have a fundamental misunderstanding. The Arduino doesn't have it's own 'programming language', it uses a rather standard open sourced C/C++ compiler called AVR gcc. The IDE is just a fancy text editor that sends your source code (the sketch) to the compiler to be end up with AVR machine language instructions that then burned into the AVR's flash memory via the serial USB link. The arduino IDE does some preprocessing steps prior to compilation that is specific to the arduino platform but it's pretty much details not needed to know to get started. The Arduino platform makes a lot of pre-written functions avalible (like subroutines in BASIC) that allow you to do things at a little higher level, such as using digitalRead(pin#); to read the state of a input pin. But you are not required to use these arduino supplied functions, and instead you may write and use your functions written in C.

So the only 'abstraction layer' there is are the many arduino pre-written C functions which you may or may not use as you choose, there is no on chip interpreter or byte code used, just pure C/C++ compiled to AVR machine instruction set and burned into non-volatile flash memory on the AVR chip.

So Arduino langauge is C/C++ with a lot of optional C/C++ functions and libraries thrown in.

That make sense?

Lefty

Hello Lefty,

Thanks for clearing that up. I'm finding more information on the FAQ, which is probably where I should've looked first.

So I can write in pure C? Sounds good. I've got some header files for printing to the PuTTY terminal, can the IDE add source files like AVR Studio? And I'm guessing I can use AVRdude to upload a hex file I create in AVR Studio to the Arduino just fine?

Sorry if these questions have been answered frequently, I'm still searching around before I buy my board and brick it.

Thanks again,

G-bo

G-bo: Hello Lefty,

Thanks for clearing that up. I'm finding more information on the FAQ, which is probably where I should've looked first.

So I can write in pure C? Sounds good. I've got some header files for printing to the PuTTY terminal,

Yes you can. however be sure to understand what specific function library files a specific header file might be dependent on. The basic C language is pretty small, but there are vast libraries differneces that many different compilers may utilize and that's where it can may take some 'porting' effort to work in the arduino enviroment.

can the IDE add source files like AVR Studio?

The IDE has tabbed edit windows where other C files can be placed and then included in with main sketch window. The Arduino IDE preprocessor will assemble all these files for complitation.

And I'm guessing I can use AVRdude to upload a hex file I create in AVR Studio to the Arduino just fine?

The IDE has no facitlity for direct uploading of a prior saved hex file, other then the one being compiled and uploaded with the IDE. However there is a way to find the location of the hex file after compilation (in a temp folder) and save it, and at a later time using AVRDUDE in command line mode, upload to the arduino board.

Sorry if these questions have been answered frequently, I'm still searching around before I buy my board and brick it.

Pretty hard to brick an arduino board. Most times if damaged, it's from electrical problem wiring to external components. Thanks again,

G-bo

Something else I would like to mention:

The purpose of the "base-level" Arduino libraries (such as what allows you to use "digitalRead()" for instance) is actually two-fold:

  1. It makes things a lot easier to develop, which has already been mentioned
  2. If you migrate to another supported (by the library) microcontroller, your existing code will likely port over with little to no changes needed.

Item 2 is the kicker here; what this means is that you could develop your software on an ATMega328 based Arduino, but then if you found you needed more pins or more memory (or conversely, if you found you needed less pins/memory!), you could easily move the code to say a ATMega644 or to an ATTiny84/85 (or something like that). The library, behind the scenes, does a lot of extra work for you to support this. Now, this means that a call to "digitalRead()" may not be the fastest, because of the overhead needed for various checks and such for this support. But at least you would have a pretty good chance of not having to rewrite a big portion of your code because you needed to move to a different microcontroller to support your needs.

The way I look at it, I would rather write my code, even if the resulting code isn't as fast as it could be, and end up with the steak - rather than the sizzle - first. Once I had tweaked the hell outta it to the point where it was the library code pieces that was holding me back, only then would I look into whether I needed to move to another platform, and whether I could just pare down the Arduino's library for that platform I chose, leaving only the code behind for the platform I planned on using (why reinvent the wheel?). If that still wasn't enough, then I would re-write -only- those pieces necessary to give me the speed or memory usage improvements I needed.

Remember - never prematurely optimize anything - save that for a future code pass. It was one the best lessons I learned a long time ago from writing game engine code (and I learned it from people who wrote game and demo code waaay better than I ever could)...