# timer in microseconds

**URL:** <https://community.sparkfun.com/t/timer-in-microseconds/16952>\
**Category:** MicroMod\
**Tags:** msp\
**Created:** [August 12, 2010, 7:02pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952 "2010-08-12T19:02:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmlonline](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pmlonline](https://community.sparkfun.com/u/pmlonline)\
**Post date:** [August 12, 2010, 7:02pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/1 "2010-08-12T19:02:53Z")

</div>

I’m using msp430-gcc. Does anyone have any code to tell how much time has passed? Preferably in microseconds or clock cycles, but milliseconds might be good enough.

Thanks,

Paul

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [August 12, 2010, 7:09pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/2 "2010-08-12T19:09:20Z")

</div>

Why can’t you do it yourself?

---

<div class="post-metadata">

**Author:** ![pmlonline](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pmlonline](https://community.sparkfun.com/u/pmlonline)\
**Post date:** [August 12, 2010, 7:26pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/3 "2010-08-12T19:26:47Z")

</div>

Why redo something when it’s already been done? Science is about standing on the shoulders of others.

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [August 12, 2010, 8:08pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/4 "2010-08-12T20:08:18Z")

</div>

Just use a timer and write it yourself. You will learn more than by copying someone else’s code. You could have had it working by now.

---

<div class="post-metadata">

**Author:** ![pmlonline](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pmlonline](https://community.sparkfun.com/u/pmlonline)\
**Post date:** [August 12, 2010, 8:24pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/5 "2010-08-12T20:24:19Z")

</div>

This low level coding shouldn’t be required by everyone to learn MCU’s. Besides I’d rather learn by coding something that hasn’t already been done-- more product for everything.

Anyhow, I have been reading about all of that, but it’s far more difficult then you make it out. Quote, see bold text →

+++

It’s possbile, but not as easy as on the PC where the Pentium processor has an internal tick counter.

You’ll need to set SMCLK or ACLK to the same clock frequency as MCLK (for ACLK it is only possible on newer devices), then set up a timer to be clocked by SMCLK or ACLK, enable overflow interrupt and write an ISR which counts the overflows. If you never disable interrupts for longer than ~65500 clock cycles, the overflow counter will hold the upper 16 bit (or 32 bits if you use a long int) and the timer counter register the lower 16 bits of the passed cpu ticks since starting the timer. **It is a bit difficult** , however, to retrieve the current value, as it is possible that an overflow occurs between collecting the two 16bit values (orbetween reading the upper and lower 16 bit of the long counter, if used).

Also, this approach consumes some processor time too, as every 65536 cpu cycles the ISR is entered and consumes 20-30 cycles for updating the overflow counter.

You can, however, use the dividers for SM/ACLK and/or timer to break down the count frequency by a factor of up to 64. This reduces the resolution of the tick counter by 64, but also reduces the cpu usage by the same factor. And maybe eliminates the need of using a long value for the overflow counter.

+++

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [August 12, 2010, 8:47pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/6 "2010-08-12T20:47:06Z")

</div>

You’ll find lots of timer examples in slac163.zip, or the equivalent for the chip you are using. It’s on the TI web site.

---

<div class="post-metadata">

**Author:** ![pmlonline](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pmlonline](https://community.sparkfun.com/u/pmlonline)\
**Post date:** [August 12, 2010, 8:57pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/7 "2010-08-12T20:57:25Z")

</div>

Thanks. I found the zip file, but it’s MSP430F21x2. I’ll scan through it anyways.

I came across this code from another source. It uses BTCTL from basic\_timer.h. The code only gets 1/2500 second resolution, but maybe it could be tweaked to get better resolution. Looks like there’s an overflow issue in this code.

+++++++++++

int seconds;

int minutes;

int hours;

int days;

int ticks;

// This will drift and will require calibration – ok for testing though.

#define TICKS\_PER\_SECOND 2500

void main(void)

{

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

ticks = seconds = minutes = hours = days = 0; // start clock at 0 run time

IE2 |= BTIE; // Enable BT interrupt

BTCTL = BTSSEL+BTIP2+BTIP1+BTIP0; // Divide System clock by 256

// The default system clock is approx 640kHz

// so this should give a tick rate of 2500Hz

\_EINT(); // Enable interrupts

for (;😉

{

\_BIS\_SR(CPUOFF); // Enter LPM0

\_NOP(); // Required only for C-spy

}

}

// Basic Timer interrupt service routine

interrupt[BASICTIMER\_VECTOR] void basic\_timer(void)

{

ticks++;

if (ticks \>= TICKS\_PER\_SECOND) {

ticks = 0;

seconds++;

if (seconds \> 59) {

seconds = 0;

minutes++;

if (minutes \> 59) {

minutes = 0;

hours++;

if (hours \> 23) {

hours = 0;

days++; // day of week, calender etc. not implemented.

}

}

}

}

}

+++++++++++

---

<div class="post-metadata">

**Author:** ![pmlonline](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pmlonline](https://community.sparkfun.com/u/pmlonline)\
**Post date:** [August 12, 2010, 9:14pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/8 "2010-08-12T21:14:16Z")

</div>

Actually it seems the basic\_timer.h is not for the MSP430x16x. So much for that code.

---

<div class="post-metadata">

**Author:** ![pmlonline](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@pmlonline](https://community.sparkfun.com/u/pmlonline)\
**Post date:** [August 12, 2010, 10:55pm UTC](https://community.sparkfun.com/t/timer-in-microseconds/16952/9 "2010-08-12T22:55:32Z")

</div>

In case this helps anyone, this code gets Timer A down to ~ 10 microseconds per tick →

// Stop watchdog timer

WDTCTL = WDTPW + WDTHOLD;

// Select SMCLK, clear TAR. MC1 starts Timer\_A in continuous mode.

TACTL = TASSEL1 | TACLR | ID\_3 | MC1;

There might be other code required to get it going. Simply read TA0R variable. When it hits 65535 it rolls over to 0. I’m sure there’s a way to set an interrupt when it rolls over.
