以下是我学42步进电机时的例子,你可以参考。
用的是4988驱动。效果是正转一圈暂停一下,再反转一圈暂停一下。不停重复。
int x;
void setup()
{
pinMode(6,OUTPUT); // Enable: EN可以使用单片机端口控制,也可以直接连接GND使能
pinMode(5,OUTPUT); // steps:脉冲个数
pinMode(4,OUTPUT); // dir:为方向控制
digitalWrite(6,LOW); // Set Enable low
}
void loop()
{
digitalWrite(4,HIGH); // Set Dir high
for(x = 0; x < 200; x++) // Loop 200 times
{
digitalWrite(5,HIGH); // Output high
delayMicroseconds(800); // Wait 1/2 a ms
digitalWrite(5,LOW); // Output low
delayMicroseconds(800); // Wait 1/2 a ms
}
delay(1000); // pause one second
digitalWrite(4,LOW); // Set Dir low
for(x = 0; x < 200; x++) // Loop 2000 times
{
digitalWrite(5,HIGH); // Output high
delayMicroseconds(800); // Wait 1/2 a ms
digitalWrite(5,LOW); // Output low
delayMicroseconds(800); // Wait 1/2 a ms
}
delay(1000); // pause one second
}
|