I'm trying to use the TIM peripheral to count external pulses until a number and throw a interruption. I'm using STM32F429 and HAL library. I am initializing the TIM2 with this code:
void InitializeTimer2()
{
TIM_SlaveConfigTypeDef sSlaveConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 5;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if(HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
/* Initialization Error */
Error_Mensage("TIM2");
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
sSlaveConfig.TriggerFilter = 15;
HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
HAL_TIM_Base_Start_IT(&htim2);
}
But I tried many HAL_TIM functions to get the interruption but with no success.
It will be a pleasure to provide any other helpful code...
-
\$\begingroup\$ Have you tried setting an Output Compare register to the desired count? \$\endgroup\$Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2015年10月30日 12:29:07 +00:00Commented Oct 30, 2015 at 12:29
-
\$\begingroup\$ No, I thought that it would be possible to get something like a interruption informing an overflow. \$\endgroup\$Leandro Lima– Leandro Lima2015年10月30日 12:34:21 +00:00Commented Oct 30, 2015 at 12:34
-
\$\begingroup\$ Count down from your required value until the counter overflows? \$\endgroup\$Tom Carpenter– Tom Carpenter2015年10月30日 12:37:25 +00:00Commented Oct 30, 2015 at 12:37
-
\$\begingroup\$ Yeah I tried it before...do you know if is there this kind of thing? \$\endgroup\$Leandro Lima– Leandro Lima2015年10月30日 12:38:32 +00:00Commented Oct 30, 2015 at 12:38
1 Answer 1
the answer is to use the function HAL_TIM_PeriodElapsedCallback
:
/* TIM5 init function */
void MX_TIM5_Init(void)
{
TIM_SlaveConfigTypeDef sSlaveConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim5.Instance = TIM5;
htim5.Init.Prescaler = 0;
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim5.Init.Period = 10;
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim5);
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
sSlaveConfig.TriggerFilter = 15;
HAL_TIM_SlaveConfigSynchronization(&htim5, &sSlaveConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM5)
{
count2++;
}
}
I initialized the timer with this: HAL_TIM_Base_Start_IT(&htim5)
.
This code permits to count external pulses and throw an interruption every 10 pulses.
Reggards!!