STM32F407 高级定时器TIM8 输入捕获。四个捕获通道,互相有影响

2019-07-20 03:41发布


//问题描述:同时能够捕获四个通道的pwm频率。但是当其中任何一个通道连接pwm输入,其他通道并没有连接pwm输入时,
其他通道也会发生捕获中断,同时不连接和连接pwm输入的通道会捕获到同样的pwm频率。


找不到是哪里出了问题,求帮助。

void TIM8_Cap_Init(u16 arr,u16 psc)
{         
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_ICInitTypeDef  TIM_IC1InitStructure;
        TIM_ICInitTypeDef  TIM_IC2InitStructure;
        TIM_ICInitTypeDef  TIM_IC3InitStructure;
        TIM_ICInitTypeDef  TIM_IC4InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);        //使能TIM8时钟
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);  //使能GPIOC时钟
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //GPIOC6
        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_DOWN; //下拉
        GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA0

        GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8); //PC6复用位定时器8
        GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM8); //PC6复用位定时器8
        GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM8); //PC6复用位定时器8
        GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_TIM8); //PC6复用位定时器8

       
        //初始化定时器8 TIM8         
        TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
        TIM_TimeBaseStructure.TIM_Prescaler =psc;         //预分频器   
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//重复计数设置
        TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  TIM_ClearFlag(TIM8, TIM_FLAG_Update);//清中断标志位

        //初始化TIM8输入捕获参数
        TIM_IC1InitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01         选择输入端 IC1映射到TI1上
  TIM_IC1InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //上升沿捕获
  TIM_IC1InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
  TIM_IC1InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //配置输入分频,不分频
  TIM_IC1InitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
  TIM_ICInit(TIM8, &TIM_IC1InitStructure);       
       
        //初始化TIM8输入捕获参数
        TIM_IC2InitStructure.TIM_Channel = TIM_Channel_2; //CC1S=01         选择输入端 IC1映射到TI1上
  TIM_IC2InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //上升沿捕获
  TIM_IC2InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
  TIM_IC2InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //配置输入分频,不分频
  TIM_IC2InitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
  TIM_ICInit(TIM8, &TIM_IC2InitStructure);
        //初始化TIM8输入捕获参数
        TIM_IC3InitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01         选择输入端 IC1映射到TI1上
  TIM_IC3InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //上升沿捕获
  TIM_IC3InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
  TIM_IC3InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //配置输入分频,不分频
  TIM_IC3InitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
  TIM_ICInit(TIM8, &TIM_IC3InitStructure);
        //初始化TIM8输入捕获参数
        TIM_IC4InitStructure.TIM_Channel = TIM_Channel_4; //CC1S=01         选择输入端 IC1映射到TI1上
  TIM_IC4InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        //上升沿捕获
  TIM_IC4InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
  TIM_IC4InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;         //配置输入分频,不分频
  TIM_IC4InitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
  TIM_ICInit(TIM8, &TIM_IC4InitStructure);
       
        //中断分组初始化
        NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;  //TIM1中断
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级2级
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //从优先级0级
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
        NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

        NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;  //TIM1中断
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //先占优先级2级
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //从优先级0级
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
        NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
       
        TIM_ITConfig(TIM8,TIM_IT_CC1,ENABLE);//允许CC1IE捕获中断
        TIM_ITConfig(TIM8,TIM_IT_CC2,ENABLE);//允许CC2IE捕获中断
        TIM_ITConfig(TIM8,TIM_IT_CC3,ENABLE);//允许CC3IE捕获中断
        TIM_ITConfig(TIM8,TIM_IT_CC4,ENABLE);//允许CC4IE捕获中断
       
        TIM_ITConfig(TIM8,TIM_IT_Update,ENABLE);
  TIM_Cmd(TIM8,ENABLE );         //使能定时器8  
}



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。