0% found this document useful (0 votes)
21 views

MicroprocessorHVT2019 Lec09 STM32 Timer

STM32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

MicroprocessorHVT2019 Lec09 STM32 Timer

STM32
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Microprocessor

(for Embedded Systems)


09. Timers
(Bộ định thời)
Huỳnh Việt Thắng
Faculty of Electronic & Telecommunication Engineering
Danang University of Science and Technology
[email protected]
<Danang, May 2020>
Course contents
• Introduction
• Number systems and data formats (review)
• Microcomputer organization
• Introduction to ARM based processors
• Introduction to STM32 MCUs
• Interfacing with STM32 MCU peripherals
– GPIO Management
– Interrupt Management
– Interrupt Priority Management
– Timer (Bộ định thời)
– ADC
– UART
• Applications
Main reference
1. Carmine Noviello, Mastering STM32, 2018

2. RM0383 Reference manualSTM32F411xC/E


advanced Arm®-based 32-bit MCUs

3. Datasheet STM32F411xC STM32F411xE


Lecture Outline
• Introduction to Timer
• General Purpose Timer
• Example / Exercise
Introduction to Timer
• A timer is a free-running counter with a counting frequency that is a
fraction of its source clock.

• The counting speed can be reduced using a dedicated prescaler for


each timer.

• Depending on the timer type, it can be clocked by the internal clock


(which is derived from the bus where it is connected), by an external
clock source or by another timer used as “master”.

• Usually, a timer counts from zero up to a given value, which cannot


be higher than the maximum unsigned value for its resolution, for
example, a 16-bit timer overflows when the counter reaches 65535,
• but it can also count on the contrary and in other ways.
Typical usages
(Các trường hợp sử dụng điển hình)
• time base generator

• measure the frequency of an external event (input capture mode)

• control an output waveform, or to indicate when a period of time has


elapsed (output compare mode)
– One pulse mode (OPM) is a particular case of the input capture mode and the
output compare mode. It allows the counter to be started in response to a
stimulus and to generate a pulse with a programmable length after a
programmable delay.

• generate PWM signals in edge-aligned mode or center-aligned


mode independently on each channel (PWM mode)
– In some STM32 MCUs (notably from STM32F3 and recent STM32L4 series),
some timers can generate a center-aligned PWM signals with a programmable
delay and phase shift.
Timer with Interrupt & DMA
Depending on the timer type, a timer can generate Interrupts or DMA
requests when the following events occur:

• Update events
– Counter overflow/underflow
– Counter initialized
– Others

• Trigger
– Counter start/stop
– Counter Initialize
– Others

• Input capture/Output compare


NVIC Controller
Timer Categories in STM32 MCUs
• Basic timers
• General purpose timers
• Advanced timers
• High resolution timers
• Low-power timers

• STM32F411 (see Reference Manual - RM)


– General purpose timer
• TIM2 to TIM5: 16-bit or 32-bit (Chapter 13 - RM)
• TIM9 to TIM11: 16-bit (Chapter 14 - RM)
– Advanced control timer: TIM1 (16-bit counter) (Chapter 12 - RM)
General Purpose Timers
TIM2 to TIM5

See: STM32F411 Reference Manual Chapter 13


Timer’s clock
Auto-Reload Register
Using Timer in Interrupt Mode

CK_PSC

Prescaler Period
Quiz
• Nếu tần số xung clock bus APB1 nối với Timer là fCK_PSC = 96MHz
(Timer clock), hãy tính giá trị Prescaler và Period để Timer tạo ra
ngắt (UpdateEvent - UEV) mỗi 0.5s?
• Prescaler =
• Period =

96.000.000 47.999 2.000 999 2Hz


(0.5s)
Internal clocks for Timers in STM32F411
See: STM32F411 Datasheet (page 15)
Bus architecture STM32F103
See: STM32F103 Reference Manual (page 47)
Bài tập
• Sử dụng TIMER (ví dụ: TIM2-TIM5) kết hợp ngắt của
TIMER để điều khiển nháy LED (ví dụ: PD12-PD15): cứ
mỗi 1s LED đảo trạng thái 1 lần?

• Đáp án:
• CK_PSC = 96.000.000 Hz
• Prescaler = 47999
• Period = 1999
Clock configuration: STM32F411

fCK_PSC
Clock configuration: STM32F103
Timer2 (TIM2) configuration
Interrupt
Setting
for TIM2
Start the TIM2 in Interrupt mode

Main loop
do nothing!
HAL_TIM_PeriodElapsedCallback
Kết quả đo kiểm tra tín hiệu PD14 có tần số 1Hz trên dao
động ký
Watch demo on YouTube
https://youtu.be/ER5pQJzaqf8

Thank You!
Video hướng dẫn thực hành
TIMER tại HOCARM.ORG
• https://dutudn-
my.sharepoint.com/:v:/g/personal/thanghv_d
ut_udn_vn/EYLV0EA2KItBrj00s-
xebT4BZZlcMiUBmnxDxW8tbI4Ysw?e=xlmSz
K

You might also like