Set the crystal and MCU speed

… visits
Parent Previous Next

Choose the crystal fitted to your board, then choose the processor speed. Let the tool keep the related clock settings together. The first steps below are enough to make the selection; the explanations and examples help you check the result.

Hardware tab for PIC18F14K50 with a 12 MHz crystal and 48 MHz MCU clock.

Hardware must match your board. Here a PIC18F14K50 uses a 12 MHz crystal to produce a 48 MHz MCU and USB clock. Choose your own exact PIC and fitted clock source.

Set the crystal and MCU speed

  1. Select the exact target in Hardware.
  2. In External crystal frequency, select the frequency fitted to the board.
  3. In MCU clock frequency (Fosc), select the processor speed you want. Only choices belonging to the selected source and target are offered.
  4. Read the clock description below the selectors. Check the source, Fosc, instruction rate, USB target and any device-specific operating conditions.
  5. Open Registers to inspect the resulting oscillator fuses and runtime values.
  6. Open Integration and use the generated declaration, configuration block and startup sequence together. Run clock startup before initializing peripherals whose operation depends on that clock.
  7. Save the .usbconfig project. Recompile and program the firmware, including its configuration words, after changing clock settings.

After changing the source, check the MCU speed again. The tool keeps your previous speed if possible. Otherwise it uses the device's default speed, or the highest available speed if that default cannot be used.

A listed frequency is not a hardware guarantee. The list shows frequencies that work mathematically with the supported PLL dividers. It does not check the crystal oscillator's electrical frequency limits. Check the PIC datasheet and the crystal circuit separately.

For PIC18F26J50, for example, the list contains 4, 8, 12, 16, 20, 24, 40 and 48 MHz. Dividers 1, 2, 3, 4, 5, 6, 10 and 12 respectively provide the required 4 MHz PLL input. Check the datasheet's electrical oscillator limits and your crystal circuit separately.


Selecting an external crystal uses the target's crystal-oscillator/PLL configuration. It does not automatically change to an external clock driven into a pin. Crystal mode and a separately driven external clock are different hardware arrangements. Do not assume that choosing the same numerical frequency makes them interchangeable.

Which speed goes where?

Use the MCU speed in Declare Xtal, not simply the number printed on the crystal. The clock circuit can multiply and divide the crystal frequency.

Term

What it means

Example

External crystal frequency

The physical crystal fitted between the PIC's oscillator pins

A 20 MHz crystal

MCU clock frequency, or Fosc

The clock supplied to the processor after the selected PLL and dividers

Fosc = 48 MHz

Instruction clock

The clock used for instruction execution

PIC18 Fosc 48 MHz gives a 12 MHz instruction clock

USB clock

The clock required by the full-speed USB peripheral

48 MHz, supporting a 12 Mbit/s USB bus

For PIC18 and the enhanced PIC16 devices discussed here, the instruction clock is normally Fosc divided by four. For PIC24/dsPIC it is Fosc divided by two. Consequently, PIC24 Fosc 32 MHz corresponds to a 16 MHz instruction clock, commonly described as 16 MIPS. Instructions that take several cycles still take several cycles; this number is not a promise that every operation finishes in one cycle.


The USB clock and MCU clock can differ. Selecting a lower MCU speed does not select a slower USB bus. The offered configurations keep the USB clock at its required 48 MHz while changing the processor clock where the device permits it.


Declare Xtal tells Positron which MCU frequency to use when calculating timing. It does not select an oscillator, enable a PLL or program a divider. The generated fuse and register settings do that work.


For example, this is a declaration fragment, not a complete startup program:

Declare Xtal = 48       ' Actual MCU Fosc is 48 MHz

It can be correct with a 20 MHz crystal when the selected clock circuit produces Fosc 48 MHz. Replacing it with Declare Xtal = 20 would tell the compiler the wrong MCU speed. Check timer, serial and display-library settings after changing the MCU frequency as well.

What the clock dividers do

A divider lowers a frequency. A PLL raises it. The input divider, also called a prescaler, works before the PLL. A later postscaler or CPU divider sets the processor speed. The USB hardware may use a different division from the same clock circuit.


For example, an input divider of five changes a 20 MHz crystal signal into a 4 MHz PLL input. The PLL and subsequent divisions then produce the clocks required by that PIC.


Not every device uses this arrangement. Some have fixed three-times or four-times PLLs without an input divider. Some 16-bit devices use separate main and auxiliary PLL paths for the CPU and USB. Divider names and the meaning of their settings also vary. Use the tool's linked selectors instead of copying a divider value from another device.

Two examples to follow

These examples show how the choices fit together. Generate settings for your own PIC; do not copy these fuse choices to another device.


PIC18F26J50 with a 20 MHz crystal


Select PIC18F26J50, 20 MHz, and MCU Fosc 48 MHz. The generated configuration uses OSC = HSPLL, PLLDIV = 5 and CPUDIV = OSC1. The crystal is divided by five for the 4 MHz PLL input; the selected PLL path provides the 48 MHz USB and MCU clocks. The instruction clock is 12 MHz, and the generated declaration is Declare Xtal = 48.


Startup also selects the primary clock path, enables the PLL through OSCTUNEbits_PLLEN and retains the generated startup delay. The fuse setting and runtime PLL enable belong together. Use the complete Integration output so that the remaining board and USB initialization is retained.


PIC24FJ32GB002 with a 4 MHz crystal


Select PIC24FJ32GB002, 4 MHz, and MCU Fosc 32 MHz. The generated settings select the primary oscillator with PLL, POSCMOD_XT, no PLL input division and the 96 MHz PLL. Its USB division gives 48 MHz; the processor path gives Fosc 32 MHz and a 16 MHz instruction clock. The declaration is Declare Xtal = 32.


For this selection, the default CLKDIV value is $0020. The generated startup sets the register and waits for PLL lock before continuing. Keep that sequence rather than replacing it with an assumed delay. The catalog qualifies Fosc 32 MHz for ambient temperatures up to +85 °C; select the offered Fosc 16 MHz configuration above +85 °C. USB remains at 48 MHz.

See also

Created with the Personal Edition of HelpNDoc: Streamline Your Documentation Creation with a Help Authoring Tool