Forum - MCS Electronics

Author Message
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

Posted: Thu Jul 10, 2014 4:38 am    Post subject: Crystal calibration via SNTP

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 )
Arera

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

 Posted: Thu Jul 10, 2014 7:26 am    Post subject: 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
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

 Posted: Thu Jul 10, 2014 1:43 pm    Post subject: 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
Arera

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

Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

 Posted: Thu Jul 10, 2014 11:01 pm    Post subject: 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
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

 Posted: Fri Jul 11, 2014 1:27 am    Post subject: 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

Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

 Posted: Fri Jul 11, 2014 2:00 am    Post subject: 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
Arera

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

 Posted: Fri Jul 11, 2014 7:28 am    Post subject: 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
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

 Posted: Fri Jul 11, 2014 9:10 am    Post subject: 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
six1

Joined: 27 Feb 2009
Posts: 552

 Posted: Fri Jul 11, 2014 9:48 am    Post subject: 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.
Arera

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

 Posted: Fri Jul 11, 2014 10:03 am    Post subject: 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!
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

 Posted: Fri Jul 11, 2014 10:08 am    Post subject: 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
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

 Posted: Fri Jul 11, 2014 10:32 am    Post subject: 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
Arera

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

 Posted: Fri Jul 11, 2014 11:33 pm    Post subject: 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.
Paulvk

Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

Posted: Mon Sep 22, 2014 1:41 pm    Post subject:

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

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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1

 Jump to: Select a forum BASCOM AVR/8051----------------BASCOM-AVRBASCOM-8051BASCOM-ARDUINOShare your working BASCOM-8051 code hereShare your working BASCOM-AVR code hereBASCOM BETA-SLA BASCOM Related----------------EASY TCP/IPAVR-DOSAR7212KokkeKat FAT-free SD card libBASCOM Project Blog Other Stuff----------------VariousPCB'sRoboticsNew WebSiteAnnouncementsAVR Archive----------------BASCOM-AVR ArchiveBASCOM-8051 ArchiveBASCOM-AVR Unsupported versionsEasy TCP/IP ArchiveBASCOM-EDB
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