Skip to main content
Rogers.Gary
Senior II
April 25, 2017
Solved

STM32F4 doesn't capture correct number of pulses

  • April 25, 2017
  • 1 reply
  • 737 views
Posted on April 25, 2017 at 19:09

Hello:

I'm using an STM32F4 to capture low frequency pulses below 50Hz. The source is sending exactly 20 pulses, with a DC of about 25%. The input pin to the STM32F4 confirms 20 pulses. However, the STM32F4 seems to capture only half of them -10. (i.e., I printf the T2_TIM2Freq value and see only 10 statements) If I set the source to burst 16 pulses, it captures (prints) 8. Here's the setup and the interrupt. Can someone see something that would cause this?

Thanks,

Gary

void TIM2configurationPA5(void)

{

    NVIC_InitTypeDef NVIC_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    

    /* TIM2 Periph clock enable */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    

    /* TIM2 channel 1 pin (PA.5) configuration */

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_Init(GPIOA, &GPIO_InitStructure);    

    /* Connect TIM pins to AF2 */

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);

    

    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

 

    TIM_TimeBaseStructure.TIM_Prescaler = 0;

    TIM_TimeBaseStructure.TIM_Period = (uint32_t)0xFFFFFFFF;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

   

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

 

    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;

    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising | TIM_ICPolarity_Falling;

    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

    TIM_ICInitStructure.TIM_ICFilter = 0x0;

    TIM_ICInit(TIM2, &TIM_ICInitStructure);

 

    /* TIM enable counter */

    TIM_Cmd(TIM2, ENABLE);

    /* Enable the CC2 Interrupt Request */

    TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);    

}

void TIM2_IRQHandler(void)

{

    if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)

    {

        /* Clear TIM2 Capture compare interrupt pending bit */

        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);  

        

        if(T2_captureNumber == 0)

        {

            T2_readValue1 = TIM_GetCapture1(TIM2);

            T2_captureNumber = 1;

        }

        else if(T2_captureNumber == 1)

        {

            T2_readValue2 = TIM_GetCapture1(TIM2);

         

            if (T2_readValue2 > T2_readValue1)

            {

                T2_uwCapture = (T2_readValue1 - T2_readValue2);                 

            }

            else if (T2_readValue2 < T2_readValue1)

            {

                T2_uwCapture = ((0xFFFFFFFF - T2_readValue2) + T2_readValue1);                 

            }

            else

            {

                T2_uwCapture = 0;

            }

            

            T2_TIM2Freq = (float32_t) (((SystemCoreClock / 2)/1.0) / (T2_uwCapture/1.0));

                    

            T2_captureNumber = 0;                            

        }

    }

}
    This topic has been closed for replies.
    Best answer by Tesla DeLorean
    Posted on April 25, 2017 at 19:47

    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising | TIM_ICPolarity_Falling; // NO

                if (T2_readValue2 > T2_readValue1)

                {

                    T2_uwCapture = (T2_readValue1 - T2_readValue2);                 

                }

                else if (T2_readValue2 < T2_readValue1)

                {

                    T2_uwCapture = ((0xFFFFFFFF - T2_readValue2) + T2_readValue1);   // NO

                }

                else

                {

                    T2_uwCapture = 0;

                }

    //  delta = current - last

    // ie 2000 - 1000 = 1000

                    T2_uwCapture = (T2_readValue2 - T2_readValue1); // VALID for ALL 32-bit unsigned values                

    1 reply

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    April 25, 2017
    Posted on April 25, 2017 at 19:47

    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising | TIM_ICPolarity_Falling; // NO

                if (T2_readValue2 > T2_readValue1)

                {

                    T2_uwCapture = (T2_readValue1 - T2_readValue2);                 

                }

                else if (T2_readValue2 < T2_readValue1)

                {

                    T2_uwCapture = ((0xFFFFFFFF - T2_readValue2) + T2_readValue1);   // NO

                }

                else

                {

                    T2_uwCapture = 0;

                }

    //  delta = current - last

    // ie 2000 - 1000 = 1000

                    T2_uwCapture = (T2_readValue2 - T2_readValue1); // VALID for ALL 32-bit unsigned values                

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Rogers.Gary
    Senior II
    April 25, 2017
    Posted on April 25, 2017 at 20:49

    Hi,

    Residual carry over from the 16 bit timer...

    Got it now....thanks.