• AVR Freaks

Hot!Unable to turn off PLL when switching clocks PIC24FJ64GA0002

Author
RoboDLC
Super Member
  • Total Posts : 402
  • Reward points : 0
  • Joined: 2006/03/03 10:49:45
  • Location: Colorado
  • Status: offline
2020/07/15 18:39:33 (permalink)
0

Unable to turn off PLL when switching clocks PIC24FJ64GA0002

I have searched for this answer, and tried about every method I can think of, but I have a problem with switching the clock on a 24FJ64GA0002.  I can switch into the 32MHz FRCPLL mode, but cannot switch to the FRC with no PLL and div by 4 to get 2MHz. 
When I try to switch "down" my clock, instead of getting an Fcy of 1MHz, I get 4MHz.  I tried turning off the PLL and div by 4 postscale, leaving the PLL on and Postcale div by 16, looking at and NOT looking at OSWEN, etc.  I stubbornly always get a 4MHz Fcy.
Here are my routines, can anyone tell me where I am going wrong?  BTW, I start in the 32MHz FRCPLL mode and shift to the slower one in order to use an ADC that has only a 1MHz sample clock, and I use the CLKO outputting Fcy on the pin to supply this clock.  I am using C30 right now, but will be moving to XC16 very soon.  I am working on correcting the last bug in legacy code before migrating forward. 
 
Here is my code:
void BoostClock(void)
{
 uint32_t t_1ms_save;
 
 IEC1bits.INT1IE = 0; // disable SPI IRQ
 IEC0bits.T2IE = 0; // disable timer2 IRQ (Timer2Init() will re-enable it)

 RCONbits.SWDTEN = 0; // turn the dog off
 
 t_1ms_save = t_1ms; // save this for later
 // Enable the PLL
 CLKDIVbits.RCDIV=0; // Run system at 32MHz (16MHz Fcy)
 __builtin_write_OSCCONH(0x01); // FRCPLL on
 __builtin_write_OSCCONL(0x01); // switch
 while(OSCCONbits.OSWEN != 0); // done with switch?
 
 while(!OSCCONbits.LOCK); // Wait for the pll to lock
 Timer2Init(); // set timers correctly to the clock rate
 t_1ms = t_1ms_save; // and get it back
 
 IEC1bits.INT1IE = 1; // enable SPI IRQ

 RCONbits.WDTO = 0; // clear WDT occurred flag
 RCONbits.SWDTEN = 1; // start the watchdog back up
}
 
/**
* \brief Drop the card clock speed back to 2MHz for ADC measurements.
*
* \NOTE: There will be a small amount of jitter in the t_1ms count in the change.
*
* INPUTS: none
* OUTPUT: none
* RETURN: none
*/
void RetardClock(void)
{
 uint32_t t_1ms_save;
 
 IEC1bits.INT1IE = 0; // disable SPI IRQ
 IEC0bits.T2IE = 0; // disable timer2 IRQ (Timer2Init() will re-enable it)
 
 RCONbits.SWDTEN = 0; // turn the dog off
 t_1ms_save = t_1ms; // save this for later
 // Disable the PLL
 CLKDIVbits.RCDIV=2; // Running system at 2MHz (1MHz Fcy)
 __builtin_write_OSCCONH(0x00); // Turn off the PLL
 __builtin_write_OSCCONL(0x01);
 
 while(OSCCONbits.OSWEN != 0); // done with switch?
 //while(!OSCCONbits.LOCK); // Wait for the pll
 Timer2Init(); // set timers correctly to the clock rate
 t_1ms = t_1ms_save; // and get it back
 
 IEC1bits.INT1IE = 1; // enable SPI IRQ

 RCONbits.WDTO = 0; // clear WDT occurred flag
 RCONbits.SWDTEN = 1; // start the watchdog back up
}


 
Thanks for any help you may have,
DLC

DLC
Robotics hobbyist, firmware engineer
dlc@frii.com
#1

2 Replies Related Threads

    nice
    Super Member
    • Total Posts : 1093
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: Unable to turn off PLL when switching clocks PIC24FJ64GA0002 2020/07/15 21:47:26 (permalink)
    0
    Try
     
     __builtin_write_OSCCONH(0x07); //  0b111 = Fast RC Oscillator with Postscaler (FRCDIV)
     __builtin_write_OSCCONL(0x01);



    #2
    RoboDLC
    Super Member
    • Total Posts : 402
    • Reward points : 0
    • Joined: 2006/03/03 10:49:45
    • Location: Colorado
    • Status: offline
    Re: Unable to turn off PLL when switching clocks PIC24FJ64GA0002 2020/07/16 08:08:36 (permalink)
    0
    I feel stupid right now.  I missed the paragraph about FRCDIV in the Oscillator only doc.  Only FRCDIV allows the use of the postscaler.  The FJ64GA002 graphic didn't have the FRCDIV specific block in it that pointed out in big letters for the unobservant and I just totally missed the very obvious FRCDIV bit pattern in the register description.  No excuses, I totally biffed that.  Of course, someone noticed it before I got back here to post the solution, so they get to slap me before I did my own forehead-slap.
     
    DLC
    post edited by RoboDLC - 2020/07/16 08:09:56

    DLC
    Robotics hobbyist, firmware engineer
    dlc@frii.com
    #3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5