AnsweredAssumed Answered

incrementation of a timer : problem

Question asked by claire.mutschler on May 14, 2012
Latest reply on Feb 25, 2013 by grubbstrom.carl
hi,

i am working on the stm32discovery
i am trying to increment a timer (timer2 channel 1) each time the signal on the pin PA.05 is rising but the timer is random incrementing and not at each rising edge of the signal
i don't understand why ?
here is my code :

int main(void)
{

    /* Configure Clocks for Application need */
    RCC_Configuration();
    /* Configure RTC Clocks */
    //RTC_Configuration();
      /* Configure SysTick IRQ and SysTick Timer to generate interrupts every 500µs */
    RCC_GetClocksFreq(&RCC_Clocks);
    //SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000);

    /* Enable GPIOs clock */     

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);

    /*PA.05 TIM2 channel 1*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);

    TIM_InitStruct.TIM_Period = 849;
    TIM_InitStruct.TIM_Prescaler = 0;
    TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2,&TIM_InitStruct);

    /*timer2 chanel 1  ETR pin PA5*/
    TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitStruct.TIM_ICSelection  = TIM_ICSelection_DirectTI;
    TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStruct.TIM_ICFilter = 0x0;
    TIM_ICInit(TIM2, &TIM_ICInitStruct);

    /*enable the TIM counter*/
    TIM_Cmd(TIM2, ENABLE);

    TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);
    TIM_ETRClockMode1Config(TIM2,TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted,0);

  while (1){
      SysTick_Config(RCC_Clocks.HCLK_Frequency / 2000);
    bitStatut = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5);
    increment = TIM_GetCapture1(TIM2);
    Delay(100);

  }
could you help me ?
thanks

Outcomes