cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble Using Input Capture with HAL Library

valentin2399
Associate
Posted on September 20, 2016 at 19:00

Hello everyone,

A week's search didn't bring me very far, so I thought I should ask You guys. I have some experience using Arduino, but this is my first hard-core microcontroller project and so far the learning curve has been insane.

Anyway, I am using a stm32f4-discovery board, as an IDE I use Eclipse with the help of the HAL library. TIM3 is set up for base interrupts every 500ms for blinking two of the LEDs. I now want to set up TIM2 with the same type handle as TIM3 and attach an input capture on Channel 1 (PA0). I have connected PA0 and one of the LED pins. The goal is to get a counter value, which should be equivalent to 500ms. 

The problem is that I never enter the input capture interrupt. Can anyone tell me what is wrong with the code? See below:

&sharpinclude <stdio.h>

&sharpinclude <stdlib.h>

&sharpinclude ''diag/Trace.h''

&sharpinclude <stm32f4xx_hal.h>

TIM_HandleTypeDef htim3;

TIM_HandleTypeDef htim2;

volatile uint32_t prev_time;

volatile uint32_t pulse_length;

volatile uint32_t count;

void InitializeLED()

{

__GPIOD_CLK_ENABLE();

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.Pin   = GPIO_PIN_12|GPIO_PIN_14;

GPIO_InitStructure.Mode  = GPIO_MODE_OUTPUT_PP;

GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;

GPIO_InitStructure.Pull  = GPIO_NOPULL;

HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);

}

void InitializeTimer3()

{

__TIM3_CLK_ENABLE();

htim3.Instance               = TIM3;

htim3.Init.Prescaler         = 48000;

htim3.Init.CounterMode       = TIM_COUNTERMODE_UP;

htim3.Init.Period            = 499;

htim3.Init.ClockDivision     = TIM_CLOCKDIVISION_DIV1;

htim3.Init.RepetitionCounter = 0;

HAL_TIM_Base_Init(&htim3);

HAL_TIM_Base_Start_IT(&htim3);

HAL_NVIC_SetPriority(TIM3_IRQn, 1, 0);

HAL_NVIC_EnableIRQ(TIM3_IRQn);

}

void InitializeTimer2()

{

__HAL_RCC_GPIOA_CLK_ENABLE();

__TIM2_CLK_ENABLE();

TIM_IC_InitTypeDef sConfigIC;

htim2.Instance = TIM2;

htim2.Init.Prescaler = 48000;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 65535;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_BOTHEDGE;

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_Init(&htim2);

HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);

HAL_NVIC_SetPriority(TIM2_IRQn, 2, 0);

HAL_NVIC_EnableIRQ(TIM2_IRQn);

}

extern ''C'' void TIM3_IRQHandler()

{

HAL_TIM_IRQHandler(&htim3);

}

extern ''C'' void TIM2_IRQHandler()

{

HAL_TIM_IRQHandler(&htim2);

}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12|GPIO_PIN_14);

}

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

pulse_length = __HAL_TIM_GetCompare(&htim2, TIM_CHANNEL_1); //read TIM2 channel 1 capture value

__HAL_TIM_SetCounter(&htim2, 0); //reset counter after input capture interrupt occurs

}

void main()

{

InitializeLED();

InitializeTimer3();

InitializeTimer2();

while(1)

{

count     = __HAL_TIM_GetCounter(&htim2);

}

}

#hal-library #input-capture
1 REPLY 1
Walid FTITI_O
Senior II
Posted on September 21, 2016 at 13:52

Hi BesenSelqnin,

Yu should configure the TIM2_CH1 PA0 as well . ( put alternate function AF1).

I think also that the line inside the main while loop is useless since you wil calculate the signal inside the callback.

I recommend that you check the ready-to-use example inside the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

called ''TIM_InputCapture'' similar to you r case, at this path : STM32Cube_FW_F4_V1.13.0\Projects\STM324xG_EVAL\Examples\TIM\TIM_InputCapture

-Hannibal-