Oscillators

From Knowledge Kitchen
Jump to navigation Jump to search


This document discusses oscillators in relation to the PIC microcontroller. However, the basic concepts can be applied to any microprocessor or microcontroller. For example, the Arduino Uno, comes with a built-in 16MHz oscillator.

Cycles per second

The oscillator sets the clock speed of the processor or microcontroller. This clock speed determines how fast the microcontroller can process the code.

Oscillators are rated in cycles per second, a.k.a. Hertz. So a 4MHz oscillator has a clock speed of 4 million cycles per second.

Instructions per second

Each instruction that a processor follows requires several clock cycles to fully process. For example, a PIC microcontroller machine instruction typically requires 4 clock cycles to complete. Therefore a PIC with a 4MHz oscillator, which clocks 4 million cycles per second, can actually only process 1 million instructions per second (1 MIPS). Other microprocessors work similarly.

Note: When we talk about machine instructions, we are referring to the machine code that gets programmed into the microcontroller's memory, not the code that you write in the code editor. Each single line of code you write in a high-level language is compiled into many machine code instructions.

Internal Oscillators

Some microcontrollers, including some PIC microcontrollers, have built-in oscillators. Internal built-in oscillators usually run at 4Mhz, which allows for exactly 1 MIPS. Sometimes, PICS will allow you to run the internal oscillator at 48KHz for low-power settings.

In general, internal oscillators are less exact than external oscillators. However, since they are built into the microcontroller, you don't need to do any circuit setup to start using internal oscillators, which makes them very convenient.

External Oscillators

External oscillators are more precise, but must be connected by you to the microcontroller in your circuit.

There are a variety of types of external oscillators:

  • crystal oscillators
  • ceramic resonators
  • RC (resistor capacitor) oscillators
  • silicon clock chips (either TTL or CMOS)

4MHz crystal oscillator]

4MHz crystal oscillator


If you are using an external oscillator for your project, crystal oscillators or ceramic resonators are probably the easiest to set up. Ceramic resonators are the cheapest.

Most crystals and resonators come with 2 leads, but some ceramic resonators come with 3. These 3-lead resonators have a built-in capacitor to reduce any feedback generated by the oscillator. The middle wire should be connected to GND.

4MHz ceramic resonator

4MHz 3-lead ceramic resonator


When using a crystal oscillator or ceramic resonator with only two leads, you must manually connect a capacitor from each of the OSC pins of the microcontroller to GND. You can typically get by with 20 pF capacitors when using 4, 10, or 20 MHz oscillators, although the exact value of these capacitors depends in theory on the exact frequency of the oscillator being used.


You are recommended to keep all leads and wires involved in the oscillator as short as possible to prevent the fast-moving cycles of the oscillator from interfering with other parts of the circuitry.

Read a detailed intro to basic oscillator circuits here.

See this site for a cost/benefit analysis of the various kinds of external oscillators.

Phase Locked Loop Multiplier

Some microcontrollers, including PICs, have the ability to create virtual oscillator speeds of 4x the speed of an external oscillator by using what's called a phase locked loop multiplier (PLL). In practical terms, this means that your actual clock speed can be 4x that of whatever external oscillator you use.

So if you have a 4MHz external oscillator, you may be able to run a virtual clock speed of 16MHz. This would allow you to process 4 million instructions per second instead of the usual 1 MIPS, simply by enabling the PLL multiplier.

Configuring the PIC microcontroller

In the PIC's software configuration, the common oscillator settings are XT, HS, EC, RC and IN. You pick one depending upon which oscillator you are using.

This configuration can either be set in the code you write or in the MPLab IDE's Configuration Bits settings for the device you are using.

The XT and HS settings are for use with external crystal oscillators:

  • XT for 4 MHz and below
  • HS for 4 MHz and above

For a 4 MHz crystal, either the XT or HS setting will work.

The HS, XT or LP options can also be used with an externally generated clock, connected to OSC1.

EC is for use with an external silicon clock chip.

RC is for using an external RC resonator (usually 8 MHz and below).

INT indicates that the microcontroller should use the internal oscillator, if the PIC you are using has one.


What links here