基于STM32F4控制步进电机(定时器单稳态加重复计数)可以实现步进电机的绝对定位和相对定位,高伺服控制
*42步进电机42BYGH34驱动器
TB6600步进电机驱动器**
两相步进电机驱动,可实现正反转控制。通 过S1 S2 S3 3位拨码开关选择8 档细分控制(1、2、4、8、16 ),通过S4 S5 S6 3位拨码开关选择6档电流控制(0.5A,1A,1.5A,2.0A,2.5A,3.0A, 3.5A, 4.0A)。适合驱动86,57,42,39 型两相、四相混合式步进电机。驱动器具有 噪音小,震动小,运行平稳的特点。
3.硬件连接 4、代码实现 主要封装的库里有四个函数
extern long target_pos;
extern long current_pos;
void Driver_Init(void);
void TIM8_OPM_RCR_Init(u16 arr,u16 psc);
void TIM8_Startup(u32 frequency);
void Locate_Rle(long num,u32 frequency,DIR_Type dir);
void Locate_Abs(long num,u32 frequency);
1、 驱动器初始化函数 驱动器初始化函数,主要就是初始化与驱动器 ENA+,DIR+相连的 2 个 IO为推挽输出。
void Driver_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
GPIO_SetBits(GPIOE,GPIO_Pin_6);
}
2、TIM8_CH2 单脉冲输出+重复计数功能初始化 此例程产生脉冲所使用的定时器均是 TIM8_CH2(PC7) ,定时器工作在单脉冲+重复计数模式,需要注意的是定时器必须初始化为 1MHz 计数频率。
void TIM8_OPM_RCR_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_ClearITPendingBit(TIM8,TIM_IT_Update);
TIM_UpdateRequestConfig(TIM8,TIM_UpdateSource_Regular);
TIM_SelectOnePulseMode(TIM8,TIM_OPMode_Single);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = arr>>1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE);
TIM_ITConfig(TIM8, TIM_IT_Update ,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearITPendingBit(TIM8, TIM_IT_Update);
TIM_Cmd(TIM8, ENABLE);
}
void TIM8_UP_TIM13_IRQHandler(void)
{
if(TIM_GetITStatus(TIM8,TIM_FLAG_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM8,TIM_FLAG_Update);
if(is_rcr_finish==0)
{
if(rcr_integer!=0)
{
TIM8->RCR=RCR_VAL;
rcr_integer--;
}else if(rcr_remainder!=0)
{
TIM8->RCR=rcr_remainder-1;
rcr_remainder=0;
is_rcr_finish=1;
}else goto out;
TIM_GenerateEvent(TIM8,TIM_EventSource_Update);
TIM_CtrlPWMOutputs(TIM8,ENABLE);
TIM_Cmd(TIM8, ENABLE);
if(motor_dir==CW)
current_pos+=(TIM8->RCR+1);
else
current_pos-=(TIM8->RCR+1);
}else
{
out: is_rcr_finish=1;
TIM_CtrlPWMOutputs(TIM8,DISABLE);
TIM_Cmd(TIM8, DISABLE);
OLED_ShowString (0,2,"position:",12);
OLED_Showfloat(75, 1, current_pos*1.8 ,5,1,16);
delay_ms(1);
DRIVER_OE = 1;
}
}
}
3、启动定时器8
void TIM8_Startup(u32 frequency)
{
u16 temp_arr=1000000/frequency-1;
TIM_SetAutoreload(TIM8,temp_arr);
TIM_SetCompare2(TIM8,temp_arr>>1);
TIM_SetCounter(TIM8,0);
TIM_Cmd(TIM8, ENABLE);
}
4、相对定位函数 相对定位函数: 在步进电机当前位置基础上顺时针(CW)或者逆时针(CCW)走 num 个脉冲, 此函数带方向控制, DIR_Type 是 driver.h 下声明的一个枚举类型,用于设置电机旋转方向,参数 dir=CW,电机顺时针旋转; dir=CCW,电机逆时针旋转。
void Locate_Rle(long num,u32 frequency,DIR_Type dir)
{
DRIVER_OE = 0;
if(num<=0)
{
printf("\r\nThe num should be greater than zero!!\r\n");
return;
}
if(TIM8->CR1&0x01)
{
printf("\r\nThe last time pulses is not send finished,wait please!\r\n");
return;
}
if((frequency<20)||(frequency>100000))
{
printf("\r\nThe frequency is out of range! please reset it!!(range:20Hz~100KHz)\r\n");
return;
}
motor_dir=dir;
DRIVER_DIR=motor_dir;
if(motor_dir==CW)
target_pos=current_pos+num;
else if(motor_dir==CCW)
target_pos=current_pos-num;
rcr_integer=num/(RCR_VAL+1);
rcr_remainder=num%(RCR_VAL+1);
is_rcr_finish=0;
TIM8_Startup(frequency);
}
5、绝对定位函数 绝对定位函数:步进电机按设定频率转动到设置的绝对位置, 开发板上电和复位时,当前位置为 0,电机的当前位置用一个 long 型变量 current_pos 指示。 在current_pos=0 的基础上顺时针转动后 current_pos 为正, 否则为负。
void Locate_Abs(long num,u32 frequency)
{
DRIVER_OE = 0;
if(TIM8->CR1&0x01)
{
printf("\r\nThe last time pulses is not send finished,wait please!\r\n");
return;
}
if((frequency<20)||(frequency>100000))
{
printf("\r\nThe frequency is out of range! please reset it!!(range:20Hz~100KHz)\r\n");
return;
}
target_pos=num;
if(target_pos!=current_pos)
{
if(target_pos>current_pos)
motor_dir=CW;
else
motor_dir=CCW;
DRIVER_DIR=motor_dir;
rcr_integer=abs(target_pos-current_pos)/(RCR_VAL+1);
rcr_remainder=abs(target_pos-current_pos)%(RCR_VAL+1);
is_rcr_finish=0;
TIM8_Startup(frequency);
}
}
6、头文件定义
#define DRIVER_DIR PEout(5)
#define DRIVER_OE PEout(6)
#define RCR_VAL 255
typedef enum
{
CW = 1,
CCW = 0,
}DIR_Type;
最后
步进电机也可以实现加减速的功能,本程序中不能够实现。在后期的更新中我会加入加减速的功能。欢迎大家一起来讨论。本工程我已经上传到了个人中心,大家如有需要可以下载。 祝大家早日会使用步进电机,以及基于步进电机做开发。
|