by Jerry Messina » Sat Apr 18, 2015 7:46 pm
If you're looking for code to set config/change osc settings, perhaps this helps
- Code: Select all
device = 24HJ128GP502 ' default FW device
clock = 80
#if (_device = 24HJ128GP502)
config FOSCSEL = {FNOSC_FRCPLL, IESO_OFF}
config FOSC = {FCKSM_CSECMD, OSCIOFNC_OFF, POSCMD_NONE, IOL1WAY_OFF} // OSC2 output on (pin 10)
config FWDT = {WDTPOST_PS256, WDTPRE_PR128, WINDIS_OFF, FWDTEN_OFF}
config FPOR = {FPWRT_PWR128, ALTI2C_OFF}
#endif
macro SetSysClock()
const sysClock = _clock
// Fosc = Fin * (M/N1*N2))
// CLKDIVbits.FRCDIV = 0 (CLKDIV[10:8]) FRC/1= 7.3728MHz
// CLKDIVbits.PLLPOST = N2 (CLKDIV[7:6]) N2: 00=2, 01=4, 11=8
// CLKDIVbits.PLLPRE = N1-2 (CLKDIV[4:0]) N1 = 2-33
// PLLFBDbits.PLLDIV = M-2
if (sysClock = 80) then // 40 MIPS
const FRCDIV = 0
const PLLPOST = 0
const PLLPRE = 1
const PLLDIV = 63 // 39.94
WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
CLKDIV = WREG4
PLLFBD = PLLDIV
elseif (sysClock = 64) then // 32 MIPS
const FRCDIV = 0
const PLLPOST = 0
const PLLPRE = 1
const PLLDIV = 50 // 31.95
WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
CLKDIV = WREG4
PLLFBD = PLLDIV
elseif (sysClock = 32) then // 16 MIPS
const FRCDIV = 0
const PLLPOST = 1
const PLLPRE = 1
const PLLDIV = 50 // 15.97
WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
CLKDIV = WREG4
PLLFBD = PLLDIV
elseif (sysClock = 16) then // 8 MIPS
const FRCDIV = 0
const PLLPOST = 3
const PLLPRE = 1
const PLLDIV = 50 // 7.99
WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
CLKDIV = WREG4
PLLFBD = PLLDIV
elseif (sysClock = 8) then // 4 MIPS
const FRCDIV = 1 // note: this is outside the VCO range... should use FNOSC_FRC mode
const PLLPOST = 3
const PLLPRE = 1
const PLLDIV = 50
WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
CLKDIV = WREG4
PLLFBD = PLLDIV
else
checkparam(etError, "unsupported system clock freq")
end if
// trim osc for accurate baud (optional)
'OSCTUN = &H03A
// wait until the PLL is locked
while (OSCCON.bits(5) = 0) // check LOCK bit
end while
end macro
sub main()
SetSysClock()
end sub
If that's not what you're after, ask again
Last edited by
Jerry Messina on Sat Aug 15, 2015 10:22 am, edited 1 time in total.