You are on page 1of 5

8051 Counter and Timer

The 8051 MCU has two timers/counters T0, T1 there main purposes are to either measure the time
between two events or Count external events. In addition, they can be used to generate clock pulses
to be used in serial communication, so called a Baud rate.

Both timers are controlled by number or SFRs, which are


- TH0, TL0, TH1, TL1: represent 16 Bit timer value. (High and Low)
- TMOD: (Bit addressable) Which select the operation of the two timers and consists on:

o Bit7 GATE1, Bit3 GATE0: enable or disable TimerX by means of signal from INT1,INT0
[Port 3.3 , Port 3.2 respectively].

If GATEx= 0 Timer will operate


regardless of port INTx

If GATEx= 1 Timer will operate only


if INTx is HIGH.
o Bit6 C/T1, Bit2 C/T0: Counter or Timer
operation
If C/Tx = 0 Timer operation and the
clock provided from internal

If C/Tx = 1 Counter operation and clock


provided on Tx port [P3.5,P3.4].

o Bits5-4 T1M1,T1M0, Bits1-0 T0M1, T0M0 Are the operation mode of T1,T0 there are four
modes:
TxM1 TxM0 mode Operation
0 0 0 13 bit timer
0 1 1 16 bit timer
1 0 2 8 bit auto-reload
1 1 3 Split mode
Timer's modes
0- Mode 0 13-bit timer [Rarely used only for compatibility with older circuits]. This will use all
8 bit from THx and the LOW 5 bits from TLx i.e. [TH(7-0) , TL(5-0)].
So on each pulse the value of TL will be incremented by one until it reach 31 (32 count). On
the 32nd count, TL will be cleared and TH will be incremented by one until 255 (256 count)
thus total count will be 256 x 32 = 8192 count.
On the End timer Overflow flag in TCON (sBit TFx) will be set.
You need to manually reset the overflow time TFx to continue. [Discussed later].
1- Mode 1 16-bit timer same as Mode 0 but uses the whole THx , TLx

2- Mode 2 8-bit auto reload

In this mode the counter will count from the value in THx till 0x0FF and automatically reload.
When it is goes from 0x0ff to 0x00 it sets the Timer overflow flag TF. In this case programmer
does not need to reload the counter value manually like on Mode 0 and Mode 1.

3- Mode 3 Split mode [Rarely used and we will leave it to rest in peace]
- TCON : (Bit addressable) Timer control register , this register is shared between Timer
operations (the upper four bits) and Interrupt operation [Discussed later] (the lower four bits):
o Bit 7 TF1, Bit 5 TF0: Timer overflow will be set automatically when timer finish and stops
the timer, must be manually reset to repeat the counting.
o Bit 6 TR1, Bit4 TR0: Enable the timer (start) when it is high.

How to use timers


1- Select mode and operation (GATE, C/T, M1, M0) in TMOD.
2- If internal clock is used it should be divided by 12 (thus if 12MHz is used there will 1µs between
each two counts)
3- Load the timer TH, TL by timing value in negative since it is up counter and stops when reach 0,
so to count up to 6565 pulse which is 0x19A5 you should put 0xE65B ( 6565 in Second
complement) or if you want to count up to 3 pulses you should load 0xFFFD (FFFE,FFFF,0000 and
stops.
4- Start the timer (enable it) by setting TR bit in TCON.
5- Wait for TF to be high indicating time is up.

You can use timers as


- Delay counter (Simply set the time and wait for overflow).
- Input pulse counter (Simply set the timer with the number of pulses you are expecting and wait
for TF) and put the input on T0 or T1 bits
Example

You might also like