AnsweredAssumed Answered

Generate Timer 3 Interrupt every 2 seconds

Question asked by Nimit Vachhani on Sep 11, 2017
Latest reply on Sep 11, 2017 by Nimit Vachhani

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.

Outcomes