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.

Tiva C Timer



I build a program using ccs V5.5 & want to calculate the time interval of this program.i found out that ccs internal clock in not working for tiva c so i opted for timer but i don't know which timer to use, how to use & mode of timer..I am new to Tiva C.

  • Hi,

    Let's rephrase first your goal: you need to know execution time of a program or of a part of it - this can be done with timer, but there is not a general recipe for that. Think about interrupts: at 80MHz, some are very short, in microseconds range, some are longer while a general program can cycle at the second range. 

    Some recomendations goes for System timer set for interrupts at one millisecond or longer and then counting in a static variable the number of interrupts. This will be good for longer programs while inappropriate for interrupts.

    To measure that, it is better to use another techniques, such as direct measurement by using a GPIO pin set for output, such as at the entry in interrupt set the pin high and at the exit set the pin low, and use an oscilloscope to measure the time interval.

    If you have many interrupts, use a separate pin for each interrupt so you can see their behavior on several channels.

    For more precise measurements there is also a special technique which uses some hidden hardware existent inside your micro, but this later, I need to find out my notices about that.

    To respond to you also to your questions from another thread:

    To measure the current consumption, on your board there is  a small resistor in series with Vdd - measure the voltage drop instead calculations (for that we do not have all needed data).

    For code size - set the compiler to generate a map file, which shows you everything.

    Petrei

  • Thank you for suggestions but for 1st stage i want to calculate time.can you tell me a simpler & precise way of doing it

  • Hi,

    About your words: …want to calculate time… if you mean by that summing up execution time for each instruction - you may use the simulator for that, but best is to measure because measurement takes into account the flash access time which cannot be predicted by you - so the measurement in cycle time is below:

    Insert in your file (at right places):

    #include "inc/hw_nvic.h"

    #include "inc/hw_memmap.h"

    #define DWT_O_CYCCNT 0x00000004

    static        uint32_t                  c_start, c_stop;

    void EnableTiming(void){

    static int enabled = 0;

    if (!enabled){

       HWREG(NVIC_DBG_INT) |= 0x01000000;            /*enable TRCENA bit in NVIC_DBG_INT*/

       HWREG(DWT_BASE + DWT_O_CYCCNT) = 0;   /* reset the counter */

       HWREG(DWT_BASE) |= 0x01;                                 /* enable the counter */

       enabled = 1;

     }

    }

    and then, in the code to be examined write these, after a call of EnableTimig():

     c_start = HWREG(DWT_BASE + DWT_O_CYCCNT); // at the beginning of the code

     // your code follows here

     c_stop = HWREG(DWT_BASE + DWT_O_CYCCNT); // at the end of the code

    then c_stop - c_start is the execution number of cycles for the code; just multiply with clock period to find out the execution time of the code.

    Petrei

  • This is what i am Getting....The compiler is showing that the expression must have a modifiable lvalue

  • Hi,

    I cannot see all yours #includes - please add this if not already added:

    #include "inc/hw_types.h"       /* for definition of HWREG */

    Petrei

  • when i run the code initially the value of c_start & c_stop is 0 & after execution the value of c_start is 17 & c_stop is 0 & my program is taking around 1 minute to execute.is these value that i am getting is correct?? & what should i do to calculate time & are these 17 clock cycles??

  • Hi,

    First, depends when you started and when you stopped. But you must take into account other aspects: that register (dbg) is 32 bits long, so 2^32 times the clock period will give you a maximum length of correct measurements, which can be in the range of seconds, depends on your settings.

    If this is the case, then this ultra precise measurement should be avoided and replace by the system timer set at one or ten milliseconds. Take care how you start it and stop it.

    Another approach can be to measure small portions of your code, the most important ones. Choose whatever is convenient to you.

    Petrei

  • Hii,

    Thanx for the reply..I want to use the timer option with 1 millisecond.I want that a variable or timer initialized at 0 in the beginning should show me the total no of clock cycles or time of exectuion of complete program at the end.can you help me in programming that?..can we use inbuilt timers to calculate these? If yes then how to program them?

  • hii,

    I had set the breakpoint at wrong position.this this i had set the breakpoint on return statement of main function & got the values for c_Start as 17 & c_stop as 660950030..are these values of any use? can we calculate time or no of clock cycles from these values?

  • what is the default clock period for tiva c??

  • Hi Rajat,

    If the code does not call any SysCtlClockSet function then PIOSC of 16MHz is the default clock for the TIVA-C devices.

    with respect to the timer.

    The default timer clock is the system clock. So whatever you set the system clock frequency as is the timer clock as well on TM4C123 devices. E.g for 80MHz system clock the clock time period is 12.5ns, which would be the default clock for timer.

    Regards

    Amit

  • Hii Amit,

    Thanx for your help & i used 16 MHz as default to calculate time & i am getting the approx correct answer.