8051 Timers
Timers /Counters Programming
The 8051 has 2 timers/counters:
- timer 0/counter 0
- timer 1/counter 1
The timer can be used as a time delay generator or setting the
speed of serial communication transmission (baud rate).
- The clock source is the internal crystal frequency of the
8051 (12 MHz)
The counter is used to count pulses (events).
- External input: P3.4/T0 (pin 14) for counter0 and P3.5
(pin 15)/T1 for counter1 can be used to count the number
of events on registers.
- These pulses could represent the number of people passing
through an entrance.
Registers Used in
Timer/Counter
Timer0: TH0 = MEM[8CH] and TL0 = MEM[8AH]
Timer1: TH1 = MEM[8D] and TL1 = MEM[8B]
TMOD (Timer Mode register) = MEM[89H]
TCON (Timer Control register)
Timer Register THx and TLx
THx and TLx are used to load the timer with an initial value
that will determine the time delay.
THx and TLx are initialized using a MOV instruction
MOV TL0 , #4FH
MOV TH0 , #11H
the above instructions will
TH0
0
TL0
0
MEM[8CH] = 11H
and
MEM[8AH] = 4FH
Timer Control Register
TCON
(MSB)
TF1 TR1
Timer 1
TF0
TR0
Timer0
IE1
IT1
IE0
(LSB)
IT0
for Interrupt (discuused later)
Upper nibble for timer/counter, lower nibble for interrupts.
TR (Timer Run start control bit)
- TR is set by programmer to start/stop timer.
TR=0: stop (off)
TR=1: start (on)
TF (Timer Flag bit)
- TF indicates time expiry.
TF=0 : time is not expired
TF=1: time expired
(MSB)
GATE
Timer Mode Register
TMOD
C/T
M1
Timer 1
M0
GATE
C/T
M1
Timer 0
(LSB)
M0
GATE :
0 = timer enabled with TR only
1 = timer enables with TR and INT
C/T
Timer or counter selection
0 = Timer operation (input from internal system clock).
1 = Counter operation (input from T0 or T1 input pin).
M1, M0 Set the timer mode
M1 and M2 (Timer Mode)
M1 M0
0 0
Mode
0
Function
13-bit timer mode TLx = 5bits THx = 8bits
16-bit timer mode
8-bit reload, THx holds a value which is to be
reloaded into TLx each time it overflows (used
for serial communications)
Split timer
Timer Mode 1
TH0-TL0 is incremented continuously when TR0 is set to 1..
The timer works with the internal system clock. In other
words, the timer counts up each clock pulse.
When the timer (TH0-TL0) reaches its maximum of FFFFH, it
rolls over to 0000, and TF0 is set.
XTAL
Oscillator
MHZ 12
12
Timer clock frequency
1 MHZ
TH0
TR
TF0
TL0
TF0 goes high
when FFFF
overflow
flag
Steps of Mode 1
The 8051 starts to count up by incrementing the TH0-TL0.
- TH0&TL0=
FFFCH , FFFDH , FFFEH , FFFFH , 0000H
TR0=1
Start timer
TH0
TR0=0
TL0
Stop timer
FFFC
FFFD
FFFE
FFFF
0000
TF = 0
TF = 0
TF = 0
TF = 0
TF = 1
TF
Monitor TF until TF=1
Steps to program in Mode 1
1) Load TMOD (specifies which timer and what mode)
MOV TMOD , #01H
2) Load THx and TLx with initial count
MOV TH0 , #0FFH
MOV TL0 , #0FCH
4) Start Timer
SETB TR0 OR SETB TCON.4
5) Keep monitoring Timer Flag for time expiration
NOT_DONE: JNB TF0 , NOT_DONE
6) Stop timer
CLR TR0 OR CLR TCON.4
7) Clear Timer Flag
CLR TF0 OR CLR TCON.5
8) Go Back to step 2
Timer Delay Calculation for Clock = 12 MHz
(a) in hex
(FFFF YYXX + 1) 1 s where YYXX are TH, TL initial
values respectively. Notice that values YYXX are in hex.
(b) in decimal
Convert YYXX values of the TH, TL register to decimal to get
a NNNN decimal number, then (65536 NNNNN) 1 s
Example:
Convert FFFC H to decimal = 6553210
(6553610 6553210) 1 s = 4 s
Example
again:
timer_delay:
monitor:
ORG
100H
MOV
MOV
MOV
CPL
ACALL
SJMP
TMOD , # 01
TL0 , #0F2H
TH0 , #0FFH
P1.5
timer_delay
again
SETB
JNB
CLR
CLR
RET
TR0
TF0 , monitor
TR0
TF0
Timer Mode 0
Mode 0 is exactly like mode 1 except that it is a 13-bit timer
instead of 16-bit.
The counter can hold values between 0000 to 1FFF in TH0TL0.
We set the initial values TH0-TL0 to count up.
When the timer reaches its maximum of 1FFFH, it rolls over
to 0000, and TF0 is raised.
Timer Mode 2
XTAL
oscillator
Timer clock frequency
1 MHZ
12
TL1
TF1
reload
TR1
TH1
TF goes high
when FF
0
overflow
flag
Steps to program in Mode 2
1)
Load TMOD (specifies which timer and what mode)
2)
Load THx with initial count
3)
Start Timer (set TRx)
4)
Keep monitoring the timer flag (TF)
5)
Clear TF
7)
Go Back to step 4
Example
BACK:
ORG
100H
MOV
MOV
SETB
JNB
CPL
CLR
SJMP
TMOD , # 20H
TH1 , # 5
TR1
TF1 , BACK
P1.0
TF1
BACK