TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 20000-1;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM8,ENABLE);//高级定时器需要设置TIM_BDTR_MOE
TIM_SelectOutputTrigger(TIM8,TIM_TRGOSource_Update);
TIM_SelectMasterSlaveMode(TIM8, TIM_MasterSlaveMode_Enable);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_Cmd(TIM8,ENABLE);
if(angle == 0)
{
TIM_SetCompare2(TIM8,19500);
}
else if(angle == 45)
{
TIM_SetCompare2(TIM8,19000);
}
else if(angle == 90)
{
TIM_SetCompare2(TIM8,18500);
}
else if(angle == 135)
{
TIM_SetCompare2(TIM8,18000);
}
else if(angle == 180)
{
TIM_SetCompare2(TIM8,17500);
}
else
{
TIM_SetCompare2(TIM8,(uint16_t)(19500-angle*11.11));
} |