8051 TIMER OPERATION
1
a series of divide-by-2 flip-flops
each stage (n) divides clock
frequency by 2
counts to 2n
16-bit timer : 65536
2
applications
interval timing (regular intervals)
pulse-width measurements (elapsed time
between two conditions)
event counting (number of occurences of an
event )
3
applications
interval timing (regular intervals)
pulse-width measurements (elapsed time
between two conditions)
event counting (number of occurences of an
event )
event
any external stimulus that provides a 1-to-0
transition to a pin on the 8051
4
8051 timer access SFRs
5
Timer MODe register
6
Timer CONtrol register
7
Timer CONtrol register
detects and initiates
external interrupts
8
timer modes and the overflow flag
13-bit timer mode
16-bit timer mode
8-bit auto-reload mode
split timer mode
9
13-bit timer mode (Mode 0)
TLx uses bits {0-4} of TLx
TLx counts to 31 and resets to 0 with the
next count
then THx is incremented by 1
the main idea is maintaining compatibility
with 8048
counts to 8192 machine cycles before
overflow
10
16-bit timer mode (Mode 1)
TLx counts to 255 and resets to 0 with the
next count
then THx is incremented by 1
counts to 65536 machine cycles before
overflow
very common mode
11
8-bit auto-reload mode (Mode 2)
TLx counts 255 and resets to the value in
THx with the next count
very commonly used for establishing a baud
rate for serial communication
12
split timer mode (Mode 3)
when timer 0 in “mode 3” it becomes two separate 8-bit timers (TL0
and TH0)
T0 controls TL0 and T1 controls TH0
both timers count from 0 to 255 and overflow back to 0
all the bits that are related to timer 1 will now be tied at TH0
timer 1 is stopped in mode 3, but when you change the mode, it starts
you cannot control timer 1 because control bits are linked to TH0
timer 1 is incremented in every machine cycle no matter what
when timer 1 mode is set to 3, timer 1 stops
might be interesting to use when you need 2 timers and a baud rate
generator at the same time; timer 1 can be used by the serial port as a
baud rate generator, and TH0 and TL0 as two separate timers
timer 1 can be used in any way not requiring interrupts
13
clocking sources
14
interval timing
1 machine cycle = 12 crystal pulses Jean-Maurice Emile BAUDOT
( 1845 – 1903 )
common crystal frequencies: French telegraph engineer
o 12 MHz (common)
o 11,059 MHz (even more common)
11,0592 MHz for generating baud rates in serial communication
baud (Bd) = symbols (of bits) per second (BAUDot code)
(bps) = bits per second
8051 can execute 921600 single-cycle instructions per second
15
event counting
counts with 1-to-0 transitions on specified source
16
event counting
counts with 1-to-0 transitions on specified source
external input
sampled
17
event counting
counts with 1-to-0 transitions on specified source
new value external input
updated sampled
18
event counting
counts with 1-to-0 transitions on specified source
new value external input
updated sampled
it takes two machine cycles to recognize 1-to-0 transition
maximum external frequency = 500 kHz for 12MHz crystal
19
starting and stopping the timers
SETB TR0 ; start Timer0
CLR TR0 ; stop Timer0
20
starting and stopping the timers
SETB TR0 ; start Timer0 TRx is cleared
after system
CLR TR0 ; stop Timer0 reset
21
starting and stopping the timers (cont’d)
22
starting and stopping the timers (cont’d)
pulse-width
measurement
23
initializing and accessing timer registers
MOV TMOD, #00010000B ; sets the mode for operation
MOV TL1, #9CH
MOV TH1, #0FFH ; for 100 sec interval
SETB TR1 ; start Timer 1
WAIT: JNB TF1, WAIT ; wait loop
CLR TR1 ; clear flags
CLR TF1 ; after overflow
24
reading a timer “on the fly”
read low-byte .. low-byte overflow .. read high-byte
25
reading a timer “on the fly”
read low-byte .. low-byte overflow .. read high-byte
phase error
26
reading a timer “on the fly”
read low-byte .. low-byte overflow .. read high-byte
phase error
AGAIN: MOV A, TH1
MOV R6, TL1
CJNE A, TH1, AGAIN
MOV R7, A
27
short intervals and long intervals
assume
12 MHz
crystal
28
short intervals and long intervals
assume
example:
12 MHz
pulse wave
generation
crystal
29
short intervals and long intervals
assume
example:
12 MHz
pulse wave
generation
crystal
LOOP: SETB P1.0 ; 1 second
CLR P1.0 ; 1 second
SJMP LOOP ; 2 second
30
short intervals and long intervals
assume
example:
12 MHz
pulse wave
generation
crystal
LOOP: SETB P1.0 ; 1 second
CLR P1.0 ; 1 second
SJMP LOOP ; 2 second
31
short intervals and long intervals
assume
example:
12 MHz
pulse wave
generation
crystal
LOOP: SETB P1.0 ; 1 second
CLR P1.0 ; 1 second
SJMP LOOP ; 2 second
pulse 25% duty cycle
train
250 kHz (ideal)
32
example: 10 kHz square wave
33
example: 10 kHz square wave
MOV TMOD, #02H ; 8-bit auto-reload mode
MOV TH0, #-50 ; -50 reload value in TH0
SETB TR0 ; start timer
LOOP: JNB TF0, LOOP ; wait for overflow
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
34
example: 10 kHz square wave
decimal notation – assembler
performs necessary conversion
MOV TMOD, #02H ; 8-bit auto-reload mode
MOV TH0, #-50 ; -50 reload value in TH0
SETB TR0 ; start timer
LOOP: JNB TF0, LOOP ; wait for overflow
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
35
example: 10 kHz square wave
decimal notation – assembler
performs necessary conversion
MOV TMOD, #02H ; 8-bit auto-reload mode
MOV TH0, #-50 ; -50 reload value in TH0
SETB TR0 ; start timer
LOOP: JNB TF0, LOOP ; wait for overflow
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
explicity clear this
36
example: 1 kHz square wave
37
example: 1 kHz square wave
MOV TMOD, #01H ; 16-bit timer mode
LOOP: MOV TH0, #0FEH ; -500 (high-byte)
MOV TL0, #0CH ; -500 (low-byte)
SETB TR0 ; start timer
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TR0 ; stop timer
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
38
example: 1 kHz square wave
MOV TMOD, #01H ; 16-bit timer mode
LOOP: MOV TH0, #0FEH ; -500 (high-byte)
MOV TL0, #0CH ; -500 (low-byte) slight error
SETB TR0 ; start timer in frequency
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TR0 ; stop timer
510? CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
39
example: 1 kHz square wave
MOV TMOD, #01H ; 16-bit timer mode
LOOP: MOV TH0, #0FEH ; -500 (high-byte)
MOV TL0, #0CH ; -500 (low-byte) slight error
SETB TR0 ; start timer in frequency
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TR0 ; stop timer
510? CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
cascading timer0 and timer1 through sofware for
intervals longer than 66 miliseconds (both timers tied)
40
example: 1 kHz square wave
MOV TMOD, #01H ; 16-bit timer mode
LOOP: MOV TH0, #0FEH ; -500 (high-byte)
MOV TL0, #0CH ; -500 (low-byte) slight error
SETB TR0 ; start timer in frequency
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TR0 ; stop timer
510? CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
cascading timer0 and timer1 through sofware for
intervals longer than 66 miliseconds (both timers tied)
one timer in 16-bit mode and sofware counts oveflows
41
example: buzzer interface
42
example: buzzer interface
HUNDRED EQU 100 ; 100 X 10000 microseconds = 1 second
COUNT EQU -10000
MOV TMOD, 01H ; timer 0 in mode 1
LOOP: JNB P1.6, LOOP ; wait for input = 1
WAIT: JB P1.6, WAIT ; wait for input = 0
SETB P1.7 ; turn buzzer on
CALL DELAY ; wait for 1 second
CLR P1.7 ; turn buzzer off
SJMP LOOP
43
example: buzzer interface
HUNDRED EQU 100 ; 100 X 10000 microseconds = 1 second
COUNT EQU -10000
MOV TMOD, 01H ; timer 0 in mode 1
LOOP: JNB P1.6, LOOP ; wait for input = 1
WAIT: JB P1.6, WAIT ; wait for input = 0
SETB P1.7 ; turn buzzer on
CALL DELAY ; wait for 1 second
CLR P1.7 ; turn buzzer off 1.001103?
SJMP LOOP
DELAY: MOV R7, #HUNDRED
AGAIN: MOV TH0, #HIGH COUNT
MOV TL0, #LOW COUNT
SETB TR0
WAIT2: JNB TF0, WAIT2
CLR TF0
CLR TR0
DJNZ R7, AGAIN
RET
44
summary
• timer modes
• interval timing and event counting
• controlling timers
45
references
46