Many times we require precise internal time delays between two actions this can be accomplished using software techniques like LOOP DELAY's but these delays keep the processor occupied because of which other important functions cannot be done. To relieve the processor of this burden we can use TIMERS provided by the controller. Most basic 8051 Microcontroller has two internal timers T0 & T1 (8952 has 3 timers T0, T1 & T2) these timers can be controlled individually. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Timers are used for three general functions:- 1) Generating Delays or calculating time between two events (TIMER MODE). 2) Counting the events (COUNTER MODE). 3) Generating BAUDRATE for SERIAL COMMUNICATION. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
If used in TIMER MODE the counter increments by every MACHINE CYCLE. A single Machine Cycle consists of 12 crystal pulses. i.e.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
So if we use 12MHz crystal then the timer will increment by 1 in every 1µS. The Timer always COUNTS UP, dosent matter in which mode it is being used the timer value is always incremented & overflows when the count goes from 0ffffh to 0000h. The TIMERS are divided into two 8-bit SFR called Timer LOW (TL0, TL1) & Timer HIGH (TH0, TH1) these registers contain the latest count of the TIMER. The TIMER action is controlled by two more SFR's called Timer Mode Control Register(TMOD) & Timer/Counter Control Register (TCON). TMOD is dedicated to the two Timers & controls the mode of operation of both the Timers. It can be considered as two duplicate 4 bit register, where the high 4 bits controls Timer 1 & the lower 4 bits controls Timer 0. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
TMOD |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
TIMER MODE 0 (13 bit mode) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
MODE 0 is a 13 bit mode. In this mode the THx acts as an 8 bit timer & TLx acts as a 5 bit timer. The TLx counts up to 31 & then resets to 00 & increment THx by 1. Suppose you load 0 in the timer then the timer will overflow in 213 i.e. 8192 machine cycles. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
TIMER MODE 1 (16 bit mode) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
MODE 1 is similar to MODE 0 except it is a 16 bit mode. In this mode the THx & TLx both acts as an 8 bit timer. The TLx counts upto 255 & then resets to 00 & increment THx by 1. Since this is a full 16 bit timer we can get maximum of 216 i.e. 65536 Machine cycle before the the timer overflows. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
TIMER MODE 2 (8 bit mode) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
In this Mode TLx accts as the timer & Thx contains the RELOAD VALUE i.e. THx is loaded in TLx everytime it overflows i.e. when TLx reaches 255 & is incremented then instead of resetting it to 0 it will be reseted to the value stored in THx. This mode is very commony used for generating baud rate used in serial communication. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
TIMER MODE 3 (Split Mode) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Timers 0 and I may be programmed to be in mode 0, 1, or 2 independently of a similar mode for the other timer. This is not true for mode 3; the timers do not operate independently if mode 3 is chosen for timer 0. Placing timer I in mode 3 causes it to stop counting; the control bit TR1 and the timer 1 flag TFI are then used by timer 0. When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0. So even if you use Timer 1 in Mode 0, 1 or 2 you won’t be able to START or STOP the timer & no INTERRUPT will be generated by Timer 1. So the Timer 1 will be incremented every machine cycle no matter what. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
TCON |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The above Table shows the TCON SFR as you can see only 4 bits have been defined thats because only these bit are used for Timers rest of then dont have anything to do with timers. The TCON register is BIT-ADDRESSABLE i.e. if you want to set just one bit you can do it using 'SETB' instruction eg. SETB TR1. The advantage of using the SETB instruction is that you can change the value of only one bit without changing the value of any other bits.From the above table you can see that TRx is to be set by the programmer for the Timerx to start incrementing. The microcontroller SETS the timer flax TFx when the corresponding timer overflows. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
SAMPLE PROGRAMNow you know how to initialize a Timer. Lets us now write a small program for LED blinking using Timer as delay. I have already explained LED Interfacing to Microcontroller where i have done a program for LED blinking. In that program we had used Software delay using Loop Techinque & LOOP within LOOP technique. Now over here we will wirte the same program using Timers for generating delay. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
We will use Timer 0 in sixteen bit mode i.e. MODE 1. The first two bits, GATE0 and C/T0 are both 0 since we want the timer to be independent of the external pins. 16-bit mode is timer mode 1 so we must clear T0M1 and set T0M0. Effectively, the only bit we want to turn on is bit 0 of TMOD. Thus to initialize the timer we execute the instruction: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Count for 0.05 Sec = 0.05/1µs = 50000 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
So we require Timer 0 to count 50000 (0C350h) internal clock pulses to generate 0.05 sec delay. Since the timer count's up, so it will be necessary to put the (ffffh - C350h = 3CAF) in timer & count Up untill it over flows. When the Timer Overflows, TF0 will be set by the controller. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The above subroutine initializes Timer 0 as 16 bit timer & loads TH0 & TL0 with values for 0.05 sec overflow duration. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Please check LED Interfacing To Microcontroller article for circuit diagram. In the above program the first line initilizes Timer0, the next two instructions initialize timer count. After that we Turn ON the LED & the start the Timer. The next instruction (JNB TF0,$) means "Jump if TF0 not set, back to the same instruction." The '$' operand means the address of the current instruction.Thus as long as the timer has not overflowed and the TF0 bit has not been set the program will keep executing this same instruction. After 0.05 second Timer 0 will overflow, set the TF0 bit, and program execution will then break out of the loop. Then we clear the TF0 flag reload the timer Count, Turn OFF the LED & then again start the Timer. |