ivanseidel / DueTimer

⏳ Timer Library fully implemented for Arduino DUE
MIT License
212 stars 89 forks source link

Change timer value on the fly #72

Open 2kohm opened 5 years ago

2kohm commented 5 years ago

Hello, i want to change the timer intervall on the fly

i generate the intervall time with an formula

int myVar = 0;
double ticks = 60000000.0 / (myVar * 96.0);

void clock1()
    {
        // do stuff here
    }

void setup()
{
    Timer2.attachInterrupt(clock1); 
}

void loop () 
{
    if ( something == 1) 
    {
        Timer2.start(ticks);
    }

    if ( somethingElse)  
        {
            myVar = 120;
        }
    }
}

how can i update the changed value on the fly? do i have to stop and start the Timer again ?

henriksod commented 3 years ago

+1

budryerson commented 3 years ago

Count me in! I start with this framework:

void T3_Handler()
{
    // do something
}

void setup()
{
    Timer3.attachInterrupt( T3_Handler);
    Timer3.start( 500000);   // timer interrupts every 0.5 seconds
}

void loop()
{
    delay( 2000);                  // do something 4 times
    Timer3.setFrequency( 100000);  // set timer to interrupt every 0.1 seconds
    delay( 2000);                  // do something 20 times
    Timer3.setFrequency( 500000);  // set timer back to 0.5 seconds
}

That does not work. In fact, a sketch like this will just seize up like a cheap watch. So I tried: Timer3.setFrequency(100000).start() And I tried: Timer3.setFrequency(100000); Timer3.start() And I tried: Timer3.start(100000); None of it works! I want to accelerate a stepper motor. a) What is going wrong; and b) What am I supposed to do?

Shoxx98 commented 2 years ago

+1 got it working with .setfrequency, then doing .start() but this seems rather inefficient.

ivanseidel commented 2 years ago

Maybe setFrequency could call .start at the end automatically. And, if a frequency <= 0 was assigned to the parameter, .stop would be called.

Can anyone test if calling .start works after all?

iddq commented 2 years ago

setFrequency calls TC_Configure which disables clock and interrupts. Although interrupts are re-enabled by setFrequency later, the clock is still disabled which can be enabled by TC_Start.

Shoxx98 commented 1 year ago

has anyone figured out how to stop the disabling of the clock? I am messing around with it rn and simply commenting out pTcCh->TC_CCR = TC_CCR_CLKDIS ; in TC_Configure Edit: "makes it stop entirely" -> doesnt do it on its own

iddq commented 1 year ago

You have to recompile libsam after the modification of tc.c

or you can simply change RC by TC_SetRC function.

In both cases, CV, counter value must be checked (see. TC_ReadCV) and if it is greater than RC, you must reset it (see. TC_Start), otherwise the next interrupt occurs only when CV wraps around, which can be several hours depending on the clock setting.