Arduino Nano Timers

I want to know more about Arduino Nano Timers .

  • What timers are there?
  • Do interrupts produce?
  • What code will the interrupt handler attach to them?
  • How are implemented delay()and delayMicroseconds()...
    • Do they use timer interrupts? (If so, how can I execute other code during this?)
    • Or are they polled repeatedly until the timer reaches a certain value?
    • Or do they increase the number X times?
    • Or do they do it differently?
+5
source share
1 answer

The best way to think about Arduino Nano timers is to think about timers in a basic chip: ATmega328 . It has three timers:

  • Timer 0: 8 bit, PWM on pin contacts 11 and 12
  • 1:16 , 15 16
  • 2: 8-, 17 5

:

  • " " , , , .
  • ,

, Arduino . , . , :

ISR(TIMER1_OVF_vect) {
   ...
}

1. TIMSK1. :

TIMSK1 |= (1<<TOIE1);

TIMSK1 |= BV(TOIE1);

TOIE1 ( 1, ) TIMSK1. , , ISR(TIMER1_OVF_vect) .


Arduino delay() (wiring.c):

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

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

micros(), timer0. Arduino timer0 , , count0 - , millis() .

delayMicroseconds(), , ; ; nop() ( ), . Arduino Nano 16 , :

// For a one-microsecond delay, simply return. The overhead
// of the function call yields a delay of approximately 1 1/8 ยตs.
if (--us == 0)
    return;

// The following loop takes a quarter of a microsecond (4 cycles)
// per iteration, so execute it four times for each microsecond of
// delay requested.
us <<= 2;

// Account for the time taken in the proceeding commands.
us -= 2;

:

  • 1 ( - )
  • .
+17

All Articles