Timer 8051

The Timers in 8051 microcontroller:

The microcontroller 8051 basically has two on-chip timers that can be used for timing durations or for counting external events.
In our previous project on Blinking LED program, the LED was turned on for a specific length of time and then turned off for a specific length of time. We achieved this through the use of time delays. Since the microcontroller operates at a specific frequency, we could work out exactly how many iterations of the time delay was needed to give us the desired delay. However, this is clumsy and prone to error. And there is another disadvantage; the CPU is occupied, stepping through the loops.
If we use the on-chip timers, the CPU could be off doing something more useful while the timers take on the menial task of keeping track of time.

The Timers' SFRs :
The 8051 has two 16-bit timers. The high byte for timer 1 (TH1) is at address 8DH while the low byte (TL1) is at 8BH. The high byte for timer 0 (TH0) is at 8CH while the low byte (TL0) is at 8AH.
Both timers can be used in a number of different modes. The programmer sets the timers to a specific mode by loading the appropriate 8-bit number into the Timer Mode Register (TMOD) which is at address 88H.

TMOD register:

The functions of the 8-bits of TMOD are described in the above table. The top four bits are for timer 1 and the bottom four bits have the exact same function but for timer 0. The Gate bits are used in conjunction with interrupts and will be dealt with at a later stage. For the moment we can take it that bits 7 and 3 are always cleared. As mentioned above, the timers can be used for counting external events or for timing intervals. If you wish the timer to be an event counter you set the corresponding C/T-bar bit. Similarly, if you wish it to be an interval timer you reset the corresponding C/T-bar bit. There are two mode bits (M1 and M0) for each timer. The table below describes their function.

There are four timer modes, set by the bits M1 and M0. Mode 0 is not commonly used. Mode 3 we will deal with later.
Mode 1 - 16-bit mode: The high byte (THx) is cascaded with the low byte (TLx) to produce a 16-bit timer. This timer counts from 0000H to FFFFH - it has 216 (65,536) states. An overflow occurs during the FFFFH to 0000H transition, setting the overflow flag (to be dealt with shortly).

Mode 2- 8-bit auto-reload mode: The timer low byte (TLx) operates as an 8-bit timer (counting to FFH) while the high-byte holds a reload value. When the timer overflows from FFH, rather than starting again from 00H, the value in THx is loaded into TLx and the count continues from there.

T-CON register:
The top four bits of TCON are the only ones that interest us at the moment since the bottom four are used with interrupts.
Bit 7 and bit 5 are the timer overflow flags (TFx). The overflow flag is set by the hardware once an overflow occurs. For example, if timer 0 is in mode 1 (16-bit mode) then, during the state transition.




Who's new

  • ravirajpatil871...
  • shubhambajoria
  • yassir
  • demiholyman890954
  • scottgillum51169040

Get Notified



We are Social


Subscribe to Syndicate