void TIM2_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
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_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
//TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); //选择时钟触发源
//TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);//触发方式
//TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
//TIM_PrescalerConfig(TIM2, 72-1, TIM_PSCReloadMode_Immediate);//72M/72=1000K
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) == SET)
{
/* Clear TIM2 Capture compare interrupt pending bit */
GPIO_WriteBit(BU,(BitAction)(1 -GPIO_ReadOutputDataBit(BU)));
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
if(CaptureNumber == 0)
{
/* Get the Input Capture value */
IC1ReadValue1 = TIM_GetCapture1(TIM2);
CaptureNumber = 1;
}
else
{
if(CaptureNumber == 1)
{
/* Get the Input Capture value */
IC1ReadValue2 = TIM_GetCapture1(TIM2);
/* Capture computation */
if (IC1ReadValue2 > IC1ReadValue1)
{
Capture = (IC1ReadValue2 - IC1ReadValue1);
}
else
{
Capture = ((0xFFFF - IC1ReadValue1) + IC1ReadValue2);
}
/* Frequency computation */
//TIM2Freq = (uint32_t) SystemCoreClock / Capture;
CaptureNumber = 0;
tim2_temp2++;
if(tim2_temp2<1000)
{cc2[tim2_temp2]=Capture;}
else
{tim2_temp2++;}
exti0_temp=Capture/2;
}
}
}
}
|