Accurate time keeping with Pic uC

TQFP44

Joined Sep 3, 2016
51
Having a micro run as a clock is fine , wait for it ....... But what about power supply , it needs to be clean, and continuous, embedded RTC require initialising after a power out. batteries are OK , depending on what other stuff your hanging off the I/O .. my
solution is to use a little I2C RTCC module ( coin cell on other side ) , then the correct and accurate time is available from switch on . ( other RTC stuff is available :)

rtc-ds1338-250x250.jpg
 

MrChips

Joined Oct 2, 2009
34,831
Having a micro run as a clock is fine , wait for it ....... But what about power supply , it needs to be clean, and continuous, embedded RTC require initialising after a power out. batteries are OK , depending on what other stuff your hanging off the I/O .. my
solution is to use a little I2C RTCC module ( coin cell on other side ) , then the correct and accurate time is available from switch on . ( other RTC stuff is available :)

View attachment 123472
yehbutt, the stability and accuracy is still a function of the crystal and temperature stability.
It is just as easy to put the 32768Hz crystal directly on the MCU if it supports that feature and use STOP mode when needed.
 

TQFP44

Joined Sep 3, 2016
51
Hi MrChips, yehbutt if you want accuracy then try .
rtc-ds3234-500x500.jpg

The DS3234 is a low-cost, extremely accurate SPI™ bus real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal.

Features
  • Highly Accurate RTC with Integrated Crystal and SRAM Completely Manages All Timekeeping Functions
    • Accuracy ±2ppm
    • Real-Time Clock Counts Seconds, Minutes, Hours, Day, Date, Month, and Year, with Leap Year Compensation Valid Up to 2099
    • Two Time-of-Day Alarms
    • Programmable Square-Wave Output
  • SPI Interface Connects to Most Microcontrollers
    • 4MHz SPI Bus Supports Modes 1 and 3
  • Dimensions 20mm x 20mm (-.8in x 0.8in)
  • Two mounting holes
  • Includes CR1225 backup battery
 

MrAl

Joined Jun 17, 2014
13,711
Hi MrChips, yehbutt if you want accuracy then try .
View attachment 123473

The DS3234 is a low-cost, extremely accurate SPI™ bus real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal.

Features
  • Highly Accurate RTC with Integrated Crystal and SRAM Completely Manages All Timekeeping Functions
    • Accuracy ±2ppm
    • Real-Time Clock Counts Seconds, Minutes, Hours, Day, Date, Month, and Year, with Leap Year Compensation Valid Up to 2099
    • Two Time-of-Day Alarms
    • Programmable Square-Wave Output
  • SPI Interface Connects to Most Microcontrollers
    • 4MHz SPI Bus Supports Modes 1 and 3
  • Dimensions 20mm x 20mm (-.8in x 0.8in)
  • Two mounting holes
  • Includes CR1225 backup battery
Hi,

Yes that is the one i was talking about. I've used one for maybe a year or two now and and very satisfied with the accuracy, using it indoors. Have not tested it over the full temperature range.
 

MrAl

Joined Jun 17, 2014
13,711
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.
Hi,

They make those 32kHz crystals just for time keeping. They tend to be better for some reason i have not looked into yet.

But cheaper ones (relatively speaking) will have higher PPM max error. What is available on the surplus market may be different though.
 

MrAl

Joined Jun 17, 2014
13,711
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.
Hi again,

Yes the typical response is one of surprise when the accuracy of crystals is explained in detail, but usually only when working within the applications that have to do actual real time time keeping.

The accuracy of a 100ppm crystal is 0.01 percent, which is one one-hundredths of a percent, which is very low. Compared to resistors, it's wayyy better. But for long term time keeping it bites :)
You can even tune to radio stations with that with very good results, so it is just a matter of what application you need to use it in. If you had to do GPS it would limit the resolution of the position to maybe within a mile (ha ha) when the modern GPS devices can resolve down to one meter.

If you want to be sure, pick up one of those RTC modules, the kind with the crystal inside the chip so you dont see it on the board itself. You need to be able to communicate in i2c or else you need the knid that puts out a 32kHz or perhaps a 1Hz signal.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Having a micro run as a clock is fine , wait for it ....... But what about power supply , it needs to be clean, and continuous, embedded RTC require initialising after a power out. batteries are OK , depending on what other stuff your hanging off the I/O .. my
solution is to use a little I2C RTCC module ( coin cell on other side ) , then the correct and accurate time is available from switch on . ( other RTC stuff is available :)

View attachment 123472
I might try to integrate something like this into a project in the future. Persistent through power outages is a neat feature. Interesting that bed side alarm clocks don't have a feature like this. I get that they often have a 9v battery installed, but why not a little RTC so that they can keep time forever.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
to put all this into perspective, I am currently working on a project which is using a timer to generate interrupts and do things at certain time intervals. In this case, accuracy means nothing, and ive learned that the internal oscillator is not accurate. But working through it i've learned neat things i can do with uC's and time. Thinking to the future about other projects i'm wondering about how to best keep more accurate time. In the interest of cost I am avoiding RTC modules in this thought exercise, also i have yet to learn i2c or SPI protocols.
 

TQFP44

Joined Sep 3, 2016
51
Hi Travm Just to point out these DS chips have some battery backed ram ( 56 bytes or so ) , now that is just the job for storing ' time to do something data ' Perhaps a good place to discover the sometimes tricky I2C / SPI ...
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Hi Travm Just to point out these DS chips have some battery backed ram ( 56 bytes or so ) , now that is just the job for storing ' time to do something data ' Perhaps a good place to discover the sometimes tricky I2C / SPI ...
I will definitely play with them.
Since i have a passion for doing everything the hard way i will probably design a section in my next revision of my universal PIC development PCB specifically for a RTC circuit and to put one of these chips on it.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
doing a couple quick searches, the price of those chips almost made me fall off my chair.
what would stop me from using a MCP7940m, and perhaps a PIC controller with a temperature sensor (or a thermistor) and making my own self calibrated temperature compensated RTC circuit? would cost only a couple bucks, rather than over $10.
 

MrChips

Joined Oct 2, 2009
34,831
I might try to integrate something like this into a project in the future. Persistent through power outages is a neat feature. Interesting that bed side alarm clocks don't have a feature like this. I get that they often have a 9v battery installed, but why not a little RTC so that they can keep time forever.
Depending on the country you are in, the bedside alarm clock running off of line frequency is more accurate than a little RTC!
There are simple reasons for that if you want to know.

Another surprise!
 

AlbertHall

Joined Jun 4, 2014
12,626
I might try to integrate something like this into a project in the future. Persistent through power outages is a neat feature. Interesting that bed side alarm clocks don't have a feature like this. I get that they often have a 9v battery installed, but why not a little RTC so that they can keep time forever.
Mine does but it doesn't need a battery. It receives the MSF transmissions from Anthorn just up the road from me. This means it also does daylight saving automatically (even when they change it) and it automatically does leap seconds too for ultimate accurcacy!
https://en.wikipedia.org/wiki/Time_from_NPL
 

MrChips

Joined Oct 2, 2009
34,831
The TS needs to state his/her requirements. Does he/she need to know the time one month from now and at what accuracy or one year from now at what accuracy?

Or how about 10,000 years from now as in my 10,000- year clock project?
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Depending on the country you are in, the bedside alarm clock running off of line frequency is more accurate than a little RTC!
There are simple reasons for that if you want to know.

Another surprise!
Where I live line frequency frequently, just stops. Trees, lightning, exploding transformers.

Accuracy isnt the value here, persistence is.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
The TS needs to state his/her requirements. Does he/she need to know the time one month from now and at what accuracy or one year from now at what accuracy?

Or how about 10,000 years from now as in my 10,000- year clock project?
TS?
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Hard to believe - and you have a nuclear power plant in your backyard?
basically yes.
But its power is taken away on giant corridors to the major cities.
Us rural folk have to deal with power that flickers regularly, and often just goes out for hours at a time.

The building i work in experiences voltage surges of up to 150V (110 nominal) every day around 4pm. The ceiling fans go into turbo mode and try to lift the roof off the building. We also blow up UPS's on the regular too.
 
Top