Keeping accurate time

View: New views
14 Messages — Rating Filter:   Alert me  

Keeping accurate time

by shred444 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey guys,
So I'm curious how to create a timer with the correct value to keep
track of seconds.

When my device starts up, it receives the time via UART communication
from another device.  Instead of having it ask every second what time
it is, I'd like the msp430 to keep track of the time from that point
on, and possibly ask for the time only every minute. (just to be safe)

I'm using a msp430f2272, so i've got two timers, I'm just not sure if
i'll need an external crystal, or if i could use the internal ones to
keep track of seconds or milliseconds.


Thanks.
Jonathan


Re: Keeping accurate time

by old_cow_yellow :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Without a crystal, your DCO can only get +-2% accuracy. So you will
gain or loss a second in one minute. If that is not acceptable, you
could use a 32.768kHz crystal and the WDT can be easily programmed to
generate an interrupt every second.

--- In msp430@..., "shred445" <shred444@...> wrote:

>
> Hey guys,
> So I'm curious how to create a timer with the correct value to keep
> track of seconds.
>
> When my device starts up, it receives the time via UART communication
> from another device.  Instead of having it ask every second what time
> it is, I'd like the msp430 to keep track of the time from that point
> on, and possibly ask for the time only every minute. (just to be safe)
>
> I'm using a msp430f2272, so i've got two timers, I'm just not sure if
> i'll need an external crystal, or if i could use the internal ones to
> keep track of seconds or milliseconds.
>
>
> Thanks.
> Jonathan
>



Re: Keeping accurate time

by Kipton Moravec :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It depends on the accuracy you need.

The internal oscillators are not very accurate, but should be good
enough for
you to ask the time only once per day or once per few days.  The
crystals should keep you good for a few months.

I have many projects that need to keep track of seconds. You can do it
however you want, you can set the timer to interrupt at a integer number
of seconds. Then each time the interrupt fires you increment the
sub-second counter. When the Counter hits the counts per second, change
it to zero, and increment the seconds counter, when it hits 60, zero it,
and increment the minutes counter. etc. etc.
 
My current project needs to poll a line at 10,000 Hz, and react if it is
low for 5 samples in a row (it can be noisy). So I count
100microseconds, milliseconds, and seconds, using the same interrupt. I
also have another counter that I increment every 100microseconds that is
a 16-bit unsigned integer. I use it for my delay routine. If I want to
delay 20 ms, I set the counter to 0, and wait for it to get to 200. Not
efficient, but easy, and not likely to go wrong.

When Debugging, I set an unused pin high when I enter the interrupt, and
low when I exit the interrupt. That way I can measure the accuracy and
compare it to other things going on with my oscilloscope.

In your case you may want to use it to compare the accuracy of your
clock to your reference system. If you send the time via a 9600 baud
serial line, then each character you send takes about a millisecond. So
if it takes 10 characters to transmit the time, you are already off by
1/100 of a second, not counting the other system delays.  Hopefully that
delay is always the same, depending on the OS of the sending unit, so
once you find it, you can just say the time I got is always X
milliseconds behind what it really is and add that constant to your time
input.

Kip

On Thu, 2008-01-17 at 16:21 +0000, shred445 wrote:

> Hey guys,
> So I'm curious how to create a timer with the correct value to keep
> track of seconds.
>
> When my device starts up, it receives the time via UART communication
> from another device.  Instead of having it ask every second what time
> it is, I'd like the msp430 to keep track of the time from that point
> on, and possibly ask for the time only every minute. (just to be safe)
>
> I'm using a msp430f2272, so i've got two timers, I'm just not sure if
> i'll need an external crystal, or if i could use the internal ones to
> keep track of seconds or milliseconds.
>
>
> Thanks.
> Jonathan
>
>
>
> To unsubscribe from the msp430 group, send an email to:
> msp430-unsubscribe@...
>
>  
> Yahoo! Groups Links
>
>
>
>
--
Kipton Moravec KE5NGX
"Always do right; this will gratify some people and astonish the rest."
--Mark Twain



RE: Keeping accurate time

by Michael Iverson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
 
I recall seeing an app note and design challenge on keeping accurate
time on the msp. A quick web search resulted in the following:
 
http://focus.ti.com/lit/an/slaa076a/slaa076a.pdf
 
http://www.designmsp430.com/third.aspx
 
The design challenge had some code and a nice explanation of errors that
can accru.
 
Michael
<http://www.designmsp430.com/Rate.aspx?submission_uuid=6a493783-cd3a-4ca
9-bf24-dec30f8f225c>

________________________________

From: msp430@... [mailto:msp430@...] On Behalf
Of shred445
Sent: January 17, 2008 08:21 AM
To: msp430@...
Subject: [msp430] Keeping accurate time



Hey guys,
So I'm curious how to create a timer with the correct value to keep
track of seconds.

When my device starts up, it receives the time via UART communication
from another device. Instead of having it ask every second what time
it is, I'd like the msp430 to keep track of the time from that point
on, and possibly ask for the time only every minute. (just to be safe)

I'm using a msp430f2272, so i've got two timers, I'm just not sure if
i'll need an external crystal, or if i could use the internal ones to
keep track of seconds or milliseconds.

Thanks.
Jonathan



 


[Non-text portions of this message have been removed]


Re: Keeping accurate time

by old_cow_yellow :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In msp430@..., Kipton Moravec <kip@...> wrote:

> ... The internal oscillators are not very accurate, but
> should be good enough for you to ask the time only once
> per day or once per few days...

*** At best, the accuracy is about 1%. Thus you risk +-14 minutes per
day. ***

> ...The crystals should keep you good for a few months...

*** For a cheap crystal, the accuracy is about 100 parts per million,
or +-4 minutes per month. ***

> ... you can set the timer to interrupt at a integer
> number of seconds. Then each time the interrupt fires you
> increment the sub-second counter. When the Counter hits
> the counts per second, change it to zero, and increment
> the seconds counter,...

*** Usually, the the interrupt interval end up to be a fraction of a
second. Your interrupt service routine should add this fractional
number to a software counter. When this software counter accumulates
to more than 1 second, keep the fractional part (do not zero it), and
increment the "seconds counter" ***


Re: Keeping accurate time

by shred444 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you guys for all your responses.  It believe the DCO will be
accurate enough, considering i will be receiving the time at least
once every 4 minutes.

I'm going to try it out and we'll see how it goes.

Thanks again


Re: Re: Keeping accurate time

by Andy Warner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 18, 2008 9:01 AM, shred445 <shred444@...> wrote:
>  [...]
> Thank you guys for all your responses. It believe the DCO will be
>  accurate enough, considering i will be receiving the time at least
>  once every 4 minutes.

If you are getting updates that frequently, you should be able
to tune the DCO to increase the accuracy between updates.

I have no idea how complex you want to make this, but tuning the
underlying osc freq would be preferable to snapping the time
every N minutes. If you tweak the osc, you can make sure time never
runs backwards, which can avoid all sorts of bizarre problems later.

Applying control system loop design concepts to this can be
helpful, in my experience.
--
Andy

Re: Keeping accurate time

by shred444 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hey guys, I gave it a go, but a had trouble triggering the Timer
interrupt.  It looks like it was constantly looking for the external
oscillator.

So here's my question,
What should i set my DCO frequency to?  Does it even matter?  The way
i figure it, as long as my interrupt counter is the same as the
frequency, it will always wait that many clock cycles before causing
an interrupt.

For instance, a 32,768hz clock that interrupts timerA every 32768
cycles will occur after 1 second...just as fast as a 1Mhz clock
interrupting every 1,000,000 cycles...correct?






Re: Keeping accurate time

by old_cow_yellow :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

By the way, I also wrote a program yesterday and it does not work. Can
you tell me what was wrong with it?

If you want timerA to generate an interrupt, you need to set up timerA
to use one of the available clocks (e.g. DCO) in one of the modes
other than the stop mode. You also need to set it up to count a
certain number of counts which cannot be smaller then 2 nor larger
than 65536 (e.g. it cannot be 1000000). You also have to enable one or
more of the interrupt enable bits in timerA as well as the general
interrupt enable bit in Status Register. And you must have the
interrupt vectors pointing to you interrupt service routines.

--- In msp430@..., "shred445" <shred444@...> wrote:

>
> hey guys, I gave it a go, but a had trouble triggering the Timer
> interrupt.  It looks like it was constantly looking for the external
> oscillator.
>
> So here's my question,
> What should i set my DCO frequency to?  Does it even matter?  The way
> i figure it, as long as my interrupt counter is the same as the
> frequency, it will always wait that many clock cycles before causing
> an interrupt.
>
> For instance, a 32,768hz clock that interrupts timerA every 32768
> cycles will occur after 1 second...just as fast as a 1Mhz clock
> interrupting every 1,000,000 cycles...correct?
>



Re: Keeping accurate time

by work_onair :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Looking thru the TI appnote on MSP and RTC - I found the following:
This seems to imply that the internal LF oscillator should provide an
accurate clock without needing an external source.
Is that true?
(Of source the example that follows - uses an external 32 KHz
oscillator - so I am a little confused)
Does this mean that if 12KHz is good enough - can I use it as the
clock source to keep accurate time??

thx:

From section 1.2 of TI app note (slaa076a.pdf)
----begin-----------------------
Internal clock generation is one of the most commonly misunderstood
subjects by MSP430
users. The primary cause of confusion lies with how a stable system
clock is produced. All
MSP430 devices contain both a digitally-controlled RC-type oscillator
and a crystal oscillator.
The RC-type oscillator is typically used for the CPU clock and the
crystal oscillator is typically
used for the peripherals. In the real-time clock application, the
crystal oscillator is used as the
clock source for the timer/counter that serves as the time base
(either Timer_A or the Watchdog
Timer in this application report). Therefore, the instability issues
that are common to RC-type
oscillators are irrelevant.
-----end------------------

--- In msp430@..., "old_cow_yellow" <old_cow_yellow@...>
wrote:

>
> Without a crystal, your DCO can only get +-2% accuracy. So you will
> gain or loss a second in one minute. If that is not acceptable, you
> could use a 32.768kHz crystal and the WDT can be easily programmed to
> generate an interrupt every second.
>
> --- In msp430@..., "shred445" <shred444@> wrote:
> >
> > Hey guys,
> > So I'm curious how to create a timer with the correct value to keep
> > track of seconds.
> >
> > When my device starts up, it receives the time via UART communication
> > from another device.  Instead of having it ask every second what time
> > it is, I'd like the msp430 to keep track of the time from that point
> > on, and possibly ask for the time only every minute. (just to be safe)
> >
> > I'm using a msp430f2272, so i've got two timers, I'm just not sure if
> > i'll need an external crystal, or if i could use the internal ones to
> > keep track of seconds or milliseconds.
> >
> >
> > Thanks.
> > Jonathan
> >
>



Re: Re: Keeping accurate time

by leon_heller :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

----- Original Message -----
From: "work_onair" <sjuid-yauid@...>
To: <msp430@...>
Sent: Monday, July 21, 2008 12:44 AM
Subject: [msp430] Re: Keeping accurate time


> Looking thru the TI appnote on MSP and RTC - I found the following:
> This seems to imply that the internal LF oscillator should provide an
> accurate clock without needing an external source.
> Is that true?
> (Of source the example that follows - uses an external 32 KHz
> oscillator - so I am a little confused)
> Does this mean that if 12KHz is good enough - can I use it as the
> clock source to keep accurate time??

No, you need a crystal.

Leon


Re: Re: Keeping accurate time

by Jon Kirwan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 21 Jan 2008 05:27:17 -0000, you wrote:

>hey guys, I gave it a go, but a had trouble triggering the Timer
>interrupt.  It looks like it was constantly looking for the external
>oscillator.
>
>So here's my question,
>What should i set my DCO frequency to?  Does it even matter?  The way
>i figure it, as long as my interrupt counter is the same as the
>frequency, it will always wait that many clock cycles before causing
>an interrupt.
>
>For instance, a 32,768hz clock that interrupts timerA every 32768
>cycles will occur after 1 second...just as fast as a 1Mhz clock
>interrupting every 1,000,000 cycles...correct?

If all you are asking is, if you wait 1000 counts of a 1000 Hz clock
tick and if you want 10000 counts of a 10000 Hz clock, will you get
the same interval either way? ...then the answer is roughly yes.  But
why would you ask such a question?  The answer is manifest and it
suggests that you are not asking good questions.

I think Andy's comment was a good one to consider, given the 4 min.
synch you mentioned.  Your DCO can be set a lot of places.  It's what
you do with it that counts.  For example, if you need shorter
execution times, use one of the faster DCO tiers.  If you don't care,
you can use one of the slower tiers.  The main thing is that you count
the number of dco cycles and lock some desired number of them to the 4
min. synch signal by making adjustments to anticipate the next period.

Andy's comment about control loop also fits.  Probably the easiest and
very good is to use a PI loop (two terms, but no D term.)  Depending
on how you set your two terms, you may have some long settling time or
else some overshoot and ringing as you settle in -- and given the 4
min cycle, some time will be needed to lock in well.  (If you don't
get the terms set well, you'll end up in a serious mess, though.) I've
done this synching the DCO to 60Hz for phase control and it worked
very nicely.  Of course, things there would settle faster than you can
manage in your case.

Or you could just leave the DCO set somewhere and track the actual
cycle count based on the 4 min reference and use that to make
predictions which you can then use in calculations to estimate desired
counts for events in between, I suppose.

Each has advantages and disadvantages.

Jon

Parent Message unknown Re: Re: Keeping accurate time

by sburck :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

During the developer days they had here, when talking about the new 5xx series - there will be an internal LF oscillator available as well (REFO) - although it is less accurate than a crystal (1% tolerance typical, 3.5% max) - not good enough for an accurate RTC, but accurate enough for many other uses (like a low speed baud rate generator).

----- Original Message -----

>From: "work_onair"  
>To:  
>Sent: Monday, July 21, 2008 12:44 AM
>Subject: [msp430] Re: Keeping accurate time
>>  Looking thru the TI appnote on MSP and RTC - I found the following:
>>  This seems to imply that the internal LF oscillator should provide an
>>  accurate clock without needing an external source.
>>  Is that true?
>>  (Of source the example that follows - uses an external 32 KHz
>>  oscillator - so I am a little confused)
>>  Does this mean that if 12KHz is good enough - can I use it as the
>>  clock source to keep accurate time??
>
>No, you need a crystal.
>
>Leon
>------------------------------------
>
>

TI forums

by leon_heller :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've just had an email from TI about their own forums, including one for the
MSP430:

https://community.ti.com/forums/

The RF forums have inputs from Chipcon people, so might be useful for people
using devices like the CC2500.

Leon

LightInTheBox - Buy quality products at wholesale price