News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PLL_SETUP () proceedure location

Started by SCV, Jun 14, 2022, 11:33 AM

Previous topic - Next topic

SCV

Have used it for many years but need to change the crystal from 8Mhz to a 16Mhz and cannot find the location of the proceedure to check the notes.


Currently for an 8Mhz crystal I use PLL_SETUP(70, 2, 2, $0300)In my notes I have (8 x 70) / (2 + 2) = 140Mhz and it works just fine.
 I assume the '2' is the PLLPRE divider and the other '2' is the PLLPOST divider but which one does what
 No idea what the $0300 refers to!


Tim.



top204

#1
It is near the bottom of the device's .def file, and these can be found here: "C:\Program Files (x86)\ProtonIDE\PDS\Includes\Defs\". For example, for a PIC24E or PIC24H or dsPIC33E device, the PLL_Setup meta-macro is:

'
'-------------------------------------------------------------------------------------
' Alter the oscillator on PIC24E or PIC24H or dsPIC33E devices
' Input     : pM represents the 9 PLLDIV bits of the PLLFBD register
'           : pN1 represents the 5 PLLPRE bits of the CLKDIV register
'           : pN2 represents the 2 PLLPOST bits of the CLKDIV register
'           : pOSCCON is the value to write to the 16-bit OSCCON SFR
' Output    : None
' Notes     : The calculation for the oscillator setup is:
'               Fosc = Fin * M / (N1 * N2), Fcy = (Fosc / 2)
'               Example:
'               Fosc = 7.37 * 43 / (2 * 2) = 79.23MHz
'
$define PLL_Setup(pM, pN1, pN2, pOSCCON) '
    CLKDIV = pN1 - 2            '
    $if (pN2 = 2)               '
        CLKDIVbits_PLLPOST0 = 0 '
        CLKDIVbits_PLLPOST1 = 0 '
    $elseif (pN2 = 4)           '
        CLKDIVbits_PLLPOST0 = 1 '
        CLKDIVbits_PLLPOST1 = 0 '
    $elseif (pN2 = 8)           '
        CLKDIVbits_PLLPOST0 = 1 '
        CLKDIVbits_PLLPOST1 = 1 '
    $endif                      '
    PLLFBD = pM - 2             '
    Write_OSCCON(pOSCCON)       '
    While OSCCONbits_lock <> 1 : Wend

Microchip did make them rather complex to work out, but once the idea is understood it comes naturally, which is why I added the example values with the meta-macros. However, on the new dsPIC33xxCK devices, it is far too complex to set oscillator frequencies, and even I get confused sometimes. :-)

SCV

Thanks Les,
That's what I was looking for.
I was checking I wasn't violating the PLL max input frequency by changing to a 16Mhz, but with a PLLPRE divider of 2 I am just in.

Tim.

top204

In the above PLL_Setup meta-macro, you will need to read the device family datasheets for the value to place into the OSCCON register, because each bit within it has a purpose, and not all of them are writable, as the oscillator stability While loop shows.

That is also why the PLL_Setup meta-macro has the Write_OSCCON meta-macro, because it has to be written in a very specific way.