Timers can be used for:
- As timers for generating a time delay
- Counters to count events happening outside the microcontrollers.
There are two timers in 8051 i.e. Timer0 & Timer1. They both have a register that is 16 bit long. The lower byte is stored in TL & higher in TH. These registers are SFR and can be accessed just like any other register.
For e.g.: MOV TL0, #50H
MODES and TMOD register
Both timer0 & timer1 use the same register ‘TMOD’ for setting their mode.
1.) M1/MO (mode selector)
There are four different modes of timer. In this tutorial we shall concentrate only on mode1 (16-bit timer) and mode2 (8 bit auto-reload).
2.) C/T (clock/ timer)
- C/T= 0; for delay generation
- C/T= 1; an event counter.
3.) Clock source for timers
4.) How do we start and stop the timers?
- By software we can do so by setting ‘GATE=0’
The TR0, TR1 are the part of TCON (timer control) register.
- If we want to control timers through hardware we can do so by setting ‘GATE=1’ (it will be discussed afterwards)
5.) Time of delay
Consider the following example –
- The maximum value the timer0 register can hold is FFFF (hex).
- (FFFF- 3E11+1) hex = 49647 (decimal)
- Time= 49646* time of each cycle
- Now suppose if the crystal frequency is 11.0592 MHZ
- Frequency of timer= 11.0592/12; time of one cycle 1.085µs
- Time of delay= 49647* 1.085µs= 538.66ms
So we can assign the value to TL & TH corresponding to the time of delay we want to create.
6.) How will the µcontroller know that the timer register has reached its maximum value?
- It monitors the over flow flag (TF), when the timer reaches its maximum value it is raised to one.
- TF0 & TF1, over flow flags are part of TCON register.
7.) If we wish that the timer should roll over?
- Mode1: We have to clear the over flow flag and load the value of timer register (i.e. TL &TH) again.
- Mode2: We have to only clear the over flow flag. (Remember in this mode we don’t have to load the value of timer register again).
Let’s see an example to understand how the mode1 is programmed.
Suppose we want to create a duty cycle of 50% at port1 using timer0.
Duty cycle means (the ratio of time for which it is on to the total time)
In this mode we have to only give value to TH (high byte of timer register) and the value of TH is copied to TL by system. This mode accepts only eight bit and is auto-reload meaning that we don’t have to assign a value to timer register after it rolls over; therefore this is also called 8 bit auto-reload.
Now suppose we want to create a square wave (50% duty cycle) at P1.5 using timer0 in mode 2. Suppose we wish to repeat the process 100 times only:-
Timer1 & Timer0 work identically.
- It is used to count some external event
- P3.4 (pin14) is used to count pulses for timer0
- P3.5 (pin15) is used to count pulses for timer1
- These pins are called timer inputs: T0 (P3.4) & T1 (P3.5)
- C/T=1 for counter
Let us see an example of the counter programming using timer0 in mode2. The value of counter is displayed at port zero.
We can use a set of eight LED or LCD at port zero to display the value of counter. In this way timers can be used for counting events.