Timers in 8051 Microcontroller
Timers in 8051 Microcontroller
The 8051 microcontroller is equipped with two 16-bit timer/counter registers, namely
Timer 0 and Timer 1. These registers can be configured independently to operate in either
timer mode or counter mode.
Timer Mode:
When used in timer mode, these registers count the internal clock pulses generated
by the microcontroller’s clock.
Each machine cycle in the 8051 consists of 12 oscillator periods. As a result, the
counting rate for the timer is 1/12th of the oscillator frequency.
o For example, if the oscillator frequency is 12 MHz, the timer increments every
1 microsecond (since 12 MHz ÷ 12 = 1 MHz).
The value loaded into the timer register allows for precise time intervals to be
measured.
Counter Mode:
Since the frequency of these external pulses may not be constant, counter mode is
typically used for counting events, rather than for precise timing operations.
Registers Overview:
The two timers, Timer 0 and Timer 1, are both 16-bit registers.
Timer Low Byte (TL): TL0 for Timer 0 and TL1 for Timer 1.
Timer High Byte (TH): TH0 for Timer 0 and TH1 for Timer 1.
o The combination of TL0 and TH0 forms the complete 16-bit register for
Timer 0, while TL1 and TH1 form the 16-bit register for Timer 1.
In both modes, the timers can be controlled using different modes such as mode 0 (13-bit
timer), mode 1 (16-bit timer), mode 2 (8-bit auto-reload), and mode 3 (split timer mode)
depending on the desired functionality.
TMOD (Timer Mode Control) Register:
The TMOD register in the 8051 microcontroller is an 8-bit register used to control the mode
of operation for Timer 0 and Timer 1. The register is divided into two 4-bit sections: one for
Timer 0 and the other for Timer 1. Each section consists of the following control bits:
mathematica
Copy code
|--------------------------|--------------------------|
o When set to 1: The timer/counter operates only when the external interrupt
pin (INTx) is high, and the timer run control bit (TR) is set.
o When cleared to 0: The timer/counter operates based solely on the state of the
TR control bit, ignoring the external interrupt pin.
o Use: It is typically used to measure the pulse width of a signal on the external
INTx pin.
o When set to 1: The timer operates as a counter and counts external events.
o When cleared to 0: The timer operates as a timer and counts internal clock
cycles.
o Use: This bit determines whether the timer is used for counting clock cycles
(timing) or external events (counting).
3. M1 and M0 (Mode Select Bits): These two bits select the operating mode of the
timers.
8 bits are stored in the timer register (TL), and 5 bits are stored in the
high register (TH).
Timer 0 is split into two 8-bit timers, with TL0 controlled by Timer 0
control bits and TH0 controlled by Timer 1 control bits.
The TCON register controls the start/stop of the timers and handles interrupt flags. It is an 8-
bit register, and its structure is as follows:
Copy code
TCON Bits:
o 1: Edge-triggered.
o 0: Level-triggered.
o 1: Edge-triggered.
o 0: Level-triggered.
The 8051 microcontroller has four different timer modes that are controlled by the mode
control bits M1 and M0 in the Timer Mode Control (TMOD) register. The timers can operate
in four different modes:
Mode 0:
In mode 0, the timer/counter is a 13-bit counter. When this mode is selected, the THx
register is used as the high byte, and the TLx register is used as the lower byte for the
timer.
The timer is enabled when the control bits TR = 1, Gate = 0, and INTx = 1.
Mode 1:
Key difference from Mode 0: Mode 1 functions similarly to mode 0, except the
timer/counter is 16-bit rather than 13-bit.
In mode 1, the timer is again controlled by the TR and Gate bits. The timer is enabled
when TR = 1 and Gate = 0. If Gate = 1, external input INTx is used to control the
timer for pulse-width measurement.
1. Load the TMOD register to select the timer (Timer 0 or Timer 1) and mode
(Mode 1).
2. Load the initial count value into registers TLx and THx.
4. Monitor the Timer Flag (TF) using the JNB TFx instruction to determine when
it overflows.
assembly
Copy code
DELAY:
AGAIN:
Mode 2:
Mode 2 is an 8-bit auto-reload mode. When the timer overflows, the content of the
THx register is automatically loaded into TLx, and the timer continues to run.
1. Load the TMOD register with the appropriate timer mode and selection.
5. Upon overflow, the timer automatically reloads the THx content to TLx.
assembly
Copy code
BACK:
Mode 3:
Mode 3 is known as the split timer mode. In this mode, Timer 0 is divided into two
8-bit counters: TL0 and TH0, operating independently. Timer 1 is halted in this mode.
o In this mode, two separate timers can be used, with one controlling machine
cycles and the other controlling external inputs.
assembly
Copy code
These timer modes allow flexibility in configuring delays, generating pulses, and creating
waveforms in embedded systems using the 8051 microcontroller
1. Parallel Transmission/Reception:
o This method is faster but requires more data lines (wires), making it suitable
for short-range communication.
o In the 8051, ports P0, P1, P2, and P3 can be used to transmit/receive 8-bit
parallel data.
2. Serial Transmission/Reception:
o Serial communication sends one bit at a time, using fewer data lines (often just
one or two).
o The TXD (P3.1) and RXD (P3.0) pins are used for transmitting and receiving
serial data, respectively.
Serial communication in the 8051 uses two main methods for data transmission and
reception:
1. Asynchronous Transmission:
2. Synchronous Transmission:
Start Bit: Always set to 0 (low) and signals the start of data transmission.
Stop Bit(s): One or two bits set to 1 (high), indicating the end of data transmission.
Example: For an 8-bit ASCII character like "A" (binary 0100 0001), the character is
framed with a start bit (0) and a stop bit (1). The least significant bit (LSB) is sent
first.
Start Bit: 0
Stop Bit: 1
Thus, the complete data frame for the letter "A" would be:
0 0100 0001 1
The 8051's UART simplifies this process by handling the serial data framing, transmission,
and reception automatically, allowing for efficient communication with external devices.
In serial communication, the baud rate is the rate at which bits are transmitted per second,
measured in bits per second (bps). It is a critical factor in determining how fast data can be
transmitted or received. Some standard baud rates include 110, 150, 300, 600, 1200, 2400,
4800, and 9600 bps. The 8051 microcontroller can transmit and receive serial data at various
baud rates using software instructions.
To achieve the desired baud rate in serial communication, Timer 1 is used in mode 2 (8-bit
auto-reload). The baud rate is controlled by setting a specific value in TH1 (Timer 1 high
byte) to adjust the transmission speed. An external crystal oscillator with a frequency of
11.0592 MHz is used to generate the clock signal for baud rate calculation.
2. The machine cycle frequency is derived by dividing the clock frequency by 12:
Machine Cycle Frequency=11.0592 MHz12=921.6 kHz\text{Machine Cycle
Frequency} = \frac{11.0592 \text{ MHz}}{12} = 921.6 \
text{ kHz}Machine Cycle Frequency=1211.0592 MHz=921.6 kHz
3. The UART circuit further divides this frequency by 32 to get the baud frequency:
Baud Frequency=921.6 kHz32=28,800 Hz\text{Baud Frequency} = \frac{921.6 \text{
kHz}}{32} = 28,800 \text{ Hz}Baud Frequency=32921.6 kHz=28,800 Hz
4. The value loaded into TH1 determines the baud rate. The table below shows the TH1
values for different baud rates:
The SBUF register is an 8-bit Special Function Register (SFR) used for serial data
transmission and reception:
During reception, the received data is stored in SBUF and can be retrieved from
there.
For example:
The serial modes are determined by the SM0 and SM1 bits of the SCON register:
Mode 1 is the most commonly used mode, allowing standard 8-bit UART communication.
The SM2 bit enables multiprocessing communication in Mode 2 and Mode 3. This
feature is useful when communicating with multiple devices.
REN is critical for enabling data reception. Setting REN to 1 allows data to be
received via the RXD pin.
TB8 and RB8 are used for the 9th bit of transmission and reception in Modes 2 and 3.
TI and RI (Transmit and Receive Interrupts)
TI is set when data transmission is complete, indicating readiness for the next byte.
RI is set when data reception is complete, indicating that the received data is available
in SBUF.
In Modes 2 and 3, a total of 11 bits (1 start bit, 9 data bits, and 1 stop bit) are transmitted or
received
The 8051 microcontroller supports four modes of serial communication, determined by the
SM0 and SM1 bits in the SCON register. The modes are as follows:
o Type: Half-duplex (data sent and received using the same pin).
o RXD pin is used for data reception, and TXD pin is used for data
transmission.
o Data width: 10 bits (1 start bit, 8 data bits, and 1 stop bit).
o Type: Full-duplex.
o Type: Full-duplex.
RS232 Standards
o Undefined range: -3 V to +3 V.
Since these voltage levels are incompatible with the TTL (Transistor-Transistor Logic)
levels of the 8051, a voltage level converter is required for communication between devices
using RS232 and the 8051 microcontroller.
MAX232
The MAX232 is a popular IC used as a voltage level converter between RS232 and TTL
levels. It is commonly referred to as a line driver.
Function: Converts RS232 voltage levels to TTL logic levels and vice versa.
Pin configuration: It has two sets of drivers for transmitting (T1, T2) and receiving
(R1, R2) signals.
Capacitors: The MAX232 requires four external capacitors (C1, C2, C3, C4) with
values ranging from 1 µF to 22 µF for internal voltage pumping.
The RXD pin of the 8051 (used for data reception) is connected to the R1OUT pin of
the MAX232.
The T1OUT and R1IN pins of MAX232 are connected to the DB9 connector pins 2
and 3, allowing the 8051 to communicate with a computer's serial port via RS232.
This setup ensures that the TTL levels of the 8051 are properly converted to RS232 levels for
compatibility with devices using the RS232 standard.
There are two primary methods for increasing the baud rate in the 8051 microcontroller
during serial communication:
o Increasing the crystal oscillator frequency can increase the baud rate.
However, this is not a practical solution for achieving standard baud rates (like
9600 bps) because using higher frequencies may not match PC baud rate
standards.
o For example, if the crystal frequency is 12 MHz, the machine cycle frequency
becomes 1 MHz (12 MHz/12 = 1 MHz), and dividing 1 MHz by 32 gives
31,250 Hz. This would not result in a precise baud rate.
The PCON (Power Control) register is an 8-bit special function register in the 8051
microcontroller. It is byte-addressable and is used for:
o By default, SMOD = 0, and the baud rate remains the same as set by Timer 1
in mode 2.
Let's see how the baud rate changes based on the value of SMOD:
When SMOD = 1, the baud rate doubles from 4800 to 9600 bps for the same value of
TH1.
The following program sets SMOD to 1, doubling the baud rate without changing the crystal
frequency:
assembly
Copy code
GF0 and GF1: General-purpose flag bits, typically used for user-defined purposes.
Introduction to Interrupts
In many real-time processes, the microcontroller may need to handle multiple tasks or events
simultaneously. Interrupts provide a way to temporarily halt the execution of the main
program, handle an event or service a device, and then return to the main program where it
left off. For example, in a scenario where the microcontroller needs to:
1. Receive data serially from an external device and send it to P0.
2. Read from port P1 and transmit it serially to the external device while also sending a
copy to P2.
Using interrupts, the microcontroller can efficiently manage all these tasks by responding to
events (such as serial data reception) while performing other operations.
1. Interrupts:
o Upon receiving the interrupt, the microcontroller stops its current task and
executes the Interrupt Service Routine (ISR).
2. Polling:
o Polling wastes time as it checks devices even if they do not need attention.
The ISR is executed, and after finishing, the microcontroller returns to the main
program at the point where it was interrupted.
3. Save Interrupt Status: The current status of interrupts is saved internally (not on the
stack).
5. Execute the ISR: The ISR is executed until it reaches the RETI instruction (Return
from Interrupt).
6. Return to Main Program: Upon executing RETI, the microcontroller retrieves the
saved PC value from the stack and resumes executing the main program from where it
was interrupted.
In the 8051 microcontroller, there is a fixed memory location for each interrupt, known as the
interrupt vector table. These addresses hold the starting points for their respective ISRs.
Each of these vector addresses contains the starting point for the corresponding ISR.
Interrupts must be enabled via software, typically using the IE (Interrupt Enable)
register.
assembly
Copy code
; Alternatively
assembly
Copy code
assembly
Copy code
Timer Interrupts
Timer interrupts occur when a timer overflows, raising the TF bit and prompting the
microcontroller to execute the ISR (Interrupt Service Routine).
assembly
Copy code
ORG 0000H
LJMP MAIN
MAIN:
BACK:
END
The 8051 supports two external hardware interrupts (INT0 and INT1) on pins P3.2
and P3.3.
Level-Triggered Interrupt
The low-level signal must be removed before the last instruction of the ISR (RETI) is
executed to prevent a new interrupt.
Edge-Triggered Interrupt
The TCON register includes flags that indicate whether an interrupt is being serviced.
assembly
Copy code
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN:
END
The TI (Transmit Interrupt) is raised after the last bit of data is sent.
assembly
Copy code
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN:
BACK:
ORG 100H
SERIAL:
CLR RI ; Clear RI
TRANS:
CLR TI ; Clear TI
END
Interrupt Priority
The default priority order is fixed but can be changed using the IP (Interrupt
Priority) register.
assembly
Copy code