3.10 Clock Oscillator
In order to synchronize all the processes taking place within the microcontroller, it is necessary to provide a clock signal. The clock signal is generated by a clock oscillator. As simple as that. This microcontroller features several oscillators capable of operating in different modes. It makes the whole story even more interesting...
As can be seen in figure below, the clock signal may be generated by either external or internal built-in oscillator.
The external oscillator is built into the microcontroller and connected to the OSC1 and OSC2 pins. It is called ‘external’ as it is connected to external components such as quartz crystal, ceramic resonator or resistor-capacitor circuit. The oscillator mode is selected by the bits of the Config Word, loaded into the microcontroller during programming.
The internal oscillator consists of two separate internal oscillators:
The HFINTOSC oscillator is a high-frequency internal oscillator which operates at 8MHz. The microcontroller can use a clock generated at this frequency or after being divided in prescaler.
The LFINTOSC oscillator is a low-frequency internal oscillator which operates at 31 kHz. The clock it generates is used for watch-dog and power-up timing, but it can also be used as a clock for the operation of the entire microcontroller.
The System Clock Select bit (SCS bit) of the OSCCON register is used for selection between external and internal oscillator.
OSCCON Register
The OSCCON register is used for selecting and controlling clock oscillator and clock frequency. It contains the following bits: frequency selection bits (IRCF2, IRCF1, IRCF0), oscillator status bits (HTS, LTS) and system clock control bits (OSTA, SCS).
IRCF2-0 - Internal Oscillator Frequency Select bits. The combination of these three bits determines the prescaler rate, hence the clock frequency of internal oscillator.
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 is stable.
- 1 - HFINTOSC is stable.
- 0 - HFINTOSC is not stable.
LTS - LFINTOSC Stable bit (31 kHz) indicates whether the low-frequency internal oscillator is stable.
- 1 - LFINTOSC is stable.
- 0 - LFINTOSC is not stable.
SCS - System Clock Select bit selects the oscillator to be used as a clock source.
- 1 - Internal oscillator is used as a clock source.
- 0 - External oscillator is used as a clock source.
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 set to operate in several different modes, which further means that it can operate at different speeds and use different components for frequency stabilization. The operating mode is selected during the process of writing a program into the microcontroller. First of all, it is necessary to start up 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 desired oscillator from the drop-down list. Information on these settings are automatically saved in Config Word.
During the process of programming the microcontroller, bits of the configuration word are written to the special registers of ROM which are not available to the user. On the basis of these bits, the microcontroller ‘knows’ what to do, even though it is not explicitly stated in the program.
The operating mode of the oscillator is selected after program writing and compiling are complete.
EXTERNAL OSCILLATOR IN EC MODE
The external clock oscillator set to EC mode uses an independent external oscillator as a clock source. The maximum frequency of this clock signal is limited to 20 MHz.
The advantages of the external oscillator when set 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 as soon as the power supply is on. No time delay is required for frequency stabilization; and
- Temporary suspension of the external clock source operation will automatically cause the microcontroller to stop operation, while leaving all data intact. After restarting the external oscillator, the microcontroller proceeds with operation as if nothing has happened.
EXTERNAL OSCILLATOR IN LP, XT OR HS MODE
The external oscillator in LP, XT and HS modes uses the external oscillator within the microcontroller as a clock source. The clock frequency depends on the quartz crystal or ceramic resonators connected to OSC1 and OSC2 pins. Depending on the operating frequency of the component in use, select one of the following modes:
- LP mode - (Low Power) is used for a 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 their 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 very similar to quartz crystals by their features and therefore connected in the same way. As opposed to quartz crystals, they are cheaper and oscillators containing them have a bit poorer features. They are used for clock frequencies ranging from 100 kHz to 20 MHz.
EXTERNAL OSCILLATOR IN RC AND RCIO MODE
Components for frequency stabilization are definitely very useful, but sometimes unnecessary. In most cases the oscillator may operate at frequencies which are not precisely defined so that the installation of such components is a waste of money therefore. The simplest and cheapest solution here is to use one resistor and one capacitor for the oscillator operation. There are two modes in which these two components sre used:
RC mode. When the external oscillator is set 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 clock signal may be used for calibration, synchronization or other applications.
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 figures.
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 a system clock source:
The
HFINTOSC oscillator is factory calibrated and operates at 8 MHz. Its frequency can be fine tuned from within the software using bits of the OSCTUNE register.
The
LFINTOSC oscillator is not factory calibrated and operates at 31kHz.
Like the external oscillator, the internal oscillator can also be set to operate in several modes. The operating mode is selected in the same way as for the external oscillator - using bits of the Config Word. In other words, all settings are performed from within the 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 general purpose I/O pins.
INTERNAL OSCILLATOR SETTINGS
The internal oscillator consists of two separate circuits.
1. The high-frequency internal oscillator HFINTOSC is factory calibrated and operates at 8MHz. It is connected to a postscaler (frequency divider). The postscaler enables this oscillator to output clock pulses at one out of seven frequencies. The frequency selection is performed within the software using IRCF2, IRCF1 and IRCF0 bits of the OSCCON register.
The HFINTOSC oscillator is enabled by selecting one out of seven frequencies (between 8 MHz and 125 kHz) and by setting the System Clock Source bit (SCS) of the OSCCON register. As can be seen in figure below, all the settings are performed 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 using bits of the OSCCON register and by setting the SCS bit of the same register.
TWO-SPEED CLOCK START-UP MODE
Two-Speed Clock Start-up mode enables the microcontroller to provide extra power savings when it 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 wake-up conditions are met, the microcontroller will not immediately start to operate because it has to wait for the clock frequency to become stable. It takes exactly 1024 pulses for the microcontroller to proceed with program execution. After 1024 pulses are counted and only a few program instructions are executed, the microcontroller is usually set back to Sleep mode. It means that most of time as well as most of power supplied from batteries are wasted. This issue 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 in this way, it is necessary to select the Int-Ext Switchover option within the 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 its place.
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 take over the role of the main clock source the operation of which is 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 the internal oscillator until the microcontroller successfully restarts the external oscillator and switches back to the external clock source.
This module is also enabled by changing configuration word just before initiating the process of programming. In this example, 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 of the microcontroller 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 |