TIMERS  A HARDWARE EXPLANATION
WHAT IS TIMER? In general, timer is nothing but a process  of time being or time based operation. When coming to microcontroller, timer is a set of registers together doing the same time being operations. The difference is, in microcontroller the time being operation was depends on crystal oscillator we used in microcontroller.
Timers in 89C51 AT89c51 equipped with 2 timers, both of which may be controlled, set, read and configure individually. They are called as timer 0 and timer 1.
Functions of timers There are three general functions the timers in 89C51 have Keeping time and calculating the amount of the time between the events Counting the event themselves Generating baud rate for serial communication.
Measuring Time This is one of the primary work of the timers Another one is counting the events The meaning of the measuring the time is “measuring the interval time between two events”. Therefore it can also called as “interval timers” .
Time required to do the process? Since we already explained that timer was running under the frequency of microcontroller’s crystal oscillator and the atmel has 12 Tcycles for each machine cycle. When timer is in interval timer mode and correctly configured,it will increment by 1 for every machine cycle. Thus running timer will be incremented  11,059,200  /  12  =  921,583 times.
Timer’s SFR As we mentioned earlier, in 8051 we have two timers One is timer0 and another is timer1. These two timers shares 2 SFRs TCON timer control TMOD timer mode select Also these two timers have their own dedicated registers for sole purpose TL0 and TH0 for timer0 TL1 and TH1 for timer1
TMOD SFR operation First we will discuss about TMOD.. Each and every bit of the this mode will give an information to the microcontroller, how to run the timers. The MSB 4 bits(4 through 7) are allocated for timer 1 The LSB 4 bits(0 through 3) are allocated for timer 0 Each 4 bits holds the information about the operation of timer
Mode 0 – 13 bit timer This is just a relic for microcontroller, which never used in any operation. But for our knowledge, It is a13 bit mode, that means the TLx will count 0 to 31 and reset to 0 and increment 1 to THx, THx will increment like this upto 255. Totally 13 bits are used and its limitation of data is 8192 values only. If you set a 13-bit mode, it will overflow after 8192 machine cycle.
Mode 1 – 16 bit timer It is similar to mode 0 except it uses all 16 bits. Therefore TLx increment from 0 to 255,when it reaches 255 it resets TLx and increment THx by 1. Since 16 bits are used in this mode, 65,536 distinct values can be accessed using this mode. When 65,536 machine cycles are over, the timer will be overflow in the microcontroller and reset to 0.
Mode 2 – auto reload timer It is an 8 bit auto reload mode. When the timer is in 8 bit auto reload mode, THx holds the reload value and TLx is the timer itself here. TLx starts counting and reaches 255. When it reaches 255, instead of resetting to 0, it loads the value stored in THx. That’s why it is called as 8 – bit Auto reload mode.
Mode 3 split timer When this mode is activated, the timer will split itself into two timers. For example, if this mode is used in timer0 then timer0 splits into two 8 bit timer, i.e.,  TL0 is timer0 and TH0 is timer1. All bits tied up to timer1 will tied up with TH0 now. TL0 and TH0 increment to 255 and resets to 0 individually.
TCON SFR This SFR is located in 0x88. In the 8 bits of this SFR only 4 bits of MSB required to be set to run the timer Those are TF1  TR1  TF0  TR0  This TCON is bit addressable, therefore if you want to set any particular bit, then it is easy to do it without disturbing other bits.
Initializing the timer  The first thing to be decided while initializing the timer is selecting the mode of operation. For 16-bit you have to select mode 1 For 8-bit auto reload mode you have to select mode 2 This is depends on your application or use of timer in you system.
Reading the values of timer There are three methods using this timers Read the TL and TH  values as 16- bit value Read the TL and TH as separate 8 bit values Calculate weather the timer is overflowed or not.
Detection of timer overflow It is very easy to check the timer overflow rather than the exact value of timer. Whenever the timer reset to 0 from a highest value the TFx  in the TCON SFR will be set. Therefore if timer 0 get overflowed then TF0 will be set If timer 1 get overflowed then TF1 will be set
Second purpose of timer The second purpose of the timer is event counting There are two methods in event counting. Counting the length of the event(eg., how many hours the tube light glown today) Counting the number of occurrence of that event(eg., how many cars has passed this way) Depends upon the purpose the method of usage varies.
Assignments Use timer in a delay process Use timer for counting purpose Generate a delay of 1 second Generate a square wave of 1 Khz. Generate a square wave using timer 0 generate a saw tooth wave using timer 1 at the same time

Timers

  • 1.
    TIMERS AHARDWARE EXPLANATION
  • 2.
    WHAT IS TIMER?In general, timer is nothing but a process of time being or time based operation. When coming to microcontroller, timer is a set of registers together doing the same time being operations. The difference is, in microcontroller the time being operation was depends on crystal oscillator we used in microcontroller.
  • 3.
    Timers in 89C51AT89c51 equipped with 2 timers, both of which may be controlled, set, read and configure individually. They are called as timer 0 and timer 1.
  • 4.
    Functions of timersThere are three general functions the timers in 89C51 have Keeping time and calculating the amount of the time between the events Counting the event themselves Generating baud rate for serial communication.
  • 5.
    Measuring Time Thisis one of the primary work of the timers Another one is counting the events The meaning of the measuring the time is “measuring the interval time between two events”. Therefore it can also called as “interval timers” .
  • 6.
    Time required todo the process? Since we already explained that timer was running under the frequency of microcontroller’s crystal oscillator and the atmel has 12 Tcycles for each machine cycle. When timer is in interval timer mode and correctly configured,it will increment by 1 for every machine cycle. Thus running timer will be incremented 11,059,200 / 12 = 921,583 times.
  • 7.
    Timer’s SFR Aswe mentioned earlier, in 8051 we have two timers One is timer0 and another is timer1. These two timers shares 2 SFRs TCON timer control TMOD timer mode select Also these two timers have their own dedicated registers for sole purpose TL0 and TH0 for timer0 TL1 and TH1 for timer1
  • 8.
    TMOD SFR operationFirst we will discuss about TMOD.. Each and every bit of the this mode will give an information to the microcontroller, how to run the timers. The MSB 4 bits(4 through 7) are allocated for timer 1 The LSB 4 bits(0 through 3) are allocated for timer 0 Each 4 bits holds the information about the operation of timer
  • 9.
    Mode 0 –13 bit timer This is just a relic for microcontroller, which never used in any operation. But for our knowledge, It is a13 bit mode, that means the TLx will count 0 to 31 and reset to 0 and increment 1 to THx, THx will increment like this upto 255. Totally 13 bits are used and its limitation of data is 8192 values only. If you set a 13-bit mode, it will overflow after 8192 machine cycle.
  • 10.
    Mode 1 –16 bit timer It is similar to mode 0 except it uses all 16 bits. Therefore TLx increment from 0 to 255,when it reaches 255 it resets TLx and increment THx by 1. Since 16 bits are used in this mode, 65,536 distinct values can be accessed using this mode. When 65,536 machine cycles are over, the timer will be overflow in the microcontroller and reset to 0.
  • 11.
    Mode 2 –auto reload timer It is an 8 bit auto reload mode. When the timer is in 8 bit auto reload mode, THx holds the reload value and TLx is the timer itself here. TLx starts counting and reaches 255. When it reaches 255, instead of resetting to 0, it loads the value stored in THx. That’s why it is called as 8 – bit Auto reload mode.
  • 12.
    Mode 3 splittimer When this mode is activated, the timer will split itself into two timers. For example, if this mode is used in timer0 then timer0 splits into two 8 bit timer, i.e., TL0 is timer0 and TH0 is timer1. All bits tied up to timer1 will tied up with TH0 now. TL0 and TH0 increment to 255 and resets to 0 individually.
  • 13.
    TCON SFR ThisSFR is located in 0x88. In the 8 bits of this SFR only 4 bits of MSB required to be set to run the timer Those are TF1 TR1 TF0 TR0 This TCON is bit addressable, therefore if you want to set any particular bit, then it is easy to do it without disturbing other bits.
  • 14.
    Initializing the timer The first thing to be decided while initializing the timer is selecting the mode of operation. For 16-bit you have to select mode 1 For 8-bit auto reload mode you have to select mode 2 This is depends on your application or use of timer in you system.
  • 15.
    Reading the valuesof timer There are three methods using this timers Read the TL and TH values as 16- bit value Read the TL and TH as separate 8 bit values Calculate weather the timer is overflowed or not.
  • 16.
    Detection of timeroverflow It is very easy to check the timer overflow rather than the exact value of timer. Whenever the timer reset to 0 from a highest value the TFx in the TCON SFR will be set. Therefore if timer 0 get overflowed then TF0 will be set If timer 1 get overflowed then TF1 will be set
  • 17.
    Second purpose oftimer The second purpose of the timer is event counting There are two methods in event counting. Counting the length of the event(eg., how many hours the tube light glown today) Counting the number of occurrence of that event(eg., how many cars has passed this way) Depends upon the purpose the method of usage varies.
  • 18.
    Assignments Use timerin a delay process Use timer for counting purpose Generate a delay of 1 second Generate a square wave of 1 Khz. Generate a square wave using timer 0 generate a saw tooth wave using timer 1 at the same time