野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17823|回复: 1

我用STM32通过ULN2003驱动步进电机,只能正转,不能反转?

[复制链接]
发表于 2020-12-10 15:18:39 | 显示全部楼层 |阅读模式
1火花
如题,用STM32F103ZET6通过ULN2003驱动步进电机,只能正转,不能反转,求解。
PS:我是通过最简单的IO口电平切换来实现电机驱动,另外想问一下用ULN2003驱动步进电机是不是就不能通过PWM的方式驱动,


硬件连接如下
单片机:STM32F103ZET6
步进电机:28BYJ-48
驱动电路:ULN2003芯片的驱动板
引脚连接如下:
IN1:PC3
IN2:PC2
IN3:PC0
IN4:PC13


OUT1:步进电机4
OUT2:步进电机3
OUT3:步进电机2
OUT4:步进电机1
微信截图_20201210144026.png

代码如下:
  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "stm32f10x.h"
  5. int j;

  6. //单四拍
  7. u16 phasecw1[4] ={0x0008,0x0040,0x0001,0x2000};// 逆时针
  8. u16 phaseccw2[4]={0x2000,0x0001,0x0040,0x0008};// 顺时针


  9. //步进电机初始化函数
  10. void Motor_Init(void)
  11. {
  12.         GPIO_InitTypeDef GPIO_InitStructure;
  13.         
  14.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//开启时钟
  15.         
  16.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_3;
  17.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
  18.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19.         GPIO_Init(GPIOC, &GPIO_InitStructure);

  20. //默认ULN2003四路输入低电平,输出高电平        
  21.         GPIO_ResetBits(GPIOC,GPIO_Pin_13);
  22.         GPIO_ResetBits(GPIOC,GPIO_Pin_0);
  23.         GPIO_ResetBits(GPIOC,GPIO_Pin_2);
  24.         GPIO_ResetBits(GPIOC,GPIO_Pin_3);        
  25. }

  26. ////步进电机正转函数
  27. void Motorcw1(void)
  28. {
  29.         {  
  30.                         uint8_t i;   
  31.                         for(i=0;i<4;i++)  
  32.                         {  
  33.                                         GPIO_Write(GPIOC,phasecw1[i]);  
  34.                                         delay_ms(6);
  35.                         }  
  36.         }               
  37. }
  38. ////步进电机反转函数
  39. void Motorcw2(void)
  40. {
  41.         {  
  42.                         uint8_t a;   
  43.                         for(a=0;a<4;a++)  
  44.                         {  
  45.                                         GPIO_Write(GPIOC,phaseccw2[a]);  
  46.                                         delay_ms(6);
  47.                         }  
  48.         }               
  49. }
  50. //电机停止函数
  51. void MotorStop(void)  
  52. {  
  53.     GPIO_Write(GPIOC,0x0000);  
  54. }


  55. int main(void)
  56. {
  57. //函数初始化
  58.         delay_init();
  59.         Motor_Init();
  60.         
  61. //电机循环执行
  62.         while(1)
  63.                 {
  64.                         Motorcw1();
  65.                 }
  66. }
复制代码




回复

使用道具 举报

发表于 2021-1-25 10:05:54 | 显示全部楼层
  if (direction == RIGHT)             //右转
  {
    switch (cnt)
    {
    case 0:   //节拍0
                StepMotor_Control(MOTOR_A,HIGH);
                StepMotor_Control(MOTOR_AA,LOW);
                StepMotor_Control(MOTOR_B,LOW);
        StepMotor_Control(MOTOR_BB,HIGH);
      break;
    case 1:  //节拍1
        StepMotor_Control(MOTOR_A,HIGH);
                StepMotor_Control(MOTOR_AA,LOW);
                StepMotor_Control(MOTOR_B,HIGH);
                StepMotor_Control(MOTOR_BB,LOW);
      break;
    case 2: //节拍2
                StepMotor_Control(MOTOR_A,LOW);
                StepMotor_Control(MOTOR_AA,HIGH);
        StepMotor_Control(MOTOR_B,HIGH);
                StepMotor_Control(MOTOR_BB,LOW);
      break;
    case 3:  //节拍3
                StepMotor_Control(MOTOR_A,LOW);
                StepMotor_Control(MOTOR_AA,HIGH);
                StepMotor_Control(MOTOR_B,LOW);
        StepMotor_Control(MOTOR_BB,HIGH);
      break;
    }
  }
  else if (direction == LEFT)                                //左转
  {
    switch (cnt)
    {
    case 0:  //节拍0
      StepMotor_Control(MOTOR_A,HIGH);
      StepMotor_Control(MOTOR_AA,LOW);
      StepMotor_Control(MOTOR_B,LOW);
      StepMotor_Control(MOTOR_BB,HIGH);
      break;
    case 1:  //节拍1
      StepMotor_Control(MOTOR_A,LOW);
      StepMotor_Control(MOTOR_AA,HIGH);
      StepMotor_Control(MOTOR_B,LOW);
      StepMotor_Control(MOTOR_BB,HIGH);
      break;
    case 2:  //节拍2
      StepMotor_Control(MOTOR_A,LOW);
      StepMotor_Control(MOTOR_AA,HIGH);
      StepMotor_Control(MOTOR_B,HIGH);
      StepMotor_Control(MOTOR_BB,LOW);
      break;
    case 3: //节拍3
      StepMotor_Control(MOTOR_A,HIGH);
      StepMotor_Control(MOTOR_AA,LOW);
      StepMotor_Control(MOTOR_B,HIGH);
      StepMotor_Control(MOTOR_BB,LOW);
      break;
    }
  }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-29 04:09 , Processed in 0.035438 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表