21ic问答首页 - MM32F103定时器PWM输入捕获失败
MM32F103定时器PWM输入捕获失败
小迷糊5232023-12-15
配置代码如下,有个PWM信号需要输入捕获计算频率,经查询数据手册PB11可以复用为TIM2_CH4,但怎么配置都不能正常进捕获中断,IC和PWM方式都试了不行,想问各位大佬该配置有啥问题,有没有什么解决办法
/*功能名称IM4_PWMINPUT_INIT(u16 arr,u16 psc)
描述 PWM输入初始化*/
void TIM2_PWMINPUT_INIT(u16 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM2_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //Open TIM4 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //open gpioB clock
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*配置中断优先级*/
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_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的时间基数单位
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_4;
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 = 0x00;
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
// TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure); //PWM输入配置
// TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); //选择有效输入端
// TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //配置为主从复位模式
// TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_ITConfig(TIM2,TIM_IT_CC3 | TIM_IT_CC4, ENABLE); //中断配置
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3 | TIM_IT_CC4); //清除中断标志位
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) { //捕获1发生捕获事件
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) { //捕获1发生捕获事件
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3 | TIM_IT_CC4 |TIM_IT_Update); //清除中断标志位
}
/*功能名称IM4_PWMINPUT_INIT(u16 arr,u16 psc)
描述 PWM输入初始化*/
void TIM2_PWMINPUT_INIT(u16 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM2_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //Open TIM4 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //open gpioB clock
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*配置中断优先级*/
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_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的时间基数单位
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_4;
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 = 0x00;
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
// TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure); //PWM输入配置
// TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); //选择有效输入端
// TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); //配置为主从复位模式
// TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM_ITConfig(TIM2,TIM_IT_CC3 | TIM_IT_CC4, ENABLE); //中断配置
TIM_ClearITPendingBit(TIM2,TIM_IT_CC3 | TIM_IT_CC4); //清除中断标志位
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) { //捕获1发生捕获事件
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) { //捕获1发生捕获事件
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3 | TIM_IT_CC4 |TIM_IT_Update); //清除中断标志位
}
赞0
评论
2023-12-27
赞0
评论
2023-12-26
赞0
评论
2023-12-26
赞0
评论
2023-12-26
赞0
评论
2023-12-26
您需要登录后才可以回复 登录 | 注册