Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Crystal calibration via SNTP

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Thu Jul 10, 2014 4:38 am    Post subject: Crystal calibration via SNTP Reply with quote

What I want to do is to read the time from SNTP/NTP then set the seconds variable (this I already do) after a number of hours get the time again from SNTP/NTP then using this calculate the variance then use this to calculate the crystal frequency which is used in my 1 second interrupt, below is the timer code as maths is not my best subject I would like some help from those who are much better than I. This I think would be not only useful to me but to many others as well. Note Xtal is the crystal frequency.

Regards Paul



Code:

Tim0_isr:



         Bres = Bres + 262144                               '262144 = prescaler:1024 * timer0:256
         If Bres > Xtal Then                                '16 000 000 = xtal frequency
         Bres = Bres - Xtal                                 'change to your crystal or RC frequency
                                                            'turn on clock output and measure frequency


         Incr L2                                            ' Increment system seconds
         Incr L3                                            'when we get to 24 hours we will call for NTP

         If L3 > 86400 Then

         Gontp = 0                                          'this will cause the server to get the time from NTP

         L3 = 0                                             'start counting to 24 hours again

         End If


         Systick = 1

         End If


         Return

 


(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Thu Jul 10, 2014 7:26 am    Post subject: Reply with quote

Paul, I dont really get your aim. Do you want to determin the "real" freq. of a crystal? How would you then adjust it?
Or do you aim to calibrate the int. RC oszillator?
Marc
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Thu Jul 10, 2014 1:43 pm    Post subject: Reply with quote

Hello Arera

No the idea is not to calibrate the crystal but to calibrate the variable that represents its frequency in the timer maths.
I have another server using the same calculations (the ENC28J60 server) with it I measured the clock output on an 8 digit frequency counter it maintains it accuracy to within a few seconds per day and is then updated to SNTP. Note the clock output 25Mhz/2 12.5Mhz of the ENC28J60 is the CPU clock
It would be good to be able to do the calibration with out a frequency counter and knowing the high accuracy of the SNTP time source it should be possible to get a good value for the variable. So need to look at how many seconds lost or gained in say 20 hours and then adjust the value of the variable to compensate this should give an average frequency for the crystal over the 20hours I just need some help with the maths.

Regards Paul
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Thu Jul 10, 2014 3:42 pm    Post subject: Reply with quote

So you need to measure the deviation of a crystal.
Here is my idea:
An average crystals deviation is about 50 parts per million (ppm).
A year contains about 31 million seconds.
This gives a deviation of 31 x 50s = 1550s per year. That is 4.2s per day.
This calculation is only to estimate the deviation we need to cope with.
The sntp resolution is 1 second, so we cannot get more precise than that. If I get you right, you can already determin how many secs per day your timer advances or retards?
Good! Our day contains 86400s.
I do not really see how you set up the timer, but there must be some value to set the timer to interrupt one per second. This "timervalue" divided by 86400 gives a value, that is suited to adjust the timer to to advance/retard 1s/day.
As you can see, not even a 16 bit timer can resolve up to 1s per day.
Example:
The timervalue is 43200. if you change it to 43201, this will lead to slow down for 2s per day.
(BTW the 2s are not exactly 2s, because the timer was a bit to fast before, but this appears to be theory here...).
I hope this is what you were asking about...
One more thougt: i doubt that a sntp is as precice as you might expect, for the network an your tcp-module will need time to transmitt the time.
If youre really after a good time-standard:
I used cheap gps receivers with a 1 pulse per sec output as time-standard.
They are REALLY precise! Their sec derives from the gps transmitter freq, wich derives from the sattelites atomic clock. Amazing...
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

australia.gif
PostPosted: Thu Jul 10, 2014 11:01 pm    Post subject: Reply with quote

I think you will find that ordinary crystals drift with ageing, temp and applied voltage, and several other external influences, enough that the calibration will not hold constant. If you have access to a timeserver, or GPS, or other standard, just use that for periodic resetting of the offset.
_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Fri Jul 11, 2014 1:27 am    Post subject: Reply with quote

Hello Adrian & Arera

Yes there are many factors that can change the frequency of a crystal, also other processes in the AVR may influence the perceived value of the crystal that is why I thought of using SNTP/NTP & GPS even better to get the value for the calculation this way every time a SNTP/NTP read is done it can be recalculated. Arera very interesting maths I will look at applying it to the ENC28J60 server as this one is on 24/7 showing a clock on its LCD and see how it goes. I have already thought about a slower way of doing this by moving the value in the variable up or down depending on slow or fast times hunting for the best value this would require a two stage process where it was done first once each hour till it falls into a certain range we then know the value at the initial point of turning it on for the first time then after this a once every 24hours which will track the drift due to external factors, it would be faster if I can get a formula to calculate it at any time.

Regards Paul
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

australia.gif
PostPosted: Fri Jul 11, 2014 2:00 am    Post subject: Reply with quote

Why not just jump the clock to the precise time from the timeserver/GPS once every 24 or so hours. Diff should only be a few secs at most. Do it at some time like 2:00 am, when it wont be noticed. Is your requirement more stringent than that ?

Tracking the precise time drift seems to me to be overkill, and prone to all sorts of complications. The drift is not necessarily linear or predictable, so measuring the drift over the last 24 hours, and then extrapolating that for the next 24 is not going to work.

If you want the basic drift lower, use a precision TXCO rather than just a raw crystal. I havent looked at specs lately, but drift of 1 ppm should be pretty easy by now. Cost is negligible compared to a lot of complex software you still have to develop.

More than you ever wanted to know about crystal drift effects:
https://en.wikipedia.org/wiki/Crystal_oscillator#Stability_and_aging

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Fri Jul 11, 2014 7:28 am    Post subject: Reply with quote

I stll dont see your aim clearly.
Do you need to know the lenght of a sec for every single sec precicely?
Then use GPS (0ppm) or a TXCO (1ppm / 30s per year).

Do you need an RTC not deviating more than 3s at any given time?
Then use a simple crystal and update the RTC twice a day via sntp.

You try to adjust a crystal once, and then go "offline"?
Then forgett it, for the deviation is not constant, but drifts.

Any other aim?
Then let us know!

Marc
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Fri Jul 11, 2014 9:10 am    Post subject: Reply with quote

Hello Arera
My first thoughts were just to use this instead of a frequency counter at the clock output to measure the crystal frequency to put the value into the variable as not every body has a frequency counter, but then I thought it should be possible to use this to calibrate against the accurate reference that is behind the NTP/GPS clock which would be good for me to check my frequency counter as it has TTL output from its 10Mhz reference which I could drive an AVR clock input with.

Regards Paul
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 552

germany.gif
PostPosted: Fri Jul 11, 2014 9:48 am    Post subject: Reply with quote

Paul,
SNTP Time isn't correct!
Read the RFC's and think about network delays...

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Fri Jul 11, 2014 10:03 am    Post subject: Reply with quote

To measure a crystals deviation, the gauge must be way more precice than the crystal.
I doubt that your freqcounter is that accurate, as it's timebase is probably just another crystal.
Don't forget: This is already accurate to 20-50ppm!
But it is useless to aim to measure up to say 2ppm using a gauge that is 10 times more inaccurate.

As we have seen by now, you will not be able to trim your timer0 to acheive a sensible better precision, as the resolution is to coarse.

(Btw: could you please put some more dots and colons, most of us are not native speakers...)danke!
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Fri Jul 11, 2014 10:08 am    Post subject: Reply with quote

Hello six1

Yes I know about delays etc that is why I would do it over along period of hours but what about the GPS could we get a high accuracy from that?

Yes Arera I have to remember that I must think about breaking up into more lines

Regards Paul
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Fri Jul 11, 2014 10:32 am    Post subject: Reply with quote

Found this http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=82842&highlight=ntp so I think it can be done since the AVR has the ability to get NTP without the need of a PC.

I will try my slow way and see what happens.

Regards Paul
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 375
Location: Wuppertal, Germany

germany.gif
PostPosted: Fri Jul 11, 2014 11:33 pm    Post subject: Reply with quote

GPS, yes, that's it if you are after a precise time and timer. GPS timing is precise enough to measure time that radio waved take to spread for a few meters.
With a simple receiver and a view to the sky you can use this source of state of the art engeneering easily!
I use GPS to determin the deviation of a RTC to about the accuracy of 3min/year within 20seconds.
Then I calculate after how many hours the RTC must drop or double a second to compensate its deviation. Every single sec is still a few ppm off, but the RTC showes whole seconds with a deviation of only a few min per year with a cheap crystal. Of corse temperature and aging have effects, but this solution is cheap, offline and far better than unadjusted RTC.
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Mon Sep 22, 2014 1:41 pm    Post subject: Reply with quote

Update
I have been experimenting with my slow way of finding the crystal frequency
I have been comparing the system seconds with the NTP seconds and adjusting the variable that represents the crystal frequency.
If the system seconds are less then the crystal is less than the variable so I reduce the variable re-sync to NTP then after 1 hour compare again.

Note the frequency measured was 159000075Hz but as the clock out pin of the Arduino 2560 is not connected I have to measure at the crystal
this loads it slightly.

From observations so far this is working.
The value is coming closer and closer to the measure crystal frequency
Setting the variable to the measured crystal frequency shows a gain of one second over 10 Hours

The code so far is simple
It needs some fast then slow adjustments to the value
also needs to ignore a wild value when NTP is not valid
Once the value is stable it then needs to be written to eeprom

Regards Paul

Code:

  If L4 = 3600 Then                                         '1 hour is up we will use NTP to determin the crystal frequency

     L4 = 0
     Call Sntp_compare
      'L5 will have NTP seconds
      'L2 will have the system seconds
      Select Case L5

        Case Is > L2
        Print "sys" ; L2
        Print "ntp" ; L5

        L6 = L5 - L2


        Print "slow>" ; L6
          If L6 > 3 Then
             Xtal = Xtal - 600    'reduce variable by 600Hz
             Print "Less 600"
             Print "Freq=" ; Xtal
             Call Sntp_get
          End If

        Case Is < L2
        L6 = L2 - L5
        Print "sys" ; L2
        Print "ntp" ; L5
        Print "fast>" ; L6

        Case Else

          Print "Same"


      End Select


  End If
 
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum