![]() |
Forum Index : Microcontroller and PC projects : MM: Temperature compensated clock
Author | Message | ||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10214 |
Lew247's post in this thread prompted me to write a tiny bit of code that has been on my to-do list for ages: to use the internal temperature of the MX170 to get the internal clock as accurate as possible over 24-hours. The picture is of my counter/timer set up with a 10-second gate time measuring a 1MHz output from the Micromite The way the code works is as follows: First you need to find the OPTION CLOCKTRIM value that would give a perfect result at any given temperature. In my case a trim value of -2.67 at 23DegC The code then uses as an input the sensitivity of the clock to temperature and to an incremental change in clocktrim const tempadjust=0.12 'No. of KHz by which a 1MHz output clock decreases per degree rise of temperature
const trimadjust=0.45 'No. of KHz by which a 1MHz output clock increases per unit of OPTION CLOCKTRIM The temperature compensation is set as a constant at 0.12KHz/deg which is the value I have measured. The impact of 1 unit of OPTION CLOCKTRIM is set at 0.45Khz/unit. Both of these are related to a 1MHz reference output. I set up a 1 second interrupt with a 10 second calculation within it. The code then works out which integer value of clocktrim to use for the first part of the 10 seconds, when to make a change and then the value of clocktrim for the second part of the 10-second period. i.e. within every 10-second period CLOCKTRIM will be updated twice to both add an effective decimal point to the clocktrim value, and link in the temperature compensation. This should give an accuracy of within 5 seconds a day. Full code below Option explicit
option default NONE const tempadjust=0.12 'No. of KHz by which a 1MHz output clock decreases per degree rise of temperature const trimadjust=0.45 'No. of KHz by which a 1MHz output clock increases per unit of OPTION CLOCKTRIM const zerotemp=23 'temperature at which the zeroadjust on CLOCKTRIM was measured const zeroadjust=-3 'Most accurate CLOCKTRIM that gives a value BELOW 1MHz const zeroerror=0.15 'Amount by which the zeroadjust value underreads 1MHz in KHz const resolution=10 dim integer timecount=0,timedivide,optiontrim=zeroadjust settick 1000,clockadj do pause 5000 loop ' sub clockadj() 'run every half second local float a if timecount =0 then optiontrim=zeroadjust a=readinternaltemp() a= (a-zerotemp)*tempadjust 'every 10 seconds do a temp timedivide=cint(((zeroerror+a)/trimadjust)*resolution) if timedivide>=resolution then optiontrim=optiontrim+timedivide \ resolution timedivide=timedivide mod resolution endif if timedivide<0 then optiontrim=optiontrim-1-timedivide \ resolution timedivide=resolution-abs(timedivide mod resolution) endif endif if timecount=0 then option clocktrim optiontrim+1 if timecount=timedivide then option clocktrim optiontrim timecount=(timecount+1) mod resolution end sub ' CFunction readinternaltemp 00000000 27bdffc8 afbf0034 afbe0030 afb7002c afb60028 afb50024 afb40020 afb3001c afb20018 afb10014 afb00010 3c030200 34638001 3c02bf81 ac43a200 3c031000 3c02bf88 ac431064 3c02bf88 ac401068 34038000 3c02bf81 ac439004 24100001 00008821 3c12bf81 3c13000d 3c14bf88 3c15bf88 3c16bf81 3c17ffff 10000002 3c1ebf81 26100001 ae539040 ae806118 aea06108 aed79050 34028f00 afc29020 24020003 3c03bf81 ac629010 240200e4 3c03bf81 ac629000 34028000 3c03bf81 ac629008 3c04bf88 3c031000 8c821030 00431024 1040fffd 3c02bf81 8c429070 02228821 24020001 12020028 3c031000 3c02bf88 ac431034 3c029d00 8c420004 0040f809 240403e8 2a020009 1440ffde 26100001 3c031000 3c02bf88 ac431064 3c02bf88 ac401068 34038000 3c02bf81 ac439004 3c02bf81 ac40a200 24020588 00518823 26220007 2a230000 0223100a 000210c3 2442001c 00021fc3 8fbf0034 8fbe0030 8fb7002c 8fb60028 8fb50024 8fb40020 8fb3001c 8fb20018 8fb10014 8fb00010 03e00008 27bd0038 3c02bf88 ac431034 3c029d00 8c420004 0040f809 240403e8 1000ffb7 00008821 End CFunction |
||||
Zonker![]() Guru ![]() Joined: 18/08/2012 Location: United StatesPosts: 767 |
So, this program runs for 24hrs and keeps adjusting the trim for a given room temp (or other environments), then you quit the program with the best value stored.. Hence, a single pass setting, or do you recommend this run as a background task while the main user program is running..? |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10214 |
That was the intention. I'm currently running the code in the background in Geoff's "SuperClock" as a test will report results after a couple of days |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I don't quite get it do you have to have a frequency counter connected while this is running? or do you have to measure the clocktrim at given temperatures then run the program? my clocktrim is 4 at 23°C would I have to measure this a 22 and 24 deg to get it to work? I don't "quite" get what this needs |
||||
isochronic Guru ![]() Joined: 21/01/2012 Location: AustraliaPosts: 689 |
It is a good idea, though I think the non-crystal oscillator is basically a R-C circuit so the frequency is affected by power supply voltage as well (?) |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |