post details Internal-Timers-Counters-8051-Microcontrollers-Programming-Tutorials | Timers-Counter-Programming | 89C51-89C52-89S51-89s52-Microcontrollers
Timers & Counters 8051 Microcontrollers
Paris
Timers And Counters Supported By 8 Bit 8051 Microcontroller Family Models :
Atmel, NXP, Philips, 8051, 8052, 89C51, 89C52, 89S51, 89s52, 89C1051, 89C1051, 89C2051, AT89C4051, AT89S8252,l AT89C1051, AT89C2051, AT89C4051,P89C51RB+, P89C51RC+, P89C51RD+, P89C51RB2Hxx, P89C51RC2Hxx, P89C51RD2Hxx, P89C660, P89C662, P89C664, P89C668, P89C669, P89C51RA2xx, P89C51RB2xx, P89C51RC2xx, P89C51RD2xx, P89C60X2, P89C61X2,P89LV51RB2, P89LV51RC2, P89LV51RD2, P89V51RB2, P89V51RC2, P89V51RD2, P89V660, P89V662, P89V664.
 
Introduction Timers/Counters 8051 Microcontrollers
8051 Microcontrollers embedded applications needs the count the external events, frequency counting or generation of internal time and time delays for between applications or modules. These task are very important tasks and very complex in embedded systems. These tasks can be solved using software techniques. Software loops for counting events and timing delays keep CPU busy so that other events can not be attended by the CPU timely. Most of time the CPU is occupied by the loops to check if then else for counting events and time delays. So software techniques are best to use Timers & Counters in 8051 Microcontrollers Programming. To keep this pressure away from the CPU timers are counters are given in the 8051 Microcontrollers to handle the counting and timing issues. Using internal timers and counters with software techniques we can have the optimum use of CPU's performance. Process is little bit complex but has huge advantage over other techniques. 
 
As an example, LED flashing program the LED is turned on and off at particular time delays using loops for some steps. Desired delay is specified into the loop. But is the bad programming techniques and it has a demerit the CPU remains busy in stepping through the loops and will not pay attention on another important task. If we use on chip timers for this purpose it is the best practice. 
 
About Timers/Counters 8051 Microcontrollers
Atmel 8051 Microcontroller is equipped with two 16 bit up Timers/Counters with the name of T0 and T1 for counting events in the 8051 Microcontrollers applications to count pulses internal or outside. The best advantage of this each of them can be used for dual purpose. Each of them can be programmed to count internal clock pulses then they are used as timer, when used in counting external events then can be defined as counter.
 
The 16 Bit counters are divided into two 8 bit registers called the timer low TL0 and TL1 low bytes and timer high TH0 and TH1 high bytes Counter functions are performed by bit states in the Timer Mode Control Register (TMOD) and timer/counter control sfr register (TCON) using certain programming instructions.
 
Using Timer/Counter As Counter 8051 Microcontrollers
As specified above the internal counters are available on the chip to set free the CPU of the 8051 Microcontroller to look after delay loops. When application need to count the pulses generated from internal or external a number is placed in one of the counters. The number show s the maximum count of events less the requested count and add 1. When the counter increments from the initial number to the maximum of its limits and rolls over to the 0 because limit is crossed and also sets the a timers flag. This flag condition tell the microcontroller that the count has been accomplished and this flag can be used as Timer Counter Interr 8051 Microcontrollers
 
Using Timer/Counter As A Timer 8051 Microcontrollers
If internal counter is used by the developer to count internal timing ticks then the counter is defined a Timer. Now this counter serves an internal timer to keep an eye on certain event happen after some time delays and stays in touch with the internal oscillator or the Microcontrollers then it will count the internal clock frequency of microcontroller oscillator divided by 12d. As an example if the crystal frequency is at the speed of 6 Mhz then the frequency of the timer 500 Kilohertz. The C/T bit in the Timer Mode (TMOD) must set to low bit means zero if we want to get timing ticks from the microcontrollers oscillators. Bit TRX in the TCON register must be set to 1 and the gate bit in the TMOD register must set to low (0), or external pin INTX must be a 1. In simple when counter is set as a timer, then the timing pulses are put into the the counter using run bit and the gate bit or the external pin bits INTX.
 
Timer Modes Of Operations 8051 Microcontrollers
Here are four modes of timers that can be implemented in the software techniques that are decided by the mode bits, M1 and M0 in the TMOD register. Here are the extensive details of the all 4 modes of timers.
 
Timer Mode 0 : 8051 Microcontrollers
setting timer X mode bits ot 00b in the TMOD register results in using the THX register as an 9-bit counter and TLX as a 5-bit counter. The pulse input is divided into 32d in TL so that TH counts the original oscillator frequency reduced by a total 384d. Example 6 Mhz oscillator frequency would result in a final frequency to TH of 15625 hertz. The Timer flag is set whenever THX goes from FFh to 00h or in .0164 seconds for a 6 Mhz crystal if THX at 00h.
 
Timer Mode 1 : 8051 Microcontrollers
This mode is similar to Mode 0 except TLX is configured as full 8-bit counter when the mode bits are set to 01b in TMOD. The Timer flag would be set in .1311 seconds using 6 Mhz crystal.
 
Timer Mode 2 : 8051 Microcontrollers
Setting the mode bits to 10b in TMOD sets the timer to use only the TLX counter as an 8-bit counter. THX is used to hold a value that is loaded into TLX every time TLX overflows from FFh to 00h. The Timer flas is also set when TLX overflows. This show the autoload features
TLX will start counting from the number at THX, overflow and be initialized again with the contents of THX. For example, placing 9CH in THX will result in a dely of exactly. 002 seconds before the overflow flag is set if a 6 megahertz crystal is used.
 
Timer Mode 3 : 8051 Microcontrollers
8051 Microcontrollers Timers 0 and Timer 1 may be programmed to be in Mode0,1,2 independently of a similar mode for the other timer. This is not true for Timer Mode 3, the timers do not operate independently if mode 3 is chosen for timer 0. Placing timer 1 in Mode 3 causes it to stop counting, the control bit TR! and timer 1 flag TF1 are then used by Timer 0.
 
Timer Mode 3 : 8051 Microcontrollers
8051 Microcontrollers Timers 0 and Timer 1 may be programmed to be in Mode0,1,2 independently of a similar mode for the other timer. This is not true for Timer Mode 3, the timers do not operate independently if mode 3 is chosen for timer 0. Placing timer 1 in Mode 3 causes it to stop counting, the control bit TR! and timer 1 flag TF1 are then used by Timer 0.
 
Timers Vs Counters 8051 Microcontrollers
The main and only difference between counting and timing is the source of the clock pulses to the counters the clock pulses are sourced from the oscillator through the divided by 12d.When used as a counter Pin T0 (P3.4) supplies pulses to counter 0, and pin T1 (P3.5) to counter 1. The C/T bit in TMOD must be set to 1 to enable pulses from the TX pin to reach the control circuits.