Is there a way or tutorial for converting Arduino code to C code?

十年热恋 提交于 2020-07-17 05:50:20

问题


I know that this question is general, but I couldn't find a tutorial or a good coding way to convert Arduino code (I mean the code that we are writing on Arduino software and it doesn't matter for Arduino Uno or Mega or ... ) even if a small sample.

Is there a tutorial?

I just want to learn the technique, and I know that it depends on the project.


回答1:


Arduino code is, more or less, C code.

The unique things that happen with Arduino is that the code is preprocessed (for example, they give simple hooks by establishing the setup and loop functions) and has a managed build/upload process that takes care of board limits, includes, libraries, etc...

It is certainly possible to use the same toolkit yourself to build and run the code, that's how I do it. Arduino and GCC, compiling and uploading programs using only makefiles is the most useful link I've found covering the steps you need to get started.

As I said, I've left the Arduino IDE and taken up the avr-gcc route myself, because if you know the GNU tools, you can do much more powerful things - like use the C++ standard libraries. I missed my vectors, what can I say. avr-libc is lagging quite a bit when it comes to full C++ functionality, making it hard to cram in the STL, but Andy Brown has gotten much of it working.

Throw those things together and you have quite a powerful development environment.




回答2:


(Edit: I missed the very useful link in Matt's answer while writing my own answer. So let's say that what is below is a non technical summary of the link).

Matt answered right. I would like to add additional information.

The key to understand Arduino programming is in the directory Arduino-1.0\hardware\arduino\cores\arduino

You will find the main.cpp file containing:

#include <Arduino.h>

int main(void)
{
    init();

    #if defined(USBCON)
        USB.attach();
    #endif

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}

Do "setup()" and "loop()" (and even "serialEventRun() if you read the Arduino documentation) ring a bell? :-)

Arduino hides only that.

Arduino uses the C++ language. Of course, you can use C if you compile your code yourself with avr-gcc, but the way the Arduino IDE is configured, it is pure C++.

However, as microcontrollers are not really adapted to object-oriented development, some features are missing. I think about the "new" and "delete" operators. They do not exist, so out of the box, you should avoid using the heap when you develop with Arduino. It is why you should be careful if you want to use some standard C++ libraries. They may not be adapted for microcontroller programming (too many memory operations).

In the Arduino-1.0\hardware\arduino\cores\arduino directory, you can also see the implementation of the Arduino library. It allows watching which "low-level" microcontroller functions (from avr-libc) exist and how you can implement your own libraries and tools.

By the way, as you wanted to know how you could port Arduino code in C, avr-libc is a C library and not a C++ one. So you can see how Arduino wraps its C++ code over C code.

In the File/Preferences menu, you can check "see verbose output" to see which parameters and files are used to build the final Arduino binary (and where the temporary build directory is).

Finally, you must also know that Arduino boards have a bootloader embedded with your code. It eases the deployment from the Arduino IDE to the Arduino board. So the Arduino board actually contains more code than your own.




回答3:


It's not a conversion tutorial but a very nice write up about what's behind arduino's hello world:
A tour of the Arduino internals. How does hello world actually work?



来源:https://stackoverflow.com/questions/10307496/is-there-a-way-or-tutorial-for-converting-arduino-code-to-c-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!