Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change timer value on the fly #72

Open
2kohm opened this issue Nov 30, 2019 · 7 comments
Open

Change timer value on the fly #72

2kohm opened this issue Nov 30, 2019 · 7 comments

Comments

@2kohm
Copy link

2kohm commented Nov 30, 2019

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
Copy link

+1

@budryerson
Copy link

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
Copy link

Shoxx98 commented Nov 11, 2022

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

@ivanseidel
Copy link
Owner

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
Copy link

iddq commented Nov 14, 2022

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
Copy link

Shoxx98 commented Dec 16, 2022

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
Copy link

iddq commented Dec 17, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants