本帖最后由 感动 于 2019-9-16 20:42 编辑
前面的**介绍了单片机控制步进电机四相四拍、四相八拍的方式。用的是持续电平驱动,这种驱动方式电机的噪音大,震动明显,正弦波驱动方式,这种方式能很好的解决噪音和震动问题。前面的**也介绍了使用定时器产生符合正弦波规律的PWM,下面教大家用正弦波替代持续电平。
这是定时器的配置部分,里面配置了PWM的输出IO管脚,配置了定时器,配置了定时器更新中断。
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, 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_TimeBaseStructure.TIM_Period=512;
TIM_TimeBaseStructure.TIM_Prescaler= 7;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse=512;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
}
定时器更新中断按照四相四拍的要求使能或禁止定时器PWM输出,代码如下
void TIM1_UP_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
pwm_index++;
if(pwm_index>35)
{
pwm_index=0;
pwm_count++;
if(pwm_count>3)pwm_count=0;
if(pwm_count==0)TIM1->CCER=0x2ABB;
if(pwm_count==1)TIM1->CCER=0x2BBA;
if(pwm_count==2)TIM1->CCER=0x3BAA;
if(pwm_count==3)TIM1->CCER=0x3AAB;
}
TIM1->CCR1 = indexWave[pwm_index];
TIM1->CCR2 = indexWave[pwm_index];
TIM1->CCR3 = indexWave[pwm_index];
TIM1->CCR4 = indexWave[pwm_index];
TIM_ClearITPendingBit (TIM1, TIM_IT_Update);
}
}
至此完成了四相四拍的正弦波驱动,实验证明,正弦波驱动步进电机噪音和震动明显减小 这是PWM输出的波形图
这是实物图
这是程序
正弦波PWM - 四相四拍.rar
(4.37 MB)
更多精彩内容关注我的博客https://blog.csdn.net/gd1984812
|