Embedded System Weeks 10
Embedded System Weeks 10
Chapter 15
General-purpose Timers
2
Timer
Free-run counter (independent of processor)
Functions
Input capture
Output compare
Pulse-width modulation (PWM) generation
One-pulse mode output
3
Timer: Clock
Reload
fCL_PSC fCL_CNT
Timer
PSC ISR
Counter
clock Prescaler Interrupt
𝑓𝑓𝐶𝐶𝐶𝐶_𝑃𝑃𝑃𝑃𝑃𝑃
𝑓𝑓𝐶𝐶𝐶𝐶_𝐶𝐶𝐶𝐶𝐶𝐶 =
𝑃𝑃𝑃𝑃𝑃𝑃 + 1
4
Timer: Output
Reload
fCL_PSC fCL_CNT
Timer
PSC ISR
Counter
clock Prescaler Interrupt
Timer Output
= (OCREF)
Compare &
Capture
Register (CCR)
5
Timer: Input Capture
Reload
fCL_PSC fCL_CNT
Timer
PSC ISR
Counter
clock Prescaler Interrupt
Compare &
Capture
Register (CCR)
6
Multi-Channel Outputs
7
Output Compare
9
Edge-aligned Mode (Up-counting)
ARR = 6, RCR = 0
clock
6 6 6 6
5 Counter
5 Counter
5 Counter
5
4 overflow 4 overflow 4 overflow 4
3 3 3 3
counter 2 2 2 2
1 1 1 1
0 0 0 0
Counter overflow
Update event (UEV)
10
Edge-aligned Mode (down-counting)
ARR = 6, RCR = 0
Clock
6 6 6 6
5 5 5 5
4 4 4 4
3 3 3 3
Counter 2 Counter 2 Counter 2 Counter 2
1 underflow 1 underflow 1 underflow 1
0 0 0 0
Counter underflow
Update event (UEV)
11
Center-aligned Mode
ARR = 6, RCR = 0
Clock
6 6
5 5 5 5
4 4 4 4
3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0
Counter Counter Counter Counter
overflow underflow overflow underflow
12
Mode 1
PWM Mode 1 Timer Output =
High if counter < CCR
Low if counter ≥ CCR
(Low-True)
Upcounting mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6 6 6
5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0 0
OCREF
CCR
Duty Cycle = Period = (1 + ARR) * Clock Period
ARR + 1
= 7 * Clock Period
3
=
7
13
Mode 2
PWM Mode 2 Timer Output =
Low if counter < CCR
High if counter ≥ CCR
(High-True)
Upcounting mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6 6 6
5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0 0
OCREF
CCR
Duty Cycle = 1 - Period = (1 + ARR) * Clock Period
ARR + 1
= 7 * Clock Period
4
=
7
14
Mode 2
PWM Mode 2 Timer Output =
Low if counter < CCR
High if counter ≥ CCR
(High-True)
Upcounting mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6 6 6
CCR = 5 5 5 5 5
4 4 4 4
3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0 0
OCREF
CCR
Duty Cycle = 1 - Period = (1 + ARR) * Clock Period
ARR + 1
= 7 * Clock Period
2
=
7
15
Mode 2
PWM Mode 2 Timer Output =
Low if counter < CCR
High if counter ≥ CCR
(High-True)
Center-aligned mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6
5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0
OCREF
CCR
Duty Cycle = 1 - Period = 2 * ARR * Clock Period
ARR
= 12 * Clock Period
1
=
2
16
Mode 2
PWM Mode 2 Timer Output =
Low if counter < CCR
High if counter ≥ CCR
(High-True)
Center-aligned mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6
5 5 5 5
4 4 4 4
3 3 3 3
Counter 2 2 2 2
1 1 1 1
CCR = 1 0 0 0
OCREF
CCR
Duty Cycle = 1 - Period = 2 * ARR * Clock Period
ARR
= 12 * Clock Period
5
=
6
17
Auto-Reload Register (ARR)
18
Repetition Counter Register (PCR)
19
Repetition Counter Register (PCR)
20
Repetition Counter Register (PCR)
21
Repetition Counter Register (PCR)
22
Repetition Counter Register (PCR)
23
PWM Output Polarity
Counter ≥
Mode Counter < CCR
CCR
PWM mode 1
Active Inactive
(Low True)
PWM mode 2
Inactive Active
(High True)
Output Polarity:
• Software can program the CCxP bit in the TIMx_CCER register
Active Inactive
Active High High Voltage Low Voltage
Active Low Low Voltage High Voltage
24
Counting up, down, center
25
Up-Counting: Left Edge-aligned
Upcounting mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6 6 6
CCR = 6 5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0 0
CCR = 3
OC1REF
CCR = 6
OC2REF
Left-aligned
All rising edges occur at the same time! PWM Period
26
PWM Mode 2: Right Edge-aligned
Upcounting mode, ARR = 6, CCR = 3, RCR = 0
Clock
6 6 6 6
CCR = 5 5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2 2 2
1 1 1 1
0 0 0 0
OC1REF CCR = 3
OC2REF CCR = 5
Right-aligned
All falling edges occur at the same time!
PWM Period
27
PWM Mode 2: Center Aligned Low if counter < CCR
Timer Output =
High if counter ≥ CCR
Clock
6 6
5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2 2 2
1 1 1 1
CCR = 1 0 0 0
CCR = 3
OC1REF
CCR = 1
OC2REF
Center-aligned
PWM signals are center aligned!
PWM Period
28
The devil is in the detail
29
Input Capture
Monitor both rising and falling edge
30
Input Capture
Monitor only rising edges or only falling edge
31
Input Capture
32
Input Filtering
33
Input Capture Diagram
34
Ultrasonic Distance Sensor
35
Ultrasonic Distance Sensor
The echo pulse width corresponds to
round-trip time.
36
Ultrasonic Distance Sensor
37
THANK
YOU