配置如下
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0;
TIM_TimeBaseStructure.TIM_Prescaler = 63;//予分频 时钟预分频+1
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//倍频系数
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方向
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM2,DISABLE); //允许重装 TIM_Period DISABLE
//TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_ICAP ;//输入扑获
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;//通道4
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;//下降沿 TIM_ICPolarity_Rising;//
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//管脚与寄存器对应关系 TIM2选择对应和IC1 或 IC2 或 IC3 或 IC4 相连
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV2;//2次事件执行1次
TIM_ICInitStructure.TIM_ICFilter = 0;//选择输入比较滤波器,滤波设置,经历几个周期跳变认定波形稳定0x0~0xF
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
// TIM_ICInit(TIM2, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR2);//选择时钟输入触发源 ,经过滤波定时器输入2
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);//TIM2复位模式为从模式,选择触发信号(TRGI)的上升沿重初始化计数器并触发寄存器的更新
//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//选择主从模式,主从模式使能,启动定时器的被动触发
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the CC2 Interrupt Request */
//TIM_ITConfig(TIM4, TIM_IT_Update|TIM_IT_CC4, ENABLE);//捕获中断源4
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);//捕获中断源4
|