cancel
Showing results for 
Search instead for 
Did you mean: 

Can't get Input Capture mode to work

DavidL_
Associate III

I want to use input capture mode in my stm32l432kc nucleo-32 board to measure the length of a signal from a sensor of mine. I have set it up the following way:

int main(void) {
  // ...
  MX_USART2_UART_Init();
  MX_TIM16_Init();
  HAL_TIM_IC_Start_IT(&htim16, TIM_CHANNEL_1);
}

static void MX_TIM16_Init(void)
{
  TIM_IC_InitTypeDef sConfigIC = {0};

  htim16.Instance = TIM16;
  htim16.Init.Prescaler = 0;
  htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim16.Init.Period = 8999;
  htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim16.Init.RepetitionCounter = 0;
  htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  HAL_TIM_IC_Init(&htim16);

  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  sConfigIC.ICFilter = 0;
  HAL_TIM_IC_ConfigChannel(&htim16, &sConfigIC, TIM_CHANNEL_1);
}

void HAL_TIM_IC_MspInit(TIM_HandleTypeDef* htim_ic)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(htim_ic->Instance == TIM16)
  {
    __HAL_RCC_TIM16_CLK_ENABLE();

    // Init PA6
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF14_TIM16;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  }
}

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
  const char* msg = "Detected!\r\n";
  HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
}


I have used GPIO_AF14_TIM16 in the GPIO configuration, since the datasheet in the manual contained the following table: https://imgur.com/a/BwVqaxh, I'm not 100% sure that it's correct though.


Does someone have an idea on what I'm doing wrong here?
Thanks in advance!

4 REPLIES 4
AScha.3
Chief II

The tim init seems ok.

And how you start the tim capture action ?

 

If you feel a post has answered your question, please click "Accept as Solution".
DavidL_
Associate III

Ok I found the problem, but I don't understand it yet.

The general idea is that I want to write to PA6 first (the start signal for my sensor) and then switch to the input capture mode to measure the response signal length of the sensor on PA6.

To be able to write to PA6, I have added:

  GPIO_InitStruct.Pin = GPIO_PIN_6;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

to MX_GPIO_Init(). But when I try to write to PA6 from main:

// Wait 1s for the sensor to start after providing power
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, RESET);
delay_ms(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, SET);

HAL_TIM_IC_Start_IT(&htim16, TIM_CHANNEL_1);

 the interrupt of my input capture never gets triggered. If I remove just the lines 2-4 though, which write to the GPIO, the IC callback works just fine.

Why is that?

DavidL_
Associate III

The issue was unrelated to input capture, another function didn't work as expected and thus blocked the execution in the main function.

You cannot use the pin for different functions same time. 

If you want do it, you have to set it to the function (AF setting) you want then,

so if using the pin as gpio , but then as af tim input, you have to switch it to this af again, before use sf function.

AScha3_0-1729430448071.png

So set pin pa6 to af14 , after using it as gpio, before expecting it to act  as af14.

If you feel a post has answered your question, please click "Accept as Solution".