Skip to content

Direct triggering of OneShotTimers

Compare
Choose a tag to compare
@luni64 luni64 released this 26 Aug 11:10
· 80 commits to master since this release

New Features

The OneShotTimers now provide a possibility to directly trigger them with a precalculated reload value. Since the normal trigger function involves a float calculation to convert the trigger time to the counter reload value it makes sense to precalculate it if you need to repeatedly trigger a signal with high frequency. This helps reducing processor load and timing errors for the slower processors like T3.2 which don't have a floating point unit. For a T4 the savings are quite insignificant.

void setup()
{
    pinMode(1, OUTPUT);

    OneShotTimer t1(GPT1);     // use  GPT1 for the test
    t1.begin([] { digitalWriteFast(1, LOW); });

    // normal triggering -----------------------------------------------------
    for (int i = 0; i < 3; i++)
    {
        digitalWriteFast(1, HIGH); // set pin
        t1.trigger(10);            // reset after 10µs
        delayMicroseconds(50);
    }
    delayMicroseconds(100);

    // direct triggering -----------------------------------------------------
    uint32_t reload;
    t1.getTriggerReload(10, &reload); // precalculate the reload value for a 10µs delay time

    for (int i = 0; i < 3; i++)
    {
        digitalWriteFast(1, HIGH); // set pin
        t1.triggerDirect(reload);  // reset after 10µs
        delayMicroseconds(50);
    }
}

void loop()
{   
}

image

-> For a T3.2 @96mhz the time to trigger reduces by about 2µs which can be significant for high speed applications.