Searching \ for '[PIC] TMR2 Not keeping Time?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/time.htm?key=time
Search entire site for: 'TMR2 Not keeping Time?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] TMR2 Not keeping Time?'
2006\07\17@114450 by Brooke Clarke

flavicon
face
Hi:

I'm using Timer 2 as an 8 ms tick interrupt clock.  The 16F88 gets it's
external clock from a 1.00000000000000 MHz source.  But after running
for 20 hours the clock is 4 to 5 seconds fast.  Any ideas why?  Timer 2
is setup:

;  Fin  Fclk     Pre  Cntr  Freq    Post    Int Hz    Int ms
;  1M   250k     4    250   250     2       125       8  

   banksel T2CON
   movlw    0x0D    ; 2 post, Cntr On, 4 Pre: 1 MHz in
   movwf    T2CON
   banksel PR2
   movlw    .251    ;    count  to 250 for 10, 5, 1 MHz
   movwf    PR2
   banksel PORTA

The mid range reference manual says that after TMR2 gets to PR2 it's
reset to FF and the 16F88 manual says it's reset to 0.  Ten minutes of
running shows that PR2 needs to be set to .251 not the 250 that seemed
correct.  I like the idea that the counter gets automatically reset.  In
a prior clock using Timer 1 it was a hassle to load the registers and
keep track of the latency, but it did work.

Have Fun,

Brooke Clarke

--
w/Java http://www.PRC68.com
w/o Java www.pacificsites.com/~brooke/PRC68COM.shtml
http://www.precisionclock.com

2006\07\17@141758 by Gerhard Fiedler

picon face
Brooke Clarke wrote:

> I'm using Timer 2 as an 8 ms tick interrupt clock.  The 16F88 gets it's
> external clock from a 1.00000000000000 MHz source.  

Is the number of zeroes indicating the precision of the source? What source
is that?

> But after running for 20 hours the clock is 4 to 5 seconds fast.  Any
> ideas why?  

That's some 55 ppm. For me the question is "fast against what?" How do you
determine that it's fast? It of course requires that both of your sources
are somewhat more precise than 55 ppm.


> In a prior clock using Timer 1 it was a hassle to load the registers and
> keep track of the latency, but it did work.

You can subtract from the current timer value. That handles the latency
automatically.

Gerhard

2006\07\17@142534 by trossin

picon face

Two reasons:

1. Your crystal is not 1.00000000000000MHz!
Most crystals has an error of 20 to 50 ppm.  That is parts per million or 50
ticks/million.  A little math is an eye opener.
20 hours = 20 * 60 * 60 seconds = 72000 seconds.  You see a 5 second error
so you count 72005 instead of 72000 or 1.000069 times too many counts.
Subtract 1 and mult by 1 million and you get 69 parts per million.  This is
not too far off the spec.

2. If you don't put the 22pF caps on the crystal the oscillator will not be
very stable.  The clock will run without them but I have found that the
error rate is much higher than advertised.

One point that I have found is that there seems to be an error but it seems
constant.  What I have done is to add a software trimmer to have my
interrupt routine only count interrupts every N out of M interrupts.  For
example every 255 out of 256 to slow the clock down or to speed it up have
it add a tick every 256 interrupts.

Back in the old days we would buy a 22pF trimmer cap and use a screw driver
to tune the frequency.  Today you can do it with software.

The other fun fact is that the frequency of the oscillator is temperature
dependent.  Years ago folks would put their crystals in mini crystal ovens
and regulate the temperature to get a very stable source.

I hope that this helps.
--
View this message in context: www.nabble.com/-PIC--TMR2-Not-keeping-Time--tf1955497.html#a5365920
Sent from the MicroControllers - PIC forum at Nabble.com.

2006\07\17@144536 by Mark Rages

face picon face
On 7/17/06, trossin <spam_OUTted_rossinTakeThisOuTspamyahoo.com> wrote:
>
> Back in the old days we would buy a 22pF trimmer cap and use a screw driver
> to tune the frequency.  Today you can do it with software.
>
> The other fun fact is that the frequency of the oscillator is temperature
> dependent.  Years ago folks would put their crystals in mini crystal ovens
> and regulate the temperature to get a very stable source.
>

The modern solution is a "TCXO" or temperature-compensated crystal
oscillator.  You can buy them with a few ppm accuracy over temperature
for less than $10 (digikey, ones).  Or steal one from a digital cell
phone.  The cell phone specs require a rather accurate oscillator.

Regards,
Mark
markrages@gmail
--
You think that it is a secret, but it never has been one.
 - fortune cookie

2006\07\17@161001 by Robin Abbott

flavicon
face
You can a software correction which automatically applies an offset and
places it in EEPROM. However for production you might find it a pain
doing this automatically over a 20 hour period. You would need an
accurate pulse source as well - say once per hour. I can let you know
some more ideas on this if you drop an email.

Robin Abbott
Forest Electronics - the home of the WIZ-C PIC RAD C Compiler system
.....robin.abbottKILLspamspam@spam@fored.co.uk
http://www.fored.co.uk



{Original Message removed}

2006\07\17@162438 by trossin

picon face

You should not have to spend so much money to get it accurate.  Think of all
the battery powered  thermostats and watches even the $1.00 battery operated
watch/clocks at Target that are accurate to a few seconds a year.  They are
just using cheap 5ppm 32.765 KHz tuning forks and triming the error away.
That is another reason the PICs have the separate OSC for the timers.
--
View this message in context: www.nabble.com/-PIC--TMR2-Not-keeping-Time--tf1955497.html#a5367800
Sent from the MicroControllers - PIC forum at Nabble.com.

2006\07\17@221709 by Bob Axtell

face picon face
Robin Abbott wrote:
> You can a software correction which automatically applies an offset and
> places it in EEPROM. However for production you might find it a pain
> doing this automatically over a 20 hour period. You would need an
> accurate pulse source as well - say once per hour. I can let you know
> some more ideas on this if you drop an email.
>
> Robin Abbott
> Forest Electronics - the home of the WIZ-C PIC RAD C Compiler system
> robin.abbottspamKILLspamfored.co.uk
> http://www.fored.co.uk
>  
If you can, try to obtain a 50 or 60hz signal, rather than a crystal.
The 50/60hz pulse is incredibly
accurate, usually within a second per day, non-accumulative.

--Bob

>  
>
>
> {Original Message removed}

2006\07\17@223559 by Brent Brown

picon face
> If you can, try to obtain a 50 or 60hz signal, rather than a crystal.
> The 50/60hz pulse is incredibly accurate, usually within a second per
> day, non-accumulative.

I am about to do this with a PIC16F88. It will auto detect 50 or 60Hz as the product
is intended to sell in two different countries. If power fails (but it's expected to be on
24/7) it will keep time using the internal RC oscillator. If I want to get really clever I
could try and make it calibrate it's internal RC oscillator against the 50/60Hz signal,
and even do temperature compensation. In reality however this app will handle
minutes or 10's of minutes per day accuracy during periods of no mains power.

It's not that I couldn't afford a few cents for a 32.768kHz crystal... I couldn't afford
the micro pins!

--
Brent Brown, Electronic Design Solutions
16 English Street, St Andrews,
Hamilton 3200, New Zealand
Ph: +64 7 849 0069
Fax: +64 7 849 0071
Cell: 027 433 4069
eMail:  .....brent.brownKILLspamspam.....clear.net.nz


2006\07\18@081518 by Howard Winter

face
flavicon
picon face
On Mon, 17 Jul 2006 11:25:33 -0700 (PDT), trossin wrote:
>...
> The other fun fact is that the frequency of the oscillator is temperature
> dependent.  Years ago folks would put their crystals in mini crystal ovens
> and regulate the temperature to get a very stable source.

When my grandfather died (he's had a radio shop in London in the 1920s and '30s) I was allowed to go through
his workroom to see if there was anything I could use (I ended up with an AVO model 6).  I found a number of
crystal ovens, but I hadn't got a use for them at the time so I left them.  But I've been wondering since then
why they don't seem to exist these days?  Are modern crystals more temperature-stable, or is there some other
reason why we don't try to keep them at a constant temperature nowadays?

Cheers,


Howard Winter
St.Albans, England


2006\07\18@082629 by Howard Winter

face
flavicon
picon face
Brent,

On Tue, 18 Jul 2006 14:35:51 +1200, Brent Brown wrote:

> > If you can, try to obtain a 50 or 60hz signal, rather than a crystal.
> > The 50/60hz pulse is incredibly accurate, usually within a second per
> > day, non-accumulative.
>
> I am about to do this with a PIC16F88. It will auto detect 50 or 60Hz as the product
> is intended to sell in two different countries. If power fails (but it's expected to be on
> 24/7) it will keep time using the internal RC oscillator.

I have an X-10 "Mini Timer" that does timed switching of devices and has buttons to allow manual override.  
The clock synchronises to the mains, and has battery backup to cover power interruptions, but it's *dreadful*
when that happens - gains between 5 and 10 minutes per *hour* while on battery!

> If I want to get really clever I
> could try and make it calibrate it's internal RC oscillator against the 50/60Hz signal,
> and even do temperature compensation.

I wish the makers of this thing had done that.  I'm tempted to open it up and see what they're doing for a
timebase, but it's the sort of case that you almost have to destroy to get it open.

>In reality however this app will handle
> minutes or 10's of minutes per day accuracy during periods of no mains power.

But how do you get back on time when the mains returns?

Cheers,


Howard Winter
St.Albans, England


2006\07\18@083558 by Tamas Rudnai

face picon face
I don't get it: Virtually any quartz watch are very accurate and works in a
quite sophisticated environment: no matter if you ware it or put it down
into a cool place... Those watches are not even expensive -- the most
expensive part of such a watch is the case and the package they put it in,
anyway. So why is that could be so accurate while the crystal you
use cannot?

Tamas



On 18/07/06, Howard Winter <EraseMEHDRWspam_OUTspamTakeThisOuTh2org.demon.co.uk> wrote:
{Quote hidden}

> -

2006\07\18@084210 by Alan B. Pearce

face picon face
>Are modern crystals more temperature-stable, or is there some other
>reason why we don't try to keep them at a constant temperature nowadays?

I think there has been a lot of research into the cutting angle of the
crystals to get very acceptable temp co.s over "normal operating temperature
ranges" for all but the most critical applications, by finding the inflexion
points in the temperature curves for a given cut.

Then by selecting a cut with an inflexion point at about 35-40C a lot of
application (marine SSB comes to mind here) can be done with very simple
temperature stabilisation in a minimal oven.

The need for a stable oven is now largely limited to the use of standards in
test instruments, or where fancy methods are used to generate microwave type
frequencies.

2006\07\18@085259 by Alan B. Pearce

face picon face
>I don't get it: Virtually any quartz watch are very accurate and
>works in a quite sophisticated environment:

Yeah, but a watch operates in a semi-stabilised temperature environment, in
that being on someones wrist limits the temperature variations somewhat.

It may also happen that the tuning fork style crystal that is used for 32kHz
is less temperature sensitive (someone mentioned 5ppm, as against a higher
frequency crystal typically being 20-50ppm).

2006\07\18@091721 by olin piclist

face picon face
Tamas Rudnai wrote:
> I don't get it: Virtually any quartz watch are very accurate and works
> in a quite sophisticated environment:

You can get this too without much per-unit expense if you are willing to
invest in the production process.  20ppm tuning fork 32768Hz crystals are
readily and cheaply available.  That by itself is better than 2 seconds/day.
With a little digital calibration at manufacturing time, you can get to 5ppm
effective.  That's 13 seconds/month, which is about what my cheap watch is
good for.


******************************************************************
Embed Inc, Littleton Massachusetts, (978) 742-9014.  #1 PIC
consultant in 2004 program year.  http://www.embedinc.com/products

2006\07\18@092304 by Dave Tweed

face
flavicon
face
Howard Winter <HDRWspamspam_OUTH2Org.demon.co.uk> wrote:
> On Mon, 17 Jul 2006 11:25:33 -0700 (PDT), trossin wrote:
> > ...
> > The other fun fact is that the frequency of the oscillator is
> > temperature dependent. Years ago folks would put their crystals
> > in mini crystal ovens and regulate the temperature to get a very
> > stable source.
>
> When my grandfather died (he's had a radio shop in London in the 1920s
> and '30s) I was allowed to go through his workroom to see if there was
> anything I could use (I ended up with an AVO model 6).  I found a number
> of crystal ovens, but I hadn't got a use for them at the time so I left
> them.  But I've been wondering since then why they don't seem to exist
> these days?  Are modern crystals more temperature-stable, or is there
> some other reason why we don't try to keep them at a constant
> temperature nowadays?

Crystal ovens still exist, but nowadays they're only needed in the most
high-precision applications, like GPS-based time and frequency references
that need good carry-through characteristics during GPS outages (e.g.,
better than 1 us/hr, or 2.7e-10). I've even seen double ovens (nested)
on the OEM market in recent years.

For most other applications, the crystals themselves are stable enough
over temperature, or it's easy enough to build a temperature-compensated
oscillator circuit around one to get the required accuracy -- Google
"TCXO". These are much less expensive to build and much more
power-efficient than ovens.

-- Dave Tweed

2006\07\18@141540 by Peter

picon face


On Tue, 18 Jul 2006, Howard Winter wrote:

> I have an X-10 "Mini Timer" that does timed switching of devices and
> has buttons to allow manual override. The clock synchronises to the
> mains, and has battery backup to cover power interruptions, but it's
> *dreadful* when that happens - gains between 5 and 10 minutes per
> *hour* while on battery!

That is the symptom of a tuning fork crystal that has been dropped hard
imho. Opening IP20 enclosures for consumer use is best done with a
dremel disc. Closing is with epoxy ;-)

Peter

2006\07\18@141935 by Brooke Clarke

flavicon
face
Hi Gerhard:

Yes.  It's an FTS4060 cesium standard, see:
http://www.pacificsites.com/~brooke/PRC68COM.shtml#PC3  <-- PIC clock
http://www.pacificsites.com/~brooke/FTS4060.shtml <-- Frequency standard
For the last few days it's been more like parts in E-14, but usually
it's parts in E-13.

For this PIC clock I used a program called TAC32 that runs on a PC and
gets a 1 Pulse Per Second from a GPS receiver.  It both sets the PC
clock and has an audible "tick".  By watching the 1 PPS LED on the PIC
clock and hearing the tick it's easy to tell that they are synchronized
to within milliseconds.  Test equipment could be used to measure the
period of the clock 1 PPS but it would be overkill.
http://www.cnssys.com/  <-- TAC32 software and GPS based timing receiver

Thanks for the idea of subtracting to set Timer 1.  I know about testing
the lsb of the timer, and I see how subtracting would also work.  This
time by using Timer 2 with the automatic resetting all those worries go
away.

Hi List:
There have been some comments about watch crystals.  It turns out that
the quality "Q" of a crystal gets higher and the aging is improved as
the frequency gets lower, so a watch crystal has the highest "Q" and
lowest aging of the common crystals.  Given a choice of crystals for
timekeeping the watch crystal has advantages.  Note that temperature and
aging can be corrected in software.  You can sense the crystal
temperature and change the clock divisor so it's correct for the
temperature.  Also the aging can be compensated either based on an
average for the lot of crystals or by serial number.  Many watches and
automobile clocks  include one or both of these corrections.

Have Fun,

Brooke Clarke

--
w/Java http://www.PRC68.com
w/o Java www.pacificsites.com/~brooke/PRC68COM.shtml
http://www.precisionclock.com



{Quote hidden}

2006\07\18@175416 by Brent Brown

picon face
On 18 Jul 2006 at 13:26, Howard Winter wrote:

{Quote hidden}

When mains power is restored the micro will observe the return of the 50/60Hz
pulses on an input pin and switch to using that as the RTC timebase again. I'm not
exactly sure how I will code everything just yet. Logic may be something like this...

Micro uses RC clock & counter to generate a clock "tick" source at say 100Hz. If
there is not at least one 50/60Hz pin input transition within two RC clock ticks then
RTC clock source will be RC ticks, else RTC will use 50/60Hz ticks with divide by 50
or 60 to generate seconds. Seperate code determines if input is 50 or 60Hz... or
perhaps this should be be a setup parameter as it could be too difiicult to distinguish
50Hz from 60Hz without an accurate timebase.

If I gain/loose 5 or 10 minutes per hour on battery then that's the way it is. This thing
will be permanently wired to mains in a house, so with extended power outages it
will be expected that the clock needs to be reset. Even an hour or so out will not
affect the actual application, eg. "at about 3pm do this, at about 3 in the morning do
that". There is an LCD display so if someone notices the time is out they can
change it.

--
Brent Brown, Electronic Design Solutions
16 English Street, St Andrews,
Hamilton 3200, New Zealand
Ph: +64 7 849 0069
Fax: +64 7 849 0071
Cell: 027 433 4069
eMail:  spamBeGonebrent.brownspamBeGonespamclear.net.nz


2006\07\19@031435 by Alan B. Pearce

face picon face
>Thanks for the idea of subtracting to set Timer 1.
>I know about testing the lsb of the timer, and I
>see how subtracting would also work.

You don't need to do any special subtracting with timer 1. Remember that the
timer counts from the value you load down to 0, and then when counting from
0 to -1 it sets the timer 1 flag that can generate the interrupt, when the
interrupt flag is enabled. Each clock cycle will count further in the
negative direction, so by the time you service the timer it might have
reached -8 to -10. Now the trick is not to load the timer with the count
value, but to ADD the count value to the negative value in the timer. This
then automagically takes account of the number of clock counts since the
flag was set as the count plus the negative count is loaded into the timer,
and so deals with the program latency from when the flag was set.

Doing things this way also allows the timer to be at a lower priority in the
interrupt chain, or if it is being handled without interrupts allows a
longer time period between flag checks, while still maintaining the timer
accuracy. The only gotcha to doing this is that you STILL have to service
the timer in less time than the time count, otherwise you will loose counts.

2006\07\19@031949 by Alan B. Pearce

face picon face
>Micro uses RC clock & counter to generate a clock "tick"
>source at say 100Hz. If there is not at least one 50/60Hz
>pin input transition within two RC clock ticks then RTC
>clock source will be RC ticks, else RTC will use 50/60Hz
>ticks with divide by 50 or 60 to generate seconds. Seperate
>code determines if input is 50 or 60Hz... or perhaps this
>should be be a setup parameter as it could be too difiicult
>to distinguish 50Hz from 60Hz without an accurate timebase.

However I would also be tempted to have some sort of auto-calibration
routine that checked the average rate of your internal 100Hz against the
50/60Hz. By using an average over a 24hr period you should be able to deal
with RC clock variations as well. Should give your clock a more accurate
time if the power is off for a significant length of time.

2006\07\19@081200 by Gerhard Fiedler

picon face
Alan B. Pearce wrote:

>> Thanks for the idea of subtracting to set Timer 1. I know about testing
>> the lsb of the timer, and I see how subtracting would also work.
>
> You don't need to do any special subtracting with timer 1. Remember that the
> timer counts from the value you load down to 0, and then when counting from
> 0 to -1 it sets the timer 1 flag that can generate the interrupt, when the
> interrupt flag is enabled. Each clock cycle will count further in the
> negative direction, so by the time you service the timer it might have
> reached -8 to -10. Now the trick is not to load the timer with the count
> value, but to ADD the count value to the negative value in the timer.

Hmm... When I was suggesting subtracting the period count, I was assuming
that timer 1 /increments/ and that the timer flag gets set when it rolls
over from 0x0000 to 0xffff. At least that's what my (possibly failing :)
memory tells me.

This provides the same mechanism you are describing, but with a subtraction
instead of an addition.

Gerhard

2006\07\19@083714 by Jinx

face picon face
> that timer 1 /increments/ and that the timer flag gets set when it
> rolls over from 0x0000 to 0xffff. At least that's what my (possibly
> failing :) memory tells me.

A decrementing counter would pass through 0 to FFFF. An
incrementing counter, which I'm sure all PIC timers are, unless
I missed something in the thread (not all of which I've read yet),
sets an IF when it increments from FF or FFFF to 0

2006\07\19@103127 by Gerhard Fiedler

picon face
Jinx wrote:

>> that timer 1 /increments/ and that the timer flag gets set when it rolls
>> over from 0x0000 to 0xffff. At least that's what my (possibly failing
>> :) memory tells me.
>
> A decrementing counter would pass through 0 to FFFF. An incrementing
> counter, which I'm sure all PIC timers are, unless I missed something in
> the thread (not all of which I've read yet), sets an IF when it
> increments from FF or FFFF to 0

Crap... :)  That's what I meant, of course. Thanks for the correction.

Gerhard

More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...