3.2 Interrupts - Counters - Timers
3.2 Interrupts - Counters - Timers
Timer in Stm32F103
§ Advanced-control timers (TIM1 and TIM8)
§ General-purpose timers (TIM2 to TIM5)
§ Basic timers (TIM6 and TIM7)
§ General-purpose timers (TIM9 to TIM14)
§ Real-time clock (RTC)
§ Independent watchdog (IWDG)
§ Window watchdog (WWDG)
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 3
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 6
§ Counter modes
▫ Upcounting mode
▫ Downcounting mode
▫ Center-aligned mode (up/down counting)
https://www.youtube.com/watch?v=VfbW6nfG4kw&t=637s
https://drive.google.com/file/d/1lPCnbnfwLIAEaNPWvPxO6MSJ1Psvruva/view?usp=sharing
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 7
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 8
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 9
Clock Selection
§ The counter clock can be provided by the following clock sources:
▫ Internal clock (CK_INT)
▫ External clock mode1: external input pin (TIx)
▫ External clock mode2: external trigger input (ETR).
▫ Internal trigger inputs (ITRx): using one timer as prescaler for another timer, for
example, Timer1 can be configured to act as a prescaler for Timer 2.
Questions?
§ How to configure the upcounter to count in response to a rising edge
on the TI2 input?
§ How to configure the upcounter to count each 2 rising edges on ETR?
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 10
Capture/Compare Channels
§ Each Capture/Compare channel is built around a capture/compare register
(including a shadow register),
§ An input stage for capture (with digital filter, multiplexing and prescaler) and an
output stage (with comparator and output control)
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 11
Capture/Compare Channels
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 12
Capture/Compare Channels
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 13
https://www.youtube.com/watch?v=RZ0uszK2PMo
https://drive.google.com/file/d/1F50A8NR4AR2zPZc7wvRDJZjfTO18WId1/view?usp=sharing
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 14
Questions
§ How to measure the period and the duty cycle of a PWM signal?
§ How to measure the period and the duty cycle of the PWM applied on
TI1?
https://drive.google.com/file/d/17RvIlt_PaihrqqBR-7AkB0DLXMItwL_b/view?usp=sharing
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 15
PWM Mode
§ Pulse width modulation mode allows generating a signal with:
▫ A frequency determined by the value of the TIMx_ARR register and
▫ A duty cycle determined by the value of the TIMx_CCRx register.
§ The user must enable the corresponding preload register by setting the
OCxPE bit in the TIMx_CCMRx register, and eventually the auto-reload
preload register by setting the ARPE bit in the TIMx_CR1 register.
https://www.youtube.com/watch?v=92_CWBWXPw0
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 16
Exercises
§ Write a program to generate a square wave on pin PD5 (1 kHz)
▫ Using HAL_Delay(uint32_t ms) function
§ Generate square waves on pins PD1 (10 Hz) and PD7 (100 Hz)
respectively.
anhpham@hcmut.edu.vn
CO3009 – Interrupts, Counters and Timers 17
Exercises (cont)
§ Use Timer2 and Timer3 interrupts to generate square waves on pins
PD1 (10 Hz) and PD7 (100 Hz) respectively.
§ Use Timer3 interrupts to generate square waves on pins PD1 (10 Hz)
and PD7 (100 Hz) respectively.
anhpham@hcmut.edu.vn