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.

How to trigger interrupts on two different load values in Timer0 (TM4C123GH6PMI)

I am having a lot of problems with programming the timers on the TM4C123GH6PMI on my Tiva C Series Launchpad.

Basically I want to trigger interrupts corresponding with two different load values in Timer0. The Timer is going to run periodically once it is started. I want the timer to tick every one microsecond so I can have periods from 1 us to 65.6 ms with 16-bit load-values.

I have set up a simple periodic timer with an interrupt handler and it works so far. However, I am very much unsure how to calculate the prescaler and load value for a given time period. Do I need to run Timer0 in dual 16-bit split mode in order to make it work with two different load values?

I would like an example how to set this up using the TivaWare library functions.

Any help is appreciated - thanks in advance :-)

  • Hello Peter,

    There are 2 alternatives

    1. You can use the timer in 32 bit periodic mode
    2. You can use the 32 bit SysTick Timer

    Regards
    Amit
  • Thanks. My problem is how to trigger interrupts when the timer reach specific values, not just when it overflows. How can I do that?
  • Hello Peter

    Then you need to use the MATCH interrupt. You can load the MATCH value to trigger an interrupt when during the count from 0 to N when the value matches the TIMER MATCH register value an interrupt is generated

    Regards
    Amit
  • You are running multiple time periods? You've got a lot of timers on this micro, you could use more than one.

    Robert
  • Yes I am trying to do that. One solution would be to have two timers (A and B) synchronized with each other and two different load values, running in one shot mode. When Timer A overflows, Timer B is started and similar, when Timer B overflows, Timer A is started. Then I have two time periods with corresponding interrupt handlers.

    The problem here is that the periods will be a little longer since it will take some time to start the timer inside the interrupt handler.

    So unless this can be done automatically/internally somehow, I think the MATCH mode is better. If I only knew how to do it....
  • Thank you. That is what I have been trying to do for the past 5 hours or so... the TM4C123 datasheet is not very helpful, nor is the TivaWare documentation. I really need an example how to set up Timer0 in MATCH mode. Thanks in advance :-)
  • Hello Peter,

    From the data sheet, under Timer Modes -> One-Shot/Periodic Timer Mode...

    By setting the TnMIE bit in the GPTMTnMR register, an interrupt condition can also be generated
    when the Timer value equals the value loaded into the GPTM Timer n Match (GPTMTnMATCHR)
    and GPTM Timer n Prescale Match (GPTMTnPMR) registers. This

    Regards
    Amit
  • Ah, it wasn't clear you needed them synchronised. Match is definitely better in that case.

    Robert
  • Thanks again. Does this mean I can't do this with the TivaWare library functions?

    I have this code right now and it only triggers the interrupt when Timer0 timeouts (or overflows).

    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    TimerClockSourceSet(TIMER0_BASE, TIMER_CLOCK_SYSTEM);
    TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC);
    TimerPrescaleSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/1000000);
    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT | TIMER_CAPA_MATCH);
    IntEnable(INT_TIMER0A);
    TimerLoadSet(TIMER0_BASE, TIMER_A, 10000);
    TimerMatchSet(TIMER0_BASE, TIMER_A, 100);
    TimerEnable(TIMER0_BASE, TIMER_A);

    I am confused by the TIMER_CAPA_MATCH flag. CAPA refers to "Capture A". I would like a flag like "TIMER_A_MATCH" since I do not want to capture anything. I am not using the "edge count capture" or "edge time capture" mode and I have a bad feeling that the TIMER_CAPA_MATCH is not what I want. Could it really be the case that TivaWare does not support MATCH mode I am looking for?
  • Hello Peter,

    The correct define to use is TIMER_TIMA_MATCH for Timer Match. Please note that when setting the timer prescalar you would need to set the Timer Prescalar match value as well.

    Regards
    Amit