Many 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.
Counters 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.
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.
=/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 Values of timers and counter in 8051
Timer 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.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)
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)
TR0=1; (Start timer)
Time Delay Calculations for 8051 Microcontroller
The 8051 microcontroller works with 11.0592 MHz frequency.
Frequency 11.0592MHz=12 pules
1 clock pulse =11.0592MHz/12
F =0.921 MHz
Time delay=1/F
T=1/0.92MHz
T=1.080506 us (for ‘1’ cycle)
1000us=1MS
1000ms=1sec
Procedure to Calculate the Delay Program
1. 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 delay
500us/1.080806us
461pulses
P=65535-461
P=65074
65074 conveted by hexa decimal =FE32
TH1=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:
Counters in 8051
We 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.
Applications of Timers and Counters in 8051
Digital Counter with 8051
The 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.
Time delay circuit Using 8051 microcontroller
The 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.
This 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.
No comments:
Post a Comment