Microcontroller Timers
Microcontroller Timers
Registers Used in
Timer/Counter
Timer0: TH0 = MEM[8CH] and TL0 = MEM[8AH]
TH0
0
TL0
0
MEM[8CH] = 11H
and
MEM[8AH] = 4FH
(MSB)
TF1 TR1
Timer 1
TF0
TR0
Timer0
IE1
IT1
IE0
(LSB)
IT0
(MSB)
GATE
M0
GATE
C/T
M1
Timer 0
(LSB)
M0
GATE :
C/T
Mode
0
Function
13-bit timer mode TLx = 5bits THx = 8bits
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
TH0
TR
TF0
TL0
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
4) Start Timer
SETB TR0 OR SETB TCON.4
6) Stop timer
CLR TR0 OR CLR TCON.4
8) Go Back to step 2
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
12
TL1
TF1
reload
TR1
TH1
TF goes high
when FF
0
overflow
flag
2)
3)
4)
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