プログラム
ST820.ino
// ピン定義。
#define PIN_MS0 13
#define PIN_MS1 12
#define PIN_MS2 11
#define PIN_EN 10
#define PIN_STEP 9
#define PIN_DIR 8
#define PIN_RST 7
#define stepsPerRevolution 200 //1ステップ1.8度の場合
#define microstep 256
#define reductionratio 720 //減速比
#define DELAY 1
//#define DELAY 2000
/*
* MS2 MS1 MS0
* 0 0 0 full step
* 0 0 1 1/2 step
* 0 1 0 1/4 step
* 0 1 1 1/8 step
* 1 0 0 1/16 step
* 1 0 1 1/32 step
* 1 1 0 1/128 step
* 1 1 1 1/256 step
*/
void setup() {
// put your setup code here, to run once:
pinMode(PIN_EN, OUTPUT);
pinMode(PIN_STEP, OUTPUT);
pinMode(PIN_DIR, OUTPUT);
delay(500);
digitalWrite(PIN_MS0, HIGH);
digitalWrite(PIN_MS1, HIGH);
digitalWrite(PIN_MS2, HIGH);
digitalWrite(PIN_EN, HIGH);
digitalWrite(PIN_DIR, LOW);
digitalWrite(PIN_RST, HIGH);
Serial.begin(9600);
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("時計回り");
digitalWrite(PIN_DIR, LOW);
for (int i = 0; i < stepsPerRevolution; i++) {
for (int j = 0; j < reductionratio; j++) {
for (int k = 0; k < microstep; k++) {
digitalWrite(PIN_STEP, HIGH);
delayMicroseconds(DELAY);
digitalWrite(PIN_STEP, LOW);
delayMicroseconds(DELAY);
}
}
}
delay(1000);
Serial.println("反時計回り");
digitalWrite(PIN_DIR, HIGH);
for (int i = 0; i < stepsPerRevolution; i++) {
for (int j = 0; j < reductionratio; j++) {
for (int k = 0; k < microstep; k++) {
digitalWrite(PIN_STEP, HIGH);
delayMicroseconds(DELAY);
digitalWrite(PIN_STEP, LOW);
delayMicroseconds(DELAY);
}
}
}
delay(1000);
}