PDA

View Full Version : Oscillator setup



PaulMaker
- 15th October 2024, 08:16
Hi guys,

I’m trying to setup the internal oscillator for a 18F2580 mcu at 32Mhz using PLL but I have a few basic questions and just wanted to make sure I get it right…
As I can read from the datasheet, I need to set the config bit, osccon and osctune (and obviously define the OSC in the code).

For the config I used:

#CONFIG
CONFIG OSC = IRCIO67 ; Internal oscillator block, port function on RA6 and RA7
.
.
.
#ENDCONFIG

For osctune I selected the following bits (not real code format):
OSCTUNE.6=1 -> enable PLL
OSCTUNE 4-0=00000 -> Center frequency

For osccon I selected the following bits (not real code format):
OSCCON 6-4=111 -> select 8Mhz
OSCCON.7=0 -> Sleep when sleep command

However, for the osccon and osctune I have the following doubts…

OSCCON bits 1-0:
What is the difference between 1x=Internal oscillator block and 00=Primary oscillator?

OSCCON bit 2:
INTOSC frequency is stable or not stable…should I set this to 0?

OSCCON bit 3:
Should I set to 0 or 1?

OSCTUNE bit 7:
What is the difference between the 2 options?
“1 = 31.25 kHz device clock derived from 8 MHz INTOSC source (divide-by-256 enabled)
0 = 31 kHz device clock derived directly from INTRC internal oscillator”

Thanks

HenrikOlsson
- 15th October 2024, 17:13
Hi,
Kudos for actually reading the datasheet!

Your CONFIG looks allright for selecting the internal oscillator.



However, for the osccon and osctune I have the following doubts…

OSCCON bits 1-0:
What is the difference between 1x=Internal oscillator block and 00=Primary oscillator?
[quote]
Look at figure 3-8 in the datasheet (https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/39637d.pdf). There they show the primary oscillator as the one interfaced to OSC1/OSC2 pins of the PIC. Then again, in the text they say that "The primary oscillators include the external crystal and resonator modes, the external RC modes, the external clock modes and the internal oscillator block".

[quote]
OSCCON bit 2:
INTOSC frequency is stable or not stable…should I set this to 0?
No. This is a read-only bit indicating if the frequency is stable or not.
If you look above the bit definition in the datasheet you'll see something like [R/W-1] or [ R-0 ]. The first means that bit is Read/Write and its default value is '1'. The second means that it's a read-only bit and its default value is 0.



OSCCON bit 3:
Should I set to 0 or 1?
Same as above. Status bit for you to read (if you need to) but you can't write to it.



OSCTUNE bit 7:
What is the difference between the 2 options?
“1 = 31.25 kHz device clock derived from 8 MHz INTOSC source (divide-by-256 enabled)
0 = 31 kHz device clock derived directly from INTRC internal oscillator”
Again, look at figure 3-8. You can see that the 31kHz into the MUX can come from either the internal oscillator block OR from the (low power) RC oscillator.
If you want a real low power solution and run at 31kHz you'd do that by shutting down the power hungry (relatively speaking) internal 8MHz oscillator and tap of the 31kHz from the RC oscillator. If you're not going to run the PIC at 31kHz it doesn't matter.

Hope that helps.
/Henrik.

tumbleweed
- 16th October 2024, 00:09
OSCCON bits 1-0:
What is the difference between 1x=Internal oscillator block and 00=Primary oscillator?


This can be confusing the way they word this.
The 'primary oscillator' is the one selected by the 'config' setting.

PaulMaker
- 16th October 2024, 07:48
Greetings,

HenrikOlsson and tumbleweed thank you so much for your help.

I think I have a better understanding of these settings with your help.
I also found this:
8-bit PIC® MCU Clock Switching - Developer Help (microchip.com)

I (https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8bit-pic/oscillator-options/clock-switching/)n there they also say


When the SCS bits = 00, the system clock is switched to the clock source selected by the Fosc bits in the Configuration Register. This can be the Internal Oscillator (https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8bit-pic/oscillator-options/internal/), External Crystal/Resonator (https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8bit-pic/oscillator-options/external-crystal-oscillator-modes/), or External Clock (https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8bit-pic/oscillator-options/external-clock-mode/).When the SCS bits = 01, the system clock is switched to the Secondary Oscillator which is an external 32.768 Khz crystal that controls the Timer1 Peripheral (https://developerhelp.microchip.com/xwiki/bin/view/products/mcu-mpu/8bit-pic/peripherals/timers/timer-1/). The external clock crystal is an optional clock source that must be part of the Timer1 design circuit.
When the SCS bits = 10 or 11, then the system clock is switched to the Internal Oscillator independent of the Fosc configuration bit settings. The IRCF bits of the OSCCON register will select the internal oscillator frequency.

This makes me believe that, if we have the internal osc setup in the FOSC<2:0>, the result will be the same if I select any of the 2 options:

"1x = Internal oscillator block" or "00 = Primary oscillator"

Am I right?