This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PM: Adjusting Timer Period at RunTime

Part Number: TM4C123GH6PM

Hello,

I am initializing a timer as below:

    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    MAP_IntMasterEnable();
    MAP_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    MAP_TimerLoadSet(TIMER0_BASE, TIMER_A, ui32SysClkFreq / 50);
    TimerIntRegister(TIMER0_BASE, TIMER_A, Timer0IntHandler);
    MAP_IntEnable(INT_TIMER0A);
    MAP_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    MAP_TimerEnable(TIMER0_BASE, TIMER_A);

Which calls Timer0IntHandler 50 times a second. Is there anyway I can adjust the frequency of the timer? I have looked at the peripheral library documentation and found:

Which says "if the timer is running then the value is immediately loaded into the timer" - so its period can be adjustied while running? Am I wrong?

Also, having a new load value set would mean the counter should reset, so the timers remainder ms restarts?

I am looking for a way to adjust the frequency minimally within +/- 1% on the fly, without having resetting the remainder time. Maybe I could add or substract from it?

Any ideas greatly appreciated.

Best Regards,

C.A.

  • Hi,

      Please refer to the datasheet on how the load and match values are updated. The load/match value can be update on the next cycle or after the next timeout. I will suggest you use the next time to have the new value take effect. Use TimerUpdateMode() to specify when the new value to take effect. 

    If software updates the GPTMTnILR or the GPTMTnPR register while the counter is counting down,
    the counter loads the new value on the next clock cycle and continues counting from the new value
    if the TnILD bit in the GPTMTnMR register is clear. If the TnILD bit is set, the counter loads the
    new value after the next timeout. If software updates the GPTMTnILR or the GPTMTnPR register
    while the counter is counting up, the timeout event is changed on the next cycle to the new value.
    If software updates the GPTM Timer n Value (GPTMTnV) register while the counter is counting up
    or down, the counter loads the new value on the next clock cycle and continues counting from the
    new value. If software updates the GPTMTnMATCHR or the GPTMTnPMR registers, the new values
    are reflected on the next clock cycle if the TnMRSU bit in the GPTMTnMR register is clear. If the
    TnMRSU bit is set, the new value will not take effect until the next timeout.

  • Hello,

    After setting timeout mode, to adjust frequency, would I need to use TimerLoadSet again, or would I need to access the registers mentioned with hwreg and modify them?

    I have looked at the source code of driverlib/timer.c and in timerLoadSet, it basically does:

    HWREG(ui32Base + TIMER_O_TAILR) = ui32Value;

    Best Regards,

    C.