Page 1 of 1

48MHz Clock Frequency from 4MHz crystal

PostPosted: Mon Oct 22, 2018 3:52 pm
by aru
I am using SK40C.
I need to have a Microcontroller clock frequency of 48MHz when using 4 MHz crystal.
Device used is 18F4550. IDE used is MPLABX V5.05. Compiler used is XC8 V2.00.

Based on the datasheet, I added the following configuration settings in the code to achieve 48MHz cpu clock;
#pragma config PLLDIV = 1
#pragma config CPUDIV = OSC1_PLL2
#pragma config FOSC = XTPLL_XT

I made a simple code below to check if the clock frequency is correct.

#pragma config PLLDIV = 1
#pragma config CPUDIV = OSC1_PLL2
#pragma config FOSC = XTPLL_XT

#include <xc.h>
#define _XTAL_FREQ 4000000
#define MyPIN LATDbits.LATD0

void main(void)
{
TRISD = 0;
while(1)
{
MyPIN = 1;
NOP();
MyPIN = 0;
NOP();
}
}
NOP() should give 1 cycle delay.
Simulation shows correctly as 41ns of HIGH and 83 seconds if LOW. This is correct for 48MHz instruction frequency.
But after programming the device using the above configuration settings, the Oscilloscope at RD0 shows HIGH and LOW with timing way low than these values

Can someone take a look and advise if the config settings are correct for 48MHz cpu clock.
Thanks for the help

Re: 48MHz Clock Frequency from 4MHz crystal

PostPosted: Tue Oct 23, 2018 9:29 am
by aru
Found the reason for the error.
I overlooked the fact that the instruction frequency is 1/4 of the CPU clock frequency.
For 48MHz CPU clock the instruction frequency is 12MHz.
The cycle time is 83nS.
The oscilloscope waveform is correct.
Thanks.
Please close this case.

Re: 48MHz Clock Frequency from 4MHz crystal

PostPosted: Sat Oct 27, 2018 5:05 am
by ober
Good catch!