之前用TIM3等其他定时器作输入捕获均没有问题,程序如下,大伙帮忙看看看
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//启用TIM1的APB2时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//启用GPIOA的AHB1时钟
/* TIM4 chennel2 configuration : PA.10 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //配置引脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //引脚复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //引脚速度为100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //引脚推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; //引脚上拉
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA外设
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1); //PA.10 配置成TIM1_CH3复用功能
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //选定TIM1_IRQn中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //主优先级,数值越低,优先级越高
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //从优先级,数值越低,优先级越高
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //启用TIM1_IRQn
NVIC_Init(&NVIC_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; //选择通道3
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //下降沿触发
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//通道方向选择
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //每次检测到捕获输入就触发一次捕获(控制几个输入周期做一次捕获)
TIM_ICInitStructure.TIM_ICFilter = 0x0; //滤波设置,经过几个周期跳变认为波形稳定
TIM_PWMIConfig (TIM1, &TIM_ICInitStructure);//TIP:改为TIM_ICInit(TIM4, &TIM_ICInitStructure) IC1value一直为0
//TIM1->PSC=41; //TIM1 分频
//清中断,以免一启用中断后立即产生中断
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
/* Select the TIM4 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2); //选择IC2为时钟触发源
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset); //TIM从模式:触发信号的下降沿重新初始化计数器和触发寄存器的更新事件
TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable); //启动TIM2的被动触发
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
/* TIM enable counter */
TIM_Cmd(TIM1, ENABLE);
}
void TIM1_CC_IRQHandler(void)
{
PPM_test++;
/* Clear TIM4 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM1, TIM_IT_CC2); //清楚TIM1的中断待处理位
/* Get the Input Capture value */
IC2Value = TIM_GetCapture2(TIM1); //读取IC2捕获寄存器的值,即为PWM周期的计数值
IC1Value = TIM_GetCapture1(TIM1); //读取IC1捕获寄存器的值,为PWM低电平的计数值
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
在你的提示下,能进入中断了,但还是无法捕获值
一周热门 更多>