Entire Forum This board This topic Members Entire Site
 Pages: [1]
 Author Topic: PIC18 OSCTUNE tune range  (Read 2017 times) 0 Members and 1 Guest are viewing this topic.
Signal
Active Member

Offline

Posts: 195

Thank You
-Given: 108

 « on: April 19, 2018, 04:00:19 16:00 »

Results of measurements:
For PIC18F26K22 the range of internal frequency tuning is about ±3% for TUN<5:0> bits of the OSCTUNE from -32 to 31.

Backstory:
I needed to obtain 16000 Hz L/R clocks for I2S driven by bit-banging by PIC18 clocked by internal oscillator.
For that task I chose Fosc=16MHz and 4 cycles between SCK edges.
Then LRCLK = Fosc/(4*64*4) = 15.625kHz that is 2.3% away from precise value.
Then I have options:
a) leave it with that variance
b) add a jitter by making 6 from 64 periods equal to 3 cycles:  16000/(4*(64*4-6)) = 16
c) use 16.384 MHz quartz
d) tune internal oscillator by setting TUN<5:0> bits of the OSCTUNE

For the latter I tried to find information about the range that frequency can be tuned over.
I found only one source that claims certain value:
The Essential PIC18® Microcontroller (By Sid Katzen) - Page 313 - Google Books Result
Quote
For example, the PIC18F452 <...>
It is possible to tune the frequency slightly, up to ±12%, by writing to the lower five bits of the OSCTUNE register TUN4:0
PIC18F452 is quite old, uses 5 bit TUN instead of 6 in K22.

So doubts were present and I have made measurement for PIC18F26K22 myself:
Code:
Measurements of INTOSC/1024 of PIC18F26K22 @24℃:
TUN  kHz @ 3.16V      kHz @ 3.55V      kHz @ 4.75V
+30: 16.135  +2.92%   16.145  +2.70%   16.171 +2.86%
+20: 15.981  +1.94%   16.023  +1.93%   16.020 +1.90%
+10: 15.825  +0.94%   15.856  +0.87%   15.857 +0.86%
0: 15.677 (-0.27%)  15.720   0       15.722  0
-10: 15.529  -0.94%   15.568  -0.97%   15.561 -1.02%
-20: 15.384  -1.87%   15.422  -1.90%   15.421 -1.91%
-30: 15.241  -2.78%   15.283  -2.78%   15.279 -2.82%
 Logged

Give a right name to a right game and play it right
motox
Cracking Team
Active Member

Offline

Posts: 167

Thank You
-Given: 228

 « Reply #1 on: April 19, 2018, 05:26:12 17:26 »

You found only one source and it is completely wrong.
The PIC18F452 neither has internal oscillator nor OSCTUNE register.
The PIC18F4520 is an extended architecture based on the PIC18F452 and this one has an internal oscillator.

Typically OSCTUNE is used to compensate small drifts (over temperature and Vdd). That book reference of ±12% seems wayyy too much. With such high value the resolution would be bad (0.39%/bit for the 18F4520).
 Logged
Signal
Active Member

Offline

Posts: 195

Thank You
-Given: 108

 « Reply #2 on: April 19, 2018, 06:05:05 18:05 »

You found only one source and it is completely wrong.
The PIC18F452 neither has internal oscillator nor OSCTUNE register.
The PIC18F4520 is an extended architecture based on the PIC18F452 and this one has an internal oscillator.
See book's page - PIC18FXX20 is also mentioned. Anyway, do you know the correct source?

Typically OSCTUNE is used to compensate small drifts (over temperature and Vdd). That book reference of ±12% seems wayyy too much. With such high value the resolution would be bad (0.39%/bit for the 18F4520).
I see you have reasonable doubts too.

This book might be not wrong but obsolete. Another quote from book:
Quote
This is calibrated by the factory with typically ±1 % (worst case ±2%) accuracy. This frequency is temperature sensitive and over the range -40℃ to +85℃ can vary by up to ±10%.
K22 has ±5% at T = -40°C to 0°C and +85°C to +125°C

The key point - it seems that there are no official data for given parameter tending a user to measure it himself if required. (Yet another little shame on Microchip;)

 Logged

Give a right name to a right game and play it right
h0nk
Active Member

Online

Posts: 196

Thank You
-Given: 180

 « Reply #3 on: April 19, 2018, 09:04:39 21:04 »

Hello,

for older PIC's like the 12F683/16F684 the range is ±12%. To be exact: -13% and +12%.
Small drifts are part of the internal oscillator and under normal conditions there is
no need to compensate.
With OSCTUNE = 0 measured FOSC/4 @ 5V: 1.0042 MHz and @ 3.3V: 0.9981 MHz
gives 0.42% error @ 5V and 0.19% @ 3.3V.
This is quite sufficient for asynchronous communication.

For audio applications i would recommend a crystal since the internal
oscillator has a lot of jitter.

Best Regards
 Logged
Signal
Active Member

Offline

Posts: 195

Thank You
-Given: 108

 « Reply #4 on: April 19, 2018, 09:49:55 21:49 »

For audio applications i would recommend a crystal since the internal
oscillator has a lot of jitter.
I agree for (almost) all cases!

But in my particular case I use MAX98357A (that itself is quite jitter immune: https://www.edn.com/Home/PrintView?contentItemId=4400490) for a toy making some sounds.

Even more in my case a precise frequency is also not important at all. While the motive to measure the tunning range was not very justified I used it to finally clarify this shaded question that I encountered several times but each time bypassed somehow before.
 Logged

Give a right name to a right game and play it right
motox
Cracking Team
Active Member

Offline

Posts: 167

Thank You
-Given: 228

 « Reply #5 on: April 19, 2018, 10:48:49 22:48 »

Calibrating the internal RC over a range of temperature and voltage is not an easy task and a climatic chamber is not something available on every corner. Measuring the calibration curves for every device is out of question.
I would definitely buy the crystal. If you cannot get that specific crystal value, you can use another good quality clock source just to use it to calibrate the internal RC periodically.

But in my particular case I use MAX98357A (that itself is quite jitter immune: https://www.edn.com/Home/PrintView?contentItemId=4400490) for a toy making some sounds.
The method of cycle-skip the clock is not applicable for an uncalibrated RC clock source, because you don't know how much the internal RC has drifted over time.
 Logged
Signal
Active Member

Offline

Posts: 195

Thank You
-Given: 108

 « Reply #6 on: April 20, 2018, 01:05:38 13:05 »

motox, I really do not clearly understand how your obvious sentences are applicable to my situation or to a separate question about measuring of range of tuning.

Calibrating the internal RC over a range of temperature and voltage is not an easy task and a climatic chamber is not something available on every corner.
That does not stop Microchip from writing that "Precision 16 MHz Internal Oscillator Block:- Factory calibrated to ± 1%"
That assumes that for some tasks a ±1% is enough to be considered as precise. Do you see difference between ±1% and +1%..+3%? What if device is supposed to be used at "room temperature" from say 20°C to 30°C?

Quote
Measuring the calibration curves for every device is out of question.
Out of what question? Why? I thought calibration by definition is meant exactly for every device. If reasonable for particular project of course. Out of my question - definitely.

Quote
I would definitely buy the crystal.
I have the tiny single layer PCB (laser printed, iron transfered) with two ICs (TQFN 3x3mm and SSOP28 10x8mm) and three 0805 capacitors and it works without a crystal. For that particular project I was initially biased to be minimalistic as possible and to find out if it is enough or not. In other case I'd use a little pic32mm with 4 times more flash and hardware I2S for the same price for IC but requiring a bit more complex PCB.

Another (maybe freak) target I was encouraged to was to check out how can I perform an I2S mono playback with some logic handling in parallel (buttons, debouncing, delays, LEDs) by using 8-bit uC. The answer - it is possible, while not well scalable. Having 8-bit hardware multiplier I even made u-law decoder (on the fly) for DPCM samples having only two commands between btg SCK.

Quote
If you cannot get that specific crystal value, you can use another good quality clock source just to use it to calibrate the internal RC periodically.
As a cheap alternative to true PLL with compromised jitter and even more compromised precision (±0.1%)? Why not - would work for some tasks.

Quote
The method of cycle-skip the clock is not applicable for an uncalibrated RC clock source, because you don't know how much the internal RC has drifted over time.
I do not know how exactly MAX98357 works inside. For example it starts to playback at officially unsupported 22.05kHz rate if BCLK is somehow not clean due to "cycle-skip". And supported rates are stated by manufacturer not in ranges but by list of precise values. In that situation I consider as a good thing to give it 16kHz ±1% instead of +1%..+3%. And not for musical precision (6% of semitone, 1% is too rough for absolute pitch humans).

My particular task was not a music playback. Though during playback I do not hear a tone drift (actually it is about 0.03% for the first second after sleep). Remember tape recorder - we lived with pitch shift in some degree without notice. Or old heavily used videotapes - fast drift is not audible on voices but clearly distinguishable on music background.
 Logged

Give a right name to a right game and play it right
 Pages: [1]