You are on page 1of 17

Timers And Counters

8051
Intro
• The 8051 has two timers/counters, they can be
used as:
• Timers to generate a time delay
• Event counters to count events happening outside
the microcontroller
8051 Timers Important Registers and Bits
• Two timers/counters  Timer0/Counter0, Timer1/Counter1
• Important registers
• TMOD reg (8-bit)  Used for both timer0/timer1 mode settings
• Timer 0 registers
• TLO (8-bit)  Timer 0 Low byte reg
• THO (8-bit)  Timer 0 High byte reg
• Timer 1 registers
• TL1 (8-bit)  Timer 1 Low byte reg
• TH1 (8-bit)  Timer 1 High byte reg
• Flags
• TFO  Timer 0 flag
• TF1  Timer 1 flag
• Timer Start Bit
• TRO  Timer 0 start bit
• TR1  Timer 1 start bit
TLx/THx
Inside
TMOD
•Gate Control
0 = Timer enabled
1 = Timer enabled if INTx is high

•C/T:Counter or Timer Selector


0 = Internal count source (clock/12)
1 = External count source T0/T1(P3.4/P3.5) pin.

•M1-M0:Mode Control
00-Mode 0, 13 bit count mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Split Timer mode
Inside
TMOD
16-Bit
mode

8-bit
Autorelo
adMode
TIMER0 formulas

In 8- bit mode In 16-bit mode


• Step 1: • Step 1:
n = (time delay needed) / [(12÷Xtal)] n = (time delay needed) / [(12÷Xtal)]

• Step 2: • Step 2:
Value= 256 – n Value= 65536 – n
xx= binary of “Value” yyxx= binary of “Value”

TLx=xx TLx=xx
THx=yy
Example: Given: Delay needed=50ms , Xtal=11.0592Mhz … for 16-bit mode
calculations , find values for timer0 registers?
Solution::
n = (50*10-3) / [12÷11.0592*106)] = 37891
Value= 65536 – 37891 = 27645 = (0110101111111101)b = (FD4B)hex
TL0=0xFD, TH0=0x4B
Timer coding steps
Load TLx/THx
Load TMOD reg Wait till TFx flag
according to
according to Start timer using becomes high
required delay or #
required settings TRx bit
of counts

Goto next step Stop timer using


according your Clear flag TRx bit
requirement
Write an 8051 C program to toggle only bit P1.5 continuously every 50 ms. Use Timer
0, mode 1 (16-bit) to create the delay. XTAL = 11.0592 MHz
Solution:
#include <reg51.h>
void T0M1Delay(void);
sbit mybit=P1^5; void T0M1Delay(void)
{
void main(void)
TMOD=0x01;
{ TL0=0xFD; // values for 50ms
while (1) TH0=0x4B;
TR0=1;
{ while (TF0==0);
mybit=~mybit; TR0=0;
T0M1Delay(); TF0=0;
}
}
}
Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use Timer 1, mode 1
to create the delay. , XTAL = 11.0592 MHz
Solution:
#include <reg51.h>
void T1M1Delay(void);
void T1M1Delay(void)
void main(void) {
{ TMOD=0x10;
unsigned char x; TL1=0xFE; // values for 25ms
P2=0x55; TH1=0xA5;
while (1) TR1=1;
while (TF1==0);
{
TR1=0;
P2=~P2; TF1=0;
for (x=0;x<20;x++) //to get 500ms from 25ms delay function }
T1M1Delay();
}
}
A switch is connected to pin P1.2. Write an 8051 C program to monitor SW and create the following frequencies
on pin P1.7:
SW=0: 500Hz
SW=1: 750Hz, use Timer 0, mode 1 for both of them. XTAL = 11.0592 MHz
Solution: void T0M1Delay(unsigned char c)
{
#include <reg51.h>
TMOD=0x01;
sbit mybit=P1^5; if (c==0)
sbit SW=P1^7; {
void T0M1Delay(unsigned char); TL0=0x67; //values for 500hz,50% duty cycle
void main(void) TH0=0xFC;
{ }
else
SW=1;
{
while (1) TL0=0x9A; //values for 750hz,50% duty cycle
{ TH0=0xFD;
mybit=~mybit; }
if (SW==0) TR0=1;
T0M1Delay(0);
while (TF0==0);
TR0=0;
else
TF0=0;
T0M1Delay(1); }
}
}
Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2 (8-bit
auto-reload) to create the delay. XTAL = 11.0592 MHz
Solution:
#include <reg51.h>
void T0M2Delay(void);
sbit mybit=P1^5; void T0M2Delay(void)
{
void main(void) TMOD=0x02;
{ TH0=-23; // value for 25 μs
TR0=1;
unsigned char x,y; while (TF0==0);
while (1) TR0=0;
TF0=0;
{ }
mybit=~mybit;
for (x=0;x<250;x++)
for (y=0;y<40;y++) //two loops to get 25 μs × 250 × 40 = 250 ms
T0M2Delay();
}
}
Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode
2 to create delay. XTAL = 11.0592 MHz
Solution:
#include <reg51.h>
void T1M2Delay(void); void T1M2Delay(void)
{
sbit mybit=P2^7; TMOD=0x20;
void main(void) TH1=-184; //for 2500hz , 50% duty cycle
TR1=1;
{ while (TF1==0);
TR1=0;
unsigned char x; TF1=0;
while (1) }

{
mybit=~mybit;
T1M2Delay();
}
}
Timer Module
as a Counter
• Clock from
external pins P3.4
or P3.5
Counter programming
Assume that a 1-Hz external clock is being fed into pin T0 (P3.4). Write a C
program for counter 0 in mode 1 (16-bit) to count the pulses and display
the state of the TH0 and TL0 register
Solution while (1)
{
#include <reg51.h> do
sbit T0=P3^4; {
TR0=1;
void main() P1=TL0;
P2=TH0;
{ }
while (TF0==0);
T0=1; // make p3.4 i/p TR0=0;
TF0=0;
TMOD=0x05; // counter 0 Mode 1 }
TL0=0; // initialize timer values to zero }

TH0=0;
TRY TO DO FOLLOWING CODE

Assume that a 1-Hz external clock is being fed into pin T1 (P3.5).
Write a C program for counter 1 in mode 2 (8-bit auto reload) to count
up and display the state of the TL1 count on P1. Start the count at 0H.

You might also like