cancel
Showing results for 
Search instead for 
Did you mean: 

capture low frequency with very high precision

mstahl
Associate
Posted on July 14, 2010 at 09:31

capture low frequency with very high precision

1 REPLY 1
jgoldberg
Associate II
Posted on May 17, 2011 at 13:58

Hey Michael,

I've used the following code before to make a 32-bit capture by counting overflows and avoiding race conditions with the update (overflow) interrupt.  Hope it helps.

void TIM4_IRQHandler (void)

{

    u32 capture32;

    static u32 ovf_ctr = 0;

    if ((TIM4->SR & TIM_IT_CC1) && (TIM4->DIER & TIM_IT_CC1)) {

        TIM4->SR &= ~TIM_IT_CC1;

        capture32 = (ovf_ctr << 16) | TIM4->CCR1;

        if ((TIM4->SR & TIM_IT_Update) && ((capture32 & 0x8000) == 0)) {

            capture32 += 0x10000;

        }

        /* add processing code here */

    }

    if ((TIM4->SR & TIM_IT_CC2) && (TIM4->DIER & TIM_IT_CC2)) {

        TIM4->SR &= ~TIM_IT_CC2;

        capture32 = (ovf_ctr << 16) | TIM4->CCR2;

        if ((TIM4->SR & TIM_IT_Update) && ((capture32 & 0x8000) == 0)) {

            capture32 += 0x10000;

        }

        /* add processing code here */

    }

    if ((TIM4->SR & TIM_IT_Update) && (TIM4->DIER & TIM_IT_Update)) {

        if (((TIM4->SR & TIM_IT_CC1) && (TIM4->DIER & TIM_IT_CC1)) ||

            ((TIM4->SR & TIM_IT_CC2) && (TIM4->DIER & TIM_IT_CC2)) ) {

            /* do not process overflow if capture is still pending */

        } else {

            TIM2->SR = (u16)~TIM_IT_Update;

            ovf_ctr++;

       }

    }

}