CH32V003输入捕获和PWM输出

我是用了定时器2的2通道产生40KHZ的方波(占空比50%),然后定时器2的1通道和3通道去捕获这个波形,现在是3能捕获到,1都捕获不到,请问是什么问题吗?

void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/*********************************************************************

 * @fn      TIM1_CC_IRQHandler

 *

 * @brief   This function handles TIM1  Capture Compare Interrupt exception.

 *

 * @return  none

 */

// 定时器2中断服务函数

void TIM2_IRQHandler(void)

{

    if(TIM_GetITStatus(TIM2, TIM_IT_CC1)!= RESET)

    {

        // 捕获到CH1通道事件,记录当前计数器的值

        capture_value1 = TIM_GetCapture1(TIM2);

        printf("capture_value1:%d\r\n",capture_value1);

        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

    }

    if(TIM_GetITStatus(TIM2, TIM_IT_CC3)!= RESET)

    {

        // 捕获到CH3通道事件,记录当前计数器的值

        capture_value3 = TIM_GetCapture3(TIM2);

        printf("capture_value3:%d\r\n",capture_value3);

        TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

    }

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除中断标志位

}


void TIM2_Init(uint16_t arr, uint16_t psc,uint16_t ccp)

{

    GPIO_InitTypeDef        GPIO_InitStructure = {0};

    TIM_ICInitTypeDef       TIM_ICInitStructure = {0};

    TIM_OCInitTypeDef       TIM_OCInitStructure= {0};

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};

    NVIC_InitTypeDef        NVIC_InitStructure = {0};


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 ,ENABLE);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;        //配置PD3引脚

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //设置为复用推挽输出

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置输出速度:30MHz

    GPIO_Init(GPIOD, &GPIO_InitStructure );         //GPIO初始化


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PC0 输入捕获

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_ResetBits(GPIOC, GPIO_Pin_0);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PD4 输入捕获

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_ResetBits(GPIOD, GPIO_Pin_4);


    TIM_TimeBaseInitStructure.TIM_Period = arr;

    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;

    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

    //TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);


    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;            //指定TIM模式

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//指定TIM输出比较状态,即使能比较输出

    TIM_OCInitStructure.TIM_Pulse = ccp;                        //指定要加载到捕获比较寄存器中的脉冲值。

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    //指定输出极性。

    TIM_OC2Init(TIM2, &TIM_OCInitStructure );                    //根据TIM_OCInitStruct中指定的参数初始化TIM2 Channel2。



    TIM_CtrlPWMOutputs(TIM2, ENABLE );                  //启用定时器2PWM输出

    TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);//使能CCR1上的TIM2外设预加载寄存器

    TIM_ARRPreloadConfig(TIM2, ENABLE );               //使能ARR上TIM2外设预加载寄存器

    // 配置CH2通道产生PWM信号


    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1|TIM_Channel_3;

    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

    TIM_ICInitStructure.TIM_ICFilter = 0x00;

    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

    TIM_ICInit(TIM2, &TIM_ICInitStructure);



    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);


    TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC3, ENABLE);

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);


    TIM_Cmd(TIM2, ENABLE);

}


image.png老哥,这里不能这么写,两个通道要分开,看下TIM_ICInit()里面的内容就知道了


感谢感谢,我分开了是可以了,但是现在有个问题是通道3和通道2用杜邦线连接起来是正常的,拔掉之后还是能捕获到信号,通道1和通道2连接和断开都正常



只有登录才能回复,可以选择微信账号登录