2010-07-14 12:31 AM
capture low frequency with very high precision
2011-05-17 04:58 AM
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++; } } }