AnsweredAssumed Answered

How to reset timer counter value once captured

Question asked by gip.jack on Nov 25, 2015
Latest reply on Nov 25, 2015 by Clive One
I have a timer set up with a capture register which loads the counter value on a rising edge with the code below. However the counter value keeps increasing until overflow (not when counter value loaded into capture register) so I cannot measure frequency of input signal. Therefore I want to reset the counter register (CNT) automatically (in hardware, not manually clearing it in a interrupt) once value loaded into capture register (CCR) so I can read the CCR to get frequency.

The code below after "Configure the slave mode" resets the CCR instead of the CNT so the value read from the CCR is always 0. How do I make it reset the CNT once it's value is loaded into CCR?

01.void MX_TIM3_Init(void)
02.{
03. 
04.    htim3.Instance = TIM3;
05.    htim3.Init.Prescaler = 0;
06.    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
07.    htim3.Init.Period = 0xFFFF;
08.    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
09. 
10.    HAL_TIM_IC_MspInit(&htim3);
11.    __TIM3_CLK_ENABLE();
12. 
13.    GPIO_InitTypeDef GPIO_InitStruct;
14.    GPIO_InitStruct.Pin = GPIO_PIN_7;
15.    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
16.    GPIO_InitStruct.Pull = GPIO_NOPULL;
17.    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
18.    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
19.    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
20. 
21.    TIM_ClockConfigTypeDef sClockSourceConfig;
22.    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
23.    HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
24. 
25.    HAL_TIM_IC_Init(&htim3);
26. 
27.    TIM_IC_InitTypeDef sConfigIC;
28.    sConfigIC.ICPolarity = TIM_ICPOLARITY_RISING;
29.    sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
30.    sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
31.    sConfigIC.ICFilter = 0;
32.    HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2);
33. 
34.    TIM_MasterConfigTypeDef sMasterConfig;
35.    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
36.    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
37.    HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
38. 
39.    HAL_TIM_IC_Start(&htim3, TIM_CHANNEL_2);
40. 
41.    /*##-3- Configure the slave mode ###########################################*/
42.      /* Select the slave Mode: Reset Mode */
43.    TIM_SlaveConfigTypeDef   sSlaveConfig;
44.      sSlaveConfig.SlaveMode     = TIM_SLAVEMODE_RESET;
45.      sSlaveConfig.InputTrigger  = TIM_TS_TI2FP2;
46.      HAL_TIM_SlaveConfigSynchronization(&htim3, &sSlaveConfig);
47. 
48.}

Outcomes