USB and CPU clock selection


Valid for the PIC18F2455/2550/4455/4550 Pic group.


The above mentioned group of PIC's has the same capabilities with respect to the USB clock settings. Most settings are done in the PIC's configuration words (during programming of the PIC), some register settings are done in the PIC's software.

When using a PIC of the above family for an USB application (or even not for USB), then the following steps have to be taken:
  1. Define first the oscillator/clock settings for USB (only in case of USB usage of course)
  2. Define next the oscillator/Clock settings for the CPU
  3. Do not forget to enable the USB voltage regulator

The oscillator block diagram (see also the datasheet of the PIC's): Oscillator Block Diagram

1. The oscillator/clock settings for USB.


Two cases can be distinguished: USB Full speed and USB Low speed.

For full speed USB a 48 Mhz clock speed is necessary. This clock can be derived directly(1) from the primary oscillator (which obviously will have to give 48 Mhz then), or from the primary oscillator via a prescaler(2) and a 96Mhz PLL. In most of the cases choice (2) will be applicable because only crysals upto 20Mhz can be connected to the PIC.

This section is only discussing the USB "full speed" choice with the USB clock generation via the PLL. See also Low speed USB settings further.

The Configurationsettings involved are (to be set in the "project" Edit in the IDE): The PIC registersettings involved are (to be set in the code initialisation):
The input for the PLL is always 4 Mhz, so this gives us the following possibilities:

Crystal (*) 96 Mhz PLL Prescaler
(=PLLDIV)
Setting
PLL Input
Clock
PLL Output
Clock
Full Speed USB Clock
Source Selection (=USBDIV)
Setting
USBDIV Output
Clock
UCFG.FSEN
Setting
USB Clock
4 Mhz No Divide (4 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
8 Mhz Divide by 2 (8 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
12 Mhz Divide by 2 (12 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
16 Mhz Divide by 4 (16 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
20 Mhz Divide by 5 (20 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
24 Mhz Divide by 6 (24 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
40 Mhz Divide by 10 (40 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
48 Mhz Divide by 12 (48 Mhz Input) 4 Mhz 96 Mhz Clock src from 96 Mhz PLL/2 48 Mhz 1 48 Mhz
(*) or external clock

From the table one can easily derive the configuration settings for the crystal used.


2. The Primary and CPU clock settings.


The "Primary" clock is mostly the one used for the CPU (exceptions are the internal clock and the Timer1 clock). Once the USB clock settings are defined (see section above), the primary clock settings can be defined. Here the choice is made by selecting the correct "Oscillator configuration" (config word) and the correct setting of the "Prescaler" (divider). The oscillatorconfiguration setting permits to choose between the actual crystal clock or the PLL output (see above).

If you want the CPU clock to be derived from the PLL (no matter if you want to use USB or not), you also will have to set the PLL Divider to be sure the PLL output is 96 Mhz.

The Configurationsettings involved are (to be set in the "project" Edit in the IDE): The PIC registersettings involved are (to be set in the code initialisation): This gives the following:
(The "MCU Clock" column shows the clock speed to mention in the compiler's IDE project setup.):

Crystal (*) PLL
Output
Oscillator Setting CPU System Clock
Postscaler (=CPUDIV)
Setting
Primary
Clock
OSCCON<1:0>
Setting
MCU
Clock
na 96 Mhz XT: XT+PLL, USB-XT or
HS: HS+PLL, USB-HS or
EC: EC+PLL, EC+PLL+CLKO{RA6}, USB-EC or
EC: EC+PLL, EC+PLL+RA6, USB-EC
[96Mhz PLL src: /2] 48 Mhz 00 48 Mhz
na 96 Mhz XT: XT+PLL, USB-XT or
HS: HS+PLL, USB-HS or
EC: EC+PLL, EC+PLL+CLKO{RA6}, USB-EC or
EC: EC+PLL, EC+PLL+RA6, USB-EC
[96Mhz PLL src: /3] 32 Mhz 00 32 Mhz
na 96 Mhz XT: XT+PLL, USB-XT or
HS: HS+PLL, USB-HS or
EC: EC+PLL, EC+PLL+CLKO{RA6}, USB-EC or
EC: EC+PLL, EC+PLL+RA6, USB-EC
[96Mhz PLL src: /4] 24 Mhz 00 24 Mhz
na 96 Mhz XT: XT+PLL, USB-XT or
HS: HS+PLL, USB-HS or
EC: EC+PLL, EC+PLL+CLKO{RA6}, USB-EC or
EC: EC+PLL, EC+PLL+RA6, USB-EC
[96Mhz PLL src: /6] 16 Mhz 00 16 Mhz
4 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
4 Mhz
2 Mhz
1.33 Mhz
1 Mhz
00 4 Mhz
2 Mhz
1.33 Mhz
1 Mhz
8 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
8 Mhz
4 Mhz
2.6 Mhz
2 Mhz
00 8 Mhz
4 Mhz
2.6 Mhz
2 Mhz
12 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
12 Mhz
6 Mhz
4 Mhz
3 Mhz
00 12 Mhz
6 Mhz
4 Mhz
3 Mhz
16 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
16 Mhz
8 Mhz
5.33 Mhz
4 Mhz
00 16 Mhz
8 Mhz
5.33 Mhz
4 Mhz
20 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
20 Mhz
10 Mhz
6.66 Mhz
5 Mhz
00 20 Mhz
10 Mhz
6.66 Mhz
5 Mhz
24 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
24 Mhz
12 Mhz
4 Mhz
3 Mhz
00 24 Mhz
12 Mhz
4 Mhz
3 Mhz
40 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
40 Mhz
20 Mhz
13.33 Mhz
10 Mhz
00 40 Mhz
20 Mhz
13.33 Mhz
10 Mhz
48 Mhz n/a XT: USB-XT or
HS: USB-HS or
EC: EC+CLKO{RA6}, USB-EC or
EC: EC+RA6, USB-EC
[OSC1/OSC2 src: /1]
[OSC1/OSC2 src: /2]
[OSC1/OSC2 src: /3]
[OSC1/OSC2 src: /4]
48 Mhz
24 Mhz
16 Mhz
10 Mhz
00 48 Mhz
24 Mhz
16 Mhz
10 Mhz
(*) or external clock

From the table one can easily derive the configuration settings for the crystal used and the CPU clock frequency wanted.

Low speed USB clock settings

For low speed USB, set the Primary clock to 24 Mhz (see above how to do it) and UCFG.FSEN (UCFG.2) to 0. This also means that the CPU clock speed is always 24 MHz when using low speed USB.

Types of oscillators

(just for info):
Name in Datasheet IDE Config Setting Meaning
XT XT: USB-XT XT oscillator (low speed crystal/resonator)
XTPLL XT: XT+PLL, USB-XT XT oscillator, PLL enabled
HS HS: USB-HS HS oscillator (high speed crystal/resonator)
HSPLL HS: HS+PLL, USB-HS HS oscillator, PLL enabled
EC EC: EC+CLKO{RA6}, USB-EC External Clock, CLKO function on RA6
ECIO EC: EC+RA6, USB-EC External Clock, port function on RA6
ECPLL EC: EC+PLL, EC+PLL+CLKO{RA6}, USB-EC External Clock, PLL enabled, CLKO function on RA6
ECPIO EC: EC+PLL, EC+PLL+RA6, USB-EC External Clock, PLL enabled, port function on RA6
INTHS INTOSC: USB-HS Internal oscillator for CPU, HS oscillator for USB
INTXT INTOSC: USB-XT Internal oscillator for CPU, XT oscillator for USB
INTIO INTOSC: INTOSC+CLKO{RA6}, USB-EC Internal oscillator for CPU, CLKO function on RA6, External Clock for USB
INTCKO INTOSC: INTOSC+RA6, USB-EC Internal oscillator for CPU, port function on RA6, External Clock for USB

3. The USB voltage regulator.

Do not forget to
-------------------------------------------