1

I want to learn core and mechanics of microcontrollers with Arduino, and I don't really know where to find good source of knowledge.

Every Arduino related tutorial is about very basic stuff like connecting the LCD display without explaining what every pin does. When it comes to programming tutorials only explain what functions are needed and do not explain what happens inside the hardware.

For example i know what delay() function does, and I know that I can find definition in the source files:

void delay(unsigned long ms)
{
    uint16_t start = (uint16_t)micros();

    while (ms > 0) {
        yield();
        if (((uint16_t)micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

But what I really want to know what that code does, and how to write own functions. I would appreciate if you could provide me with any starting point. Should I just follow the source files until I get it or is there any better reference?

I have Arduino Leonardo, and experience in programming in C/C++.

closed as too broad by Ignacio Vazquez-Abrams, dlu, Nick Gammon, TheDoctor, Greenonline Dec 29 '15 at 16:31

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

For information about the “core and mechanics of microcontrollers with Arduino”, there are numerous articles with lots of details on the arduino.cc forum. However, most of the code posted there sucks, as does most of the code posted here on Arduino Stackexchange. That's a natural consequence of people posting code that doesn't work and they wonder why.

For a reasonably organized and accurate body of information about Arduino hardware and software, see Nick Gammon's Arduino pages. Also see arduino.land pages. For better understanding of Arduino software internals, see garretlab's “Internal Structure of Arduino Software” pages, which are well organized and quite informative about the subjects they cover.

Regarding the specific delay() code shown in the question, first consider this slightly simpler version, from Arduino 1.6.3:

void delay(unsigned long ms) {
    uint16_t start = (uint16_t)micros();
    while (ms > 0) {
        if (((uint16_t)micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

Ignoring data type constraints, it's evident that the while loop counts down ms from its original value to zero. The burden of the if executes about once per millisecond, because start advances 1000 microseconds each time within the if.

The casts of micros() – ie, where uint16_t appears in parentheses before micros() – tell the compiler to throw away the upper two bytes of micros() [which is actually an unsigned long, or uint32_t] and treat it as an unsigned 16-bit integer. The low two bytes of micros() overflow every 65536 microseconds, on which occasions within a millisecond it will come to pass that although (uint16_t)micros() is less than start, due to use of unsigned arithmetic ((uint16_t)micros() - start) >= 1000.

The code you included has one more statement in the while loop:

    yield();

For most models of Arduino, yield() is an empty function, a do-nothing if compiled at all. On the Due, with Scheduler.h in use to support cooperative (non-preemptive) multitasking using multiple stacks, yield() gives the scheduler an opportunity to schedule another task, if one is ready to run. Note that if a “cooperative” but malignant task always takes just under 65536 microseconds to do its work, it can happen that delay() as shown never returns.

3

There are two main topics there: Digital Electronics, and C++ programming, and the Arduino happens to bridge the gap between the two.

That's why it's hard to find tutorials in the style you want. So instead of looking for Arduino tutorials that do what you want you need to split the topics apart and deal with them separately.

Search for C++ programming tutorials. That should help you understand the structure and syntax of a program better. Search for digital electronics tutorials. Good keywords are things like logic gates, combinational logic or digital interfacing and just digital electronics. That should help you understand things like logic levels, logical functions, inputs and outputs, and such like.

Once you have basic understanding in both fields you can begin to see how the two relate together. One of the most important skills any engineer has is understanding and interpreting datasheets. From datasheets you can find how the hardware operates and what you would need your program to do to make the hardware do what you want.

Not the answer you're looking for? Browse other questions tagged or ask your own question.