Basic example how to use timer-interrupts

recently I wrote a demo-code for Arduino Uno how to configure timer2 to invoke interrupts with a regular frequency for creating stepper-motor-pulses.

After some search I did find this demo file

    #include <TimerTCC0.h>

    bool isLEDOn = false;
    char time = 0;

    void setup() 
    {
    //SerialUSB.begin(115200);
    //while(!SerialUSB);

    pinMode(13, OUTPUT);    

    TimerTcc0.initialize(1000000);
    TimerTcc0.attachInterrupt(timerIsr);
    }
     
    void loop()
    {
    /*
    time ++;
    if(time == 10)TimerTcc0.detachInterrupt();
    else if(time == 20)
    {
       TimerTcc0.attachInterrupt(timerIsr);
       time = 0;
    }
    delay(1000);
    */
    }

    void timerIsr()
    {    
    digitalWrite(13, isLEDOn);
    isLEDOn = !isLEDOn;
    }

With this demo-code it is the same like with most other demo-codes
very poor documentation.

there is the function-call
TimerTcc0.initialize(1000000);

initialize timer with 1 million. So what does this exactly mean?
Count up to one million before an interrupt is invoked?
Where can I find information how to calculate a “calling-frequency”

best regards Stefan

P.S. This forum looks very similar to the new arduino-forum
but formatting behaves different. What do I have to write to make an entire sketch appearing as a code-section?

The header file. This is the method declaration:

void initialize(long microseconds = 1000000);

So the example which you have put together/found does not even need to supply an argument because it is simply using the default value.

Just specify the time period in microseconds.

long HALF_A_SECOND  =   500000;
long TWO_SECONDS    =  2000000;
long ONE_MINUTE     = 60000000;

Surround the code with three tick (`) symbols, for example:

```C
#include <Arduino.h>
void setup() { }
void loop() { }
```

will look like this:

#include <Arduino.h>
void setup() { }
void loop() { }

Hi Bill Thank you for answering.

best regards Stefan

1 Like