改变占空比不能调节步进电机速度,
另外,
有必要开启更新中断吗?
void PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_ClockDivision=0;//系统时钟不分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_Period=4999; //装载值
TIM_TimeBaseStructure.TIM_Prescaler=29;//分频系数
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//PWM1模式
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//输出极性高
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//使能
TIM_OCInitStructure.TIM_Pulse=3600;//比较值
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);//使能
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3,ENABLE);//使能
}
int main(void)
{
PWM_Init();//PWM初始化
delay_init();//延时初始化
while(1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>