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

  • 26
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值