cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476 input capture

jonathan239955_stm1_st
Associate II
Posted on July 13, 2016 at 14:08

Hello,

I am working with the boardNucleo-L476RG and I wantto capture frequency on the pin PA0 with TIM2 Channel1. My timer is working but I can't get the interrupt in the function

TIM2_IRQHandler()

. I am probably doing something wrong but I can't find example with this board. Here is my code :

#include ''mbed.h''
TIM_HandleTypeDef timerStruct;
TIM_IC_InitTypeDef counterStruct;
void TIM2_Configuration(void);
void TIM2_Configuration()
{
__HAL_RCC_TIM2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
// Clock source
TIM_ClockConfigTypeDef sClockSourceConfig;
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&timerStruct, &sClockSourceConfig);
// GPIO
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Interrupt enable
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
/* Input Capture Init */
timerStruct.Instance = TIM2;
timerStruct.Init.Period = 0xffff;
timerStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
timerStruct.Init.Prescaler = 0;
timerStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_IC_Init(&timerStruct);
/* Config Channel */
counterStruct.ICFilter = 0;
counterStruct.ICPolarity = TIM_ICPOLARITY_RISING;
counterStruct.ICPrescaler = TIM_ICPSC_DIV1;
counterStruct.ICSelection = TIM_ICSELECTION_DIRECTTI;
HAL_TIM_IC_ConfigChannel(&timerStruct, &counterStruct, TIM_CHANNEL_1);
}
int main(void)
{
TIM2_Configuration(); 
HAL_TIM_IC_Start_IT(&timerStruct, TIM_CHANNEL_1);
printf(''Timer configured \r\n'');
while(1) 
{
}
}
/* IRQ */
void TIM2_IRQHandler(void)
{
printf(''IRQ'');
}

Thanks for your help. Best Regards, Jonathan
2 REPLIES 2
Nesrine M_O
Lead II
Posted on July 13, 2016 at 15:16

Hi Jonathan,

“ I am probably doing something wrong but I can't find example with this board.�

You find many example with the STM32L476RG-Nucleo Board under the STM32L4 cube firmware package:

•STM32Cube_FW_L4_V1.5.0\Projects\STM32L476RG-Nucleo\Examples

Also you find an example that show how to use the TIM peripheral to measure the frequency of an external signal under the same package.

•STM32Cube_FW_L4_V1.5.0\Projects\STM32L476RG-Nucleo\Examples\TIM\TIM_InputCapture

-Syrine-

jonathan239955_stm1_st
Associate II
Posted on July 18, 2016 at 09:18

Hi Syrine,

Thanks for your answer and the examples. This is what I was looking for. 

Best Regards,

Jonathan