Timers and Counters in 8051 Microcontroller and its ApplicationsMany of the microcontroller applications require counting of external events such as frequency of the pulse trains and generation of precise internal time delays between computer actions. Both these tasks can be implemented by software techniques, but software loops for counting, and timing will not give the exact result rather more important functions are not done. To avoid these problems, timers and counters in the micro-controllers are better options for simple and low-cost applications. These timers and counters are used as interrupts in 8051 microcontroller.There are two 16-bit timers and counters in 8051 microcontroller: timer 0 and timer 1. Both timers consist of 16-bit register in which the lower byte is stored in TL and the higher byte is stored in TH. Timer can be used as a counter as well as for timing operation that depends on the source of clock pulses to counters. Timers and countersCounters and Timers in 8051 microcontroller contain two special function registers: TMOD (Timer Mode Register) and TCON (Timer Control Register), which are used for activating and configuring timers and counters.Timer Mode Control (TMOD): TMOD is an 8-bit register used for selecting timer or counter and mode of timers. Lower 4-bits are used for control operation of timer 0 or counter0, and remaining 4-bits are used for control operation of timer1 or counter1.This register is present in SFR register, the address for SFR register is 89th.Timer Mode Control (TMOD)Gate: If the gate bit is set to ‘0’, then we can start and stop the “software” timer in the same way. If the gate is set to ‘1’, then we can perform hardware timer.C/T: If the C/T bit is ‘1’, then it is acting as a counter mode, and similarly when set C+ =/T bit is ‘0’; it is acting as a timer mode. Mode select bits: The M1 and M0 are mode select bits, which are used to select the timer operations. There are four modes to operate the timers.Mode 0: This is a 13-bit mode that means the timer operation completes with “8192” pulses. Mode 1: This is a16-bit mode, which means the timer operation completes with maximum clock pulses that “65535”.Mode 2: This mode is an 8-bit auto reload mode, which means the timer operation completes with only “256” clock pulses.Mode 3: This mode is a split-timer mode, which means the loading values in T0 and automatically starts the T1.Mode Selection BitsMode selection Values of timers and counter in 8051Mode selection values of timers and countersTimer Control Register (TCON): TCON is another register used to control operations of counter and timers in microcontrollers. It is an 8-bit register wherein four upper bits are responsible for timers and counters and lower bits are responsible for interrupts.Timer Control Register (TCON)TF1: The TF1 stands for ‘timer1’ flag bit. Whenever calculating the time-delay in timer1, the TH1 and TL1 reaches to the maximum value that is “FFFF” automatically.EX: while (TF1==1)Whenever the TF1=1, then clear the flag bit and stop the timer.TR1: The TR1 stands for timer1 start or stop bit. This timer starting can be through software instruction or through hardware method.EX: gate=0 (start timer 1 through software instruction) TR1=1; (Start timer)TF0: The TF0 stands for ‘timer0’ flag-bit. Whenever calculating the time delay in timer1, the TH0 and TL0 reaches to a maximum value that is ‘FFFF’, automatically.EX: while (TF0==1) Whenever the TF0=1, then clear the flag bit and stop the timer.TR0: The TR0 stands for ‘timer0’ start or stop bit; this timer starting can be through software instruction or through hardware method.EX: gate=0 (start timer 1 through software instruction) TR0=1; (Start timer)Time Delay Calculations for 8051 MicrocontrollerThe 8051 microcontroller works with 11.0592 MHz frequency.Frequency 11.0592MHz=12 pules1 clock pulse =11.0592MHz/12F =0.921 MHzTime delay=1/FT=1/0.92MHzT=1.080506 us (for ‘1’ cycle)1000us=1MS1000ms=1secProcedure to Calculate the Delay Program1. First we have to load the TMOD register value for ‘Timer0’ and ‘Timer1’in different modes. For example, if we want to operate timer1 in mode1 it must be configured as “TMOD=0x10”.2. Whenever we operate the timer in mode 1, timer takes the maximum pulses of 65535. Then the calculated time-delay pulses must be subtracted from the maximum pulses, and afterwards converted to hexadecimal value. This value has to be loaded in timer1 higher bit and lower bits. This timer operation is programmed using embedded C in a microcontroller.Example: 500us time delay500us/1.080806us461pulsesP=65535-461P=6507465074 conveted by hexa decimal =FE32TH1=0xFE;TL1=0x32;3. Start the timer1 “TR1=1;”4. Monitor the flag bit “while(TF1==1)”5. Clear the flag bit “TF1=0”6. Cleat the timer “TR1=0”Example Programs:Program- 1Program- 2Program- 3Counters in 8051We can use a counter by keeping C/T bit high, i.e., logic ‘1’ in the TMOD register. For better understanding, we have given one program which uses timer 1 as a counter. Here the LEDs are connected to 8051 Port 2, and the switch to the timer1 pin P3.5; and therefore, if the switch is pressed, the value will be counted. Otherwise, an externally connected sensor to this counter pin as input does this counting operation.Counter ProgramApplications of Timers and Counters in 8051Digital Counter with 8051The Digital counter with 8051 is achieved by programming the microcontroller as discussed above and by attaching a sensor system to it. This object counter uses IR sensor that detects the obstacle near to it and also enables the pin of the microcontroller 06. When an object passes through the sensors, then the microcontroller gets an interrupt signal from the IR sensors and increment the count which is displayed in the7-segment display.Digital Counter with 8051Time delay circuit Using 8051 microcontrollerThe below figure shows how the timer operation can be implemented for switching the LEDs in an effective way. The time delay operation for the set of LEDs is programmed in a microcontroller in the manner discussed above. Here, a set of LEDs are connected to the port 2 with a common supply system. When this circuit is turned on based on the time delay program in the microcontroller appropriately, these LEDs are switched on.Time delay circuitThis is all about the 8051 microcontroller timer and counters with basic programming and application circuits. We hope that the information of this article might have given you sufficient data to understand the concept better. Furthermore, any technical doubts on programming 8051 and its circuits, you can contact us by commenting below.Photo Credits:Digital Counter with 8051 by circuitstodayTime delay circuit by vshamu Share This Post: Facebook Twitter Google+ LinkedIn Pinterest Post navigation ‹ Previous Infrared Remote Control Switch Working OperationNext › Latest Microcontroller based Robotics Projects for Engineering Students Related Content Design of Android Controlled Automobiles Using 8051 Microcontroller How to Interface an LED With 8051 Microcontroller Digital Temperature Controller Circuit LCD Interfacing with 8051 Microcontroller 3 Commentsi need pic programing in c language a to z by doing frequency modulation and response scales ,rpm,in/out trigger ,counter/timer.temperature,pressure,modeum gsm,rs232 etc thank youReplyHi Adil We are very sorry to inform you that we cannot provide you like that.ReplyThank you Soo much..it helped me alotReplyAdd Comment Cancel replyComment:Name * Email * Website
i need pic programing in c language a to z by doing frequency modulation and response scales ,rpm,in/out trigger ,counter/timer.temperature,pressure,modeum gsm,rs232 etc thank youReply