Microprocessor and Microcontroller
Muhammad Faizan Malik
Week 5 Timers and Counters
2
Course Outline
Week Topic
1 Introduction
8051Microcontroller
2
Architecture
3 Assembly Language
4 Assembly Language Contd.
Timers and Counters
5, 6 Serial Port
Interrupt
7 Design and Interface Examples
8086 / 8088 Microprocessor
8
Introduction and Architecture
9 Midterm Exam
10 Hardware and Design
11 Programming in Assembly
PIC 18 F Microcontroller
12
Introduction, Architecture, I/O Pins
13 Programming
14 Timers
15, 16 Peripherals of PIC 18F Microcontrollers
17 Revision
3
Organization
Introduction
Timers
Delay Generation
Measuring Time
Counters
4
Timers and Counters
2 Timers in 8051
T0
T1
Timers can be used to
1. Measure time between events
2. Generate required amount of time
3. Count events
4. Generate baud rate for Serial communication
5
Timers and Counters
Modes of operation are
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
6
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
Time of 1 Machine Cycle, for 12 MHZ crystal?
1 micro second for crystal of 12 MHz
7
Timers and Counters
Time of 1 Machine Cycle for 11.0592 MHz crystal?
Oscillator (Crystal) : 11.0592 MHz
12 Clock cycles make 1 Machine cycle
1 Machine Cycle = 12 Clock cycles
Time of 1 clock cycle = 1 / 11.0592M
Time of 1 Machine Cycle = 12* (1 / 11.0592M )
1 Machine Cycle = 1.0851 Micro Seconds
8
Timers and Counters
Timer SFRs
9
Timers and Counters
Timer SFRs
SFR Timer Description
TMOD T0 and T1 Set Timer Mode
TCON T0 and T1 Control Timer Operation
TL0 T0 Timer 0 Low Byte
TH0 T0 Timer 0 High Byte
TL1 T1 Timer 1 Low Byte
TH1 T1 Timer 1 High Byte
10
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
11
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
TMOD =1 Input on P3.5 drives time. Counter
6 C/T1 Mode 1
= 0 Crystal drives Timer. Timer Mode
5 T1M1 Timer mode bit 1
4 T1M0 Timer mode bit 1
= 1 Timer will only run when INT0 is
high. (Calculate Time between
3 GATE0 0
events)
= 0 for Timer0
=1 Counter
2 C/T0 0
= 0 Timer
1 T0M1 Timer mode bit 0
12
0 T0M0 Timer mode bit 0
Timers and Counters
13
Timers
14
Timers and Counters
Modes of operation are
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
15
Timers
Timer Operation
1. Configure timers (TMOD)
2. Initialize values (TH and TL)
3. Start and Stop Timer (TCON)
16
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?
17
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?
Maximum value = 1111 111 1111 1111
TH TL
= FF FF = 65,535
18
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
19
Timers
Timer Modes
Mode 0 13 Bit Timer
Mode 1 16 Bit Timer
Mode 2 8 Bit Timer
Mode 3 Split Timer
20
Timers
Timer Modes
Mode 0 13 Bit Timer
To keep compatibility with 8048 (old)
TL counts 0 to 31
From 31, TL resets to ZERO and increments TH by 1
Not used generally
21
Timers
Timer Modes
65,535d
Mode 1 16 Bit Timer FF FFh
Commonly used
Counts up to FF FF and resets to zero
Working: as discussed previously
Timer
Value
(TH , TL)
22
Timers
Timer Modes
Mode 2 8 Bit Timer
8 Bit Auto-Reload Mode
TH = Reload value
TL = Time Value Reset
Reload
Timer
Value
Value
23 Time
Timers
Timer Modes
Mode 3 Split Timer
When Timer 0 is placed in mode 3, it becomes 2 x 8 bit Timers
Timer 0 is TL0
Timer 1 is TH0
Bits of Timer1 are related to TH0
Timer 1 can be in mode 0,1 or 2.
Timer 1 will be incremented every machine cycle in any case no
control
24
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
TMOD = 0 for Timer1
=1 Input on P3.5 drives time. Counter
6 C/T1 Mode 1
= 0 Crystal drives Timer. Timer Mode
5 T1M1 Timer mode bit 1
4 T1M0 Timer mode bit 1
= 1 Timer will only run when INT0 is
high. (Calculate Time between
3 GATE0 0
events)
= 0 for Timer0
=1 Counter
2 C/T0 0
= 0 Timer
1 T0M1 Timer mode bit 0
25 0 T0M0 Timer mode bit 0
Timers
Configuring Timers
TMOD
Timer Mode
TxM1 TxM0 Timer Mode Description of Mode
0 0 0 13-bit Timer.
0 1 1 16-bit Timer
1 0 2 8-bit auto-reload
1 1 3 Split timer mode
26
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
27 mode
Timers
Configuring Timers
TMOD
Timer0 in Mode1
Timer1 in Mode3
MOV tmod,#00100001b
OR
MOV tmod,#21h
28
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
Lower Bits are related to Interrupts and will be covered later on
29
Timers
Controlling Timers
TCON
setb tr0 ;running the timer
Label: JNB tf0, Label ; waiting till overflow flag is set
30
Timers
Using 8051 / 8052 Timers
How to run timers for a given time?
How to measure time between 2 events?
31
Timers
Using 8051 / 8052 Timers
How to run timers for a given time?
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
32
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
33
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
Timer Value = 65535 (Desired
Number of Machine Cycle)
Value Reset
Timer
Value
34 Time
Timers
Practice
Set P3.0 high for 0.05 seconds using Timer 0
35
Timers
Set P3.0 high for 0.05 seconds using Timer 0
Finding Value of Timer
0.05 sec = 50,000 micro seconds
1.0851 micro sec = 1 Machine Cycle
1 micro sec = (1 / 1.0851) Machine Cycles
50,000 micro sec = 50,000 * (1/1.0851)
= 46079 Machine Cycles
36
Timers
Using 8051 / 8052 Timers
Set P3.0 high for 0.05 seconds using Timer 0
Value to load in TH and TL?
Timer Value = 65535 (Desired Number of Pulses)
= 65535 46079
= 19456 = 4C 00h
TH = 4Ch ; TL = 00h
37
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
38
Timers
Using 8051 / 8052 Timers
Set P3.0 high for 0.05 seconds using Timer 0
MOV TMOD,#00000001b;
MOV TH0,#4Ch;
MOV TL0,#00h;
setb P3^0
setb TR0
Here: JNB TF0, Here
CLR P3^0
39
Timers
How to measure time between 2 events?
40
Timers
Using 8051 / 8052 Timers
How to measure time between 2 events?
2 Timers
Serial
Timers Counters
Port
Measure Generate
Time Time
41
Timers
How to measure time between 2 events?
Set GATE Bit in TMOD
42
Timers
How to measure time between 2 events?
43
Timers
How to measure time between 2 events?
44
Timers
How to measure time between 2 events?
Set GATE Bit in TMOD
C / T = 0 to ensure that crystal is connected to timers
When TR = 1, timer will run ONLY WHEN P3.2or P3.3 is set
So, timer value be corresponding to the time P3.2 or P3.3
remains high
45
Counters
46
Counters
47
Counter
Counter
If TCON Bit C/T = 1
Timer runs as counter
Timer 0 Pin 3.4 (T0)
Timer 1 Pin 3.5 (T1)
Events (pulses) on these pins will be counted
Frequency consideration
48
Counter
Counter
Value of Timer is incremented by 1 when there is 1-to-0
(High to Low) transition on the pin
For counter to count, there must be 1 during one
machine cycle and 0 during next machine cycle
Maximum frequency: 2 machine cycles = 2 micro sec (for
12 MHz crystal)
Frequency = 500 kHz
49
Acknowledgement
Material used with permission of
Dr. Javaid Iqbal
Head of Department, Mechatronics Engineering
College of EME, NUST
I am extremely thankful to Dr. Javaid who has been a great
teacher and still helps and supports me
50