cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F7 tim3 rollover with reload works, how do I get the interrupt for each external pulse?

jhanc.11
Senior

I have tim3 setup to count to 25,000 using a 2.5Mhz input signal. This works. I enable the global interrrupt and it fires whenever the counter rolls. I have an issue though, that when I poll the timer for the count, it is double it should be and I can't find out why.

So I thought I would try to an interrupt whenever I get an incoming pulse and I can't figure out how to do this. So I need to either figure out why I am getting double pulses (not only on the inquiry with  __HAL_TIM_GetCounter(&htim3) but also it rolls the counter early) or how do I set the interrupt on each pulse so I can count it myself? I am confident I am only getting one pulse for each of the double count. I've tried to do a callback with a number of callback functions but the only one that works is HAL_TIM_PERIOD_ELAPSED_CB_ID. I would like to get either an accurate count returned on __HAL_TIM_GetCounter(&htim3) or figure out a way to count it myself. If I did that latter though, I would still have to fix the rollover which is happening early.

Thoughts? I am really close to finishing this project. By the way, using an external interrupt on a GPIO pin, the count is correct (not doubled).

Thanks

Jerry

1 REPLY 1
jhanc.11
Senior

I don't know why I can't get any of the count callbacks working, just the overflow, but I changed the input signal to TIM_TS_TI1FP1 (filtered) from TIM_TS_TI1F_ED (edge detect) and now I'm no longer getting that extra pulse. No idea why. It could be that in testing I am using a much longer input.

So before moving back to the UI design, it would be nice to be able to process my own counter interrupts instead of using the  __HAL_TIM_GetCounter(&htim3), though that is more academic than having any practical advantage for this application. I don't think the interrupts need to be enabled, specifically, as the only tim3 interrupt I see is the global enable.

Thanks for all the help. You guys are very responsive.

Here's my tim3 config.

void MX_TIM3_Init(void)
{
  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_IC_InitTypeDef sConfigIC = {0};
 
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 0;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 59;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE ;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
 // sSlaveConfig.InputTrigger = TIM_TS_TI1F_ED;  //double counting??
  sSlaveConfig.InputTrigger=TIM_TS_TI1FP1;
  sSlaveConfig.TriggerPolarity= TIM_TRIGGERPOLARITY_RISING;
  sSlaveConfig.TriggerFilter = 0;
  if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  sConfigIC.ICFilter = 0;
  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_3) != HAL_OK)
  {
    Error_Handler();
  }
 
}

Jerry