将步进电机与STM32F103C8T6接口

在本教程中,我将向您展示将步进电机与STM32F103C8T6 MCU基于STM32的STM32蓝色药丸板接口的程序。我将首先简要介绍步进电机,其类型,其驾驶技术以及最后如何与STM32连接步进电机。

Introduction

步进电动机是工业和商业应用中使用的重要类型。它们是简单的无刷电动机,可将数字脉冲转换为角旋转。

步进电机的独特功能是,每次革命通常分为不同的相等步骤,通常为200个。由于360有200个步骤0旋转,每个步骤将导致旋转1.80。为了从一个步骤移到另一个步骤,必须发送一个单独的脉冲,并且旋转速度与脉冲频率成正比。

Another important thing to know about stepper motors is there are two types of Stepper Motors. They are Unipolar Stepper Motors and Bipolar Stepper Motors. The main difference between these two is that in unipolar stepper motor, each phase has a winding with centre tap while in bipolar stepper motor, there is just a single winding per phase.

双极单极步进电动机

另一个区别是这些汽车dri的方式ven. Since the polarity of the magnet in the coil can be reversed in Unipolar type, without the switching the current direction, driving them is very easy. You only need a simple transistor-based motor driver like ULN2003 IC, for example.

But in case of Bipolar stepper motors, it is difficult to reverse the magnetic polarity directly as there is only a single winding per phase. We usually need a dedicated H-Bridge type Motor Driver IC like L293D or L298N IC to drive a bipolar stepper motor.

将步进电机与STM32F103C8T6接口

在这个项目中,我将使用一个简单的5V单极步进电动机,在该电动机中,两个绕组的中心水龙头都很普遍。这意味着电动机只有5条电线而不是六根电线。

A common type of hobbyist unipolar stepper motor that is very popular and cheap is the 28BYJ-48. If you refer to the data sheet of this motor, it is mentioned that it has a gear ratio of 64 and the stride angle is 5.6250。这意味着一个旋转需要的数字是360 / 5.625,等于64。

同样,由于齿轮比为64,因此输出轴的有效步骤是每革命的步骤乘以齿轮比,即64 * 64 = 4096。

在这个项目中,我将与ULN2003驱动器IC一起使用此步进电机,并使用STM32 MCU控制它。该项目将是沿顺时针方向和逆时针方向的旋转扫描。

Components Required

  • STM32F103C8T6基于MCU的STM32蓝色药丸板
  • 5V单极步进电动机(5线)
  • ULN2003 Driver IC
  • Connecting Wires
  • USB到UART转换器(如果通过UART编程)

电路原理图

The following image shows the circuit diagram of Interfacing Stepper Motor with STM32F103C8T6 MCU.

与STM32F103C8T6电路图的接口步进电机

连接解释了

首先,步进电动机的红线连接到5V。接下来,ULN2003 IC的前四个输入In1至In4 IN与PA0连接到PA3。ULN2003的前四个输出即以下顺序连接到步进电动机线:

  • OUT1 –> Blue
  • out2 - >粉红色
  • OUT3 –> Yellow
  • OUT4 - >橙色

Also, connect the GND terminals of ULN2003, STM32 and the 5V power supply.

用于步进电机控制的STM32编程

首先,选择PA0至PA3作为ULN2003的输入,并将其初始化为STM32的输出。

Now use a variable to denote the maximum number of steps as 4095. What this represents is the count for one full rotation. Now assume the initial direction as clockwise and start the stepper motor by using half stepping technique.

一旦步骤计数达到0,这意味着电动机已经完成了一个旋转。现在,给出一秒钟或两秒钟的较小延迟,然后将方向更改为逆时针。将步骤计数重置为最大值,即4095,并以相反顺序开始序列。步进电动机将开始沿相反的方向旋转。在循环中重复此过程。

本代码中无需任何库,您甚至可以修改代码以接受用户输入以选择方向和步骤数。

如果您有不同的步进电动机,请参考其数据表并计算一个旋转所需的步长,齿轮比和总数。

代码

#define in1 pa0
#define in2 pa1
#define in3 pa2
#define IN4 PA3
int Steps = 0;
布尔方向= true;
unsigned long prevTime;
未签名的长期米利斯;
int stepsLeft=4095;
很久;
void setup()
{
pinmode(in1,输出);
pinmode(in2,输出);
pinmode(in3,输出);
pinmode(in4,输出);
}
void loop()
{
while(stepsleft> 0)
{
currentMillis = micros();
if(CurrentMillis-PrevTime> = 1000)
{
步进(1);
时间=时间 + micros() - previme;
PrevTime = micros();
步长 - ;
}
}
延迟(2000);
方向=!方向;
stepsLeft=4095;
}

void步进(int x)
{
for (int i=0; i < x; i++)
{
switch(Steps)
{
case 0:
DigitalWrite(1,低);
DigitalWrite(In2,低);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
休息;
情况1:
DigitalWrite(1,低);
DigitalWrite(In2,低);
DigitalWrite(In3,高);
digitalWrite(IN4, HIGH);
休息;
案例2:
DigitalWrite(1,低);
DigitalWrite(In2,低);
DigitalWrite(In3,高);
DigitalWrite(In4,低);
休息;
案例3:
DigitalWrite(1,低);
digitalWrite(IN2, HIGH);
DigitalWrite(In3,高);
DigitalWrite(In4,低);
休息;
案例4:
DigitalWrite(1,低);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
DigitalWrite(In4,低);
休息;
案例5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
DigitalWrite(In4,低);
休息;
案例6:
digitalWrite(IN1, HIGH);
DigitalWrite(In2,低);
digitalWrite(IN3, LOW);
DigitalWrite(In4,低);
休息;
案例7:
digitalWrite(IN1, HIGH);
DigitalWrite(In2,低);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
休息;
默认:
DigitalWrite(1,低);
DigitalWrite(In2,低);
digitalWrite(IN3, LOW);
DigitalWrite(In4,低);
休息;
}
setDirection();
}
}
void setDirection()
{
如果(方向== 1)
{
Steps++;
}
如果(方向== 0)
{
Steps–;
}
如果(步骤> 7)
{
Steps=0;
}
如果(步骤<0)
{
步骤= 7;
}
}

Conclusion

A simple project is implemented here to demonstrate the Interfacing of Stepper Motor with STM32F103C8T6 MCU based STM32 Blue Pill Board.

Leave a Reply

您的电子邮件地址不会被公开。

电子豪华粉丝
<\/i>","library":""}}" data-widget_type="nav-menu.default">
Baidu