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.

Help on implementing a delay with timer on tm4c123glx

I have read post on making an accurate delay function and not sysctldelay But none gave I certain solution with code.

on a post an employee said something about :"Better way would be to use a WDT to fire a NMI which will always be serviced in a fixed amount of time and that can be factored into the wait time"


can you explain it and give an example?

i was thinking on setting timer to the half frequency and use a Boolean on the delay function.Everytime the bool is true then the delay will decrease the number of time(given in argument) till it reaches zero. The interupt handler will change the state of Boolean so the half cases the delay function will wait. The only thing I am not sure is how to make delay function to decrease one time the number when the trigger sets it in the condition that decrement happens.

also there is a site that does something with timer and sysctldelay and a volatile variable Millis that was I cemented according sysctldelay and timer uses a subtraction of Millis and a temp store of itsself To check that is smaller that the delay time asked.I will try to find the site to post it.

is there any ready solution?

  • Hi,

    Kyriakos Pavlidis said:
    also there is a site that does something with timer and sysctldelay and a volatile variable Millis that was I cemented according sysctldelay and timer uses a subtraction of Millis and a temp store of itsself To check that is smaller that the delay time asked.I will try to find the site to post it.

    Is it mine?

    Here is a example with a timer example. It keeps the time in milliseconds and it has a delay function.

    https://sites.google.com/site/luiselectronicprojects/tutorials/tiva-tutorials/tiva-general-purpose-timers/timer-delay

    The idea is, you have a interrupt every 1ms. There you increment a volatile variable called Millis. So with that you now have a way to keep track of the milliseconds, right?
    Now since we have a way to keep track of the time let's make a function that waits a amount of time passed by parameter.
    How should that function work (call it "Wait"? Let's consider that the function takes the parameter "time"

    • First store the current time in a variable, "temp" for example.
    • Second, do a while until the the current time - "temp" > "temp"

    You are just calculating inside the "while" the interval of time since entering that loop. When the desired time is passed, then it exits the "while" and the function Wait".

    But you have a dedicated timer for that, the system tick, thought it's not like you will run out of timers on the Tiva (it has so many)
    Here is a example using the system tick. It's a code that i use it as a template.
    https://sites.google.com/site/luiselectronicprojects/tutorials/tiva-tutorials/create-a-tiva-project

  • Yes it was yours :)

    I have a question. i thought that this has to be done this way. i misunderstood the  function for the interupt of the timer with the general systick. Now i saw that was the interrupt handler for the timer and not for systick.

    can you explain the code of the interupt? I have read the driverlib pdf with the functions but i am sure about  this.

    the code is 

    void SysTickInt(void)
    {
      uint32_t status=0;
    
      status = TimerIntStatus(TIMER5_BASE,true);
      TimerIntClear(TIMER5_BASE,status);
      millis++;
    }

    i am not sure what is the meaning of the status. what is the meaning of the returning type of TimerIntStatus?Isn't it a flag setted in a uint32? Why ? Isn't the flag set to 1 when the interupt happens?Why not to use a boolean instead of a int.

    
    

    And basic  what is the difference with:

    TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

    Why do you use the status variable? What is the different between the variable and the macro that is used in the lanchpads workshop from ti?

    
    

  • Those are good questions,

    If you check the source code the function TimerIntStatus returns a uin32_t value. But why is that?
    The Tiva is based on a ARM-M4 which is a 32bit processor. So the registers are 32bits in length. The functions in TivaWare work directly with the registers (after some parameter checking). 
    The TimerIntStatus, when the 2nd parameter is TRUE returns the value of the register GPTMIMR which is 32bits in length (even though not all 32bits have useful data.
    That register hold all the masked flags.

    All interrupts (on the same timer) go to the same handler when a masked interrupt occurs.

    You need to check which one happened.
    Could be interrupt X or interrupt Y, or both! 
    So then when you clear the interrupt, instead of choosing which flags, just clear any that is set to 1.
    To check which interrupt occur you check the status variable, Ex:

    if( (status & TIMER_TIMA_TIMEOUT) == TIMER_TIMA_TIMEOUT){
    
    //your code
    
    }
    
    //check other flags...
    else if(//imagine here other check like the first){
    
    
    }
    
    //end of code
    
    

     

    Although in this case there should be only 1 source of interrupt, i find it good practice to always do this to keep this in my head, and everyone's learning from my examples, this method.

     

    I hope you were clarified and didn't get more confused. 

     

    Note: When the second parameter of TimerIntStatus is false, then it returns the raw interrupt register. Those work like the masked when enabled but they don't call a handler (it's the NVIC that calls handlers but i think you understand)

     

     

     

     

  • Kyriakos Pavlidis said:
    i am not sure what is the meaning of the status. what is the meaning of the returning type of TimerIntStatus?Isn't it a flag setted in a uint32? Why ? Isn't the flag set to 1 when the interupt happens?Why not to use a boolean instead of a int.

    Here is just 1 more thing to clarify a bit more. The interrupt flag is set on a 32bit register. But you seem to think that the TimerIntStatus will return the flag state? Look what is returned by the function:

    You can see that there are reserved bits, those don't matter, never touch them.
    You can see all the other flags. In that example i use timer A. That flag is the bit called TATOIM, bit0. So probably a bool would kinda work :p But don't do that