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.

MSP430 program execution time since it started

Other Parts Discussed in Thread: ENERGIA

Hi,All,

In Arduino, there is a handy function called "micros()" which return you the microseconds since the program started running. I am just wondering if there is any equivalent function in MSP430? If not, how can I do a quick implementation of this function?

Thanks a Lot!

  • Normally you would have to write your own routine for that but you can also try to use Energia: http://energia.nu/Micros.html (Arguino-like IDE for MSP430s).

    Regards,
    Maciej 

  • Ben Ma said:
    If not, how can I do a quick implementation of this function?

    1. Program a timer to generate an interrupt every 1 microsecond (configuration depends on your specific system and clock rate).

    2. Have an ISR for the timer that increments a static unsigned long variable (that you initialized to 0 at program start).

    3. Write a micros() function that returns the value of the counter variable.

  • Brian Boorman said:
    Program a timer to generate an interrupt every 1 microsecond (configuration depends on your specific system and clock rate).

    It will be a lot safer to generate interrupt at a lower rate.

    From Arduino

    micros()

    Description

    Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes. On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds.

  • " ... It will be a lot safer to generate interrupt at a lower rate. ... "

    But on the other hand, doing useless things could prevent one from doing harmful or unsafe things.
    Serving ISR every microsecond could easily use up all the CPU cycles and renders it useless.
    This could be a blessing in disguise!

  • Sorry, don't really get it. Shouldn't it be wasteful if CPU is doing useless thing (1 us ISR) all the time? I was thinking of using a counter by tracking the number of overflows. Could that work? 

  • MaciejKucia said:

    Normally you would have to write your own routine for that but you can also try to use Energia: http://energia.nu/Micros.html (Arguino-like IDE for MSP430s).

    Regards,
    Maciej 

    I am using CCS V4 IDE and build tools V3.2, I looked into "energia" and  it does have a micros() function, just like Arduino. My next question is, how can I use it in my CCS project? Thanks.

  • I do not know how useful micros() is. I have two objections. One is it starts “since the program started running”. Instead, starts “since the year Jesus was born”, or starts “since pin #1 becomes high” would have been more meaningful.

    My other objection is using microseconds as the units. It is too small to record some events and not small enough for some others.

    My suggestion is to have multiple functions like that. One of them may be a RTC, which returns year, month, day, hour, minutes and seconds. Others may behave like stopwatches. They return time intervals between “hardware start event” and “hardware stop event”. The resolution may be down to 1/25 of a microsecond.

    Yes, I agree with you about too frequent interrupts being wasteful. Actually, that was my point of saying it is useless. Using a 16-bit “Timer”, one could capture time intervals down to 1/25 of a microsecond and that will only generate an overflow interrupt every ~2600 microsecond.

  • old_cow_yellow said:

    I do not know how useful micros() is. I have two objections. One is it starts “since the program started running”. Instead, starts “since the year Jesus was born”, or starts “since pin #1 becomes high” would have been more meaningful.

    ...

    Is it possible to return the time intervals between "software start event" and "software stop event"? I know how to generate an interrupt when an I/O pin is high/low, but what if the event is just a software state, for example, a variable's value is less than a pre-defined constant? Thanks a lot for the help. 

  • Ben Ma said:
    Is it possible to return the time intervals between "software start event" and "software stop event"? I know how to generate an interrupt when an I/O pin is high/low, but what if the event is just a software state, for example, a variable's value is less than a pre-defined constant?

    One way (not a very good one) to do that is to scan the source code to find all the statements that change the variable. Insert an if statement after each such occurrence to check the value. Recompile it and run.

    What is bad is that when you insert those if statements, you "disturbed" the timing of the software and can only get a distorted result.

  • Peter Dvorak said:
    It will be a lot safer to generate interrupt at a lower rate.

    Well, you can user the timer counter itself directly for the lower 16 bit, counting microseconds or CPU cycles, depending on clock source and divider. Then the interrupt comes only ever timer overflow, to increment the upper 16 bits of the 32 bit counter.
    The mircros() funciton then assembles the two (taking care of any overflow-related racing condition, by checking the timer for being near overflow point)

**Attention** This is a public forum