Sie sind auf Seite 1von 52

Timer

256
Timers On STM32F4

2
General Purpose Timer Features
Overview

3
Advanced Timer Features
Overview

4
General Purpose 2 Channels Timer (TIM9 & TIM12)
Features Overview

5
General Purpose 1 Channels timer (TIM10..11 & TIM13..14)
Features overview

6
Timer Synchronization – Configuration
examples (1/3)

7
Timer Synchronization – Configuration
examples (2/3)

8
Timer Synchronization – Configuration
examples (3/3)

9
STM32F4xx Timer features
overview (1/2)

10
STM32F4xx Timer features
overview (2/2)

11
Timer with interrupt
Use TIM with interrupt
• Objective
– Learn how to setup TIM with Interrupt in Cube MX
– How to generate project in Cube MX and use HAL
functions
– Indicate TIM interrupt with LED toggle
• Goal
– Configure TIM in Cube MX and generate project
– Learn how start timer and handle interrupt
– Verify the correct functionality

13
Use TIM with interrupt
• Create project in Cube MX
– Menu > File > New Project
– Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• CubeMX TIM selection
– Select TIM clock source Internal clock
– Enable GPIO for LED PG14

14
Use TIM with interrupt
• CubeMX TIM configuration
– Tab->Configuration->Control>TIM1
– Check the settings

15
Use TIM with interrupt
• CubeMX TIM configuration
– Tab->Parameter Settings
– Prescaler to 18000
– Counter period to 10000
– Together with 180MHz TIMER1
clock we get period 1Hz

16
Use TIM with interrupt
• CubeMX TIM configuration
– Tab->NVIC Settings
– Enable TIM1 Update interrupt
– Button OK

17
Use TIM with interrupt
• Now we set the project details for generation
– Menu > Project > Project Settings
– Set the project name
– Project location
– Type of toolchain
• Now we can generate project
– Menu > Project > Generate Project

18
Use TIM with interrupt
HAL Library TIM with IT flow
TIM Initializations
including peripheral interrupt NVIC
initializations

Start process with interrupt


generation at end of process
HAL_TIM_Base_Start_IT

HAL_TIM_IRQHandl TIM1_UP_TIM10_IRQHa
HAL_OK HAL_ERROR HAL_BUSY er ndler

process callback
HAL_TIM_PeriodElapsedCallback

process Error callback


HAL_TIM_ErrorCallback

19
Use TIM with interrupt
• Open the project in our IDE
– The functions we want to put into main.c
– Between /* USER CODE BEGIN 2 */ and /* USER CODE
END 2 */ tags
• For TIM start use function
– HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
• TIM callback
– void TIM1_UP_TIM10_IRQHandler(void)
• GPIO LED toggle
– HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t
GPIO_Pin)

20
Use TIM with interrupt
• Solution
/* – TIM
USER start
CODE BEGIN 2 */

HAL_TIM_Base_Start_IT(&ht
im1);
/* USER CODE END 2 */

/* USER CODE BEGIN 4 */


void

HAL_TIM_PeriodElapsedCallback(TIM_HandleType
Callback
Def *htim)
handling
{
HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_14);
}
/* USER CODE END 4 */

21
Minggu ke-11

277
TIM with PWM output

278
Use TIM with PWM output
• Objective
– Learn how to setup TIM with PWM out in Cube
MX
– How to generate project in Cube MX and use HAL
functions
– Indicate TIM PWM on LED
• Goal
– Configure TIM in Cube MX and generate project
– Learn how start timer and set PWM out
– Verify the correct functionality with LED

24
Use TIM with PWM output
• Create project in Cube MX
– Menu > File > New Project
– Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• CubeMX TIM selection
– Select TIM clock source - Internal clock
– Set Channel1 to PWM generation

25
Use TIM with PWM output
• CubeMX TIM configuration
– TAB->Configuration
->Control>TIM1
– TAB->Parameter settings
– Prescaler to 18000
– Counter period to 10000
– Together with 180MHz TIMER1
clock we get period 1Hz
– PWM pulse to 5000 this give
us 1Hz blinking frequency

26
Use TIM with PWM output
• Now we set the project details for generation
– Menu > Project > Project Settings
– Set the project name
– Project location
– Type of toolchain
• Now we can generate project
– Menu > Project > Generate Project

27
Use TIM with PWM output
• Start process TIM with PWM(same for DMA, ADC)
– Non blocking start process

Peripheral
Initializatio
ns

Start Process
HAL_TIM_PWM_Start

28
Use TIM with PWM output
• Open the project in our IDE
– The functions we want to put into main.c
– Between /* USER CODE BEGIN 2 */ and /* USER
CODE END 2 */ tags
• For TIM start use function
– HAL_TIM_PWM_Start(TIM_HandleTypeDef
*htim, uint32_t Channel)
• GPIO LED toggle
– We wire the Channel1 PE9 with LED PG14

29
Use TIM with PWM output
• Solution
/* – TIM
USER PWM
CODE BEGINstart
2 */

HAL_TIM_PWM_Start(&htim1,TIM_CH
ANNEL_1);
/* USER CODE END 2 */

– TIM1 Channel 1 and LED connection

30
Multi Channel PWM dan
Pengaturan Kecepatan Motor
DC
Configure GPIO for Multi
channel PWM Output
• Objective
– Learn how to setup pin and GPIO port in Cube
MX for PWM output
– How to generate project in Cube MX and use
HAL functions for Multi channel PWM
– How to add C code file to control DC motor

• Goal
– Configure GPIO pin in Cube MX and generate
project
– Add C code to modify PWM duty cycle
– Add C code to control DC motor

32
Configure for Multi
channel PWM Output
 PWMmerupakan
sailitas yang
disediakan oleh
TIMER, dalam satu
timer dapat
menghasilkan
beberapa chanel
output PWM
 Inisialisai GPIO
PWM adalah
TIMx_CHx
 Pada Pelatihan ini
PWM untuk
pengaturan motor
DC pada GPIOB,
B10 & B11.
Sedangkan Untuk
mengatur arah PWM
putar digunakan Generation
B12 – B115
33
Configure for Multi
channel PWM Output

34
Configure for Multi
channel PWM Output

10000

35
Contoh Program <Motor>
int main(void)
{ HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
HAL_TIM_Base_Start(&htim2);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);

while (1)
{
HAL_GPIO_WritePin(Motor1_CW_GPIO_Port,Motor1_CW_Pin,GPIO_PIN_SET);

HAL_GPIO_WritePin(Motor1_CCW_GPIO_Port,Motor1_CCW_Pin,GPIO_PIN_RESET);

HAL_GPIO_WritePin(Motor2_CW_GPIO_Port,Motor2_CW_Pin,GPIO_PIN_SET);

HAL_GPIO_WritePin(Motor2_CCW_GPIO_Port,Motor2_CCW_Pin,GPIO_PIN_RESET);
TIM2->CCR3 = 5000; TIM2->CCR4 = 7500;
HAL_Delay(2000);

HAL_GPIO_WritePin(Motor1_CW_GPIO_Port,Motor1_CW_Pin,GPIO_PIN_RESET);

HAL_GPIO_WritePin(Motor1_CCW_GPIO_Port,Motor1_CCW_Pin,GPIO_PIN_SET);
36
HAL_GPIO_WritePin(Motor2_CW_GPIO_Port,Motor2_CW_Pin,GPIO_PIN_RESET);
PWM Frequency ?
• Pada microcontroller ARM Cortex M4, PWM dapat
dihasilkan melalui timer dengan frekwensi dan duty cycle
yang dapat diatur sesuai dengan kebutuhan. Ilustrasi cara
menghasilkan PWM melalui timer seperti ditunjukkan pada
gambar berikut ini.
Timer Period (Maximum 16 or 32bit)

Compare Value

Timer CNT

Timer_CNT_Clock

PWM Ouput

PWM period 37
PWM Frequency ?
Besarnyafrekwesi PWM
dapatdihitungmelaluipersamaanberikut:

𝑑𝑒𝑠𝑖𝑟𝑒𝑑 𝑃𝑊𝑀 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 (𝐻𝑧) =


𝑇𝑖𝑚𝑒𝑟_𝐶𝑁𝑇𝐶𝑙𝑜𝑐𝑘 (𝐻𝑧)
(1)
𝑡𝑖𝑚𝑒 𝑝𝑒𝑟𝑖𝑜𝑑 𝑜𝑟 𝑉𝑐+1

𝑇𝑖𝑚𝑒𝑟_𝐶𝑁𝑇𝐶𝑙𝑜𝑐𝑘 (𝐻𝑧)
𝑇𝑖𝑚𝑒 𝑝𝑒𝑟𝑖𝑜𝑑 =
(𝑑𝑒𝑠𝑖𝑟𝑒𝑑 𝑃𝑊𝑀 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 𝐻𝑧 −1)
(2) 38
PWM Frequency ?

Timer 1 merupakan timer dibawah kendali APB 2 serpti pada


gambar berikut :

39
PWM Frequency ?

MAX
MAX TIMER
Timer Type Resolution Prescaler Channels INTERFACE APB
CLOCK*
CLOCK
TIM1, TIM8 Advanced 16bit 16bit 4 SysClk/2 SysClk 2
General SysClk,
TIM2, TIM5 32bit 16bit 4 SysClk/4 1
purpose SysClk/2
General SysClk,
TIM3, TIM4 16bit 16bit 4 SysClk/4 1
purpose SysClk/2
General
TIM9 16bit 16bit 2 SysClk/2 SysClk 2
purpose
TIM10, General
16bit 16bit 1 SysClk/2 SysClk 2
TIM11 purpose
General SysClk,
TIM12 16bit 16bit 2 SysClk/4 1
purpose SysClk/2
TIM13, General SysClk,
16bit 16bit 1 SysClk/4 1
TIM14 purpose SysClk/2
SysClk,
TIM6, TIM7 Basic 16bit 16bit 0 SysClk/4 1
SysClk/2 40
PWM Frequency ?
𝑇𝑖𝑚𝑒𝑟𝐶𝑁𝑇𝐶𝑙𝑜𝑐𝑘 𝐻𝑧 dapatdihitungdenganpersamaanberikut:

APB2 Timer Clock


𝑇𝑖𝑚𝑒𝑟𝐶𝑁𝑇𝐶𝑙𝑜𝑐𝑘 𝐻𝑧 =
Prescaller_set + 1
Dalamhalini, diasumsikan timer 1 menggunakanfrekwensi yang paling tinggisehinggad
maka :
168000000
𝑇𝑖𝑚𝑒𝑟𝐶𝑁𝑇𝐶𝑙𝑜𝑐𝑘 𝐻𝑧 =  𝑇𝑖𝑚𝑒𝑟𝐶𝑁𝑇𝐶𝑙𝑜𝑐𝑘 𝐻𝑧 = 168000
0+1

Untukfrekwensi PWM 10 kHz, 𝑇𝑖𝑚𝑒 𝑝𝑒𝑟𝑖𝑜𝑑dapatdihitungsebagaiberikut :


𝑇𝑖𝑚𝑒𝑟_𝐶𝑁𝑇
𝐶𝑙𝑜𝑐𝑘 (𝐻𝑧) 168000000
𝑇𝑖𝑚𝑒 𝑝𝑒𝑟𝑖𝑜𝑑 = (𝑑𝑒𝑠𝑖𝑟𝑒𝑑 𝑃𝑊𝑀 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦  𝑇𝑖𝑚𝑒 𝑝𝑒𝑟𝑖𝑜𝑑 =
𝐻𝑧 −1) (10000− 1)

Karenacounter pada microcontroller berupabilangnbulat,


makasemuaperhitunganberlakupembulatankebawahsehinggamenjadi 16801, Timer CNT dihitung
41
Contoh Program PWM

42
TIM with DMA
Use TIM with DMA transfer
• Objective
– Learn how to setup TIM with DMA in Cube MX
– How to generate project in Cube MX and use HAL
functions
– Indicate TIM DMA transfer with LED toggle
• Goal
– Configure TIM in Cube MX and generate project
– Learn how start timer and setup DMA
– Verify the correct functionality with DMA transfer
into GPIO register

44
Use TIM with DMA transfer
• Create project in Cube MX
– Menu > File > New Project
– Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
• CubeMX TIM selection
– Select TIM clock source Internal clock
– Enable GPIO for LED PG14

45
Use TIM with DMA transfer
• CubeMX TIM configuration
– Tab->Configuration->Control>TIM1
– Check the settings

46
Use TIM with DMA transfer
• CubeMX TIM configuration
– Tab->Parameter Settings
– Prescaler to 18000
– Counter period to 10000
– Together with 180MHz TIMER1
clock we get period 1Hz

47
Use TIM with DMA transfer
• CubeMX TIM configuration
– TAB->DMA Settings
– Button ADD
– Select TIM1_UP DMA request
– Memory to peripheral direction
– Set Memory increment
– Circular mode
– Half word data width
– Button OK

48
Use TIM with DMA transfer
• Now we set the project details for generation
– Menu > Project > Project Settings
– Set the project name
– Project location
– Type of toolchain
• Now we can generate project
– Menu > Project > Generate Project

49
Use TIM with DMA transfer
HAL Library TIM with DMA flow

Peripheral Initializations
including DMA stream
initializations

Start process with DMA


HAL_DMA_Start

HAL_OK HAL_ERROR HAL_ERROR

Start process with TIM


HAL_TIM_Base_Start

HAL_OK HAL_ERROR HAL_ERROR

50
Use TIM with DMA transfer
• Open the project in our IDE
– The functions we want to put into main.c
– Between /* USER CODE BEGIN 2 */ and /* USER
CODE END 2 */ tags
• For TIM start use function
– HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef
*htim, uint32_t *pData, uint16_t Length)
• TIM1 trigger DMA transfer
– __HAL_TIM_ENABLE_DMA
• DMA start function
– HAL_DMA_Start(DMA_HandleTypeDef *hdma,
uint32_t SrcAddress, uint32_t DstAddress,
uint32_t DataLength)
• GPIO LED register address
– (uint32_t)(&GPIOG->ODR)
51
Use TIM with DMA transfer
• Solution
/* USER CODE BEGIN 2 */
__HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_UPDATE);

HAL_DMA_Start(&hdma_tim1_up,(uint32_t)data,(uint32_t)&GPIO
G->ODR,2);
HAL_TIM_Base_Start(&htim1);
/* USER CODE END 2 */

52

Das könnte Ihnen auch gefallen