0% found this document useful (0 votes)
2K views16 pages

Timers - Counters in 8051

The document discusses timers and counters in microcontrollers. It specifically discusses the timers and counters in the 8051 microcontroller. There are two 16-bit timers/counters, Timer 0 and Timer 1, that can operate as either timers or counters. The timers count internal clock cycles while counters count external events. The timers have modes that control how they operate as 8-bit, 13-bit, or 16-bit timers or to split the timers.

Uploaded by

rajesh kone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views16 pages

Timers - Counters in 8051

The document discusses timers and counters in microcontrollers. It specifically discusses the timers and counters in the 8051 microcontroller. There are two 16-bit timers/counters, Timer 0 and Timer 1, that can operate as either timers or counters. The timers count internal clock cycles while counters count external events. The timers have modes that control how they operate as 8-bit, 13-bit, or 16-bit timers or to split the timers.

Uploaded by

rajesh kone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Timers / Counters

Timer
• It is a specialized type of clock which is used to measure time
intervals.
• A timer that counts from zero upwards for measuring time
elapsed is often called a stopwatch.
• It is a device that counts down from a specified time interval and
used to generate a time delay timer.
Counter
• A counter is a device that stores (and sometimes displays) the
number of times a particular event or process occurred, with
respect to a clock signal.
• It is used to count the events happening outside the
microcontroller..
• The 8051 has two counters/timers which can be used
either as timer to generate a time delay or as counter to
count events happening outside the microcontroller.

• The 8051 has two timers:

➢ Timer0 and Timer1.

➢ They can be used either as Timers or as Counters.

➢ Both Timers are 16 bits wide.

➢ Each 16-bit is accessed as two separate registers of


Lower byte (TLx) and higher byte (THx).
Timer Mode
• In Timer Mode ,The internal machine cycles are counted.
• So this register is incremented in each machine cycle.
• So when the clock frequency is 12MHz, then the timer
register is incremented in each microsecond.

crystal frequency of microcontroller 11.0592 MHz


Timer clock frequency= (Xtal Osc.frequency)/12
= 11.0592 MHz /12 = 921.6 KHz
T period = 1/ 921.6 KHz =1.085 μS
Counter Mode
• In the counter mode, the external events are counted.
• In this mode, the timer register is incremented when external event
is occurred.
• At least two machine cycles are needed.
• When the frequency is 12MHz, then the maximum count frequency
will be 12MHz/24 = 500KHz.
• So for event counting the time duration is 2 µs

Timer Counter
The register incremented for every The register is incremented considering 1
machine cycle. to 0 transition at its corresponding to an
external input
Maximum count rate is 1/12 of the Maximum count rate is 1/24 of the
oscillator frequency. oscillator frequency.
A timer uses the frequency of the A counter uses an external signal to
internal clock and generates delay. count pulses.
TMOD (timer mode) Register: This is an 8-bit register which is used by both Timer 0 and 1
to set the various timer modes.

GATE:
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.

C/T (Counter/Timer): Timer or Counter select bit


1 = Use as Counter 0 = Use as Timer

M1 M0 Mode Operation
0 0 • 0 (13-bit timer mode) ✓ 13-bit timer/counter, 8-bit of THx & 5-bit of TLx
0 1 • 1 (16-bit timer mode) ✓ 16-bit timer/counter, THx cascaded with TLx
✓ 8-bit timer/counter (auto reload mode),
• 2 (8-bit auto reload
1 0 ✓ TLx reload with value held by THx
mode)
✓ each time TLx overflow
✓ Split 16-bit timer into two 8-bit timer i.e.
1 1 • 3 (split timer mode)
✓ THx and TLx like two 8-bit timer
TCON Register : TCON is 8-bit control register and contains timer and interrupt flags.

Bit 7 - TF1: Timer1 Overflow Flag


1 = Timer1 overflow occurred 0 = Timer1 overflow not occurred.
It is cleared through software. In Timer1 overflow interrupt service routine, this bit will get cleared automatically
while exiting from ISR.
Bit 6 - TR1: Timer1 Run Control Bit
1 = Timer1 start. 0 = Timer1 stop.
It is set and cleared by software.
Bit 5 – TF0: Timer0 Overflow Flag
1 = Timer0 overflow occurred 0 = Timer0 overflow not occurred.
Bit 4 – TR0: Timer0 Run Control Bit
1 = Timer0 start. 0 = Timer0 stop.
Bit 3 - IE1: External Interrupt1 Edge Flag
1 = External interrupt1 occurred. 0 = External interrupt1 Processed.
It is set and cleared by hardware.
Bit 2 - IT1: External Interrupt1 Trigger Type Select Bit
1 = Interrupt occur on falling edge at INT1 pin. 0 = Interrupt occur on low level at INT1 pin.
Bit 1 – IE0: External Interrupt0 Edge Flag
1 = External interrupt0 occurred. 0 = External interrupt0 Processed.
Bit 0 – IT0: External Interrupt0 Trigger Type Select Bit
1 = Interrupt occur on falling edge at INT0 pin. 0 = Interrupt occur on low level at INT0 pin.
Mode 0 (13-bit timer mode)
Mode 0 is 13-bit timer mode for which 8-bit of THx and 5-bit of TLx (as Prescaler).
• Xtal oscillator frequency is 11.0592 MHz
• we have machine cycle of 1.085uSec.
• Hence, required count to generate delay of 1mSec. is,
• Count =(1m)/(1.085μ) ≈ 921
• Value=(8191-Count)= 8191 – 921=7270 = 0x1C70
• we need additional 13 MC (machine cycles) from call to
return of delay function.
Mode 1 (16-bit timer mode)
Mode 1 is 16-bit timer mode for which 8-bit of THx and 8-bit of TLx

• Xtal oscillator frequency is 11.0592 MHz


• we have machine cycle of 1.085uSec.
• Hence, required count to generate delay of 1mSec. is,
• Count =(1m)/(1.085μ) ≈ 921
• Value=(8191-Count)= 65536– 921=64615 = 0xFC67
• TH0 = 0xFC & TL0 = 0x67
Delay of 1 Sec
• Xtal oscillator frequency is 11.0592 MHz
• we have machine cycle of 1.0852uSec.
• Hence, required count to generate delay of 1Sec
➢ 50ms x 20 =1000 ms = 1 Sec
• Count =(50m)/(1.0852μ) ≈ 46074
• Value=(65536-Count)= 65535-46074+1=19460= 0x4C05
TH1 = 0x4C TL1= 0x05
• Timer -1 - Mode 1
TMOD = 0x10
void delay()
#include<regx51.h>
sbit led=P2^0;
{
void delay(); int a;
void main() for ( a=0;a<20;a++)
{ {
while(1) TMOD=0X10;
{ TH1=0X4C;
led=0; TL1=0X05;
delay(); TR1=1;
led=1; while(TF1==0);
delay(); TR1=0;
} TF1=0;
} }
}
Mode2 (8-bit auto-reload timer mode)
• Mode 2 is 8-bit auto-reload timer mode.
• In this mode we have to load the THx-8 bit value only.
• when the Timer gets started, THx value gets automatically loaded into
the TLx and TLx start counting from that value.
• After the value of TLx overflows from the 0xFF to 0x00, the TFx flag gets
set and again value from the THx gets automatically loaded into the TLx
register.
• That’s why this is called as auto reload mode.
➢ Generating a square wave with 200uSec
• Xtal oscillator frequency is 11.0592 MHz
• we have machine cycle of 1.0852uSec.
• Hence, required count to generate delay of 100μSec
• Count =(100μ)/(1.0852μ) ≈ 92
• Value=(255-Count)= 255-92+1=164= 0xA4
• TH1 = 0xA4
Mode 3 of Timer/Counter (Split Mode)
• TL0 will be used as an 8-bit timer/counter.
• It will be controlled by the standard Timer0 control bits, T0
and INT0 inputs.
• The TH0 is used as an 8-bit timer but not the counter.
• This is controlled by Timer1 Control bit TR1.
• When the TH0 overflows from FFH to 00H, then TF1 is set to 1

You might also like