Microcontroller Timers
Microcontroller Timers
Timer 0 registers
low byte register is called TL0 (Timer 0 low byte)
and the high byte register is referred to as TH0
(Timer 0 high byte)
can be accessed like any other register, such as A,
B, R0, R1, R2, etc.
"MOV TL0, #4 FH" moves the value 4FH into TL0
"MOV R5, TH0" saves TH0 (high byte of Timer 0) in
R5
PROGRAMMING 8051 TIMERS
5
Timer 1 registers
also 16 bits
split into two bytes TL1 (Timer 1 low
byte) and TH1 (Timer 1 high byte)
accessible in the same way as the
registers of Timer 0.
PROGRAMMING 8051 TIMERS
7
Mode 1 programming
16-bit timer, values of 0000 to FFFFH
TH and TL are loaded with a 16-bit initial value
timer started by "SETB TR0" for Timer 0 and "SETB TR1"
for Timer l
timer count ups until it reaches its limit of FFFFH
rolls over from FFFFH to 0000H
sets TF (timer flag)
when this timer flag is raised, can stop the timer with
"CLR TR0" or "CLR TR1“
after the timer reaches its limit and rolls over, the
registers TH and TL must be reloaded with the original
value and TF must be reset to 0
PROGRAMMING 8051 TIMERS
12
T = 1/50 Hz = 20 ms
1/2 of it for the high and low portions of
the pulse = 10 ms
10 ms / 1.085 us = 9216
65536 - 9216 = 56320 in decimal =
DC00H
TL = 00 and TH = DCH
The calculation for 12MHz crystal uses
the same steps
Example (cont)
Assuming XTAL = 11.0592 MHz, write a program to generate a
square wave of 50 Hz frequency on pin P2.3.
19
PROGRAMMING 8051 TIMERS
20
Mode 0
works like mode 1
13-bit timer instead of 16bit
13-bit counter hold values 0000 to
1FFFH
when the timer reaches its maximum
of 1FFFH, it rolls over to 0000, and TF
is set
PROGRAMMING 8051 TIMERS
24
Mode 2 programming
8-bit timer, allows values of 00 to FFH
TH is loaded with the 8-bit value
a copy is given to TL
timer is started by ,"SETB TR0" or "SETB TR1“
starts to count up by incrementing the TL register
counts up until it reaches its limit of FFH
when it rolls over from FFH to 00, it sets high TF
TL is reloaded automatically with the value in TH
To repeat, clear TF
mode 2 is an auto-reload mode
PROGRAMMING 8051 TIMERS
25
to LEDs
TCON register
TR0 and TR1 flags turn on or off the timers
bits are part of a register called TCON (timer
control)
upper four bits are used to store the TF and TR
bits of both Timer 0 and Timer 1
lower four bits are set aside for controlling the
interrupt bits
"SETB TRl" and "CLR TRl“
"SETB TCON. 6" and "CLR TCON. 6“
COUNTER PROGRAMMING
38
Table 9–2 Equivalent Instructions for the Timer Control Register (TCON)
COUNTER PROGRAMMING
39