Go Down

Topic: Programming in C (Read 5 times) previous topic - next topic

dhenry

Quote
Does anybody know of a link to an Arduino blink sketch for example, shown right next to a C program to do exactly the same function?


They are very similar. A "standard" C version would be something like this:

Code: [Select]

#include <avr/io.h>

void setup(void) {
//reset your chip here
}

void loop(void) {
//put your code here
}

int main(void) {
  setup();
  while (1) {
    loop();
  }
}
[/quote]

All the arduino ide does is to put your setup() and loop() into a canned main() and compile - you can check the temporary folder to see the recombined code.

For a successful career in embedded programming, you need two things:

1) a good understanding of C: structure and write bug-free code;
2) ability to read and understand datasheets: 90% of programming a mcu is to operate those registers.

The stock C-functions are not that helpful.

dhenry

A good toolchain to learn C on avr is the Code::Blocks + winavr combo.

Nick Gammon

dhenry has skipped over a few things here.

The standard included libraries initialize the timers, and in particular sets up Timer 0 so that it "ticks" every 1.024 mS, which is then used by the millis(), micros() and delay() function calls.

So if you were to totally bypass the IDE you would have to do that yourself. The init() function in the Arduino library is the one that does that. The file (wiring.c) also includes an interrupt handler to count those ticks.

However the answers above are correct. The Arduino "sketch" is treated as a C++ program (after a bit of text munging), so anything you can do in C++ you can do on the Arduino. Some compiler options disable some things, however, such as exception handling.

You are free to create new "tabs" in the IDE, and name your files xxx.c or xxx.cpp, giving you the ability to code in "straight" C or C++. Again, within the constraints of what compiler options are enabled, and what the included libraries allow. For example, the standard library does not let you write to stdout, because such a concept doesn't really exist on the microprocessor.


WizenedEE

For example, the standard library does not let you write to stdout, because such a concept doesn't really exist on the microprocessor.

unless you do this... http://arduino.cc/forum/index.php/topic,120440.0.html

Nick Gammon

Very nifty. However that's why I used the words "standard library". You can do all sorts of fancy things if you include the right libraries. That's one of its strengths. :)

Go Up