Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:11 09 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM2: Tuning the clock

     Page 1 of 2    
Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 02:21am 24 Jul 2015
Copy link to clipboard 
Print this post

The 28 and 40-pin Micromites run using the internal clock rather than an external crystal. This means the RTC may not be very accurate and in extreme cases serial communications may be compromised

The clock can be tuned by using the OPTION CLOCKTRIM command but this is very long process to try and optimise.

However, if you buy one of these or an equivalent then it is a couple of minutes work to tune the clock for maximum accuracy.

This code (tested on 4.7b23)

cpu 40
option clocktrim -8 'Adjust for reading as close as possible to 1,000,000
' Once adjustment is established for the chip, write the trim value on the chip for future use
dim i%
i%=clockon(40)
INPUT "Press return to terminate";i%
i%=clockoff()
end
'
CFunction clockon
00000000
27bdffe8 afbf0014 afb00010 00808021 3c02bf81 8c43f220 7c63d800 3c020661
24420053 10620008 3c029d00 3c02bf81 8c43f220 7c63d800 3c020660 24420053
14620008 3c029d00 8c420010 2404000f 24050008 0040f809 00003021 01000007
3c02bf81 8c420010 2404002a 24050008 0040f809 00003021 3c02bf81 8c43fb44
24040007 7c831804 ac43fb44 3c02bf81 8c43f020 7c037bc4 ac43f020 8c43f020
7c831804 ac43f020 96040000 8c43f020 7c83f404 ac43f020 8c43f020 24040001
7c837bc4 ac43f020 8c43f020 7c836304 ac43f020 8fbf0014 8fb00010 03e00008
27bd0018
End CFunction 'MIPS32 M4K
'
CFunction clockoff
00000000
27bdffe8 afbf0014 3c02bf81 8c43fb44 7c031804 ac43fb44 3c02bf81 8c43f020
7c037bc4 ac43f020 3c02bf81 8c43f220 7c63d800 3c020661 24420053 10620007
3c02bf81 8c43f220 7c63d800 3c020660 24420053 14620009 3c029d00 3c029d00
8c420010 2404000f 00002821 0040f809 00003021 01000007 8fbf0014 8c420010
2404002a 00002821 0040f809 00003021 8fbf0014 03e00008 27bd0018
End CFunction 'MIPS32 M4K


will cause a nominal 1MHz clock pulse to be output on pin 42 of the 44-pin MM or pin 15 of a 28-pin chip. Connect the meter inputs to the relevant pin and GND

Then adjust the value of clocktrim to get a reading on the meter as close as possible to 1MHz.

I suggest then writing the CLOCKTRIM value on the chip and then this can be used in all programs with that chip.

Remember that the accuracy will vary with temperature, voltage, day-of-the-month etc. but this will very quickly get a baseline which will be much better than the default.

 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 02:47am 24 Jul 2015
Copy link to clipboard 
Print this post

Hi Peter,

I tried it in a 28Pin with 4.7b23 and got the following:

CPU exception #6 (bus error on ifetch) at address 0x0000
Processor restarted

I did a copy paste into notepad++ and an xmodem transfer to the MM. The code looks fine.

Any hints of where to look for my error?

Cheers
Chris

http://caroper.blogspot.com/
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2398
Posted: 06:33am 24 Jul 2015
Copy link to clipboard 
Print this post

from the MM 4.7 beta 23 manual: "This oscillator is used as the basis for all timing (eg, date, time, pause, PWM frequency, serial baudrate, etc). 'n' is the trim value which can range from -31 to +31 (this equates to an adjustment range of about -12.5% to +12.5%). On power up the trim value is zero."

if 63 steps cover a 25% variation (+/- 12.5%), then each step represents a 0.4% change. putting this into a simpler perspective, the timekeeping can be adjusted in multiples of about 14 seconds per hour. this is quite coarse.

from elsewhere in the above manual: "the 28 and 44-pin Micromite uses a fast RC oscillator which has a specified tolerance of ±0.9% but typically is within ±0.1% at 24șC."

so if we can only adjust the timing in 0.4% steps, and it is already typically within 0.1% (+/- 4 seconds per hour) then making any adjustment will be unlikely to improve things unless the MX170 is untypically fast or slow.

given that the step size of OPTION CLOCKTRIM n is indeed multiples of 14 seconds/hour then a better approach may be to calculate a correction multiplier that one applies to all frequencies and times used in the basic code.


cheers,
rob :-)
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 06:37am 24 Jul 2015
Copy link to clipboard 
Print this post

Hi Chris

Sorry about that, finger trouble - please try this version:

cpu 40
option clocktrim -8 'Adjust for reading as close as possible to 1,000,000
' Once adjustment is established for the chip, write the trim value on the chip for future use
dim i%
i%=clockon(40)
INPUT "Press return to terminate";i%
i%=clockoff()
end
'
CFunction clockon
00000000
27bdffe8 afbf0014 afb00010 00808021 3c02bf81 8c43f220 7c63d800 3c020661
24420053 10620008 3c029d00 3c02bf81 8c43f220 7c63d800 3c020660 24420053
14620008 3c029d00 8c420010 2404000f 24050008 0040f809 00003021 10000007
3c02bf81 8c420010 2404002a 24050008 0040f809 00003021 3c02bf81 8c43fb44
24040007 7c831804 ac43fb44 3c02bf81 8c43f020 7c037bc4 ac43f020 8c43f020
7c831804 ac43f020 96040000 8c43f020 7c83f404 ac43f020 8c43f020 24040001
7c837bc4 ac43f020 8c43f020 7c836304 ac43f020 8fbf0014 8fb00010 03e00008
27bd0018
End CFunction 'MIPS32 M4K

CFunction clockoff
00000000
27bdffe8 afbf0014 3c02bf81 8c43fb44 7c031804 ac43fb44 3c02bf81 8c43f020
7c037bc4 ac43f020 3c02bf81 8c43f220 7c63d800 3c020661 24420053 10620007
3c02bf81 8c43f220 7c63d800 3c020660 24420053 14620009 3c029d00 3c029d00
8c420010 2404000f 00002821 0040f809 00003021 10000007 8fbf0014 8c420010
2404002a 00002821 0040f809 00003021 8fbf0014 03e00008 27bd0018
End CFunction 'MIPS32 M4K


 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 06:47am 24 Jul 2015
Copy link to clipboard 
Print this post

Thanks Peter,

Works perfectly, a setting of -3 gets me spot on 1.000Mhz, -4 was 999.6Khz.
Measured with my EXTECH.

Useful tool to add to my MicroMite folder. Once determined I can flash the chip with the ChipKit bootloader, or any other, and know its clock correction in advance.

Cheers
Chris

http://caroper.blogspot.com/
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 06:48am 24 Jul 2015
Copy link to clipboard 
Print this post

Robert

I'm seeing a change in frequency of about 4.5 in 10000 for each step in clocktrim.

I make this about 38 seconds a day.

The -8 value for clocktrim in the code is what it took on a 44-pin part, way outside 0.1%. The 28-pin part I just tested required -2

Referring to the 12.5%, the MX170 manual says:
" The tuning step size is an approximation, and is neither
characterized, nor tested"

How about that for a spec ? Edited by matherp 2015-07-25
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2398
Posted: 06:56am 24 Jul 2015
Copy link to clipboard 
Print this post

interesting - i dread to think how they implement the tuning! i'd just assumed that it was changing a divider or multiplier somewhere, so would be defined by the cold immutable laws of mathematics. whereas from that quote it sounds more likely that they are in some way changing the capacitance or resistance of the RC circuit!

the 1.6 seconds/hour trim step you are seeing certainly sounds far more useful. what would be even more neat would be to link the trim value to the die temperature, using the on-board sensor.


cheers,
rob :-)
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 07:51am 24 Jul 2015
Copy link to clipboard 
Print this post

Here is a plot of measured frequency in MHz against clocktrim on my 44-pin part. Bears no relationship to the spec. but a nice straight line.
You can see the where the line crosses the 1MHz at a CLOCKTRIM of -8



Edited by matherp 2015-07-25
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6218
Posted: 01:28pm 24 Jul 2015
Copy link to clipboard 
Print this post

I found similar results here.
At temperatures below 20 C, the frequency is reasonably stable.
Higher temperatures will be difficult to manage.



The full test results and the method I used are here:
http://www.c-com.com.au/stuff/Frequency%20stability%20of%20the%20MX170.pdf

Peter,
Have you found much variation between chips (of the same type)?

Jim

VK7JH
MMedit
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 10:10pm 24 Jul 2015
Copy link to clipboard 
Print this post

Jim

That is a great piece of work. Are you able to set up a similar test reasonably easily?

The attached code (tested on 4.7B23) reads the internal temperature of the chip. It makes an attempt to convert to meaningful numbers and the output does change with temperature. If you could run it in your test rig over a range of temperatures but with CLOCKTRIM fixed then we could probably work out an algorithm to temperature compensate the clock fairly easily.

We could set up an interrupt every 5 mins or so, read the internal temperature and then change CLOCKTRIM to compensate


do
print readinternaltemp()
pause 1000
loop
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 'MIPS32 M4K
Edited by matherp 2015-07-26
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6218
Posted: 12:53pm 25 Jul 2015
Copy link to clipboard 
Print this post

  matherp said   Are you able to set up a similar test reasonably easily?

It will be a few days before I have the time to play with that again.
I will put it to the top of the todo list.

Jim
VK7JH
MMedit
 
Chris Roper
Senior Member

Joined: 19/05/2015
Location: South Africa
Posts: 280
Posted: 11:15am 27 Jul 2015
Copy link to clipboard 
Print this post

Just by way of feedback, I have had a MM-170-28 Running for over 24 Hours after tuning it with this CFunction.

During that time the temperature has swung by as much as 10'C, possibly more, and the clock has lost less than 2 seconds.

So worst case scenario it would lose 1 Minuet per month.
I have had Quarts Clocks and watches that have performed far worse than that.

I am impressed that the PIC can get such good accuracy from an RC oscillator.

Cheers
Chris

http://caroper.blogspot.com/
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3269
Posted: 08:42pm 27 Jul 2015
Copy link to clipboard 
Print this post

  matherp said  The attached code (tested on 4.7B23) reads the internal temperature of the chip.

That is a great CFunction Peter. Would it be OK with you if I included it in the standard MMBasic distribution?

Geoff
Geoff Graham - http://geoffg.net
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 09:34pm 27 Jul 2015
Copy link to clipboard 
Print this post

  Quote  Would it be OK with you if I included it in the standard MMBasic distribution?


Yes of course, the only issue is that it won't run on the MX470 as it relies on the CTMU and this is completely non-functional on the MX470 (see the chip errata)
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:33pm 19 Sep 2016
Copy link to clipboard 
Print this post

Just been trying this on a 28 pin
any ideas why I'm getting [code]CPU exception #6 at address 0x0000
Processor restarted[/code]
When I type "RUN" ?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 11:05pm 19 Sep 2016
Copy link to clipboard 
Print this post

Make sure you are using this version and run it on the chip as flashed - no OPTIONS, no other code loaded. I've just tested it and it works fine on 5.2 28-pin

cpu 40
option clocktrim -8 'Adjust for reading as close as possible to 1,000,000
' Once adjustment is established for the chip, write the trim value on the chip for future use
dim i%
i%=clockon(40)
INPUT "Press return to terminate";i%
i%=clockoff()
end
'
CFunction clockon
00000000
27bdffe8 afbf0014 afb00010 00808021 3c02bf81 8c43f220 7c63d800 3c020661
24420053 10620008 3c029d00 3c02bf81 8c43f220 7c63d800 3c020660 24420053
14620008 3c029d00 8c420010 2404000f 24050008 0040f809 00003021 10000007
3c02bf81 8c420010 2404002a 24050008 0040f809 00003021 3c02bf81 8c43fb44
24040007 7c831804 ac43fb44 3c02bf81 8c43f020 7c037bc4 ac43f020 8c43f020
7c831804 ac43f020 96040000 8c43f020 7c83f404 ac43f020 8c43f020 24040001
7c837bc4 ac43f020 8c43f020 7c836304 ac43f020 8fbf0014 8fb00010 03e00008
27bd0018
End CFunction 'MIPS32 M4K

CFunction clockoff
00000000
27bdffe8 afbf0014 3c02bf81 8c43fb44 7c031804 ac43fb44 3c02bf81 8c43f020
7c037bc4 ac43f020 3c02bf81 8c43f220 7c63d800 3c020661 24420053 10620007
3c02bf81 8c43f220 7c63d800 3c020660 24420053 14620009 3c029d00 3c029d00
8c420010 2404000f 00002821 0040f809 00003021 10000007 8fbf0014 8c420010
2404002a 00002821 0040f809 00003021 8fbf0014 03e00008 27bd0018
End CFunction 'MIPS32 M4K
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:12pm 19 Sep 2016
Copy link to clipboard 
Print this post

Yes no other code other than what was listed,
I think it could be my micromite I think it's a bit corrupted.
It started printing weird characters the other day , I replaced it with a new one and it's working fine now
I just reflashed 5.2 on the original MM and that's working now as well
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 723
Posted: 07:29am 20 Sep 2016
Copy link to clipboard 
Print this post

change the 20 mhz crystal...
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:53am 20 Sep 2016
Copy link to clipboard 
Print this post

  atmega8 said   change the 20 mhz crystal...

IF it had a crystal that would be easy to do
 
atmega8

Guru

Joined: 19/11/2013
Location: Germany
Posts: 723
Posted: 11:22am 20 Sep 2016
Copy link to clipboard 
Print this post

Ahh, wrong PIC😅
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025