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.

Timer count

Other Parts Discussed in Thread: MSP430F5438A

I need to calculate the time between 2 subsequent interrupts and have most of the code going. I am a little confused about the timer part.

 

I wanted to know if there is a function like millis() or micros() as available on an Arduino.

 

What I need to know is that what is the current tick count of say Timer_A. If there is nothing like this present already, I am thinking of latching the timer to the ACLK governed by the shipped 32 KHz crystal and generate an interrupt where a counter increments. The counter has a value of about 30.5 uS for every tick as 1 second / 32 KHz is approx 30 uS.

 

Does this sound right? Or am I talking gobbledygook?

  • You can read the count directly from the count register TAR.  Be careful though if your timer and main clocks run from different sources as you could read the value while it is changing, resulting in an error - to avoid this you can use a majority vote system.

     

    Regards,

    Chris.

  • Hi,

    you need to use timer as your time reference. It should be free runing with no interrupts on overflow.

    For each of your two interrupts you should read TAR register (like chris_m suggested). When the second interrupt happens you need to calculate a differnce between two readings. Please note that hardware timer in MCU will overflow, so you need to take it into account in your defference calculation. Don't be surprised when latter value will be smaller than former, as long as time between interrupts is shorter that maximum time determined by timer overflow you can still determine correct period.

    When you obtain the difference expressed in timer clock units you need to scale it to ms or us.

    Regards,
    Piotr Romaniuk, Ph.D.
    ELESOFTROM

  • In addition to what was already written, what is your interrupt source? Most likely an external one. So you can direct the signal to a capture input. Each of the timers capture/compare units (CCRs) has one port pin assigned. It can be use to output a PWM signal when in compare mode (the units setting is compared against the current timer value and triggers an output action if a match is found) or as an input signal in capture mode. Here the current value of the timer counter register (TAR) is captured to the CCR register. Additionally, you may set it to trigger an interrupt. So when a signal comes, you not only get notified by an interrupt, you will also find the exact value of TAR in the moment of the interrupt (no matter how long you need to get into the interrupt routine or to check the interrupt flag). Kepp it, build the difference ot the previous value and you have the number of elapsed timer ticks. (clipped to 65535, so the maximum timespan you can detect when clocking the timer with 1MHz is 65.535ms)

    Don't write code if the hardware can do it for you :)
    (It's usually faster and more precise to use the hardware features)

    There are other threads dealing with this.

  • Hi,

    I am using 16MHz crystal for msp430f5438a controller, I want to calculate number for ticks used for a instruction to execute? Can you Please help. Is there any function to calculate cpu_ticks.

    Thanks,

    Ramana.

  • You could try bit banging one of the IO pins on a scope and your theoretical max is the clock speed of your processor, to do a real test you will need to do port operations rather than use high level.

    Google for raspberry pi or arduino gpio speed, I was starting to have a tough time to get my duino code going on the msp so dropped this off in lieu of a different project.

**Attention** This is a public forum