Professional Documents
Culture Documents
8051 Timers
8051 Timers
8051 Timers
Generating Delays
8051 Timers
Timer 0
Timer 1
Timer registers
Timer Registers
The lower 4 bits are for Timer 0, The upper 4 bits are
for Timer 1
In each case, The lower 2 bits are used to set the timer
mode, The upper 2 bits to specify the operation.
TMOD Register
TMOD Register
Bit 7: Gate bit; when set, timer only runs while INT high (T0)
Bit 6: Counter/timer select bit; when set timer is an event counter when
cleared timer is an interval timer (T0)
Bit 3: Gate bit; when set, timer only runs While INT high (T1)
Bit 2: Counter/timer select bit; when set timer is an event counter when
cleared timer is an interval timer (T1)
TMOD Register
configuration
TMOD Register
TMOD Register
TCON Register
TCON Register
TCON Register
;starting timers
CLR TR0
CLR TR1
; stop timer 0
; stop timer 1
; reading timers
17
Mode 1 programming
This is done by SETB TR0 for timer 0 and SETB TR1 for
timer 1
3. (cont)
When it rolls over from FFFFH to 0000, it sets high a flag bit
called TF (timer flag)
Each timer has its own timer flag: TF0 for timer 0, and
TF1 for timer 1
This timer flag can be monitored
When this timer flag is raised, one option would be to stop
the timer with the instructions CLR TR0 or CLR TR1, for
timer 0 and timer 1, respectively
4. After the timer reaches its limit and rolls over, in order to
repeat the process
TF must be reloaded to 0
Mode 1 programming
Example 1
HR:MOV TL0,#0F2H
MOV TH0,#0FFH
CPL P1.5
;Toggle P1.5
ACALL DELAY
SJMP HR
[source: page no: 207 from Mazidi & Mazidi Text book]
Example 1 (cont..)
Delay:
AN:
SETB TR0
;Start Timer 0
JNB TF0,AN
;monitor Timer 0
;flag until it
;rolls over
CLR TR0
;Stop timer 0
CLR TF0
RET
1. TMOD is loaded.
3. P1.5 is toggled for the high and low portions of the pulse.
Example 2
[source: page no: 207 from Mazidi & Mazidi Text book]
Example 3
HERE:
MOV TL0,#0F2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
DELAY: SETB TR0
AGAIN: JNB TF0,AGAIN
CLR TR0
CLR TF0
RET
2
2
1
2
2
1
14
1
1
2
[source: page no: 207 from Mazidi & Mazidi Text book]
(b) In decimal
Example 4
AGAIN:
CLR P1.3
MOV TMOD,#10H
MOV TH1,#0FFH
MOV TL1,#00H
SETB P1.3
SETB TCON.6
JNB TCON.7, AGAIN
CLR TCON.6
CLR TCON.7
CLR P1.3
Example 5
BACK:
MOV TMOD,#00H
MOV TL1,#00H
MOV TH1,#0FH
SETB TR1
JNB TF1, BACK
CLR TR1
CPL P0.6
CLR TF1
SJMP AGAIN
Assuming XTAL=11.0592MHz
Example 6
Example 6
CLR P2.3
MOV TMOD,#01
HERE: MOV TL0,#0
MOV TH0,#0EEH
SETB P2.3
SETB TR0
AGAIN:JNB TF0,AGAIN
CLR P2.3
CLR TR0
CLR TF0
SJMP HERE
Sol:
Example 7
Example 7
Example 7
Sol(cont..)
MOV TMOD,#10H
BACK: MOV TL1,#0F7H
MOV TH1,#0FFH
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TF1
CPL P2.3
CLR TF1
SJMP BACK
Example 8
Example 8
Sol:
Example 8
Sol(cont..):
MOV TMOD,#01H
BACK: MOV TL0,#75H
MOV TH0,#0B8H
MOV P0,#00H
ACALL DELAY
MOV TL0,#8AH
MOV TH0,#0EAH
MOV P0,#0FFH
ACALL DELAY
SJMP BACK
Example 8
Sol(cont..):
DELAY: SETB TR0
AGAIN: JNB TF0,AGAIN
CLR TR0
CLR TF0
RET
Sol:
MOV TMOD,#10H
REPT: MOV R0,#28
CPL P2.4
BACK: MOV TL1,#00H
MOV TH1,#00H
SETB TR1
AGAIN:JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R0,BACK
SJMP REPT
Mode 2 programming
2. After TH is loaded with the 8-bit value, the 8051 gives a copy
of it to TL
Mode 2 programming
Mode 2 programming
Example 9
Example 9
Q(cont..):
MOV TMOD,#20H
MOV TH1, #5
SETB TR1
BACK: JNB TF1,BACK
CPL P1.0
CLR TF1
SJMP BACK
Example 9
Sol:
Counter programming
Counter programming
Counter programming
Example 10
Example 10
Sol:
RPT:
AGAIN:
BACK:
MOV TMOD,#15H
SETB P3.4
MOV TL0,#00
MOV TH0,#00
SETB TR0
MOV R0,#28
MOV TL1,#00
MOV TH1,#00
SETB TR1
JNB TF1, BACK
CLR TF1
CLR TR1
DJNZ R0, AGAIN
MOV A,TL0
MOV P2,A
MOV A,TH0
MOV P1,A
SJMP RPT
Example 11
Example 11
Sol:
AGAIN:
BACK:
MOV TMOD,#00000110B
MOV TH0,#00
SETB P3.4
SETB TR0
MOV A,TL0
ACALL DISPLAY
JNB TF0,BACK
CLR TR0
CLR TF0
SJMP AGAIN
References