cancel
Showing results for 
Search instead for 
Did you mean: 

Input Capture - Nucleo STM32L152RE

nicolas2399
Associate II
Posted on November 13, 2015 at 17:22

Hi,

I'm trying to measure a frequency of tenth kHz using TIM2IC2 on PA1. I first took example code STM32Cube_FW_F3_V1.3.0\Projects\STM32F3-Discovery\Examples\TIM\TIM_InputCapture and tried to adapt it to my platform and program.

First try wasn't successful. GPIOA register and PA1 wasn't configured correctly. I set it as AF Then TIM2 register wasn't configured as well (even while using MX_TIM_Config function). Problem was TIM2 clock wasn't enabled. After enabling its clock I got TIM2 register configured.

I see now TIM2->CNT counting but no interrupt occurs. I don't exactly understand why I'm facing so many problems while using HAL functions and code example. Hopefully someone can help out or tell me where could be the problem. My program also uses SPI1, RTC and TempSensor and VREFINT measurements.

Here are parts of my code:

/*

* Configure TIM2 port:

* PA1 - TIM2_CH2

*/

GPIO_InitStruct.Pin = GPIO_PIN_1;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF14_TIM_IC2;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* TIM2 init function */

void

MX_TIM2_Init(

void

)

{

__TIM2_CLK_ENABLE(); //Added

TIM_SlaveConfigTypeDef sSlaveConfig;

TIM_MasterConfigTypeDef sMasterConfig;

TIM_IC_InitTypeDef sConfigIC;

htim2.Instance = TIM2;

htim2.Init.Prescaler = 0;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 0xFFFF;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

HAL_TIM_Base_Init(&htim2);

HAL_TIM_IC_Init(&htim2);

// sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;

// sSlaveConfig.InputTrigger = TIM_TS_ITR0;

// HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig);

//

// sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

// sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

// HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);

sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;

sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;

sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;

sConfigIC.ICFilter = 0;

// HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);

HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);

// HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_3);

//

// HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_4);

/**

* @brief Measure Frequency

* @param Period

* @retval None

*/

void

TIMMeas(int8_t *Period)

{

measDone =

false

;

/*##-3- Start the Input Capture in interrupt mode ##########################*/

HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);

while

(measDone !=

true

);

HAL_TIM_IC_Stop_IT(&htim2, TIM_CHANNEL_2);

}

call_back function as in code example:

/**

* @brief Conversion complete callback in non blocking mode

* @param htim : hadc handle

* @retval None

*/

void

HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

if

(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)

{

if

(uhCaptureIndex == 0)

{

/* Get the 1st Input Capture value */

uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

uhCaptureIndex = 1;

}

else

if

(uhCaptureIndex == 1)

{

/* Get the 2nd Input Capture value */

uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

/* Capture computation */

if

(uwIC2Value2 > uwIC2Value1)

{

uwDiffCapture = (uwIC2Value2 - uwIC2Value1);

}

else

if

(uwIC2Value2 < uwIC2Value1)

{

uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2);

}

else

{

}

/* Frequency computation: for this example TIMx (TIM1) is clocked by

APB2Clk */

uwFrequency = HAL_RCC_GetPCLK2Freq() / uwDiffCapture;

uhCaptureIndex = 0;

measDone =

true

;

}

}

}

Thanks for the help. Let me know if anything is unclear in my explanation.

4 REPLIES 4
nicolas2399
Associate II
Posted on November 16, 2015 at 15:21

Why do I see this in TIM2 register?

 0690X00000603LLQAY.jpg

Posted on November 16, 2015 at 17:22

Guess you'd want to double check the clocks, and check IAR's documentation about how their debugger works, or why it might report in this fashion.

BTW

uwDiffCapture = (uwIC2Value2 - uwIC2Value1);

// Works for ALL unsigned 16-bit values, your other math is broken
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
nicolas2399
Associate II
Posted on November 17, 2015 at 10:14

So now I get the frequency (polling fro flags) but still interrupts aren't working.

'W' means write-only register. But DIER is actually a RW register. So I guess it is only a debugger problem. DIER contains bits to enable IC interrupts as far as I know.

Thanks for the remark on computation.

nicolas2399
Associate II
Posted on November 17, 2015 at 11:03

Ok it seems to work now. I must yet try to compute 4-5 channels simultaneously.

Thanks for the helps