cancel
Showing results for 
Search instead for 
Did you mean: 

Input capture timer - measure frequency

cchartier
Associate II
Posted on July 20, 2015 at 16:58

Hi, I had to measure frequency but in a large range of frequency.

the target is a STM32L

I would like to realize something like :

T2 generate a 10hz signal on PA0(using internal clock, and compare/match)

wiring this signal to T21 Capture source PA2 (both edge),

while T21 used external clock as input on PA1.

Capture should set Interrupt to compute frequency.

Is this possible ?

All try I maid with STM32CubeMx fail.

Have you an idea to help me ?

Best regards,

Saphymoo.

4 REPLIES 4
Posted on July 20, 2015 at 17:17

Ok, but what range exactly?

Basically a couple of methods, you can use PWM Input to measure frequency/duty. You'd need to adjust the timebase according to the range.

You can measure/timestamp edges with the Input Capture.

For high frequencies you can use External Count mode, and count pulse over an observation/integration window.

Can't help you with Cube/HAL stuff.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
raptorhal2
Lead
Posted on July 20, 2015 at 17:18

Unfortunately the L053 series does not have a 32 bit timer.

To measure a wider frequency range, keep count of the timer counter overflows and adjust the difference between successive counter capture values to allow for overflows.

Cheers, Hal

cchartier
Associate II
Posted on July 21, 2015 at 15:23

First thank you for this quick answer,

range of pulse frequency is [1e-4..5e+6 hz]

so  I can't use something like the timer capture sample code:  

The TIM2CLK frequency is set to SystemCoreClock (Hz), the Prescaler is 0 so the

  counter clock is SystemCoreClock (Hz).

  SystemCoreClock is set to 32 MHz for STM32L0xx Devices.

  TIM1 is configured in Input Capture Mode: the external signal is connected to

  TIM1 Channel2 used as input pin.

  To measure the frequency we use the TIM1 CC2 interrupt request, so in the

  TIM1_CC_IRQHandler routine, the frequency of the external signal is computed.

That's why my wish is to desing a timer (TIM2) to generate a 10Hz clock

and use an other timer (TIM21) with external input clock and 10Hz clock for capture (both edge)

the code I use is :

/* TIM2 init function */

void MX_TIM2_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_OC_InitTypeDef sConfigOC;

  htim2.Instance = TIM2;

  htim2.Init.Prescaler = 4000;

  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim2.Init.Period = 0;

  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;

  HAL_TIM_Base_Init(&htim2);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);

  HAL_TIM_OC_Init(&htim2);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);

  sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

  sConfigOC.Pulse = 100;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);

}

/* TIM21 init function */

void MX_TIM21_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_IC_InitTypeDef sConfigIC;

  htim21.Instance = TIM21;

  htim21.Init.Prescaler = 0;

  htim21.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim21.Init.Period = 0;

  htim21.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  HAL_TIM_Base_Init(&htim21);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;

  sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;

  sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;

  sClockSourceConfig.ClockFilter = 0;

  HAL_TIM_ConfigClockSource(&htim21, &sClockSourceConfig);

  HAL_TIM_IC_Init(&htim21);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim21, &sMasterConfig);

  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;

  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;

  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;

  sConfigIC.ICFilter = 0;

  HAL_TIM_IC_ConfigChannel(&htim21, &sConfigIC, TIM_CHANNEL_1);

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(htim_base->Instance==TIM2)

  {

  /* USER CODE BEGIN TIM2_MspInit 0 */

  /* USER CODE END TIM2_MspInit 0 */

    /* Peripheral clock enable */

    __TIM2_CLK_ENABLE();

    /**TIM2 GPIO Configuration

    PA0-WKUP     ------> TIM2_CH1

    */

    GPIO_InitStruct.Pin = GPIO_PIN_0;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF2_TIM2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM2_MspInit 1 */

  /* USER CODE END TIM2_MspInit 1 */

  }

  else if(htim_base->Instance==TIM21)

  {

  /* USER CODE BEGIN TIM21_MspInit 0 */

  /* USER CODE END TIM21_MspInit 0 */

    /* Peripheral clock enable */

    __TIM21_CLK_ENABLE();

    /**TIM21 GPIO Configuration

    PA1     ------> TIM21_ETR

    PA2     ------> TIM21_CH1

    */

    GPIO_InitStruct.Pin = GPIO_PIN_1;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

    GPIO_InitStruct.Alternate = GPIO_AF5_TIM21;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_2;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

    GPIO_InitStruct.Alternate = GPIO_AF0_TIM21;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* Peripheral interrupt init*/

    HAL_NVIC_SetPriority(TIM21_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(TIM21_IRQn);

  /* USER CODE BEGIN TIM21_MspInit 1 */

  /* USER CODE END TIM21_MspInit 1 */

  }

But using this I don't even have the clock output of timer2...

cchartier
Associate II
Posted on July 21, 2015 at 15:32

Thank and agree, that why I use a 100  Hz Clock to capture frequency  0xFFFF x 100 < 5 MHz

Best regards,

Christophe