CH32V307 定时器输出翻转模式

    GPIO_InitTypeDef GPIO_InitStructure={0};
    NVIC_InitTypeDef NVIC_InitStructure = {0};
    TIM_OCInitTypeDef TIM_OCInitStructure={0};
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_TIM8 | RCC_APB2Periph_AFIO, ENABLE );

    GPIO_PinRemapConfig(GPIO_Remap_TIM8 , ENABLE);

    GPIO_InitStructure.GPIO_Pin = X_PUL_GPIO;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( X_PUL_PORT, &GPIO_InitStructure );

    TIM_TimeBaseInitStructure.TIM_Period = PWM_ARR;     /* 自动重装载值 */
    TIM_TimeBaseInitStructure.TIM_Prescaler = PWM_PSC;  /* 定时器分频 */
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( X_TIM, &TIM_TimeBaseInitStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = PWM_PLU;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC3Init( X_TIM, &TIM_OCInitStructure );

    TIM_CtrlPWMOutputs(X_TIM, DISABLE );
    TIM_OC3PreloadConfig( X_TIM, TIM_OCPreload_Disable );
    TIM_ARRPreloadConfig( X_TIM, ENABLE );
    TIM_Cmd( X_TIM, ENABLE );
    TIM_ITConfig(X_TIM, TIM_IT_CC3, DISABLE);

    NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


开中断

    TIM_ITConfig(X_TIM, TIM_IT_CC3, ENABLE);

这样配置对面?中断里面设置比较值没有用?
        Tim_Count = TIM_GetCounter(X_TIM);
        tmp = 0xFFFF & (Tim_Count + g_toggle_pulse);
        printf("tmP = %d\r\n",tmp);
        TIM_SetCompare3(X_TIM,tmp);//修改占空比



有例程吗?

您好,若程序中使用了中断函数,注意要加中断声明,如下图。附件为参考例程,可以参考一下

icon_rar.gifCH32V307 TIM翻转模式.zip

image.png


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