Professional Documents
Culture Documents
EEE415 Week10 Timer
EEE415 Week10 Timer
Week Plan
• Week 09: (3 lecture) GPIO
Timer
• Free-run counter (independent of processor)
• A timer is a hardware component in ARM Cortex-M microprocessor. Each
timer has a special register, which is called the timer counter. The counter
runs freely.
• It is hardware, not software, which keeps repeatedly incrementing or
decrementing the counter value.
• Functions
• Input capture
• Output compare
• Pulse-width modulation (PWM) generation
• One-pulse mode output
Glossary of Registers
• CR – Control Register
• CCMR – Capture/Compare Mode Register
• CCER – Capture/Compare Enable Register
• ARR – Auto Reload Register
• CNT - Counter
• PSC - Prescalar
• RCR – Repetition Counter Register
• CCR – Capture/Compare Register
fCL_PSC TIMx_SR
fCL_CNT event
Timer
PSC UIF interrupt
Counter
&
clock Prescaler
Counter is UIE
incremented/decremented once TIMx_DIER
per cycle.
Timer: Clock
Auto-Reload
Register (ARR)
Reload trigger
16 𝑀𝐻𝑧
𝑓 _ = = 1 𝐾𝐻𝑧
15999 + 1
11
Up-counting
ARR = 6,
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
13
Center-aligned Mode
ARR = 6
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
Timer: Output
Auto-Reload
Register (ARR)
Reload trigger
Timer Output
=
(OCREF)
Compare &
Capture
Register (CCR)
Dr. Sajid Muhaimin Choudhury 15
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
15
• Note that, when the function of a timer is output, the CCR register
is used only for compare.
17
fCL_PSC TIMx_SR
fCL_CNT event
Timer
PSC UIF interrupt
Counter
&
clock Prescaler UIE
TIMx_DIER
Compare &
Capture
Register (CCR)
Dr. Sajid Muhaimin Choudhury 18
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
9
18
DRAFT 8/14/2023
Multi-Channel Outputs
19
Multi-Channel Outputs
• Each timer has four channels.
• Each channel has its own Compare and Capture Register.
CCR1, CCR2, CCR3, CCR4.
• These four channels share the timer counter, and the auto-
reload register (ARR).
• Therefore, If we use these to generate PWM outputs, they
will have exactly the same period. However, their duty
cycle can be different, because the value of these CCR
registers can differ from each other. [details later]
Dr. Sajid Muhaimin Choudhury 20
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
10
20
DRAFT 8/14/2023
Output Compare
21
23
25
CCER
OC1N polarity
OC1N enable
OC1 polarity
OC1 enable
27
29
• Suppose the system clock is 80 MHz and it is selected as the timer clock that
drives the timer 1.
• Lets Slow down the clock of the timer counter to 2 KHz by a prescaler
• To turn an LED on for 1 second and then off for 1 second repeatedly, counting
period = 2s. Fclock_CNT = 2KHz. So from formula, ARR = 1999.
31
33
Flow chart
• Enable GPIOE clock
Flow chart
• Enable TIM1 clock
35
Flow chart
• Main Output Enable (MOE) in BDTR
reg
37
Counting Direction
Enable Timer
39
Counting Direction
Enable Timer
If RCR (Repition counter register) = N, then UEV event will occur every N+1 overflow (for up
counting), or underflow (for down counting), or overflow & underflow (center aligned)
Dr. Sajid Muhaimin Choudhury 41
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
41
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)
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)
43
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
45
LED_Pin Initialization
47
49
Octave
• Our ears are trained to detect change in pitch. Usually we can
detect the difference between the frequency between two sounds.
• The pitch difference is perceived as ratio of the pitch
51
Scale
• For western musical scale, the interval between 2
frequencies of an octave is divided into 12 logarithmically
spaced frequency.
• Each frequency represents a note
• Standard practice is to take note A4 = 440Hz as reference
and calculate other notes. 4 represents the 4th octave. A3 =
220Hz, A5 = 880Hz
220 440
*2(2/12)
53
55
PWM
• Pulse width modulation (PWM) is a simple digital technique to
control the value of an analog variable.
• PWM uses a rectangular waveform to quickly switch a voltage
source on and off to produce a desired average voltage output.
• The output is binary at any time instant, the average output
over a time span can be any value between 0 and the
maximum voltage.
• Specifically, the percentage of time in the on state within one
period is proportional to the mean value of the voltage output.
Consequently, when software changes the duration of the on
state, the output voltage is adjusted accordingly to emulate an
analog signal.
57
59
PWM Mode
PWM mode 1
Active Inactive
(Low True)
PWM mode 2
Inactive Active
(High True)
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
Dr. Sajid Muhaimin Choudhury 61
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
61
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
Dr. Sajid Muhaimin Choudhury 62
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
31
62
DRAFT 8/14/2023
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
Dr. Sajid Muhaimin Choudhury 63
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
63
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
Dr. Sajid Muhaimin Choudhury 64
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
32
64
DRAFT 8/14/2023
OCREF
CCR
Duty Cycle = 1 - Period = 2 * ARR * Clock Period
ARR
= 12 * Clock Period
5
=
6
Dr. Sajid Muhaimin Choudhury 65
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
65
Counting Direction
Enable Timer
Dr. Sajid Muhaimin Choudhury 66
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
33
66
DRAFT 8/14/2023
67
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
69
Clock
6 6 6 6
CCR = 6 5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2
1
2
1
2
1 1
0 0 0 0
CCR = 3
OC1REF
CCR = 6
OC2REF
Left-aligned
All rising edges occur at the same time! PWM Period
the up-counting PWM mode 1, when multiple PWM signals are generated by the same timer, all rising
edges occur at the same time at left clk edge. That is why the up-counting mode is left edge-aligned.
Dr. Sajid Muhaimin Choudhury 70
EEE 415 - Department of EEE, BUET Yifeng Zhu Embedded Systems with ARM Cortex-M Microcontrollers © 2015
35
70
DRAFT 8/14/2023
Clock
6 6 6 6
CCR = 5 5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2
1
2
1
2
1 1
0 0 0 0
OC1REF CCR = 3
OC2REF CCR = 5
Right-aligned
All falling edges occur at the same time!
PWM Period
71
Clock
6 6
5 5 5 5
4 4 4 4
CCR = 3 3 3 3 3
Counter 2 2
1
2 2
1
1 1
CCR = 1 0 0 0
CCR = 3
OC1REF
CCR = 1
OC2REF
Center-aligned
PWM signals are center aligned!
PWM Period
73
• Suppose the refresh rate of the LED is 100 Hz. (human eye cannot
detect flicker at a refresh rate higher than 100 Hz. Thus, we will see
a steady and continuously source of light even though the LED is
switched on and off 100 times per second.)
75
77
Code
79
The devil is in
the detail
• Timer output control
• Enable Timer Output
• MOE: Main output enable
• OSSI: Off-state selection for Idle mode
• OSSR: Off-state selection for Run mode
• CCxE: Enable of capture/compare output for channel x
• CCxNE: Enable of capture/compare complementary output for
channel x
• These control bits are located in the timer register CCER,
and register BDTR.
• For example, in order to enable the output (OC) for
channel x, we need to set the MOE bit to 1 in register BD
TR.
• Based on the need of software application, we can choose
one of these three configurations.
BDTR
OSSI: Only important when MOE = 0
81
1
𝑂𝑢𝑡𝑝𝑢𝑡 𝑆𝑖𝑔𝑛𝑎𝑙 𝑃𝑒𝑟𝑖𝑜𝑑 = × 𝐴𝑅𝑅 + 1
𝑓
𝑓
𝑓 =
𝑃𝑆𝐶 + 1
𝐴𝑅𝑅 + 1 (𝑃𝑆𝐶 + 1)
𝑂𝑢𝑡𝑝𝑢𝑡 𝑆𝑖𝑔𝑛𝑎𝑙 𝑃𝑒𝑟𝑖𝑜𝑑 =
𝑓
Appendix: TIM1 Reg Map (Reg’s discussed for TIM1 CH1 are
highlighted)
83
85
87
89
91
46