3
\$\begingroup\$

I'm little confused about oscillator settings for PIC18F2550. I have 20 MHz crystal (not oscillator) and I want PIC to work (if it is possible) at 48 MHz (both PIC core and USB).

This is part of a table from the datasheet: enter image description here

In the table you can see that for 20MHz input crystal, if I want that uC is operating at 48MHz, I must set clock mode to HSPPL and MCU Clock Division to 2. At least that is how I understand the table. Is my interpretation of the table correct ?

This is how I've set up my mikroC project, based on that table: enter image description here

Is this correct? Will uC operate at 48MHz (12 MIPS)? Also, on the right side (field Oscillator Frequency [MHz]), do I enter 20MHz or 48MHz?

Also, I've noticed (if I'm correct) that I can get uC to operate at 48MHz with 4MHz crystal and 20MHz crystal. If there any benefit in using 20MHz crystal? Or should I be using 4MHz crystal?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

First: Do you have a 20 MHz crystal or crystal oscillator? Those are two different things. A crystal oscillator will all on its own generate a 20 MHz clock signal for the PIC and you use the external oscillator option with it.

On the other hand, the quartz crystal is an external part of the internal oscillator and internal components together with the crystal and load capacitors make a complete oscillator. In such configuration, you use various crystal modes. Also take a look at figure 2.2 on page 27 of the datasheet.

Now to set up this part correctly, you need to understand a few things, so I'll quote the datasheet:

When the PIC18F4550 is used for USB connectivity, it must have either a 6 MHz or 48 MHz clock for USB operation, depending on whether Low-Speed or Full-Speed mode is being used.

You need to combine things so that the USB clock is 48 MHz or 6 MHz and then you need to set-up the microcontroller operating frequency so that it works at suitable speed. Those two clocks may be different.

On page 26 of the datasheet, you have a nice diagram which you should take time to analyze. The USB PLL input expects 4 MHz frequency which it will use to generate the 96 MHz from which it will derive the operating frequency for USB and the microcontroller.

In your screenshot, the 20 MHz are divided by 5 to get the 4 MHz needed for USB PLL which then raises that to 96 MHz, as seen in the PLL prescaler section.

Then you have the system clock postscaler section. It is currently set to use the 96 MHz created by USB PLL and divided by 2 as the main system clock. You also have other options to set the main systme clock. I can't remember exactly what they are and I've just formatted my HDD, so mikroC isn't installed yet. They should offer you to derive the system clock from an internal oscillator or directly from the clock used to generate the 4 MHz for the USB PLL or as it is shown in the screenshot from the 96 MHz generated by the USB PLL.

The point here is that you can independently select the main clock and the USB clock. For example, if you have a 20 MHz oscillator, you could run the PIC main clock at those 20 MHz and at the same time run the USB clock at needed 48 MHz.

Next you have the oscillator selection part. For real crystal oscillators, you should use EC options and connect the output of the oscillator to the OSC1/CKLI pin (in your case pin 9). You can then use the 20 MHz oscillator to drive the PIC.

In case you're using a crystal, you need to use the crystal options. They are XT, for low frequency crystals, up to 4 MHz, and HS for high frequency crystals up to 20 MHz, if I remember correctly.

As for which crystal is better, well that depends on a lot of things such as which exact crystal you're using, its characteristics, characteristics of the PLL used in the PIC and so on.

Usually low frequency crystals drift less over time and produce cleaner signal while high frequency crystals often give as their output a harmonic of some lower frequency and the signal is usually weaker. I myself would use the 4 MHz crystal here.

Also I forgot the last part of your question: In the "Oscillator frequency" field, you should enter the effective operating frequency of the PIC, that is to say the frequency the "primary clock" on figure 2.1 on page 25 of the datasheet sees. In your particular case, that would be 48 MHz.

So to sum this up: In the 20 MHz crystal case, you should first set the "oscillator selection" to HSPLL. That will give 20 MHz at the input of "primary oscillator" in the above-mentioned figure 2.1. Next, you should set the PLL prescaler to divide by 5, so you get 4 MHz which are multiplied by 24 to get the 96 MHz for USB. Next set the "USB clock selection" to 96 MHz divided by 2 and set the "System clock postscaler selection" to 96 divided by two. Finally, set the Oscillator frequency to 48 MHz and you're done with this part.

For the 4 MHz crystal, you should first set HSPLL. Set the PLL prescaler to divide by 1 and then set the "USB clock selection" to 96 MHz divided by 2 and set the "System clock postscaler selection" to 96 divided by two and set the Oscillator frequency to 48 MHz and that's it.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you for such big answer :) I have 20 MHz crystal, not oscillator (I've corrected that in the question). I'm just little confused with some things, so I'll try to ask them here. 1) You are talking about multiple PLL units. On the Figure 2-1 in the datasheet I can only see one PLL and two configurable postscalers. You have also mentioned overclocking. I don't think that I'm doing that, because I can't use PLL directly on input frequency (20 MHz). I will continue this message in another comment. \$\endgroup\$
    – xx77aBs
    May 14, 2012 at 12:57
  • \$\begingroup\$ If I configure the PIC for HSPLL, then first 20MHz is divided by 5 to get 4MHz. That 4MHz clock is used by PLL to generate 96MHz clock. After that, we have CPUDVI element (PLL postscaler) that is dividing 96MHz by 2 - and we get 48MHz clock signal that is used as primary clock. Please tell me if I've misunderstood something. Thank you :) \$\endgroup\$
    – xx77aBs
    May 14, 2012 at 12:57
  • 1
    \$\begingroup\$ @xx77aBs Actually, I think that you're right about two PLLs, so HSPLL should be used. I don't remember what I was thinking at the time... I think that your second comment is right. \$\endgroup\$
    – AndrejaKo
    May 14, 2012 at 13:21
  • 1
    \$\begingroup\$ @xx77aBs In case of doubt, remember that you can always output system clock divided by 4 on a pin and see that with a scope, counter, multimeter or something else. \$\endgroup\$
    – AndrejaKo
    May 14, 2012 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.