我在目标板上已经试验也好了,测量频率从0.1hz到50kHz。且精度较高! 下面是我的最终代码: void TIM2_IRQHandler(void) { static u16 Capture_Value1 = 0; /* value of the TIM2 capture */ u16 Capture_Value2 = 0;
static u16 Overflow_Counter = 0; u16 TIM2_SR=0; static u8 flag=0; u8 i=0; u16 Auto_Reload_REG;
Auto_Reload_REG = TIM2->ARR ; TIM2_SR = TIM2->SR;
if((TIM2_SR & 0x0003) == (u16)0x0003) { TIM2->SR &= ~((u16)0x0003);
if(flag != 1 ) { Capture_Value1 = TIM2->CCR1; flag = 1; } else { Capture_Value2 = TIM2->CCR1; if(Capture_Value2 >= Capture_Value1) { Channel1_Frequency = 360000000/((Overflow_Counter+1) * (Auto_Reload_REG+1) - (Capture_Value2 - Capture_Value1)); Capture_Value1 = Capture_Value2; Overflow_Counter = 0; } else { Channel1_Frequency = 360000000/(Overflow_Counter * (Auto_Reload_REG+1) + (Capture_Value1 - Capture_Value2)); Capture_Value1 = Capture_Value2; Overflow_Counter = 1; } } }
else if(TIM2_SR & 0x0002) { TIM2->SR &= ~((u16)0x0002);
if(flag != 1 ) { Capture_Value1 = TIM2->CCR1; flag = 1; } else { Capture_Value2 = TIM2->CCR1; if(Capture_Value2 >= Capture_Value1) { Channel1_Frequency = 360000000/(Overflow_Counter * (Auto_Reload_REG+1) - (Capture_Value2 - Capture_Value1)); Capture_Value1 = Capture_Value2; Overflow_Counter = 0; } else { Channel1_Frequency = 360000000/(Overflow_Counter * (Auto_Reload_REG+1) + (Capture_Value1 - Capture_Value2)); Capture_Value1 = Capture_Value2; Overflow_Counter = 0; } } }
else if( TIM2_SR & 0x0001 ) { TIM2->SR &= ~((u16)0x0001); Overflow_Counter++; }
} |