3.10 Clock Oscillator
As seen in figure below, the clock signal may be generated by one out of two built-in oscillators.
An external oscillator is installed within the microcontroller and connected to the OSC1 and OSC2 pins. It is called ‘external’ because it relies on an external circuit for the clock signal and frequency stabilization, such as a stand-alone oscillator, quartz crystal, ceramic resonator or resistor-capacitor circuit. The oscillator mode is selected by the bits of bytes, called Config Word, sent during programming.
Internal oscillator consists of two separate internal oscillators:
The HFINTOSC is a high-frequency internal oscillator which operates at 8MHz. The microcontroller can use clock source generated at this frequency or after being divided in prescaler.
The LFINTOSC is a low-frequency internal oscillator which operates at 31 kHz. Its clock sources are used for
watch-dog and
power-uptiming, but it can also be used as a clock source for the operation of the entire microcontroller.
The system clock can be selected between external or internal clock source via the System Clock Select (SCS) bit of the OSCCON register.
OSCCON Register
The OSCCON register controls the system clock and frequency selection options. It contains the following bits: frequency selection bits (IRCF2, IRCF1, IRCF0), frequency status bits (HTS, LTS), system clock control bits (OSTA, SCS).
IRCF2-0 - Internal Oscillator Frequency Select bits. The divider rate depends on the combination of these three bits. The clock frequency of internal oscillator is determined in the same way.
IRCF2 |
IRCF1 |
IRCF0 |
FREQUENCY |
OSC. |
1 |
1 |
1 |
8 MHz |
HFINTOSC |
1 |
1 |
0 |
4 MHz |
HFINTOSC |
1 |
0 |
1 |
2 MHz |
HFINTOSC |
1 |
0 |
0 |
1 MHz |
HFINTOSC |
0 |
1 |
1 |
500 kHz |
HFINTOSC |
0 |
1 |
0 |
250 kHz |
HFINTOSC |
0 |
0 |
1 |
125 kHz |
HFINTOSC |
0 |
0 |
0 |
31 kHz |
LFINTOSC |
OSTS - Oscillator Start-up Time-out Status bit indicates which clock source is currently in use. It is read-only.
- 1 - External clock oscillator is in use.
- 0 - One of internal clock oscillators is in use (HFINTOSC or LFINTOSC).
HTS - HFINTOSC Status bit (8 MHz - 125 kHz) indicates whether the high-frequency internal oscillator operates in a stable way.
- 1 - HFINTOSC is stable.
- 0 - HFINTOSC is not stable.
LTS - LFINTOSC Stable bit (31 kHz) indicates whether the low-frequency internal oscillator operates in a stable way.
- 1 - LFINTOSC is stable.
- 0 - LFINTOSC is not stable.
SCS - System Clock Select bit determines which oscillator is to be used as a clock source.
- 1 - Internal oscillator is used for system clock.
- 0 - External oscillator is used for system clock.
The oscillator mode is set by bits in Config Word written to the microcontroller memory during the process of programming.
EXTERNAL CLOCK MODES
The external oscillator can be configured to operate in one out of several modes, which enables it to operate at different speeds and use different components for frequency stabilization. Mode of operation is selected during the process of writing a program into the microcontroller. First of all, it is necessary to activate the program on a PC to be used for programming. It is the
PICflash program in this case. Click on the oscillator field and select one oscillator from the drop-down list. The appropriate bits will be automatically set, thus becoming a part of several bytes which together form a
Config Word.
During the process of programming the microcontroller, these bytes of Config Word are written to the microcontroller’s ROM memory and stored in special registers which are not available to the user. On the basis of these bits, the microcontroller 'knows' what to do, although it is not explicitly stated in the program. Mode of operation is selected after the process of writing and compiling a program
EXTERNAL OSCILLATOR IN EC MODE
The external clock (EC) mode uses external oscillator as a clock source. The maximum frequency of this clock is limited to 20 MHz.
The advantages of the external oscillator when configured to operate in EC mode:
- The independent external clock source is connected to the OSC1 input and the OSC2 is available as a general purpose I/O;
- It is possible to synchronize the operation of the microcontroller with the rest of on-board electronics;
- In this mode the microcontroller starts operation immediately after the power is on. No time delay is required for frequency stabilization; and
- Temporary disabling the external clock source causes device to stop operation, while leaving all data intact. After restarting the external clock, the device proceeds with operation as if nothing has happened.
EXTERNAL OSCILLATOR IN LP, XT OR HS MODE
The LP, XT and HS modes use external oscillator as a clock source the frequency of which is determined by quartz crystal or ceramic resonators connected to the OSC1 and OSC2 pins. Depending on the features of the component in use, select one of the following modes:
- LP mode - (Low Power) is used for low-frequency quartz crystal only. This mode is designed to drive only 32.768 kHz crystals usually embedded in quartz watches. It is easy to recognize them by small size and specific cylindrical shape. The current consumption is the least of the three modes.
- XT mode is used for intermediate-frequency quartz crystals up to 8 MHz. The current consumption is the medium of the three modes.
- HS mode - (High Speed) is used for high-frequency quartz crystals over 8 MHz. The current consumption is the highest of the three modes.
CERAMIC RESONATORS IN XT OR HS MODE
Ceramic resonators are by their features similar to quartz crystals and are connected in the same way, therefore. Unlike quartz crystals, they are cheaper and oscillators containing them have a bit poorer characteristics. They are used for clock frequencies ranging from 100 kHz to 20 MHz.
EXTERNAL OSCILLATOR IN RC AND RCIO MODE
There are certainly many advantages in using elements for frequency stabilization, but sometimes they are really unnecessary. In most cases the oscillator may operate at frequencies not precisely defined so that embedding of such elements is a waste of money. The simplest and cheapest solution in these situations is to use one resistor and one capacitor for the operation of oscillator. There are two modes:
RC mode. When the external oscillator is configured to operate in RC mode, the OSC1 pin should be connected to the RC circuit as shown in figure on the right. The OSC2 pin outputs the RC oscillator frequency divided by 4. This signal may be used for calibration, synchronization or other application requirements.
RCIO mode. Likewise, the RC circuit is connected to the OSC1 pin. This time, the available OSC2 pin is used as an additional general-purpose I/O pin.
In both cases, it is recommended to use components as shown in figure.
The frequency of such an oscillator is calculated according to the formula f = 1/T in which:
- f = frequency [Hz];
- T = R * C = time constant [s];
- R = resistor resistance [Ω]; and
- C = capacitor capacity [F].
INTERNAL CLOCK MODES
The internal oscillator circuit consists of two separate oscillators that can be selected as the system clock source:
The
HFINTOSC oscillator is factory calibrated and operates at 8 MHz. Its frequency can be set by the user via software using bits of the OSCTUNE register.
The
LFINTOSC oscillator is not factory calibrated and operates at 31kHz.
Similar to the external oscillator, the internal one can also operate in several modes. The mode of operation is selected in the same way as with external oscillator - using bits of the
Config Word register. In other words, everything is performed within PC software prior to writing a program into the microcontroller.
INTERNAL OSCILLATOR IN INTOSC MODE
In this mode, the OSC1 pin is available as a general purpose I/O, while the OSC2 pin outputs selected internal oscillator frequency divided by 4.
INTERNAL OSCILLATOR IN INTOSCIO MODE
In this mode, both pins are available as a general purpose I/O.
INTERNAL OSCILLATOR SETTINGS
The internal oscillator consists of two separate circuits.
1. The high-frequency internal oscillator HFINTOSC is connected to the postscaler (frequency divider). It is factory calibrated and operates at 8MHz. By using postscaler, this oscillator can output clock sources at one out of seven frequencies. The frequency selection is performed within software using the IRCF2, IRCF1 and IRCF0 pins of the OSCCON register.
The HFINTOSC is enabled by selecting one out of seven frequencies (between 8 MHz and 125 kHz) and setting the System Clock Source (SCS) bit of the OSCCON register. As seen in figure below, everything is performed by using bits of the OSCCON register.
2. The low-frequency oscillator LFINTOSC is uncalibrated and operates at 31 kHz. It is enabled by selecting this frequency (bits of the OSCCON register) and setting the SCS bit of the same register.
TWO-SPEED CLOCK START-UP MODE
Two-Speed Clock Start-up mode is used to provide additional power savings when the microcontroller operates in sleep mode. What is this all about?
When configured to operate in LP, XT or HS mode, the external oscillator will be switched off on transition to
sleep in order to reduce the overall power consumption of the device.
When the conditions for wake-up are met, the microcontroller will not immediately start to operate because it has to wait for the clock signal frequency to become stable. Such delay lasts for exactly 1024 pulses, then the microcontroller proceeds with program execution. It usually happens that only a few instructions are performed before the microcontroller is set back to
Sleep mode. It means that most of time as well as most of power obtained from batteries is wasted. The problem is solved by using an internal oscillator for program execution while the counting of these 1024 pulses is in progress. As soon as the external oscillator frequency becomes stable, it will automatically take over the 'leading role'. The whole process is enabled by setting one bit of the configuration word. In order to program the microcontroller, it is necessary to select the
Int-Ext Switchover option in software.
FAIL-SAFE CLOCK MONITOR
As its name suggests, the Fail-Safe Clock Monitor (FSCM) monitors the operation of external oscillator and allows the microcontroller to proceed with program execution even though the external oscillator fails for some reason. In this case, the internal oscillator takes over its role.
The fail-safe clock monitor detects the failure by comparing internal and external clock sources. If it takes more than 2mS for the external oscillator clock to come, the clock source will be automatically switched. The internal oscillator will thereby continue the operation controlled by the bits of the OSCCON register. When the OSFIE bit of the PIE2 register is set, an interrupt will be generated. The system clock will keep on being sourced from internal clock until the device successfully restarts the external oscillator and switches back to external operation.
Similarly, this module is enabled by changing configuration word directly before the process of programming chip starts. This time, it is done by selecting the Fail-Safe Clock Monitor option.
OSCTUNE Register
Modifications in the OSCTUNE register affect the HFINTOSC frequency, but not the LFINTOSC frequency. There is no indication during the operation that frequency shift has occurred.
TUN4 - TUN0 Frequency Tuning bits. By combining these five bits, the 8MHz oscillator frequency shifts. In this way, the frequencies obtained by its division in the postscaler shift too.
TUN4 |
TUN3 |
TUN2 |
TUN1 |
TUN0 |
FREQUENCY |
0 |
1 |
1 |
1 |
1 |
Maximal |
0 |
1 |
1 |
1 |
0 |
|
0 |
1 |
1 |
0 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
0 |
0 |
0 |
0 |
1 |
|
0 |
0 |
0 |
0 |
0 |
Calibrated |
1 |
1 |
1 |
1 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
0 |
0 |
1 |
0 |
|
1 |
0 |
0 |
0 |
1 |
|
1 |
0 |
0 |
0 |
0 |
Minimal |
Eeprom is a separate memory segment, not part of program memory (ROM), nor data memory (RAM). Even though these memory locations are not easily and quickly accessed as other registers, their purpose is irreplaceable as the EEPROM data is permanently saved even after the loss of power and can be changed at any moment. These exceptional features make each byte of EEPROM valuable.