cancel
Showing results for 
Search instead for 
Did you mean: 

How to reset timer counter value once captured

jackygip
Associate II
Posted on November 25, 2015 at 17:32

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 theCNT once it's value is loaded intoCCR?


void
MX_TIM3_Init(
void
)

{


htim3.Instance = TIM3;

htim3.Init.Prescaler = 0;

htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

htim3.Init.Period = 0xFFFF;

htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;


HAL_TIM_IC_MspInit(&htim3);

__TIM3_CLK_ENABLE();


GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin = GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);


TIM_ClockConfigTypeDef sClockSourceConfig;

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);


HAL_TIM_IC_Init(&htim3);


TIM_IC_InitTypeDef sConfigIC;

sConfigIC.ICPolarity = TIM_ICPOLARITY_RISING;

sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;

sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;

sConfigIC.ICFilter = 0;

HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2);


TIM_MasterConfigTypeDef sMasterConfig;

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);


HAL_TIM_IC_Start(&htim3, TIM_CHANNEL_2);


/*##-3- Configure the slave mode ###########################################*/

/* Select the slave Mode: Reset Mode */

TIM_SlaveConfigTypeDef sSlaveConfig;

sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;

sSlaveConfig.InputTrigger = TIM_TS_TI2FP2;

HAL_TIM_SlaveConfigSynchronization(&htim3, &sSlaveConfig);


}

1 REPLY 1
Posted on November 25, 2015 at 18:21

The method I prefer is to let the counter keep running and delta the measurements, one to the next.

You should consider using PWM Input mode, it does reset the counter at the period of the input signal, and uses 2 channel registers, one holds the period (ticks between rising edges), and the other the duty (ticks to the falling edge, from the rising one), in a usual configuration.

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