Timers in 8051

What is a Timer

The timer is a clock that controls the sequence of an event while counting in fixed intervals of time. A Timer is used for producing precise time delay. Secondly, it can be used to repeat or initiate an action after/at a known period of time. This feature is very commonly used in several applications. An example could be setting up an alarm which triggers at a point in time or after a period of time.


Timers in a controller: Why to use them

Most of the microcontrollers have inbuilt Timers. Timers in a controller not only generate time delays but they can also be used as counters. They are used to count an action or event. The value of counter increases by one, every time its corresponding action or event occurs. Timers in a controller are inbuilt chips that are controlled by special function registers (SFR) assigned for Timer operations.  These SFRs are used to configure Timers in different modes of operations.

While working with microcontrollers, it is more than often required to generate time delays. There are two possible ways of generating time delays. First is by using the code, like using for or while loops in a C program. However, the delays provided by the software are not very precise. The other method is to use Timers. Timers provide time delays that are very precise and accurate.

The Timers SFR:

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 89H.





Timer Mode Register



TMOD
Bit
Name
Timer
Description
7
Gate
1
Gate bit; when set, timer only runs while INT-bar is high. This bit is used in conjunction with interrupts and will be dealt with later.
6
C/T-bar
1
Counter/timer select bit; when the set timer is an event counter when the cleared timer is an interval timer.
5
M1
1
Mode bit 1
4
M0
1
Mode bit 0
3
Gate
0
Gate bit; when set, timer only runs while INT-bar is high.
2
C/T-bar
0
Counter/timer select bit; when a set timer is an event counter when a cleared timer is an interval timer.
1
M1
0
Mode bit 1
0
M0
0
Mode bit 0

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.



M1
M0
Mode
Description
0
0
0
13-bit timer mode (this mode exists simply to keep the 8051 backwards compatible with its predecessor, the 8048, which had a 13-bit timer) - we will not be using mode 0.
0
1
1
16-bit timer mode
1
0
2
8-bit auto-reload mode
1
1
3
Split timer mode - this mode will be dealt with at a later stage

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.






A diagrammatic representation of mode 1 and mode 2. (The 8051 Microcontroller, 3rd Edition - I. Scott MacKenzie)



Timer Control Register



TCON
Bit
Symbol
Bit Address
Description
7
TF1
8FH
Timer 1 overflow flag; set by hardware upon overflow, cleared by software.
6
TR1
8EH
Timer 1 run-control bit; manipulated by software - setting starts timer 1, resetting stops timer 1.
5
TF0
8DH
Timer 0 overflow flag; set by hardware upon overflow, cleared by software.
4
TR0
8CH
Timer 0 run-control bit; manipulated by software - setting starts timer 0, resetting stops timer 0.
3
IE1
8BH
The bottom four bits of TCON are used in conjunction with interrupts - they will be dealt with at a later stage.
2
IT1
8AH
1
IE0
89H
0
IT0
88H

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 from FFFFH to 0000H the overflow flag TF0 is set by the hardware.



Bit 6 and bit 4 are the timer run-control bits (TRx). A timer is started by setting TRx and stopped by clearing TRx.



The TCON register is bit addressable because the programmer needs ease of access to the individual bits. You may wish to test the contents of TF1, to see when timer 1 overflows. Or you may wish to stop timer 0 without effecting timer 1, as shown below. Clearing TR0 does not affect the other seven bits of TCON.

CLR TR0





Initialising the Timers

The timers are initialised (ie; put into a particular mode of operation) by moving the appropriate value into the TMOD register. For example, if you wished to use timer 1 as a 16-bit interval timer and timer 0 as an 8-bit auto-reload event counter you would load the following value into TMOD.




bit
value
note
Timer 1
7
0
gate (set when using interrupts, reset otherwise)
6
0
C/T-bar (0 because timer 1 is to be an interval timer)
5
0
mode bits (01 for mode 1 - 16-bit)
4
1
Timer 0
3
0
gate (set when using interrupts, reset otherwise)
2
1
C/T-bar (1 because timer 0 is to be an event counter)
1
1
mode bits (10 for mode 2 - 8-bit auto-reload)
0
0

Comments

Popular posts from this blog

General puprose registers in 8051.

TCON Special Function register.

TMOD Special Function Register.