Week 05 Timers
Week 05 Timers
7
Organization
Introduction
Timers
Delay Generation
Measuring Time
Counters
8
Timers and Counters
2 Timers in 8051
T0
T1
9
Timers and Counters
Modes of operation are
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
10
Timers and Counters
Always counts up in any operational mode
Timer
Counter
Timer
Timer increments by 1 every machine cycle
Counter
Increments at every input pulse
13
Timers and Counters
Timer SFRs
14
Timers and Counters
Separate Registers – TH and TL
High and Low byte of Timer
Timer 0:
TH0 TL0
(1 Byte) (1 Byte)
TIMER 0
Timer 1:
TH1 TL1
(1 Byte) (1 Byte)
TIMER 1
15
Timers and Counters
Bit Name Explanation of Function Timer
Configuring = 1 Timer will only run when INT1 is
Timers high. (Calculate Time between
7 GATE1 1
events)
= 0 for Timer1
Not gate
17
Timers
18
Timers and Counters
Modes of operation are
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
19
Timers
Timer Operation
1. Configure timers (TMOD)
2. Initialize values (TH and TL)
3. Start and Stop Timer (TCON)
20
Timers
Using 8051 Timers TH0 TL0
(1 Byte) (1 Byte)
TH and TL contain the TIMER 0
‘value’ of timer
TH1 TL1
Timers count UP. This value (1 Byte) (1 Byte)
will be incremented by 1 every TIMER 1
machine cycle
When TH and TL have maximum value, next machine cycle would set
them both to ZERO.
Practice
What is the maximum value TH and TL (1 Byte each) can have?
21
Timers
Using 8051 Timers
TH0 TL0
(1 Byte) (1 Byte)
What is the maximum value TIMER 0
TH and TL (1 Byte each) can have?
TH TL
= FF FF = 65,535
22
Timers
Using 8051 / 8052 Timers
Maximum value of Timer TH0 TL0
(1 Byte) (1 Byte)
Registers= FF FF = 65,535 TIMER 0
Timer
Operation Timer
Value
(TH , TL)
Time
23
Timers
Timer Modes
24
Timers
Timer Modes
25
Timers
Timer Modes
65,535d
Mode 1 – 16 Bit Timer FF FFh
Commonly used
Counts up to FF FF and resets to zero
26
Timers
Timer Modes
Mode 2 – 8 Bit Timer
8 Bit Auto-Reload Mode
TH = Reload value
TL = Time Value Reset
Reload
Timer
Value
Value
Time
27
Timers
Timer Modes
28
Timers
Bit Name Explanation of Function Timer
Configuring = 1 Timer will only run when INT1 is
high. (Calculate Time between
Timers 7 GATE1
events)
1
Timer Mode
30
Bit Name Explanation of Function Timer
Timers = 1 for External Time
7 GATE1 Measurement 1
Configuring Timers = 0 for Timer 1
TMOD =1 Counter
6 C/T1 1
= 0 Timer
5 T1M1 Timer mode bit 1
Practice 4 T1M0 Timer mode bit 1
Timer 0 in Mode 1 = 1 for External Time
3 GATE0 Measurement 0
Timer 1 in Mode 2 = 0 for Timer 0
Timer Description =1 Counter
TxM1 TxM0 2 C/T0 0
Mode of Mode = 0 Timer
0 0 0 13-bit Timer. 1 T0M1 Timer mode bit 0
0 1 1 16-bit Timer 0 T0M0 Timer mode bit 0
8-bit auto-
1 0 2
reload
Split timer
1 1 3
31 mode
Timers
Configuring Timers
TMOD
Timer0 in Mode1
Timer1 in Mode3
MOV tmod,#00100001b
OR
MOV tmod,#21h
32
Timers
Controlling Timers
TCON
Bit Name Explanation of Function Timer
Timer 1 Overflow.
7 TF1 1
= 1 when T1 Overflows
Timer 1 Run.
6 TR1 = 1 Timer1 Runs 1
= 0 Timer 1 Stops
Timer 0 Overflow.
5 TF0 0
= 1 when T0 Overflows
Timer 0 Run.
4 TR0 = 1 Timer0 Runs 0
= 0 Timer 0 Stops
34
Timers
35
Timers
Using 8051 / 8052 Timers
How to run timers for a given time?
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
36
Timers
Using 8051 / 8052 Timers
How to run timers for a given time?
1. Select Timer Mode – TMOD
2. Store a Value in Timer Register – TH and TL
3. Wait for Timer to Over-flow or Reset – TCON
37
Timers
Using 8051 / 8052 Timers
How to run timers for a given time?
1. Select Timer Mode
2. Set an appropriate Value in Timer Register
38 Time
Timers
Practice
Set P3.0 high for 0.05 seconds using Timer 0
39
Timers
Set P3.0 high for 0.05 seconds using Timer 0
40
Timers
Using 8051 / 8052 Timers
Set P3.0 high for 0.05 seconds using Timer 0
TH = 4Ch ; TL = 01h
41
Timers
Using 8051 / 8052 Timers
Set P3.0 high for 0.05 seconds using Timer 0
Timer Mode
Mode 1 = 16 Bit timer Mode can be used
TMOD = 0000 0001b
T1 T0
42
Timers
Using 8051 / 8052 Timers
Set P3.0 high for 0.05 seconds using Timer 0
MOV TMOD,#00000001b;
MOV TH0,#4Ch;
MOV TL0,#01h;
setb P3^0
setb TR0
43
Timers
44
Timers
Using 8051 / 8052 Timers
How to measure time between 2 events?
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
45
Timers
How to measure time between 2 events?
46
Timers
How to measure time between 2 events?
0 1
0/1
47
Timers
How to measure time between 2 events?
0/1
48
Timers
How to measure time between 2 events?
49
Counters
50
Counters
51
Counter
Counter
Frequency consideration
52
Counter
Counter
53