AnsweredAssumed Answered

Trouble Using Input Capture with HAL Library

Question asked by BesenSelqnin on Sep 20, 2016
Latest reply on Sep 21, 2016 by FTITI.Walid
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:

#include <stdio.h>
#include <stdlib.h>
#include "diag/Trace.h"
#include <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()
     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()
     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_NVIC_SetPriority(TIM3_IRQn, 1, 0);

void InitializeTimer2()
     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.ICPrescaler = TIM_ICPSC_DIV1;
     sConfigIC.ICFilter = 0;
     HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);
     HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
     HAL_NVIC_SetPriority(TIM2_IRQn, 2, 0);

extern "C" void TIM3_IRQHandler()

extern "C" void TIM2_IRQHandler()

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()

          count     = __HAL_TIM_GetCounter(&htim2);