更改后的中断程序
/******************************************************************************* * Function Name : TIM2_IRQHandler * Description : This function handles TIM2 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ u16 Capture_Value1 = 0; /* value of the TIM2 capture */ u16 Capture_Value2 = 0; static u8 flag=0; u16 Overflow_Counter = 0; u16 TIM2_SR=0; void TIM2_IRQHandler(void) { u8 i=0; u16 Auto_Reload_REG; Auto_Reload_REG = TIM2->ARR; TIM2_SR = TIM2->SR; if(TIM2_SR == 0x0003) { i++; } 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 - (Capture_Value2 - Capture_Value1)); Capture_Value1 = Capture_Value2; Overflow_Counter = 0; } else { Channel1_Frequency = 360000000/(Overflow_Counter * Auto_Reload_REG + (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 - (Capture_Value2 - Capture_Value1)); Capture_Value1 = Capture_Value2; Overflow_Counter = 0; } else { Channel1_Frequency = 360000000/(Overflow_Counter * Auto_Reload_REG + (Capture_Value1 - Capture_Value2)); Capture_Value1 = Capture_Value2; Overflow_Counter = 0; } } } else if( TIM2_SR & 0x0001 ) { TIM2->SR &= ~((u16)0x0001); Overflow_Counter++; } }