void TIM3_GPIO_Config(void) //IO
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM3_Mode_Config(void) //MODE
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseStructure.TIM_Period=0xffff;
TIM_TimeBaseStructure.TIM_Prescaler= 71;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*--------------------输入捕获结构体初始化-------------------*/
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(TIM3, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
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(TIM3, &TIM_ICInitStructure);
TIM_ITConfig(TIM3, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_Update, DISABLE);
TIM_ClearITPendingBit(TIM3, TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2);
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_Config(void) //TIM3初始化
{
TIM3_GPIO_Config();
NVIC_Config(); //NVIC写的超级长 就不贴了
TIM3_Mode_Config();
}
/************************************************************************
* @描述: 定时器3中断服务函数,双通道 输入捕获 测频率
* @参数: None
* @返回值: None
**********************************************************************/
FlagStatus LEDStatus = SET;
u32 FAN_Frequency1=0,FAN_Frequency2=0;
u16 IC_ReadValue1=0,IC_ReadValue2=0,CaptureNumber1=0;
u16 IC_ReadValue3=0,IC_ReadValue4=0,CaptureNumber2=0;
u32 TIM2Capture1=0,TIM2Capture2=0;
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
if(CaptureNumber1 ==0 )
{
IC_ReadValue1 = TIM_GetCapture1(TIM3);
CaptureNumber1 = 1;
}
else if (CaptureNumber1 ==1 )
{
IC_ReadValue2 = TIM_GetCapture1(TIM3);
if(IC_ReadValue2 >IC_ReadValue1 )
{
TIM2Capture1 =(IC_ReadValue2 -IC_ReadValue1 );
}
else
{
TIM2Capture1 =(0xFFFF-IC_ReadValue1+IC_ReadValue2);
}
CaptureNumber1 = 0;
}
FAN_Frequency1 = (u32)1000000/(TIM2Capture1);
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
if(CaptureNumber2 ==0 )
{
IC_ReadValue3 = TIM_GetCapture2(TIM3);
CaptureNumber2 = 1;
}
else if (CaptureNumber2 ==1 )
{
IC_ReadValue4 = TIM_GetCapture2(TIM3);
if(IC_ReadValue4 >IC_ReadValue3 )
{
TIM2Capture2 =(IC_ReadValue4 -IC_ReadValue3 );
}
else
{
TIM2Capture2 =(0xFFFF-IC_ReadValue3+IC_ReadValue4);
}
CaptureNumber2 = 0;
}
FAN_Frequency2 = (u32)1000000/(TIM2Capture2);
}
} |