CH32V307定時(shí)器TIM1的PWM多通道輸出

本人在嘗試使用定時(shí)器1來驅(qū)動舵機(jī)時(shí),發(fā)現(xiàn)通道2無法輸出正確的信號來驅(qū)動舵機(jī),通道1、3、4均可以正常使用;

附上舵機(jī)驅(qū)動程序

#include?"Server.h"
#include?"ch32v30x_rcc.h"

static?void?TIM1_PWMOut_Init(u16?arr,?u16?psc,?u16?ccp)?{
????GPIO_InitTypeDef?GPIO_InitStructure?=?{?0?};
????TIM_OCInitTypeDef?TIM_OCInitStructure?=?{?0?};
????TIM_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure?=?{?0?};

????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_GPIOA?|?RCC_APB2Periph_TIM1,?ENABLE);

????GPIO_InitStructure.GPIO_Pin?=?GPIO_Pin_8?|?GPIO_Pin_9?|?GPIO_Pin_10?|?GPIO_Pin_11;
????GPIO_InitStructure.GPIO_Mode?=?GPIO_Mode_AF_PP;
????GPIO_InitStructure.GPIO_Speed?=?GPIO_Speed_50MHz;
????GPIO_Init(?GPIOA,?&GPIO_InitStructure);

????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_TimeBaseInit(?TIM1,?&TIM_TimeBaseInitStructure);

#if?(PWM_MODE?==?PWM_MODE1)
????TIM_OCInitStructure.TIM_OCMode?=?TIM_OCMode_PWM1;

#elif?(PWM_MODE?==?PWM_MODE2)
????TIM_OCInitStructure.TIM_OCMode?=?TIM_OCMode_PWM2;
#endif

????TIM_OCInitStructure.TIM_OutputState?=?TIM_OutputState_Enable;
????TIM_OCInitStructure.TIM_Pulse?=?ccp;
????TIM_OCInitStructure.TIM_OCPolarity?=?TIM_OCPolarity_High;
????TIM_OC1Init(?TIM1,?&TIM_OCInitStructure);
????TIM_OC2Init(?TIM1,?&TIM_OCInitStructure);
????TIM_OC3Init(?TIM1,?&TIM_OCInitStructure);
????TIM_OC4Init(?TIM1,?&TIM_OCInitStructure);
????TIM_CtrlPWMOutputs(TIM1,?ENABLE);

????TIM_OC1PreloadConfig(?TIM1,?TIM_OCPreload_Disable);
????TIM_OC2PreloadConfig(?TIM1,?TIM_OCPreload_Disable);
????TIM_OC3PreloadConfig(?TIM1,?TIM_OCPreload_Disable);
????TIM_OC4PreloadConfig(?TIM1,?TIM_OCPreload_Disable);
????TIM_ARRPreloadConfig(?TIM1,?ENABLE);

????TIM_Cmd(?TIM1,?ENABLE);
}

void?server_init()?{
????TIM1_PWMOut_Init(20000,?96?-?1,?20000?-?2500);
}

void?SetServerAnger(int?angle)?{
????TIM_SetCompare1(TIM1,?20000?-?(angle?*?ANGLE)?-?500);
????TIM_SetCompare2(TIM1,?20000?-?(angle?*?ANGLE)?-?500);
????TIM_SetCompare3(TIM1,?20000?-?(angle?*?ANGLE)?-?500);
????TIM_SetCompare4(TIM1,?20000?-?(angle?*?ANGLE)?-?500);
}


您好,可以檢查一下該通道所用PA9是不是和串口打印的TX引腳沖突了。例程應(yīng)該默認(rèn)串口1打印,Tx引腳用的也是PA9引腳,打印串口可以通過相關(guān)宏定義改為串口2試一下,如下圖

image.png


謝謝,這個(gè)問題得到了完美的解決!再次感謝你的幫助


只有登錄才能回復(fù),可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频