0
\$\begingroup\$

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...

Bence Kaulics
6,47312 gold badges35 silver badges61 bronze badges
asked Oct 30, 2015 at 12:14
\$\endgroup\$
4
  • \$\begingroup\$ Have you tried setting an Output Compare register to the desired count? \$\endgroup\$ Commented 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\$ Commented Oct 30, 2015 at 12:34
  • \$\begingroup\$ Count down from your required value until the counter overflows? \$\endgroup\$ Commented Oct 30, 2015 at 12:37
  • \$\begingroup\$ Yeah I tried it before...do you know if is there this kind of thing? \$\endgroup\$ Commented Oct 30, 2015 at 12:38

1 Answer 1

2
\$\begingroup\$

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!!

answered Oct 30, 2015 at 18:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.