12.4.2 Setup for Using Clock Generator with 8 MHz Internal FRC
The following process is used to set up the CLKGEN1 to operate the device with an 8 MHz Internal FRC:
- Enable the clock generator (if not enabled by default) by setting the ON bit in the CLKxCON register.
- To set up the fail-safe for the clock generator, follow these steps:
- Select the backup clock source by selecting the BOSC bits in the CLKxCON register.
- Enable a fail-safe clock failure by setting the FSCMEN bit in the CLKxCON register.
- Enable ClkFailInterrupt to generate an interrupt during clock failure.
- To switch to a new oscillator for the clock generator, follow these steps:
- Select a clock source to switch by writing to NOSC bits in the CLKxCON register.
- Enable switching by writing to the OSWEN bit in the CLKxCON register.
- To further divide the clock out of the clock generator using CLKxDIV, follow these
steps:
- Set the integer divide factor bit setting INTDIV bits in the CLKxDIV register.
- Set the fractional divide factor bit setting FRACDIV bits in the CLKxDIV register. FRACDIV will not work if INTDIV is configured to 0.
- Set the DIVSWEN bit in the CLKxCON register to enable divide factors to get updated.
Code Example for Clock Generator 6 Switching to FRC
// Enable clock generator
CLK6CONbits.ON = 1;
// Configure backup oscillator in case of failure
CLK6CONbits.BOSC = 2; // BFRC
CLK6CONbits.FSCMEN = 1; // Enable fail safe monitor
// Configure clock divide Fdiv = Fin / 2*(INTDIV+(FRACDIV/512))
CLK6DIVbits.INTDIV = 1; // Integer divide factor
CLK6DIVbits.FRACDIV = 128; // Fractional divide factor
CLK6CONbits.DIVSWEN = 1; // Enable divide factors to get updated
while (CLK6CONbits.DIVSWEN != 0); // Wait for switching (hardware cleared)
// Enable clock switching
CLK6CONbits.NOSC = 1 ; // Select FRC clock source
CLK6CONbits.OSWEN = 1; // Enable clock switching
while (CLK6CONbits.OSWEN != 0); // Wait for switching (hardware cleared)
// Enable clock failure interrupt
IFS0bits.CLKFAILIF = 0;
IEC0bits.CLKFAILIE = 1;