9.0 Timing and Events: Timers and Counters
Precise timing is critical for countless embedded applications, from generating delays to measuring external events. The 8051 includes two 16-bit timer/counters to handle these tasks. A timer is used to measure time intervals by counting internal machine cycles. A counter is used to count external events by monitoring an input pin. This section details how to configure and use these powerful peripherals.
9.1 Timer Registers
The 8051 has two 16-bit timers: Timer 0 and Timer 1. Because the 8051 has an 8-bit architecture, each 16-bit timer is accessed as a pair of 8-bit registers.
- Timer 0: Accessed via TL0 (Timer 0 Low byte) and TH0 (Timer 0 High byte).
- Timer 1: Accessed via TL1 (Timer 1 Low byte) and TH1 (Timer 1 High byte).
TMOD Register
The Timer Mode (TMOD) register is an 8-bit register used to configure the operating mode for both timers. The lower 4 bits are for Timer 0, and the upper 4 bits are for Timer 1. The key bits for each timer are:
- GATE: When set, the timer only runs while its corresponding external interrupt pin (INT0 or INT1) is high.
- C/T (Counter/Timer): Selects the function. If 0, it acts as a Timer, counting internal machine cycles. If 1, it acts as a Counter, counting 1-to-0 transitions on an external pin (T0 or T1). Remember, the timer frequency is always 1/12th of the frequency of the crystal attached to the 8051.
- M1 and M0: These two bits select one of the four operating modes for the timer.
9.2 Timer Operating Modes
The 8051 timers can be configured in one of four modes.
- Mode 0 (13-bit Timer Mode): An older mode that uses a 13-bit count. It is rarely used in new designs.
- Mode 1 (16-bit Timer Mode): This is the most common mode. It uses the full 16 bits of the timer registers (THx and TLx), allowing for a counting range from 0 to 65,535.
- Mode 2 (8-bit Auto-Reload Mode): In this mode, the timer operates as an 8-bit timer (using only the TLx register). When TLx overflows from 255 back to 0, it is automatically reloaded with the value stored in the THx register. This is extremely useful for generating fixed, periodic intervals, such as for a serial port baud rate.
- Mode 3 (Split Timer Mode): This mode applies only to Timer 0, splitting it into two independent 8-bit timers (TL0 and TH0).
9.3 Using the Timers
The basic procedure for using a timer to generate a delay is as follows:
- Initialize the TMOD register to select the desired timer (0 or 1) and its operating mode (e.g., Mode 1 for 16-bit timing).
- Load the initial count value into the THx and TLx registers. This value determines the length of the delay.
- Start the timer by setting its run control bit. For Timer 0, this is SETB TR0.
- Monitor the Timer Flag (TFx) bit. This flag is located in the TCON register. The hardware automatically sets TFx to 1 when the timer overflows (rolls over from its maximum value back to zero). Your program must wait for this flag to be set.
- Stop the timer, clear the flag, and reload. Once the TFx flag is set, you should stop the timer (e.g., CLR TR0), clear the flag (CLR TF0), and reload the initial values into THx and TLx to prepare for the next cycle.
While this polling method of checking the TFx flag works, a much more efficient way to handle events like a timer overflow is through interrupts.