cancel
Showing results for 
Search instead for 
Did you mean: 

Generate Timer 3 Interrupt every 2 seconds

Nimit Vachhani
Associate III
Posted on September 11, 2017 at 09:12

Greetings,

I want to generate timer3 interrupt every 2 seconds.  Below is my timer3 init code. I am using CUBE MX

static void MX_TIM3_Init(void)

{

TIM_ClockConfigTypeDef sClockSourceConfig;

TIM_MasterConfigTypeDef sMasterConfig;

htim3.Instance = TIM3;

htim3.Init.Prescaler = 47999;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 1999;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

{

_Error_Handler(__FILE__, __LINE__);

}

}

Below is timer 3 interrupt handler

void TIM3_IRQHandler(void)

{

HAL_TIM_IRQHandler(&htim3);

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim3)

{

if(htim3->Instance == TIM3)

{

HAL_TIM_Base_Stop_IT(htim3);

TIMER_Started = false;

}

}

I want to make sure if i am going in the right direction or not. Actually i am counting the external interrupts on falling edge on pin PB10 of STM32F0 discovery. I count it for 2 seconds and then calculate the value of rpm. Yes, my main aim is to measure rpm. As i am not getting correct value for rpm i doubt my timer code. The same method i have implemented on 8051 controller and it works.

#timer-interrupt #external-interrupt
2 REPLIES 2
Posted on September 11, 2017 at 10:05

Hi!

>>>'As i am not getting correct value for rpm i doubt my timer code.'

Two factors involved,  proper count of rounds and known period of time base.

Assuming that your  HCLK  is 48MHZ and APB prescaler divides by 1 then  your timer settings give 2 second period.

Consider to stop also EXTI interrupts from inside the  timers callback funtion.

and consider also that input-Capture functionality of timers is a good solution to measure rpms without software involved except the initialisation code.

Rgrds

vf

Nimit Vachhani
Associate III
Posted on September 11, 2017 at 11:25

FORTOUNAS.EVANGELOS

: Thanks for the reply. I have cross checked my rpm circuit and there was one connection problem. Now i am getting proper readings and as per your comments i am getting proper 2 sec period. Thanks