The internal clock on the micromite using the PIC32MX series of CPUs can be inaccurate because it is derived from a capacitor oscillator on the die. As a consequence, the internal DATE$ and TIME$ functions drift over time and temperature. MMBasic does provide the CLOCKTRIM option to adjust the clock to bring it more in line, but without a good reference it is a matter of trial and error.
The following generates a nominal 1MHz square wave on pin
42 of the 44-pin or pin
15 of the 28-pin MX170 microcontroller. Measuring this with a frequency meter allows the inaccuracy to be seen easily and the CLOCKTRIM argument is less of a long-term guess. Adjusting the value and re-measuring allows the trim to be derived in a short time. The resultant number can then be written on the chip with a fine-point marker for future reference in applications where
reasonable accuracy is required. As it is procedurally generated, it is to be expected for the reading to wander about, but so long as you set the value so it is the nearest to 1MHz most of the time the value will be good enough. In testing, the closest value was seen to
vary between about 999969 to 100053 but spent most of its time just a few Hz either side of 1MHz:
Originally published here:
http://www.thebackshed.com/forum/forum_posts.asp?TID=7822Credit: matherp
cpu 40
option clocktrim -3 '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