- TIM1_ICInitStructure.TIM_ICFilter = 0x03;//Filter:过滤
- TIM_PWMIConfig(TIM1, &TIM1_ICInitStructure);//PWM输入配置
- TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);//选择有效输入端
- TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);//配置为主从复位模式
- TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);//启动定时器的被动触发
- // TIM_ITConfig(TIM1, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中断配置
- TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE); //通道2 捕获中断打开
- //TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
- TIM_Cmd(TIM1, ENABLE);
- }
- void TIM1_CC_IRQHandler(void)
- {
- {
- if (TIM_GetITStatus(TIM1, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
- {
- duty_TIM1=TIM_GetCapture1(TIM1); //采集占空比
- if(TIM_GetCapture2(TIM1)>600)period_TIM1=TIM_GetCapture2(TIM1);
- CollectFlag_TIM1 = 0;
- }
- }
- TIM_ClearITPendingBit(TIM1, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
- }
- void TIM2_PWMINPUT_INIT(u16 arr,u16 psc)
- {
- TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
- NVIC_InitTypeDef NVIC_InitStructure;//中断配置
- TIM_ICInitTypeDefTIM2_ICInitStructure;//TIM2PWM配置结构体
- GPIO_InitTypeDef GPIO_InitStructure;//IO口配置结构体
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//Open TIM2 clock
- // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//open gpioB clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);//使能GPIO外设和AFIO复用功能模块时钟
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//关闭JTAG
- GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer2完全重映射TIM2_CH2->PB3
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//GPIO 3
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//浮空输入 上拉输入
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
- TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2;
- TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
- TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
- TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_div1;
- TIM2_ICInitStructure.TIM_ICFilter = 0x3;//Filter:过滤
- TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure);//PWM输入配置
- TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);//选择有效输入端
- TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);//配置为主从复位模式
- TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//启动定时器的被动触发
- TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_Update, ENABLE);//中断配置
- TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
- TIM_Cmd(TIM2, ENABLE);
- }
- void TIM2_IRQHandler(void)
- {
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)//捕获1发生捕获事件
- {
- duty_TIM2=TIM_GetCapture1(TIM2); //采集占空比
- if(TIM_GetCapture2(TIM2)>600)period_TIM2=TIM_GetCapture2(TIM2);
- CollectFlag_TIM2 = 0;
- }
- }
- TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //清除中断标志位
- }
-
- void TIM5_PWMINPUT_INIT(u16 arr,u16 psc)
- {
- TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM的初始化结构体
- NVIC_InitTypeDef NVIC_InitStructure;//中断配置
- TIM_ICInitTypeDefTIM5_ICInitStructure;//TIM4PWM配置结构体
- GPIO_InitTypeDef GPIO_InitStructure;//IO口配置结构体