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:

  1. Enable the clock generator (if not enabled by default) by setting the ON bit in the CLKxCON register.
  2. To set up the fail-safe for the clock generator, follow these steps:
    1. Select the backup clock source by selecting the BOSC bits in the CLKxCON register.
    2. Enable a fail-safe clock failure by setting the FSCMEN bit in the CLKxCON register.
    3. Enable ClkFailInterrupt to generate an interrupt during clock failure.
  3. To switch to a new oscillator for the clock generator, follow these steps:
    1. Select a clock source to switch by writing to NOSC bits in the CLKxCON register.
    2. Enable switching by writing to the OSWEN bit in the CLKxCON register.
  4. To further divide the clock out of the clock generator using CLKxDIV, follow these steps:
    1. Set the integer divide factor bit setting INTDIV bits in the CLKxDIV register.
    2. Set the fractional divide factor bit setting FRACDIV bits in the CLKxDIV register. FRACDIV will not work if INTDIV is configured to 0.
    3. 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;