Accurate time keeping with Pic uC

Thread Starter

Travm

Joined Aug 16, 2016
363
For the moment this is more relative high level thought/learning process than it is a direct "how do i do this".
I have a few project ideas that require some accurate time keeping.
I want/will use Pic's. most likely any chip from the enhanced mid-range chips.
I want to use the internal oscillator the run the chip at maximum speed, simply to keep the PWM resolution up at high speeds.
most of these pics (maybe all of them?) have a Timer that can be connected to an external clock source.

Can i simply hook this timer up to a 32khz clock crystal and use interrupts to advance a variable that will be the time?

The chip i am considering at the moment is a PIC16F1574, but there is a large range of chips that have these features.

accuracy in line with a wrist watch would be acceptable, not looking to put a rocket into orbit or anything.

Not considering the code at the moment, but it would just be something simple that ran an interrupt every time the crystal timer overflowed, which simply added the correct ammount of time to the clock, with other code to turn over the hours, or maybe just turn over once a day, or hell, once a year. at 32mhz there should be plenty of processing power to decode the "time" number into something that could be output to a display, or used for some other purpose.

Does this sound practical? I am pretty new with these things.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
The 32Khz xtal on the TMR1 source is a good way, when the 16bits roll over every second.
The FOSC can be the maximum the Pic can handle.
Max.
 

WBahn

Joined Mar 31, 2012
29,976
If accuracy comparable to a typical wrist watch is sufficient, then using a watch crystal should do just fine (since that is what watches use and, as a result, watch crystals are very accurate and very cheap). If you really need to run the PIC at the higher speed then do so, but you might be surprised at how much processing you can do using the watch crystal as your main clock signal, even if using a PIC that divides that by four to get the instruction clock. It all depends on what you are trying to do, but there is a LOT you can do with 8192 instructions a second to work with. The lower you run the PIC's clock, the lower the power. If you run at 32 kHz instead of 32 MHz, then you are talking about a factor of 1000 for the processing portion of your power consumption. That may or may not be a significant portion of your overall power consumption.

If you ever need more accurate time keeping, then you can either go with a more stable and accurate oscillator (such as something that is oven-controlled and temperature compensate), which will increase your cost substantially, or you can use the mains voltage zero crossings (at 60 Hz or 50 Hz, depending on where you are from) as an extremely accurate and stable source of time keeping, particularly on average. Of course, that will tie your design to a source of mains power, which may or may not be an option.
 

Kjeldgaard

Joined Apr 7, 2016
476
The chip i am considering at the moment is a PIC16F1574, but there is a large range of chips that have these features.
Accuracy in line with a wrist watch would be acceptable, not looking to put a rocket into orbit or anything.
The idea of a fast clock to the processor and a more accurate time base clock can easily be done, but it does not seem that PIC16F1574 can do it.

Many of Microchip types have a secondary X-Tal oscillator for Timer 1 (and Timer 3, if it's implemented), but PIC16F1574 have neither Primary or Secondary X-Tal oscillators.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
The idea of a fast clock to the processor and a more accurate time base clock can easily be done, but it does not seem that PIC16F1574 can do it.

Many of Microchip types have a secondary X-Tal oscillator for Timer 1 (and Timer 3, if it's implemented), but PIC16F1574 have neither Primary or Secondary X-Tal oscillators.
As I pointed out, Timer1 (i double checked the data sheet), can be configured to use an external clock source, and using a 32khz watch crystal as this source, should allow accurate time keeping. If i'm thinking correctly. couple people seem to think so.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
but you might be surprised at how much processing you can do using the watch crystal as your main clock signal, even if using a PIC that divides that by four to get the instruction clock. It all depends on what you are trying to do, but there is a LOT you can do with 8192 instructions a second to work with. The lower you run the PIC's clock, the lower the power. If you run at 32 kHz instead of 32 MHz, then you are talking about a factor of 1000 for the processing portion of your power consumption.
As i'm learning, im finding reasons, depending on what i'm doing, to require high frequency PWM. Unless the pic is run up at 32MHZ you end up compromising on either pwm frequency, or resolution. Today i'm assuming pwm frequency matters to me.
Also since i'm not into learning assembly yet, and i'm not sure yet on how to determine timing of instructions in C, my preference is to throw clock speed at it and not worry about it.
Haven't started worrying about battery life yet, as i don't mind being tied to either a vehicle, or mains.
 

AlbertHall

Joined Jun 4, 2014
12,343
It doesn't have the built-in timer1 oscillator but it does have an external clock input for this timer and it's not difficult to make 32kHz watch crystal oscillator.
 

MrChips

Joined Oct 2, 2009
30,704
Here are some general guidelines on MCU clocks:

1) Use internal oscillator for lowest parts count and least concern on frequency stability and accuracy.
2) For lowest power, battery operation, time of day (TOD) time-keeping functions, use 32768Hz crystal.
3) For high resolution PWM and accurate time-keeping, use high frequency external crystal, e.g. 8MHz with internal phase-lock-loop system oscillator. You can still use the system clock for accurate TOD time-keeping functions.
4) For both low power operation, TOD in power down mode, and high resolution PWM, use both 32768Hz crystal and high frequency external crystal.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Here are some general guidelines on MCU clocks:

1) Use internal oscillator for lowest parts count and least concern on frequency stability and accuracy.
2) For lowest power, battery operation, time of day (TOD) time-keeping functions, use 32768Hz crystal.
3) For high resolution PWM and accurate time-keeping, use high frequency external crystal, e.g. 8MHz with internal phase-lock-loop system oscillator. You can still use the system clock for accurate TOD time-keeping functions.
4) For both low power operation, TOD in power down mode, and high resolution PWM, use both 32768Hz crystal and high frequency external crystal.
with regard to #3, this is better than using internal osc, plus cheap 32khz watch crystal? i havent checked prices but am assuming it would be cheaper? Also if i put the pic to sleep? T1 can keep clocking along generating interrupts, while the main high speed osc is stopped?
 

WBahn

Joined Mar 31, 2012
29,976
As i'm learning, im finding reasons, depending on what i'm doing, to require high frequency PWM. Unless the pic is run up at 32MHZ you end up compromising on either pwm frequency, or resolution. Today i'm assuming pwm frequency matters to me.
Also since i'm not into learning assembly yet, and i'm not sure yet on how to determine timing of instructions in C, my preference is to throw clock speed at it and not worry about it.
Haven't started worrying about battery life yet, as i don't mind being tied to either a vehicle, or mains.
Which is all fine for now. Do what makes sense for your current needs and keep all of the other thoughts in your hip pocket for future projects.
 

TQFP44

Joined Sep 3, 2016
51
Using a 32k xtal as the PIC clock source , worked for me with several time devices, I used a T1 and interrupt to give a 1 sec call to update hh/mm/ss counters , good thing about it is you can +/- count from T1 to compensate for xtal freuency . some years ago now but was with assembler and PIC16F84A .
 

Picbuster

Joined Dec 2, 2013
1,047
For the moment this is more relative high level thought/learning process than it is a direct "how do i do this".
I have a few project ideas that require some accurate time keeping.
I want/will use Pic's. most likely any chip from the enhanced mid-range chips.
I want to use the internal oscillator the run the chip at maximum speed, simply to keep the PWM resolution up at high speeds.
most of these pics (maybe all of them?) have a Timer that can be connected to an external clock source.

Can i simply hook this timer up to a 32khz clock crystal and use interrupts to advance a variable that will be the time?

The chip i am considering at the moment is a PIC16F1574, but there is a large range of chips that have these features.

accuracy in line with a wrist watch would be acceptable, not looking to put a rocket into orbit or anything.

Not considering the code at the moment, but it would just be something simple that ran an interrupt every time the crystal timer overflowed, which simply added the correct ammount of time to the clock, with other code to turn over the hours, or maybe just turn over once a day, or hell, once a year. at 32mhz there should be plenty of processing power to decode the "time" number into something that could be output to a display, or used for some other purpose.

Does this sound practical? I am pretty new with these things.
how accurate do you want to be?
All depends on quality and Xtal selected.
but here is a simple hint
Code:
//  interrupts  =====================
if (TMR1IF)
{
  ADCON0bits.GO = 1;  //start measure ano_0/_1
 
TMR1IF=0;  // allways running
  TMR1H=128;  //  second rollover

  DateTime.Seconds++;   // part of a Date_Time struct   see below or use  Second ++;   if you feel more confident.
  if (DateTime.Seconds > 59)
  {
  DateTime.Seconds=0;
  DateTime.Minutes++;
 
  }
  if (DateTime.Minutes>59)
  {
  DateTime.Minutes=0;
  DateTime.Hours++;
  }
  if (DateTime.Hours>23)
  {
  DateTime.Hours=0;
  DateTime.Days++;
  }
  //all other things like lapyear day's in month etc
}

//==============  in main settup

TMR1IE=On;
TMR1IF=0;
GIE=On;
  (INTCONbits.)PEIE=On;

pic dependent (INTCONbits.) true for pic18f8722


struct
  {
  unsigned int Seconds;
  unsigned int Minutes;
  unsigned int Hours;
  unsigned int Days;
  unsigned int Date;
  unsigned int Months;
  unsigned int Years;
  unsigned int Wday;
  }DateTime;
Picbuster

Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

MrAl

Joined Jun 17, 2014
11,389
For the moment this is more relative high level thought/learning process than it is a direct "how do i do this".
I have a few project ideas that require some accurate time keeping.
I want/will use Pic's. most likely any chip from the enhanced mid-range chips.
I want to use the internal oscillator the run the chip at maximum speed, simply to keep the PWM resolution up at high speeds.
most of these pics (maybe all of them?) have a Timer that can be connected to an external clock source.




Can i simply hook this timer up to a 32khz clock crystal and use interrupts to advance a variable that will be the time?

The chip i am considering at the moment is a PIC16F1574, but there is a large range of chips that have these features.

accuracy in line with a wrist watch would be acceptable, not looking to put a rocket into orbit or anything.

Not considering the code at the moment, but it would just be something simple that ran an interrupt every time the crystal timer overflowed, which simply added the correct ammount of time to the clock, with other code to turn over the hours, or maybe just turn over once a day, or hell, once a year. at 32mhz there should be plenty of processing power to decode the "time" number into something that could be output to a display, or used for some other purpose.

Does this sound practical? I am pretty new with these things.
Hi there,

It sounds like you want to keep some sort of real time? That's like a wall clock application.
The accuracy of the time keeping depends highly on the type of oscillator used.

An RC oscillator is not very good but will work for the most part if good accuracy is not needed. With a PIC chip that would mean the on chip RC oscillator which is something like 1 percent accurate. That's about 90 hours per year max error.

A crystal oscillator can keep the time error down to about 100 minutes per year with no adjustment and down to maybe 50 minutes per year (max) with a decent quality crystal and a good circuit and down a bit lower with some adjustment. The adjustment is tricky though and is still subject to temperature variation.

Next in line is the RTC, or "real time clock" module. This is a small separate board that is wired to the controller chip via some protocol like i2c. With a good RTC you can get down to 2PPM accuracy which equates to about 60 seconds per year max, but that's over the full temperature range too so usually you'll get better accuracy than that. My last experiment with this was over a period of about 6 months and i could not detect an error of more than 1 second over that time period. The reason this works so well is because the good type of RTC has a built in crystal and temperature compensation built right into the chip, so there is no external crystal. The type with the external crystal is not nearly as good and can vary much more. There is also the type that puts out a 32kHz signal so you can use that for timing. The i2c type is used with a program that sets the initial time and then just reads the time string when the time is to be read.
The type that link to an atomic time clock will be the best, but most people dont want to be bothered with this.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Hi there,

A crystal oscillator can keep the time error down to about 100 minutes per year with no adjustment and down to maybe 50 minutes per year (max) with a decent quality crystal and a good circuit and down a bit lower with some adjustment. The adjustment is tricky though and is still subject to temperature variation.
This explains why the clock in my car loses a couple minutes a month.

I'm surprised that just using a quality crystal could have so much variation.

This exercise for me is also one of keeping cost as low as possible to do any given job. My thoughts were also that I wanted the uC to be persistently "aware" of the time, so that I could use the time for any purpose imaginable;
time stamping events,
display a clock,
do things at specific times,
make me breakfast.
 

MrChips

Joined Oct 2, 2009
30,704
You may be surprised to discover that there is little difference between a "cheap" crystal and a "quality" crystal.
All crystal oscillators need to be trimmed to achieve an "exact" frequency.

As MrAl pointed out, crystals will drift with age and temperature. For optimum frequency stability, a crystal must be housed in a constant temperature oven.
 
Top