Oscillator
Oscillator
Oscillator
HIGHLIGHTS
This section of the manual contains the following topics:
7
7.1 Introduction .................................................................................................................... 7-2
7.2 CPU Clocking................................................................................................................. 7-3
Oscillator
7.3 Oscillator Configuration Registers ................................................................................. 7-4
7.4 Special Function Registers ............................................................................................ 7-7
7.5 Primary Oscillator (Posc) ............................................................................................. 7-14
7.6 Internal Fast RC (FRC) Oscillator ................................................................................ 7-18
7.7 Phase-Locked Loop ..................................................................................................... 7-20
7.8 Secondary Oscillator (Sosc) ........................................................................................ 7-25
7.9 Low-Power RC (LPRC) Oscillator................................................................................ 7-26
7.10 Fail-Safe Clock Monitor (FSCM) .................................................................................. 7-27
7.11 Clock Switching............................................................................................................ 7-28
7.12 Two-Speed Start-up ..................................................................................................... 7-32
7.13 Register Maps.............................................................................................................. 7-33
7.14 Related Application Notes............................................................................................ 7-34
7.15 Revision History ........................................................................................................... 7-35
7.1 INTRODUCTION
The dsPIC33F oscillator system includes these characteristics:
• Four external and internal oscillator options
• On-chip Phase-Locked Loop (PLL) to boost the internal operating frequency on selected
internal and external oscillator sources
• On-the-fly clock switching between various clock sources
• Doze mode for system power-saving
• Fail-Safe Clock Monitor (FSCM) that detects clock failure and permits safe application
recovery or shutdown
• Nonvolatile Configuration bits for clock source selection
A block diagram of the dsPIC33F oscillator system is shown in Figure 7-1.
DOZE
(1) S1/S3
S1 PLL
OSC2
POSCMD<1:0>
FP
÷ 2
FOSC
FRC
FRCDIV
FRCDIVN
Oscillator S7
FRCDIV<2:0>
TUN<5:0>
FRCDIV16
÷ 16 S6
FRC S0
LPRC LPRC S5
Oscillator
LPOSCEN
SOSCI Clock Fail Clock Switch Reset
Timer1
Oscillator
(Posc)”).
TCY
FOSC
FCY
PC PC PC + 2 PC + 4
Table 7-1 lists the configuration settings that select the device oscillator source and operating
mode at a POR.
U U U U U U U U
— — — — — — — —
bit 15 bit 8
Legend: 7
R = Readable bit P = Programmable bit U = Unused bits, program to Logic ‘1’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Oscillator
bit 15-8 Unimplemented: Read as ‘1’
bit 7 IESO: Internal External Start-up Option bit
1 = Start-up device with the Internal FRC Oscillator, then automatically switch to the user-selected oscillator
source when ready
0 = Start-up device with user-selected oscillator source
bit 6-3 Unimplemented: Read as ‘1’
bit 2-0 FNOSC<2:0>: Initial Oscillator Source Selection bits
111 = Fast RC Oscillator with Divide-by-N (FRCDIVN)
110 = Fast RC Oscillator with Divide-by-16 (FRCDIV16)
101 = Low-Power RC (LPRC) Oscillator
100 = Secondary Oscillator (SOSC)
011 = Primary Oscillator with PLL (XTPLL, HSPLL, ECPLL)
010 = Primary Oscillator (XT, HS, EC)
001 = Fast RC Oscillator with PLL (FRCPLL)
000 = Fast RC Oscillator (FRC)
U U U U U U U U
— — — — — — — —
bit 15 bit 8
Legend:
R = Readable bit P = Programmable bit U = Unused bits, program to Logic ‘1’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Note 1: The IOL1WAY bit is not available on all dsPIC33F devices. Refer to the specific device data sheet for
more information.
Oscillator
Internal FRC Oscillator frequency to be adjusted over a range of ±12%.
Note: The oscillator SFRs (OSCCON, CLKDIV, PLLFBD and OSCTUN) are reset only on
a POR.
Note 1: The IOLOCK bit is not available on all dsPIC33F devices. Refer to the specific device data sheet for more
information.
2: Writes to this register require an unlock sequence. For details and examples refer to 7.11 “Clock
Switching”.
Note 1: The IOLOCK bit is not available on all dsPIC33F devices. Refer to the specific device data sheet for more
information.
7
2: Writes to this register require an unlock sequence. For details and examples refer to 7.11 “Clock
Switching”.
Oscillator
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Note 1: This bit is cleared when the ROI bit is set and an interrupt occurs.
2: For more information on Doze mode, refer to Section 9. “Watchdog Timer and Power-Saving Modes”
(DS70196).
bit 4-0 PLLPRE<4:0>: PLL Phase Detector Input Divider Select bits (also denoted as ‘N1’, PLL prescaler)
11111 = Input divided by 33
•
•
•
00001 = Input divided by 3
00000 = Input divided by 2 (default)
Note 1: This bit is cleared when the ROI bit is set and an interrupt occurs. 7
2: For more information on Doze mode, refer to Section 9. “Watchdog Timer and Power-Saving Modes”
(DS70196).
Oscillator
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
7
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Oscillator
bit 15-6 Unimplemented: Read as ‘0’
bit 5-0 TUN<5:0>: Fast RC Oscillator Tuning bits
011111 = Center frequency + 11.625% (8.23 MHz)
011110 = Center frequency + 11.25% (8.20 MHz)
•
•
•
000001 = Center frequency + 0.375% (7.40 MHz)
000000 = Center frequency (7.37 MHz nominal)
111111 = Center frequency – 0.375% (7.345 MHz)
•
•
•
100001 = Center frequency – 11.625% (6.52 MHz)
100000 = Center frequency – 12% (6.49 MHz)
A diagram of the crystal oscillator circuit that is recommended for the dsPIC33F device is
presented in Figure 7-1.
dsPIC33F
OSC1
C1
To Internal Logic
XTAL R
POSCMD
C2
OSC2
The optimum load capacitance (CL) for a given crystal is specified by the crystal manufacturer.
CL can be calculated as shown in Equation 7-1.
Equation 7-1: Crystal Load Capacitance
C1 x C2
CL = CS +
C1 + C2
Where:
Cs is the stray capacitance.
Assuming C1 = C2, Equation 7-2 gives the capacitor value (C1, C2) for a given load and stray
capacitance.
7
Equation 7-2: External Capacitor for Crystal
Oscillator
C1 = C2 = 2 x (CL – CS)
For additional information on crystal oscillators and their operation, refer to 7.14 “Related
Application Notes”.
VIH
Voltage
VIL
0V
To ensure that a crystal oscillator (or ceramic resonator) has started and stabilized, an Oscillator
Start-up Timer (OST) is provided with the POSC and SOSC. The OST is a simple 10-bit counter
that counts 1024 cycles before releasing the oscillator clock to the rest of the system. This
time-out period is denoted as TOST.
The amplitude of the oscillator signal must reach the VIL and VIH thresholds for the oscillator pins
before the OST can begin to count cycles. The TOST interval is required every time the oscillator
restarts (e.g., on a POR, BOR and wake-up from Sleep mode).
When the POSC is enabled, it takes a finite amount of time to start oscillating. This delay is
denoted as TOSCD. After TOSCD, the OST timer takes 1024 clock cycles (TOST) to release the
clock. The total delay for the clock to be ready is TOSCD + TOST. If the PLL is used, an additional
delay is required for the PLL to lock, see 7.7 “Phase-Locked Loop”.
POSC start-up behavior is illustrated in Figure 7-5, indicating where the CPU begins toggling an
I/O pin when it starts execution after the TOSCD + TOST interval.
Oscillator
2: OSC2 pin function is determined by the Primary Oscillator Mode (POSCMOD<1:0>)
and the OSC2 Pin Function Configuration bits (OSCIOFNC).
OSC1
Clock from External
System dsPIC33F
FCY OSC2
OSC1
Clock from External
System
dsPIC33F
I/O OSC2
Note 1: Refer to the specific device data sheet for the accuracy of the FRC clock frequency
over temperature and voltage variations.
2: The FRC Oscillator Tuning bits (TUN<5:0>) should not be changed dynamically
when operating in internal FRC with PLL.
To change the FRC Oscillator Tuning bits:
a) Switch the clock to non-PLL mode (e.g., Internal FRC).
b) Make the necessary changes.
c) Switch the clock back to PLL mode.
The Internal FRC Oscillator starts up instantly. Unlike a crystal oscillator, which can take several
milliseconds to begin oscillation, the Internal FRC Oscillator starts oscillating immediately.
The Initial Oscillator Source Selection Configuration bits (FNOSC<2:0>) in the Oscillator Source
Selection register (FOSCSEL<2:0>) select the FRC clock source. The FRC clock source options
at the time of a POR are shown in Table 7-4. The Configuration bits are programmed at the time
of device programming.
Optionally, the FRC postscaler output can be used with the internal PLL to boost FOSC to 80 MHz
for 40 MIPS instruction cycle execution speed.
Note: The FRC divider should not be changed dynamically when operating in Internal FRC
with a PLL.
To change the FRC divider:
1. Switch the clock to non-PLL mode (for example, Internal FRC).
2. Make the necessary changes.
3. Switch the clock back to PLL mode.
Oscillator
PLLPRE<4:0>
PLLPOST<1:0>
÷M
PLLDIV<8:0>
For a proper PLL operation, the Phase Frequency Detector (PFD) input frequency and Voltage
Controlled Oscillator (VCO) output frequency must meet the following requirements:
• The PFD input frequency (FREF) must be in the range of 0.8-8.0 MHz
• The VCO output frequency (FVCO) must be in the range of 100-200 MHz
The PLL Phase Detector Input Divider Select bits (PLLPRE<4:0>) in the Clock Divisor register
(CLKDIV<4:0>) specify the input divider ratio (N1), which is used to scale down the input
frequency (FIN) to meet the PFD input frequency range of 0.8-8 MHz.
The PLL Feedback Divisor bits (PLLDIV<8:0>) in the PLL Feedback Divisor register
(PLLFBD<8:0>) specify the divider ratio (M), which scales down FVCO for feedback to the PFD.
FVCO is ‘M’ times FREF.
The PLL VCO Output Divider Select bits (PLLPOST<1:0>) in the Clock Divisor register
(CLKDIV<7:6>) specify the divider ratio (N2) to limit FOSC to 80 MHz.
Equation 7-3 shows the relation between FIN and FOSC.
Equation 7-3: FOSC Calculation
M ( PLLDIV + 2 )
F OSC = F IN × ⎛ ---------------------⎞ = F IN × ⎛ ----------------------------------------------------------------------------------------⎞
⎝ N1 × N2⎠ ⎝ ( PLLPRE + 2 ) × 2 ( PLLPOST + 1 )⎠
Where:
N1 = PLLPRE + 2
N2 = 2 x (PLLPOST + 1)
M = PLLDIV + 2
M ( PLLDIV + 2 )
F VCO = F IN × ⎛ -------⎞ = F IN × ⎛ -------------------------------------⎞
⎝ N1⎠ ⎝ ( PLLPRE + 2 )⎠
Oscillator
1
F REF = F IN ⎛ -------⎞ = 0.5 ( F IN )
⎝ N1⎠
M
F OSC = F IN ⎛ -------------------⎞ = 6.25 ( F IN )
⎝ N1 ⋅ N2⎠
Given the preceding equations, the FIN to the PLL module must be limited to 4 MHz < FIN < 8
MHz to comply with the FVCO requirement (100M < Fvco < 200M), if the default values of
PLLPRE, PLLPOST, and PLLDIV are used.
The POSC can support the following input frequency ranges, which are not within the frequency
limit required (4 MHz < FIN< 8 MHz) at a POR.
• POSC in XT mode supports: 3-10 MHz crystal
• POSC in HS mode supports: 10-40 MHz crystal
• POSC in EC mode supports: 0.8-64 MHz input
To use a PLL when the input frequency is not within the 4-8 MHz range, you must follow this
process:
1. Power-up the device with the Internal FRC Oscillator, or the POSC, without a PLL.
2. Change PLLDIV, PLLPRE, and PLLPOST bit values, based on the input frequency, to
meet these PLL requirements:
• FREF must be in the range of 0.8-8.0 MHz
• FVCO must be in the range of 100-200 MHz
3. Switch the clock to a PLL mode in software.
Note: PLLPRE bits and PLLDIV bits should not be changed when operating in the PLL
mode. You must clock switch to the non-PLL mode (e.g., Internal FRC) to make the
necessary changes, and then clock switch back to the PLL mode.
Example 7-1 illustrates code for using the PLL with the POSC. (See also Section 7.11 “Clock
Switching” for clock switching example code.)
Example 7-1: Code Example for Using the PLL with the POSC
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC);
int main()
{
7
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD=30; // M = 32
CLKDIVbits.PLLPOST = 0;// N2 = 2
CLKDIVbits.PLLPRE = 0; // N1 = 2
Oscillator
// Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0b011)
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
7.7.2.2 SETUP FOR USING THE PLL WITH 7.37 MHZ INTERNAL FRC
The following process is used to set up the PLL to operate the device at 40 MIPS with a 7.37 MHz
Internal FRC.
1. To execute instruction at 40 MHz, ensure that the system clock frequency is:
FOSC = 2 x FCY = 80 MHz
2. Ensure that the default Reset values of PLLPRE, PLLPOST and PLLDIV meet the PLL
and user requirements.
3. If the PLL and user requirements are met – directly configure the FNOSC<2:0> bits
(FOSCSEL<2:0>) to use the POSC with PLL at a POR.
If the PLL and user requirements are not met – follow these steps:
a) Select the PLL postscaler to meet VCO output frequency requirement
(100 < FVCO < 200 MHz).
b) Select the PLL prescaler to meet PFD input frequency requirement
(0.8 < FREF < 8 MHz).
c) Select the PLL feedback divisor to generate required VCO output frequency based
on the PFD input frequency.
d) Configure the FNOSC<2:0> bits (FOSCSEL<2:0>) to select a clock source without
the PLL (e.g., Internal FRC) at a POR.
e) In the main program, change the PLL prescaler, PLL postscaler and PLL feedback divisor
to meet the PLL and user requirements, and then perform a clock switch to the PLL
mode.
Example 7-2 illustrates code for using the PLL with a 7.37 MHz Internal FRC. (See also
7.11 “Clock Switching” for clock switching example code.)
Example 7-2: Code Example for Using the PLL with 7.37 MHz Internal FRC
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC);
int main()
{
Oscillator
When the SOSC is not being used to provide the system clock, or the device enters Sleep mode,
the SOSC is disabled to save power.
Note: The clock frequency of the LPRC Oscillator will vary, depending on the device
voltage and operating temperature. Refer to the “Electrical Characteristics”
section in the specific device data sheet for details.
Note: The LPRC runs in Sleep mode only if WDT is enabled. Under all other conditions,
the LPRC is disabled in Sleep mode.
Note: The FSCM does not wake-up the device if the clock fails while the device is in Sleep 7
mode.
The FSCM module takes the following actions when it switches to the Internal FRC Oscillator:
Oscillator
• Current Oscillator Selection bits COSC<2:0> (OSCCON<14:12>) are loaded with ‘000’
(Internal FRC Oscillator)
• Clock Fail Detect bit CF (OSCCON<3>) is set to indicate the clock failure
• Oscillator Switch Enable Control bit OSWEN (OSCCON<0>) is cleared to cancel any
pending clock switches
For additional information, refer to Section 8. “Reset” (DS70192). The most recent
documentation can always be found on the Microchip web site, www.microchip.com.
After the previous steps are completed, the clock switch logic performs the following steps:
1. The clock switching hardware compares the COSC<2:0> Status bits (OSCCON<14:12>)
with the new value of the NOSC<2:0> Control bits (OSCCON<10:8>). If they are the
same, the clock switch is a redundant operation. In this case, the OSWEN bit
(OSCCON<0>) is cleared automatically and the clock switch is aborted.
2. If a valid clock switch has been initiated, the PLL Lock Status bits (OSCCON<5>) and
Clock Fail Status bits (OSCCON<3>) are cleared.
3. The new oscillator is turned on by the hardware (if it is not currently running). If a crystal
oscillator (the POSC or SOSC) must be turned on, the hardware waits for TOSCD until the
crystal starts oscillating and TOST expires. If the new source uses the PLL, the hardware
waits until a PLL lock is detected (OSCCON<5> = 1).
4. The hardware waits for the new clock source to stabilize and then performs the clock 7
switch.
5. The hardware clears the OSWEN bit (OSCCON<0>) to indicate a successful clock
transition. In addition, the NOSC<2:0> bit values (OSCCON<10:8>) are transferred to the
Oscillator
COSC<2:0> Status bits (OSCCON<14:12>).
6. The old clock source is turned off at this time, with the exception of the LPRC (if the WDT
or FSCM is enabled) or the SOSC (if the SOSCEN remains set). The timing of the transition
between clock sources in shown in Figure 7-9.
Note 1: Clock switching between the XT, HS and EC Primary Oscillator modes is not pos-
sible without reprogramming the device.
2: Direct clock switching between the PLL modes is not possible. For example, clock
switching should not occur between the POSC with PLL and the Internal FRC
Oscillator with PLL.
3: Setting the CLKLOCK bit (OSCCON<7>) prevents clock switching when clock
switching is enabled and the FSCM is disabled by the Configuration bits
FCKSM<1:0> (FOSC<7:6>) = 01. The CLKLOCK bit (OSCCON<7>) cannot be
cleared when it has been set by software; it clears on a POR.
4: The processor continues to execute code throughout the clock switching sequence.
Timing-sensitive code should not be executed during this time.
5: The clock switch will not wait for the PLL lock if the PLLKEN bit in the FWDT Fuse
Configuration register (FWDT<5>) is set to ‘0’.
System Clock
OSWEN
Note: The system clock can be any selected source – POSC, SOSC, FRC or LPRC.
A recommended code sequence for a clock switch includes the following actions:
1. Disable interrupts during the OSCCON register unlock-and-write sequence.
2. Execute the unlock sequence for the OSCCON high byte. In 2 back-to-back instructions:
• write 0x78 to OSCCON<15:8>
• write 0x9A to OSCCON<15:8>
3. In the instruction immediately following the unlock sequence, write the new oscillator
source to the NOSC<2:0> Control bits (OSCCON<10:8>).
4. Execute the unlock sequence for the OSCCON low byte. In 2 back-to-back instructions:
• write 0x46 to OSCCON<7:0>
• write 0x57 to OSCCON<7:0>
5. In the instruction immediately following the unlock sequence, set the OSWEN bit
(OSCCON<0>).
6. Continue to execute code that is not clock-sensitive (optional).
7. Check to see if the OSWEN bit (OSCCON<0>) is ‘0’. If it is, the switch was successful.
Note: MPLAB® C Compiler for dsPIC DSCs provides the following built-in C language
functions for unlocking the OSCCON register:
__builtin_write_OSCCONL(value)
__builtin_write_OSCCONH(value)
Example 7-3 illustrates the code sequence for unlocking the OSCCON register and switching
from the FRC with PLL clock source to the LPRC clock source.
wait:
btsc OSCCONL, #OSWEN
bra wait
Oscillator
• If the new clock source uses a PLL, a clock switch will not occur until lock has been
achieved. Your software can detect a loss of a PLL lock because the LOCK bit
(OSCCON<5>) is cleared and the OSWEN bit (OSCCON<0>) is set.
• Switching to a low-frequency clock source, such as the secondary oscillator, will result in
slow device operation.
Note: Two-Speed Start-up is redundant if the selected device clock source is FRC.
Table 7-8 maps the bit functions for the Oscillator Special Function Control registers. Table 7-9 maps the bit functions for the Oscillator
Configuration registers.
Section 7. Oscillator
DS70186D-page 7-33
7
Oscillator
dsPIC33F Family Reference Manual
Note: Please visit the Microchip web site (www.microchip.com) for additional Application
Notes and code examples for the dsPIC33F family of devices.
Oscillator
011111 = Center frequency + 11.625% (8.23 MHz)
011110 = Center frequency + 11.25% (8.20 MHz)
• Tables:
- Oscillator Special Function Control Registers table (see Table 7-9): Bit 5 register is
modified as blank.
• Additional minor corrections such as language and formatting updates have been
incorporated throughout the document.
NOTES: