Product categories

Have You Seen

B0505S-1WR3 MORNSUN Isolated DC - DC Converter

B0505S-1WR3 MORNSUN Isolated DC - DC Converter

Mornsun B0505S-1WR3 DC-DC Isolated ConverterMORNSUN B0505S 1WR3 1 watt, 5 Volts Isolated DC to DC Po..

Rs.106.20 (inc GST)
Rs.90.00 + GST

SKU: 2969 | DAE663
Stock: 100
XL1509-Adj E1 Buck DC to DC Converter IC (SOP8L Package)

XL1509-Adj E1 Buck DC to DC Converter IC (SOP8L Package)

XL1509-Adj E1 Buck DC to DC Converter IC (SOP8L Package)XL1509-Adj E1 Buck DC to DC Converter IC, Ad..

Rs.25.96 (inc GST)
Rs.22.00 + GST

SKU: 3657 | DAF397
Stock: 485

Using 8051 Timer



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.

1 MACHINE CYCLE = Crystal Time/12.

Crystal Time = 1/Crystal Frequency.

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

BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
GATE1 C/T1 T1M1 T1M0 GATE0 C/T0 T0M1 T0M0

 

 

BIT SYMBOL Function
7/3 GATE Controls the RUN/STOP of Timer 1/0.When this bit is set the timer will only run when INT1/0 (P3.3/P3.2) is high. When this bit is clear the timer will run regardless of the state of INT1/0.
6/2 C/T When this bit is set the timer will count events on T1/T0 (P3.5/P3.4). When this bit is clear the timer will be incremented every machine cycle.
5/1 T1M1 Timer Mode Bit
4/0 T1M0 Timer Mode Bit

 

 

M1 M0 Mode Description
0 0 0 13-bit Timer
0 1 1 16-bit Timer
1 0 2 8-bit auto reload
1 1 3 Split Timer

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

BIT Name Explanation
7 TF1 Timer 1 Overflow flag. Set when timer rolls from all ones to zero.
6 TR1 Timer 1 run control bit. When bit is Set Timer  is enabled to count ;when cleared timer is halted.
5 TF0 Timer 0 Overflow flag. Set when timer rolls from all ones to zero.
4 TR0 Timer 0 run control bit. When bit is Set Timer  is enabled to count ;when cleared timer is halted.

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 PROGRAM

Now 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:

 

MOV TMOD,#01h

 

Now we have to decide the ON time & OFF time of the LED. Suppose we are using 12MHz crystal so 1 Machine Cycle will be 1µs. Suppose we have to have LED's ON & OFF Time = 0.05 Sec, then we have to calculate the Value of TH0 & TL0.

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.

  • ini_timer0:
      • MOV TMOD,#01h
        • mov TH0,#0C3h
      • mov TL0,#50h
  • ret

The above subroutine initializes Timer 0 as 16 bit timer & loads TH0 & TL0 with values for 0.05 sec overflow duration.

 

  • ORG 0000h

     

    MOV TMOD,#01h

    //Timer0 16-bit Mode

    loop:

     

  • MOV TH0,#0C3h
  •  

  • MOV TL0,#50h
  • //For generating 0.05 Sec delay

  • CLR LED
  • //Turn ON LED

  • SETB TR0
  • // Start Timer 0

  • JNB TF0,$
  • // Wait till Timer 0 overflows

  • CLR TF0
  • //Timer0 has overflowed clear the Flag

  • CLR TR0
  • //Stop Timer0

  • SETB LED
  • //Turn OFF LED

  • MOV TH0,#0C3h
  •  

  • MOV TL0,#50h
  • //Reload Timer0 For generating 0.05 Sec delay

  • SETB TR0
  • // Start Timer 0

  • JNB TF0,$
  • // Wait till Timer 0 overflows

  • CLR TF0
  • //Timer0 has overflowed clear the Flag

  • CLR TR0
  • //Stop Timer0

  • ajmp loop
  •  

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.

 

Written by Amol Shah

Amol Shah

Founder of DNA Technology an Electronic Engineer by choice. Started working on this website as an Hobby and now its a full time venture. Very passionate about Electronics and like to learn new stuff. Want to make DNA Technology one of the best Online Store for Electronics Components in India.
Follow Me Twitter | Facebook | Google Plus | Instagram