ENع
→setup() and loop()
الدرس 04 · Getting started with a microcontroller

setup() and loop(): the superloop

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

10 دقائق10 خطوات5 أسئلةBeginner
عن هذا الدرس

An Arduino sketch has no main(), yet it runs. Follow an Arduino Uno R3 from power on: startup code sets your globals, the Arduino core's hidden main() calls init(), then your setup() once, then your loop() again and again inside for (;;). One pass at a time, a global count goes up and the LED on pin 13 turns on and off. Set up once, then loop forever: that shape is called the superloop. The C++ basics lessons cover the language itself; here we stay with how the firmware runs.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 4 · تفاعلي
01 / 10

Power on or reset

Plug in the Uno, or press reset, and the chip starts at its reset address. On the Uno that is a tiny program called a bootloader, kept at the end of flash: it runs first, then jumps to your code at the start of flash.

board   Arduino Uno R3  (ATmega328P, 16 MHz)
power on or reset  ->  jump to the reset address
bootloader  Optiboot, last 512 bytes of flash
            (BOOTRST fuse set: reset starts here)
            runs first, then jumps to address 0
your code   at the start of flash
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Power on or reset

    Plug in the Uno, or press reset, and the chip starts at its reset address. On the Uno that is a tiny program called a bootloader, kept at the end of flash: it runs first, then jumps to your code at the start of flash.

    board   Arduino Uno R3  (ATmega328P, 16 MHz)
    power on or reset  ->  jump to the reset address
    bootloader  Optiboot, last 512 bytes of flash
                (BOOTRST fuse set: reset starts here)
                runs first, then jumps to address 0
    your code   at the start of flash
  2. Globals get their first values

    Before any of your functions run, startup code gets memory ready. Global variables, declared outside every function, get their first values: count = 0 is written into SRAM. A global stays there for the whole run.

    sketch line 1   int count = 0;
    global   declared outside every function
    startup code (before main) clears it to 0
    SRAM     count = 0, kept until power off
  3. The hidden main()

    Every C++ program starts in a function called main(). Your sketch has none: the Arduino core supplies it, hidden from you. The arrow marks the line running now. This is where the code you can see begins.

    C++ rule   every program starts in main()
    sketch     has no main()
    core       cores/arduino/main.cpp supplies it
    
    int main() {
      init();
      setup();
      for (;;) {
        loop();
      }
    }
  4. init() starts the timers

    main() first calls init(). It sets up the chip's timers. Timer0 counts clock ticks and interrupts the program every 1.024 ms, so millis() and delay() can tell the time. init() also turns interrupts on.

    main line 2   init();
    sei()         interrupts on
    Timer0        1 tick per 64 clock cycles
                  overflow every 256 ticks
                  64 x 256 = 16,384 cycles = 1,024 µs at 16 MHz
    millis()      counts those overflows
  5. setup() runs once

    Next, main() calls setup(). The arrow jumps into your sketch. pinMode(13, OUTPUT) makes pin 13, wired to the board's LED, an output. Then setup() ends and the arrow returns to main(). setup() runs once per power on or reset.

    main line 3     setup();       jump into the sketch
    sketch line 3   pinMode(13, OUTPUT);
    pin 13           now an output, starts LOW (LED off)
    sketch line 4   }              return to main()
    setup()  once per power on or reset
  6. for (;;) calls loop()

    Now main() reaches for (;;): a loop with no exit test, so it never ends. Inside, it calls loop(). The first line adds one to count: 0 + 1 = 1, written back into SRAM.

    main line 4     for (;;) {     no test: repeats forever
    main line 5       loop();      jump into the sketch
    sketch line 6   count = count + 1;
                    count = 0 + 1 = 1
  7. Pin 13 HIGH: LED on

    digitalWrite(13, count % 2). The % sign gives the remainder after dividing: 1 % 2 = 1. Writing 1, which is HIGH, puts 5 volts on pin 13 and the LED turns on.

    sketch line 7   digitalWrite(13, count % 2);
    count % 2       1 % 2 = 1  (remainder)
    1 = HIGH        pin 13 at 5 V, LED on
  8. delay(500) waits

    delay(500) waits 500 milliseconds, half a second. Your code does nothing else meanwhile: delay() just watches the time Timer0 keeps. Interrupts still run, but no other line of your sketch can.

    sketch line 8   delay(500);
    waits           500 ms, about 488 Timer0 overflows
    meanwhile       interrupts run, your sketch does not
  9. Round again: LED off

    loop() ends and returns to main(). The for (;;) goes round and calls loop() again. count still holds 1, because globals keep their values: 1 + 1 = 2, and 2 % 2 = 0, so the LED turns off.

    sketch line 9   }              return to main()
    main line 4     for (;;)       round again
    main line 5     loop();        back into the sketch
    sketch line 6   count = 1 + 1 = 2
    sketch line 7   2 % 2 = 0 = LOW, LED off
  10. The superloop

    This is the superloop: set up once, then run one loop forever, with no operating system. Each pass does every job in turn, here about half a second per pass. So a slow job delays every other job.

    superloop   setup once, then loop forever
    pass        about 500 ms (mostly the delay)
    LED         on one pass, off the next: a 1,000 ms blink
      count 1  ->  1 % 2 = 1  ->  LED on
      count 2  ->  2 % 2 = 0  ->  LED off
      count 3  ->  3 % 2 = 1  ->  LED on
      count 4  ->  4 % 2 = 0  ->  LED off
    one slow job delays every other job

اختبار يتاح بعد الخطوة الأخيرة.