8051
Timers & Counters
1
Time delay
2
Two ways to create time delay in 8051
Using simple loop
In Assembly
DELAY:
MOV R1, #255
LOOP2: MOV R2, #255
LOOP1: DJNZ R2, LOOP1
DJNZ R1, LOOP2
RET
In ‘C’
unsigned int x;
for (x=0; x<40000; x++)
{
;
}
Timer & Counter
3
Timer
If it is Programmed to count internal clock pulses, it is
said to be a “Timer”
Counter
If it is programmed to count external clock pulses,
then it is said to be a counter
T0, T1 pins (dual functions of P3 : P3.4, P3.5) are used
to supply external clock pulses to counter
8051 Timers & Counters
4
8051 has two up timers / counters T0, T1
Each timer has two 8- bit registers
TL0, TH0 Low byte and High byte of
Timer 0
TL1, TH1 Low byte and High byte of
Timer 1
Timer / counter action is controlled by two
registers each of 8- bit width
TCON Timer CONtrol register
TMOD Timer MODe control register
Timer CONtrol SFR (TCON * - 88H)
5
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer MODe control register (TMOD – 89H)
6
GATE C/T_bar M1 M0 GATE C/T_bar M1 M0
TIMER 1 TIMER 0
Timer Operation
7
Timer / Counter Control Logic
Modes of Operations
8
Mode 0
13 – bit timer/ counter mode
8 bits of TLX and 5 bits of THX are used in counting
operation
It allows cont values range from 0000H to 1FFFH to
be loaded into the timer registers TLX, THX
Modes of Operations (Contd..)
9
Mode 1
16 – bit timer/ counter
It allows values range from 0000h to FFFFH to be
loaded into the timer registers TLX, THX
Modes of Operations (Contd..)
10
Mode 2
8 – bit auto reload mode
THx holds a value which is to be reloaded into TLx
each time it overflows.
Modes of Operations (Contd..)
11
Mode 3
Split timer/
counter mode
Mode 0, 1, 2
timers can
independently
program, but not
in mode 3
Timer 0 is split
into two separate
8 bit timers as
TL0, TH0
Generating Time delay
12
16 – bit Timer/Counter
13 – bit Timer/Counter
8 – bit Timer / Counter
Steps to Program
13
To generate desired time delay in mode 1
1. Calculate the initial count to generate a time delay of
Td
2. Load TMOD to indicate timer mode and which timer
is using
3. Load TLx, THx registers with initial count.
4. Start timer (TRx)
5. Keep monitoring the timer flag (TFx). Get out of loop
if TFx is high
6. Stop timer and clear TFx flag
7. Go back to step 3 to generate the same delay again.
Problem 1
14
ASSUME T H AT X TA L = 11.0592 MHZ.
W H A T VA LU E D O W E L O A D I N T O T H E
T I M E R S I S W E WA N T T O G E N E R A T E A
S Q U A R E WAV E O F F R E Q U E N C Y 1 0 0 H Z O N
P2.3.
ALP
15
ORG 0
P2 EQU 0A0H
TMOD EQU 89H
TCON EQU 88H
TL0 EQU 8AH
TH0 EQU 8CH
MOV TMOD, #01H ;Timer 0 in Mode 1 of
operation
CLR P2.3 ;P2.3 as Output
next: MOV TL0, #00H ;Load Initial count
MOV TH0, #0EEH
CPL P2.3 ;Complement P2.3
SETB TR0 ;Start timer 0
Wait: JNB TF0, Wait ;Wait till TF0=1
CLR TR0 ;stop timer 0
CLR TF0 ;Clear timer flag0
SJMP next ;Repeat
C program
16
#include <reg51.h>
sbit wave = P2^3;
void main()
{
TMOD = 0x01;
wave = 0;
while(1)
{
TL0 = 0x00;
TH0 = 0xEE;
wave = ~wave;
TR0 = 1;
while(TF0 != 1);
TR0 = 0;
TF0 = 0;
}
Problem 2
17
G E N E R A T E A R E C T A N G U L A R WAV E W I T H
AN ON- TIME OF 3 MSEC AND AN OFF
TIME OF 10 MSEC ON ALL PINS OF PORT
0 . A S S U M E X TA L = 1 1 . 5 9 2 M H Z .