CH32V307控制舵机MG995转向
只保留改变CCR寄存器值,输出占空比稳定
当取消下面注释之后,输出占空比紊乱,希望大佬帮助一下,在STM32中同样的程序运行输出占空比稳定
代码部分:
void initPwm(void)
{
? ? GPIO_InitTypeDef GPIO_InitStructure={0};
? ? ? ? TIM_OCInitTypeDef TIM_OCInitStructure={0};
? ? ? ? TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
? ? ? ? RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE );
? ? ? ? RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );
? ? ? ? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
? ? ? ? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
? ? ? ? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
? ? ? ? GPIO_Init( GPIOB, &GPIO_InitStructure );
? ? ? ? TIM_TimeBaseInitStructure.TIM_Period = 1000-1;
? ? ? ? TIM_TimeBaseInitStructure.TIM_Prescaler = 1440-1;
? ? ? ? TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
? ? ? ? TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
? ? ? ? TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStructure);
? ? ? ? TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
? ? ? ? TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
? ? ? ? TIM_OCInitStructure.TIM_Pulse = 50;
? ? ? ? TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
? ? ? ? TIM_OC3Init( TIM3, &TIM_OCInitStructure );
? ? ? ? TIM_OC4Init( TIM3, &TIM_OCInitStructure );
? ? ? ? TIM_CtrlPWMOutputs(TIM3, ENABLE );
? ? ? ? TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Disable );
? ? ? ? TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Disable );
? ? ? ? TIM_ARRPreloadConfig( TIM3, ENABLE );
? ? ? ? TIM_Cmd( TIM3, ENABLE );
}
int main(void)
{
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
initPwm();
while(1)
{
? ? TIM_SetCompare4(TIM3, 100);
? ? Delay_Ms(10000);
? ? TIM_SetCompare4(TIM3, 50);
? ? Delay_Ms(10000);
}
}