0% found this document useful (0 votes)
2 views

Timers in 8051 Microcontroller

The 8051 microcontroller features two main timers, Timer 0 and Timer 1, which can function as timers or counters. The TMOD register configures these timers into four modes, allowing for various timing and counting applications, while the TCON register controls their operation. Each mode serves specific use cases, such as generating delays or handling external events.

Uploaded by

fahad.aqeel005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Timers in 8051 Microcontroller

The 8051 microcontroller features two main timers, Timer 0 and Timer 1, which can function as timers or counters. The TMOD register configures these timers into four modes, allowing for various timing and counting applications, while the TCON register controls their operation. Each mode serves specific use cases, such as generating delays or handling external events.

Uploaded by

fahad.aqeel005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Timers in 8051 Microcontroller

Timers in the 8051 microcontroller are crucial for timing operations, generating
delays, counting external events, and serving as clock sources. The 8051 has two
timers: Timer 0 and Timer 1 (some variants have Timer 2 as well). These timers can
operate as timers (incrementing based on the internal clock) or counters
(incrementing based on external events).

1. Timer Registers
The 8051 has four main registers for timers:

1. TMOD (Timer Mode Register)


2. TCON (Timer Control Register)
3. THx (Timer High Byte)
4. TLx (Timer Low Byte)

Where x = 0 or 1 (for Timer 0 and Timer 1, respectively).

1.1 TMOD (Timer Mode Register)

The TMOD (Timer Mode) register is an 8-bit register used to configure the modes
and functions of Timer 0 and Timer 1 in the 8051 microcontroller.

Each timer has 4 bits in the TMOD register:

 Upper 4 bits (Bits 4-7) → Timer 1 Configuration


 Lower 4 bits (Bits 0-3) → Timer 0 Configuration

Bit Structure of TMOD Register


Bit 7 6 5 4 3 2 1 0
Use GATE1 C/T1 M1_1 M0_1 GATE0 C/T0 M1_0 M0_0

 GATE (Bit 7 & 3) → Enables/Disables external control of the timer.


 C/T (Bit 6 & 2) → Selects Timer or Counter mode.
 M1, M0 (Bits 5-4 & 1-0) → Selects the Timer mode.
Bit Descriptions
1. GATE (Gate Control)

 0 → Timer runs independently (software control).


 1 → Timer starts only when the external INT (Interrupt) pin (INT0 for Timer 0, INT1 for Timer
1) is HIGH.

Example:

 GATE = 1: Timer starts only when INT0 = 1 (for Timer 0) or INT1 = 1 (for Timer 1).
 GATE = 0: Timer starts as soon as TR0/TR1 is set.

2. C/T (Counter/Timer Control)

 0 → Timer Mode (Counts internal clock pulses).


 1 → Counter Mode (Counts external pulses on T0 (P3.4) / T1 (P3.5)).

Example:

 C/T = 0: Timer increments based on internal system clock.


 C/T = 1: Timer increments on each external event pulse.

3. Mode Selection (M1, M0)

Mode M1 M0 Description
Mode 0 0 0 13-bit Timer (8-bit THx + 5-bit TLx)
Mode 1 0 1 16-bit Timer (THx + TLx)
Mode 2 1 0 8-bit Auto Reload (THx holds reload value)
Mode 3 1 1 Split Mode (Timer 0 acts as two 8-bit timers)

Examples of TMOD Configuration


Example 1: Setting Timer 0 in Mode 1 (16-bit Timer)

TMOD Value: 00000001 (0x01)

 Gate = 0 (Timer operates independently)


 C/T = 0 (Timer Mode)
 Mode 1 (M1 = 0, M0 = 1) (16-bit Timer)

Assembly Code:
MOV TMOD, #01H ; Timer 0 in Mode 1 (16-bit)
MOV TH0, #0xFC ; Load High Byte
MOV TL0, #0x66 ; Load Low Byte
SETB TR0 ; Start Timer 0

Example 2: Setting Timer 1 in Mode 2 (8-bit Auto-Reload)

TMOD Value: 00100000 (0x20)

 Gate = 0 (Software-controlled timer)


 C/T = 0 (Timer mode)
 Mode 2 (M1 = 1, M0 = 0) (8-bit Auto-Reload for Timer 1)

Assembly Code:

MOV TMOD, #20H ; Timer 1 in Mode 2


MOV TH1, #250 ; Reload value for Timer 1
SETB TR1 ; Start Timer 1

Example 3: Setting Timer 0 as a Counter in Mode 1

TMOD Value: 00000101 (0x05)

 Gate = 0 (Software-controlled)
 C/T = 1 (Counter Mode)
 Mode 1 (M1 = 0, M0 = 1) (16-bit Counter)

Assembly Code:

MOV TMOD, #05H ; Timer 0 in Counter Mode (16-bit)


SETB TR0 ; Start Counter

 Here, Timer 0 counts pulses on the T0 (P3.4) pin.

Example 4: Using Timer 0 in Split Mode (Mode 3)

TMOD Value: 00000011 (0x03)

 Mode 3 (M1 = 1, M0 = 1) (Splits Timer 0 into two 8-bit timers)


 Timer 1 can still be used in Modes 0, 1, or 2.

Assembly Code:

MOV TMOD, #03H ; Timer 0 in Mode 3 (Split Mode)


SETB TR0 ; Start Timer 0 (Low Byte)
SETB TR1 ; Start Timer 0 (High Byte)

 TH0 acts as one independent timer.


 TL0 acts as another independent timer.
 Timer 1 is still available.

Summary of TMOD Settings


TMOD Value Mode Description
0x00 Mode 0 13-bit Timer 0 & Timer 1
0x01 Mode 1 Timer 0 in 16-bit Mode
0x02 Mode 2 Timer 0 in 8-bit Auto-Reload Mode
0x03 Mode 3 Timer 0 Split Mode
0x10 Mode 1 Timer 1 in 16-bit Mode
0x20 Mode 2 Timer 1 in 8-bit Auto-Reload
0x50 Mode 1 Timer 1 in Counter Mode
0x80 - Timer 1 with Gate Enabled

1.2 TCON (Timer Control Register)

The TCON register is used to control and monitor the timers.

Bit 7 6 5 4 3 2 1 0
Use TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

 TF1, TF0 (Timer Overflow Flags)

o 1 → Timer overflow occurred


o 0 → No overflow

 TR1, TR0 (Timer Run Control)

o 1 → Starts the timer


o 0 → Stops the timer

 IE1, IE0 (External Interrupt Flags)


 IT1, IT0 (External Interrupt Trigger Types)
Timer Modes in 8051
Microcontroller
Timers in 8051 microcontroller can operate in four different modes to handle
different timing and counting applications. These modes are set using the TMOD
(Timer Mode) Register.

Timer Mode Selection (TMOD Register)


Mode M1 M0 Description
Mode 0 0 0 13-bit Timer (8-bit THx + 5-bit TLx)
Mode 1 0 1 16-bit Timer (THx + TLx)
Mode 2 1 0 8-bit Auto-Reload (THx holds reload value)
Mode 3 1 1 Split Mode (Timer 0 acts as two 8-bit timers)

Mode 0: 13-bit Timer


Description

 Uses 8-bit THx + lower 5-bit TLx (total 13-bit).


 Max count: 2¹³ = 8192 (0x1FFF).
 When overflow occurs, TFx (Timer Flag) is set.

Example

 Using Timer 0 in Mode 0.

Assembly Code

MOV TMOD, #00H ; Timer 0 Mode 0 (13-bit)


MOV TH0, #1FH ; Load TH0 (High 8 bits)
MOV TL0, #00H ; Load TL0 (Lower 5 bits)
SETB TR0 ; Start Timer 0

WAIT: JNB TF0, WAIT ; Wait for overflow


CLR TF0 ; Clear overflow flag
CLR TR0 ; Stop Timer

C Code
TMOD = 0x00; // Timer 0 Mode 0
TH0 = 0x1F; // Load High byte
TL0 = 0x00; // Load Low byte
TR0 = 1; // Start Timer
while (TF0 == 0); // Wait for overflow
TF0 = 0; // Clear overflow flag
TR0 = 0; // Stop Timer

Use Case

 Used in older systems (rarely used today).

Mode 1: 16-bit Timer


Description

 16-bit Timer (Uses THx and TLx).


 Max count: 2¹⁶ = 65536 (0xFFFF).
 When overflow occurs, TFx is set.

Example

 Using Timer 0 in Mode 1 to generate a delay.

Assembly Code

MOV TMOD, #01H ; Timer 0 Mode 1 (16-bit)


MOV TH0, #0xFC ; Load High Byte (for 1ms delay)
MOV TL0, #0x66 ; Load Low Byte
SETB TR0 ; Start Timer

WAIT: JNB TF0, WAIT ; Wait for overflow


CLR TF0 ; Clear overflow flag
CLR TR0 ; Stop Timer

C Code

TMOD = 0x01; // Timer 0 Mode 1


TH0 = 0xFC; // Load High byte
TL0 = 0x66; // Load Low byte
TR0 = 1; // Start Timer
while (TF0 == 0); // Wait for overflow
TF0 = 0; // Clear overflow flag
TR0 = 0; // Stop Timer

Use Case

 General-purpose timing applications (e.g., delays).

Mode 2: 8-bit Auto-Reload


Description

 Uses only TLx (8-bit).


 THx holds reload value, which is automatically copied to TLx after overflow.
 Max count: 2⁸ = 256 (0xFF).
 When overflow occurs, TLx is reloaded automatically from THx.

Example

 Timer 1 in Mode 2 to generate a 1ms delay.

Assembly Code

MOV TMOD, #20H ; Timer 1 Mode 2 (8-bit auto-reload)


MOV TH1, #0xF6 ; Load High byte (reload value)
MOV TL1, #0xF6 ; Load Low byte (initial value)
SETB TR1 ; Start Timer

WAIT: JNB TF1, WAIT ; Wait for overflow


CLR TF1 ; Clear overflow flag

C Code

TMOD = 0x20; // Timer 1 Mode 2


TH1 = 0xF6; // Reload value
TL1 = 0xF6; // Load initial value
TR1 = 1; // Start Timer
while (TF1 == 0); // Wait for overflow
TF1 = 0; // Clear overflow flag

Use Case

 Baud rate generation for serial communication (UART).


Mode 3: Split Mode
Description

 Only for Timer 0.


 Splits Timer 0 into two separate 8-bit timers.

o TH0 operates as an 8-bit timer.


o TL0 operates as an 8-bit timer.

 Timer 1 is still available.

Example

 Using Timer 0 in Split Mode.

Assembly Code

MOV TMOD, #03H ; Timer 0 in Mode 3 (Split Mode)


SETB TR0 ; Start Timer 0 (Low Byte)
SETB TR1 ; Start Timer 0 (High Byte)

C Code

TMOD = 0x03; // Timer 0 in Split Mode


TR0 = 1; // Start Timer 0 (Low Byte)
TR1 = 1; // Start Timer 0 (High Byte)

Use Case

 Multi-tasking applications where two timers are needed.

Comparison of Timer Modes


Mode Max Count Use Case
Mode 0 8192 (13-bit) Rarely used (Legacy systems)
Mode 1 65536 (16-bit) Delays, event timing
Mode 2 256 (8-bit Auto-Reload) Baud rate generation, PWM
Mode 3 8-bit + 8-bit Two 8-bit independent timers

You might also like