2015年2月23日 星期一

Arduino-步進馬達 A3967驅動板


利用兩輸入腳位控制步進馬達( 兩相四線 )


//定義腳位
#define DIR_PIN 2
#define STEP_PIN 3

//腳位模式
void setup() { 
  pinMode(DIR_PIN, OUTPUT); 
  pinMode(STEP_PIN, OUTPUT); 
} 

void loop(){ 

  //rotate a specific number of degrees 
  rotateDeg(360, 1); 
  delay(1000);

  rotateDeg(-360, .1);  //reverse
  delay(1000); 


  //rotate a specific number of microsteps (8 microsteps per step)
  //a 200 step stepper would take 1600 micro steps for one full revolution
  rotate(1600, .5); 
  delay(1000); 

  rotate(-1600, .25); //reverse
  delay(1000); 
}


//迴轉
void rotate(int steps, float speed){ 
  
  //先確認要正/逆時鐘,所以先確認腳位的高低電壓
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);
      
  digitalWrite(DIR_PIN,dir); 

  float usDelay = (1/speed) * 70;

  
  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
} 

//旋轉角度
void rotateDeg(float deg, float speed){ 
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir); 

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

A3967 DateSheet : 連結在此
參考網址 : 連結在此

歡迎轉載,請註明出處

沒有留言:

張貼留言