Jump to content

How to read the current counter value of timer

Recommended Posts



I want to perform something simple like a time measurement.


With AtTiny i can use the TCNT0 register/variable to read the current counter value and reset it with TCNT0 = 0;




I used this Code for timer init:  (MSP430G2533 )

TA1CCR0 = 1000-1;                 // Count limit (16 bit)
TA1CCTL0 = 0x10;                 // Enable Timer A0 interrupts, bit 4=1
TA1CTL = TASSEL_2 + MC_1 + ID_0; // TASSEL_2  = SMCLK @ 1MHz, Up Mode, no prescaler





Is this the correct syntax for reset the counter vaulue?





How can i read the current counter value before it reaches the value TA1CCR0 ?



Link to post
Share on other sites

Reading TxR is the right answer to the first approximation. There are complexities, though.


When the clock driving a timer is asynchronous to MCLK, a read of TxR may return garbage. If the clock source is much slower than MCLK, you can avoid this by reading in a loop until you see the same value twice (the "majority vote" technique).


If the clock source is faster, you need to dedicate a capture/compare register and toggle CCIS0 in TxCCTL to synchronously capture the counter value.


You can see code that implements these different read techniques in and around this part of BSP430's timer peripheral interface.

Link to post
Share on other sites

Thanky you very much for your hints, but I am overwhelmed with the very detailed timer.h file. 

Can you please direct me to a concrete example?

Not really. The concrete examples simply invoke one of those functions.


My intent was just to make you aware of issues that might become important in the future; most people use timers without worrying about these cases. In your original example, the timer is synchronous with MCLK because you're using SMCLK as the source, so you don't have anything to worry about. If you were using an external high-speed clock, you would need to be careful.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...