Understanding Oscillator Circuits
Before we dive into the specifics of 8051 oscillator circuits, let’s establish a solid foundation
by understanding what oscillator circuits are and why they’re crucial for microcontroller
operation.
What is an Oscillator Circuit?
An oscillator circuit is an electronic circuit that generates a repetitive, oscillating electronic
signal, typically a sine wave or a square wave. In the context of microcontrollers, these
circuits provide a clock signal that synchronizes all internal operations.
The smallest interval of time to accomplish any simple instruction or part of complex
instruction is called as the machine cycle. The machine cycle is made up of six states. A state
is the basic time interval for discrete operations of the microcontroller, such a fetching an
opcode byte, executing an opcode or writing data byte. Two oscillator pulses define each
state. That means, 8051 controller machine cycle is 12 clock pulses.
The Importance of Oscillator Circuits in 8051 Microcontrollers
The oscillator circuit plays a vital role in the 8051 microcontroller:
1. Timing Control: It provides the basic timing for the execution of instructions.
2. Synchronization: Ensures all internal components operate in harmony.
3. Speed Determination: The frequency of the oscillator directly affects the processing
speed of the microcontroller.
The oscillator frequency selection is often dictated by serial communicated baud rate needs.
Oscillator with frequency 11.0592 MHz works well for standard baud rates supported by PC.
Crystal frequency 11.0592 MHz gives machine cycle frequency of (11.0592/ 12) = 921.6 kHz.
UART divides the machine cycle frequency by 32 and then used by timer 1 to set baud rate.
So time 1 gets 28800 Hz clock.
Time period of each clock tick: T0 = 1/f = 1/28800
Duration of timer : n*T0 (n is the number of clock ticks)
9600 baud ->duration of 1 byte: 1/9600
1/9600 = n*T0 = n*1/28800
n = f/9600 = 28800/9600 = 3 ->TH1 =-3 or FD hex.
Baud Rate TH1 (Decimal) TH1 (Hex)
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8
Timer 1 TH1 register values for different baud rates
Types of Oscillator Circuits for 8051 Microcontrollers
The 8051 microcontroller family supports various oscillator configurations. Let’s explore the
most common types:
1. Crystal Oscillator Circuit
The crystal oscillator is perhaps the most widely used oscillator type for 8051
microcontrollers. It offers excellent frequency stability and precision.
How It Works
On Chip oscillator is a single stage inverter with parallel feedback resister. The XTAL1 and
XTAL2 pins are input and output of the inverter, which can be configured with off chip
oscillator circuit, like a crystal oscillator.
A crystal oscillator uses the piezoelectric effect of a quartz crystal to create an electrical
signal with a precise frequency. When connected to the 8051’s XTAL1 and XTAL2 pins, along
with two capacitors, it forms a feedback loop that sustains oscillation.
Circuit Diagram
In this diagram:
• Crystal is the quartz crystal
• C1 and C2 are load capacitors (typically 30pF for crystals up to 30MHz)
CL is load capacitance of the crystal oscillator, typically given by manufacturer. CI is the
internal capacitance of the MCU. If the stray capacitance is negligible, then we need to
satisfy the following equation
CL = (C1 X C2) /(C1 + C2) + CI
if C1 = C2 = CF, then
CL = CF/ 2 + CI
CF = 2 X (CL-CI)
Advantages
• High stability: Crystal oscillators provide very stable frequencies.
• Wide frequency range: Available from a few kHz to over 100MHz.
• Low cost: Relatively inexpensive for the precision they offer.
Disadvantages
• Susceptible to mechanical shock: Can be affected by vibrations.
• Temperature sensitive: Frequency can drift slightly with temperature changes.
2. Ceramic Resonator Circuit
Ceramic resonators offer a balance between the precision of crystal oscillators and the
simplicity of RC oscillators.
How It Works
Similar to crystal oscillators, ceramic resonators use the piezoelectric effect, but with a
ceramic material instead of quartz. They’re often used in applications where the utmost
precision isn’t required but good stability is still needed.
Advantages
• More robust: Less susceptible to mechanical shock than crystals.
• Lower cost: Generally cheaper than crystal oscillators.
• Compact: Often come with built-in load capacitors, saving board space.
Disadvantages
• Less precise: Not as accurate as crystal oscillators.
• Limited frequency range: Typically available up to about 50MHz.
3. RC Oscillator Circuit
RC oscillators are the simplest type of oscillator circuit for the 8051, using only a resistor and
a capacitor to generate the clock signal. They provide cost advantages to timing insensitive
applications.
How It Works
An RC oscillator relies on the charging and discharging of a capacitor through a resistor to
generate a clock signal. While not as precise as crystal or ceramic oscillators, they’re simple
and inexpensive. The RC oscillator frequency is function of supply voltage, resister, capacitor,
and operating temperature.
Advantages
• Simplicity: Easiest to implement with minimal components.
• Low cost: The most economical option.
• Flexible frequency: Easy to adjust frequency by changing R or C values.
Disadvantages
• Low precision: Frequency can vary significantly with temperature and voltage
changes.
• Limited frequency range: Generally suitable only for lower frequencies.
Understanding Real-Time Clocks
Before we dive into the specifics of interfacing, let’s take a moment to understand what
makes RTCs tick (pun intended). A Real-Time Clock is a dedicated integrated circuit
(IC) designed to keep track of time with high accuracy, even when the main system power is
off. These devices typically use a 32.768 kHz crystal oscillator as their time base, which
provides a stable and precise frequency for timekeeping.
Key features of RTCs include:
• Battery backup: Allows the RTC to continue keeping time when main power is
removed
• Low power consumption: Enables long battery life for backup power
• Calendar functions: Many RTCs provide date information in addition to time
• Programmable alarms: Some RTCs can generate interrupts at specific times or
intervals
Popular RTC Chips for 8051 Interfacing
When it comes to choosing an RTC for your 8051 project, there are several excellent options
available. Let’s explore some of the most popular RTC chips and their key features:
DS1307
The DS1307 is a classic choice for many 8051 projects. It offers:
• I2C interface for easy communication
• 56 bytes of battery-backed RAM for storing user data
• Square wave output with programmable frequency
• Low power consumption in battery backup mode
PCF8563
The PCF8563 is another versatile RTC option, featuring:
• I2C interface for simple integration
• Programmable clock output for timer applications
• Alarm function with interrupt capability
• Low backup current for extended battery life
DS3231
For applications requiring high accuracy, the DS3231 is an excellent choice:
• Extremely accurate temperature-compensated crystal oscillator (TCXO)
• I2C interface for straightforward communication
• Two programmable time-of-day alarms
• Integrated temperature sensor for monitoring ambient conditions
Understanding the Watchdog Timer
The Watchdog Timer is a hardware-based timer that operates independently of the main
program execution. Its primary purpose is to detect and recover from software
malfunctions. Think of it as a vigilant sentinel, constantly monitoring your system’s behavior
and ready to take action if something goes awry.
How Does the Watchdog Timer Work?
At its core, the Watchdog Timer is a countdown mechanism. When enabled, it starts
counting down from a predetermined value. The main program is responsible for
periodically resetting this timer before it reaches zero. If the timer does expire, it assumes
that the program has entered an erroneous state and triggers a system reset.
This simple yet effective mechanism provides a safety net for our code, ensuring that:
1. The system can recover from unexpected loops or hangs
2. Critical processes are executed within their expected timeframes
3. Software bugs that might cause the program to deviate from its intended flow are
mitigated
Implementing the Watchdog Timer in 8051
Now that we understand the concept, let’s explore how to implement the Watchdog Timer
in the 8051 microcontroller family. The exact implementation may vary slightly depending on
the specific 8051 variant you’re using, but the general principles remain the same.
Enabling the Watchdog Timer
To enable the Watchdog Timer, we typically need to set specific bits in the microcontroller’s
Special Function Registers (SFRs). Here’s a general example of how this might look:
1// Enable Watchdog Timer
2WDTCN = 0xA5; // Disable Watchdog Timer
3WDTCN = 0x07; // Enable Watchdog Timer with maximum timeout
In this code snippet, we first disable the Watchdog Timer by writing a specific value (0xA5) to
the Watchdog Timer Control Register (WDTCN). This is a safety measure to ensure we start
with a clean slate. We then enable the timer by writing a different value (0x07) to the same
register, which sets the maximum timeout period.
Resetting the Watchdog Timer
Once enabled, we need to periodically reset the Watchdog Timer to prevent it from
triggering a system reset. This is typically done by writing a specific sequence of values to
the control register:
1// Reset Watchdog Timer
2WDTCN = 0xA5; // First value of the reset sequence
3WDTCN = 0x5A; // Second value of the reset sequence
It’s crucial to place these reset commands at strategic points in your main program loop or in
interrupt service routines that are guaranteed to execute regularly.
Configuring the Timeout Period
The timeout period of the Watchdog Timer can often be configured to suit the specific needs
of your application. This is typically done by setting specific bits in the control register. For
example:
1// Set Watchdog Timer timeout to approximately 1 second
2WDTCN = 0xA5; // Disable Watchdog Timer
3WDTCN = 0x03; // Enable Watchdog Timer with ~1 second timeout
The exact values and resulting timeout periods will depend on your specific 8051 variant and
system clock frequency, so always consult your microcontroller’s datasheet for accurate
information.