PIC18F4550 oscillator config problem

Thread Starter

Vaughanabe13

Joined May 4, 2009
102
So I have a 16MHz resonator attached to the PIC (built-in capacitors on the resonator). I verified the oscillating frequency with a scope - it's a solid 16 MHz. I want to run the PIC at 48 MHz (16 MHz instruction cycle) using the internal PLL. So this is how I have it configured:

Rich (BB code):
#pragma config PLLDIV = 4, CPUDIV = OSC1_PLL2, FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOR = ON, BORV = 3, WDT = OFF, WDTPS = 32768
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = ON, STVREN = ON, LVP = OFF, XINST = OFF, VREGEN = OFF, ICPRT = OFF, DEBUG = ON
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF, EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF
As it is now, the clock should be divided by 4 to get the 4MHz base frequency PLL input. Then the PLL will multiply to 96MHz and finally it will get divided by 2 (CPUDIV). So I should end up with 48MHz (16MHz instruction cycle). Now, how do I test the operating frequency of the PIC? I can write a simple while loop to toggle a pin on and off constantly like this:

Rich (BB code):
    while(1) {
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
    }
But because of the instructions that get executed in the loop, it won't show up as the true operating frequency on a scope. Right now I only see a frequency of 3 MHz. Even with the extra instructions it executes to evaluate the loop condition, it seems like 3 MHz is rather low. I should be seeing > 10 MHz if the PIC is running at 16MHz. So it seems to me like something is wrong with my oscillator configuration, because 3MHz pin toggling is too slow. Is there anything I can write in assembly that will constantly toggle a pin on and off at the operating frequency of the PIC? How do I verify the operating speed? Is there anything wrong with my oscillator configuration?
 

coldpenguin

Joined Apr 18, 2010
165
You could try
Rich (BB code):
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
which should be a little more accurate (if you can get your timebase correct on your o'scope)
 
Looks right to me, 48Mhz/4 is 12Mips, which is as fast as you can go with that device. Toggling a port pin in a tight loop like that takes 4 instructions so 12Mips/4 is 3Mips. Unroll the loop like Coldpenguin for better performance.

Not sure how you can use the clkout pin to the full effect with the 18f4550. Take a 18f14k22, config a 16Mhz intosc with clkout, 4X PLL, and you can scope a 16Mhz waveform on the clockout pin.
 

nsaspook

Joined Aug 27, 2009
13,081

Thread Starter

Vaughanabe13

Joined May 4, 2009
102
You could try
Rich (BB code):
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
        PORTDbits.RD0 = 1;
        PORTDbits.RD0 = 0;
which should be a little more accurate (if you can get your timebase correct on your o'scope)
This is the right answer. I did a facepalm when I thought about it, haha. I did this and observed 6MHz on the scope, which is consistent with my calculations that the PIC should be running at 12 MHz. Thanks, problem solved!
 
Top