本人在尝试使用定时器1来驱动舵机时,发现通道2无法输出正确的信号来驱动舵机,通道1、3、4均可以正常使用;
附上舵机驱动程序
#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); }
热门产品 :
CH641: PD及无线充电专用MCU