Change timer value on the fly
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 ?
+1
+1
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?
+1 got it working with .setfrequency, then doing .start() but this seems rather inefficient.
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?
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.
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
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.