cancel
Showing results for 
Search instead for 
Did you mean: 

Hello All, I am using, STM32F446RE controller evaluation board. I want to count external pulses. I had set TIM2 as counter and started the counter. But the counter interrupt is not getting generated with the frequency we are setting the pulse.

TH.16.336
Associate III

Appending more details here,

  1. Configured TIM2 as counter. Below is the TIM2_INit code,

static void MX_TIM2_Init(void)

{

 TIM_SlaveConfigTypeDef sSlaveConfig = {0};

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_DOWN;

 htim2.Init.Period = STEP_RESOLUTION - 1; // STEP_RESOLUTION = 4

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

 if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

 {

  Error_Handler();

 }

 sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;

 sSlaveConfig.InputTrigger = TIM_TS_ETRF;

 sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;

 sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;

 sSlaveConfig.TriggerFilter = 0;

 if (HAL_TIM_SlaveConfigSynchro(&htim2, &sSlaveConfig) != HAL_OK)

 {

  Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

 {

  Error_Handler();

 }

}

2. And here is the API used to trigger counter.

HAL_TIM_Base_Start_IT(&htim2);

3. With this code, I am having an ISR for counter which triggers but with the same frequency as we set. I mean, I am using signal generator to generate an 66.66kHz (15 Micro sec) pulse and feeding it to TIM2_ETR pin of evaluation board.

4. With this I was expecting the ISR to be hit for each 4 pulses of clock. Means for every 60Micro sec in timing. But it's not happening in the way.

Can someone suggest what might be the issue here? Can this TIM2 module configured as counter can count pulses with 15 micro sec timing? Any restriction on Timer or cntroller?

1 REPLY 1

Configuring the pin anywhere? MSP code.

TIM2 should work in External Count mode, bit of a waste of a 32-bit time for 4 cycles.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..