cancel
Showing results for 
Search instead for 
Did you mean: 

problems about STM32F446 TIM2 as External counter

lzyfighting2012
Associate
Posted on April 05, 2016 at 15:49

I'm using STM32F446RE NUCLEO and I need to count the external pulse.I don't know how to configurate it to enable the counter.the value of counter is 0 whatever the input fruquency.

the following code is my configuration function.Thanks for your helping!

void TIM2_Configuration(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;

    RCC_ClocksTypeDef RCC_ClockFreq;

    

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);

    

    RCC_GetClocksFreq(&RCC_ClockFreq);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    

   TIM_DeInit(TIM2);

   TIM_TimeBaseStructure.TIM_Period = 0xFFFF;

TIM_TimeBaseStructure.TIM_Prescaler = 2;

TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Time base configuration

TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);

TIM_SetCounter(TIM2, 0);

TIM_Cmd(TIM2, ENABLE);

}

#timer #stm32f4
1 REPLY 1
Walid FTITI_O
Senior II
Posted on April 05, 2016 at 18:27

Hi u.zhiyan,

The

TIM_Deinit() is disabling the clock of your Timer. Try to avoid it in that level and keep it on the top before enabling clock.

-Hannibal-