Home / 8051 Tutorial / Using Timers in 8051
pcbway
8051 microcontroller

Using Timers in 8051

Timers are among the most essential peripherals in any microcontroller, and the 8051 is no exception. With two built-in 16-bit timers (Timer 0 and Timer 1), the 8051 can perform a variety of timing tasks, such as creating delays, measuring time intervals, and generating pulses. In this tutorial, we’ll discuss the configuration and usage of these timers, complete with an example code in C using Keil C51.

Overview of 8051 Timers

The 8051 microcontroller has two timers, Timer 0 and Timer 1. Both timers are 16-bit registers, meaning they can count from 0 to 65,535. They can operate in several modes, allowing for different functionalities:

  • Mode 0: 13-bit timer/counter.
  • Mode 1: 16-bit timer/counter.
  • Mode 2: 8-bit auto-reload timer/counter.
  • Mode 3: Split timer mode (Timer 0 only).

Timer Registers

Each timer has the following associated registers:

  • THx: High byte of the timer.
  • TLx: Low byte of the timer.
  • TMOD: Timer Mode register for selecting the mode of operation.
  • TCON: Timer Control register for starting, stopping, and monitoring the timer.

Configuring the Timer

The steps to use a timer in 8051 are as follows:

  1. Select the Mode: Use the TMOD register to configure the desired mode for the timer.
    • When selecting a timer mode, avoid directly overwriting the TMOD register, as it controls both Timer 0 and Timer 1. Instead, use a bitwise operation to preserve the settings of the other timer. For example, to set Timer 0 to Mode 1 without affecting Timer 1:

      TMOD = (TMOD & 0xF0) | 0x01; // Set Timer 0 to Mode 1

      Here:

      • (TMOD & 0xF0) clears the lower 4 bits (related to Timer 0) while retaining the upper 4 bits (related to Timer 1).
      • | 0x01 sets Timer 0 to Mode 1.
  2. Load Initial Value: Load the initial count value into the THx and TLx registers.
  3. Start the Timer: Set the appropriate bit in the TCON register to start the timer.
  4. Monitor or Interrupt: Poll the timer overflow flag or use an interrupt to handle the overflow event.

Example: Creating a Delay Using Timer 0

In this example, we’ll create a delay of approximately 1 millisecond using Timer 0 in Mode 1.

#include <reg51.h>  // Include 8051 header file

void delay_ms(unsigned int ms); // this will be a new delay function using timer

void main() {
    while (1) {
        P1 = 0xFF;  // Turn ON all LEDs connected to Port 1
        delay_ms(1000);  // 1-second delay
        P1 = 0x00;  // Turn OFF all LEDs connected to Port 1
        delay_ms(1000);  // 1-second delay
    }
}

void delay_ms(unsigned int ms) {
    unsigned int i;
    for (i = 0; i < ms; i++) {
        TMOD = (TMOD & 0xF0) | 0x01;  // Timer 0 in Mode 1 (16-bit mode)
        TH0 = 0xFC;   // Load high byte (for 1 ms delay at 12 MHz)
        TL0 = 0x66;   // Load low byte (for 1 ms delay at 12 MHz)
        TR0 = 1;      // Start Timer 0
        while (TF0 == 0);  // Wait for overflow flag
        TR0 = 0;      // Stop Timer 0
        TF0 = 0;      // Clear overflow flag
    }
}

Explanation of the Code

  • Timer Mode Selection: The TMOD register is set using bitwise operations to select Timer 0 in Mode 1 (16-bit timer mode) without affecting Timer 1.
  • Timer Initialization: The values 0xFC66 are loaded into TH0 and TL0, respectively. This corresponds to a delay of 1 millisecond at a 12 MHz clock frequency.
  • Starting and Stopping the Timer: The TR0 bit in the TCON register is used to start and stop Timer 0.
  • Overflow Flag: The TF0 flag in the TCON register is polled to check when the timer overflows.

Applications of Timers

Timers in the 8051 can be used for:

  1. Creating Time Delays: As demonstrated in the example above.
  2. Pulse Width Modulation (PWM): Generating variable-width pulses for motor control or LED dimming.
  3. Event Counting: Counting external events when configured in counter mode.
  4. Baud Rate Generation: Used in conjunction with serial communication for baud rate control.

Timer Modes in Detail

  1. Mode 0: A 13-bit timer that counts from 0x0000 to 0x1FFF.
  2. Mode 1: A full 16-bit timer that counts from 0x0000 to 0xFFFF.
  3. Mode 2: An 8-bit timer with auto-reload. The value in THx is reloaded into TLx after overflow.
  4. Mode 3: Splits Timer 0 into two 8-bit timers. Timer 1 remains inactive in this mode.

Conclusion

Timers are versatile peripherals that greatly enhance the functionality of the 8051 microcontroller. Whether you’re creating delays, measuring time intervals, or generating pulses, understanding how to configure and use timers is essential for embedded development. Experiment with different timer modes and applications to fully harness the power of the 8051 microcontroller!

 

Check Also

8051 microcontroller

Introduction to 8051 Microcontroller using C

The 8051 microcontroller is much older than the ones featured here. Yes, even older than …

Index