cancel
Showing results for 
Search instead for 
Did you mean: 

I want to use timer interrupt for some operations in my application.But i am unable to set the exact parameters for timer..I am using two timers...TIM7 for 1ms & TIM 14 for 20 ms...what should be the setting for prescaler & period?

Bs.1
Associate II

Please find the details below:

Part - STM32F072VB

stm32cube mx for code generation

timer clock - 24MHz

static void MX_TIM7_Init(void)

{

 /* USER CODE BEGIN TIM7_Init 0 */

 /* USER CODE END TIM7_Init 0 */

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 /* USER CODE BEGIN TIM7_Init 1 */

 /* USER CODE END TIM7_Init 1 */

 htim7.Instance = TIM7;

 htim7.Init.Prescaler = 2000-1 ;

 htim7.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim7.Init.Period = 1;

 htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_Base_Init(&htim7) != HAL_OK)

 {

  Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN TIM7_Init 2 */

 /* USER CODE END TIM7_Init 2 */

}

/**

 * @brief TIM14 Initialization Function

 * @param None

 * @retval None

 */

static void MX_TIM14_Init(void)

{

 /* USER CODE BEGIN TIM14_Init 0 */

 /* USER CODE END TIM14_Init 0 */

 /* USER CODE BEGIN TIM14_Init 1 */

 /* USER CODE END TIM14_Init 1 */

 htim14.Instance = TIM14;

 htim14.Init.Prescaler = 1600-1;

 htim14.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim14.Init.Period = 20;

 htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_Base_Init(&htim14) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN TIM14_Init 2 */

 /* USER CODE END TIM14_Init 2 */

}

1 ACCEPTED SOLUTION

Accepted Solutions

> timer clock - 24MHz

> TIM7 for 1ms

1/1ms = 1kHz. You can leave prescaler = 0 and set period = (24MHz/1kHz - 1) = 24000 - 1

> & TIM 14 for 20 ms

1/20ms = 50Hz. You'll need some prescaler here, e.g. prescaler = 20 - 1 and period = 24000 - 1.

JW

View solution in original post

3 REPLIES 3

> timer clock - 24MHz

> TIM7 for 1ms

1/1ms = 1kHz. You can leave prescaler = 0 and set period = (24MHz/1kHz - 1) = 24000 - 1

> & TIM 14 for 20 ms

1/20ms = 50Hz. You'll need some prescaler here, e.g. prescaler = 20 - 1 and period = 24000 - 1.

JW

Thank you so much for the guidance.

Can you share the formulas how prescaler & period is calculated???

timer update frequency = timer input frequency / (TIM_PSC + 1) / (TIM_ARR + 1)

JW