PIC18F4550 oscillator config problem

Discussion in 'Embedded Systems and Microcontrollers' started by Vaughanabe13, Jun 18, 2010.

  1. Vaughanabe13

    Thread Starter Active Member

    May 4, 2009
    102
    5
    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:

    Code ( (Unknown Language)):
    1. #pragma config PLLDIV = 4, CPUDIV = OSC1_PLL2, FOSC = HSPLL_HS, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOR = ON, BORV = 3, WDT = OFF, WDTPS = 32768
    2. #pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = ON, STVREN = ON, LVP = OFF, XINST = OFF, VREGEN = OFF, ICPRT = OFF, DEBUG = ON
    3. #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
    4. #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:

    Code ( (Unknown Language)):
    1.     while(1) {
    2.         PORTDbits.RD0 = 1;
    3.         PORTDbits.RD0 = 0;
    4.     }
    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?
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Can you see the assembler output created by the C? The compiler may be adding extra code.
     
  3. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    You could try
    Code ( (Unknown Language)):
    1.  
    2.         PORTDbits.RD0 = 1;
    3.         PORTDbits.RD0 = 0;
    4.         PORTDbits.RD0 = 1;
    5.         PORTDbits.RD0 = 0;
    6.         PORTDbits.RD0 = 1;
    7.         PORTDbits.RD0 = 0;
    8.         PORTDbits.RD0 = 1;
    9.         PORTDbits.RD0 = 0;
    10.         PORTDbits.RD0 = 1;
    11.         PORTDbits.RD0 = 0;
    12.  
    which should be a little more accurate (if you can get your timebase correct on your o'scope)
     
  4. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    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.
     
  5. forum.user

    New Member

    Jun 19, 2010
    2
    1
    Hi,
    PIC18 is 10Mips core so maximum frequency will of 40MHz.
     
  6. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    The 18f4550 is a USB chip which goes 12Mips (48Mhz), and the newer 18fxxKxx can go 16Mips. Check it out here.

    It's hard to keep up to date with all the new released hardware they spin out.
     
  7. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,910
    2,173
    The K (XLP) series is pretty cool. Totally new process with a lot more headroom for future improvements. It's amazing what you can do with Intel hand-me-downs. :)
    http://www.microcontroller.com/news/Microchip_nanoWatt_XLP_eXtreme_Low_Power.asp
     
  8. Vaughanabe13

    Thread Starter Active Member

    May 4, 2009
    102
    5
    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!
     
Loading...