使用 Stm32Hal 驱动 ULN2003 控制步进电机
步进电机代码编写相对于减速电机好写很多, 不需要配置PWM波,只需要控制GPIO就可以实现驱动步进电机的效果
Hal库配置如下
然后对rcc和sys配置下就可以驱动了
驱动步进电机有很多种方式 我使用的是8拍驱动
如图所示 每一步为一拍 第一步的时候 A相为高电平 其余低电平 第二步A,B相为高电平其他为低电平,以此类推。
void MOTOR_CONTROL(uint8_t direction)
{
static uint8_t step = 0 ;
if(1 == direction) // reverse
{
if(0 == step) step = 8;
step–;
}
if(0 == step) // first step
{
MOTOR_A_H;
MOTOR_B_L;
MOTOR_C_L;
MOTOR_D_L;
}
else if(1== step) //second step
{
MOTOR_A_H;
MOTOR_B_H;
MOTOR_C_L;
MOTOR_D_L;
}
else if(2== step) //third step
{
MOTOR_A_L;
MOTOR_B_H;
MOTOR_C_L;
MOTOR_D_L;
}
else if(3== step) //fourth step
{
MOTOR_A_L;
MOTOR_B_H;
MOTOR_C_H;
MOTOR_D_L;
}
else if(4== step) //fifth step
{
MOTOR_A_L;
MOTOR_B_L;
MOTOR_C_H;
MOTOR_D_L;
}
else if(5== step) //sixth step
{
MOTOR_A_L;
MOTOR_B_L;
MOTOR_C_H;
MOTOR_D_H;
}
else if(6== step) //seventh step
{
MOTOR_A_L;
MOTOR_B_L;
MOTOR_C_L;
MOTOR_D_H;
}
else if(7== step) //eighth step
{
MOTOR_A_H;
MOTOR_B_L;
MOTOR_C_L;
MOTOR_D_H;
}
if(0 == direction) // forward
{
step++;
if(8 == step) step = 0 ;
}
}
然后就是如何控制角度
这个就是计算公式
void STEP_MOTOR_START(uint16_t angle,uint8_t direction)
{
int i = 0 ;
int pulse = (int)((double)(angle/5.625)*64); // use double to prevent loss num
for(i = 0 ; i < pulse ; i++)
{
MOTOR_CONTROL(direction);
HAL_Delay(2);
}
}
完整工程在这
链接:https://pan.baidu.com/s/19ccyRLEZk2OH3_HzNuZA9w?pwd=asf1
提取码:asf1
作者:Dkangiy