Tri Duino Stepper

From RepRap
Revision as of 21:54, 20 November 2010 by Glasshopper (talk | contribs)
Jump to: navigation, search
Crystal Clear action run.png
Tri Duino Stepper

Release status: unknown

No image available.png
Description
Unipolar 3 Stepper driver with an Arduino
License
Author
Contributors
Based-on
Categories
CAD Models
External Link


Tri diuno bb.jpg


This is a stepper driver using an ATMEGA168 for three steppers. I takes step and direction for your gcode interpreter (in this case another ATMEGA168 running 5-D on Arduino)and drives three stepper motors accordingly.

(The code is crude and simplistic but I have not been programing long & it DOES work) There are no libraries needed to run this It really is just as simple as it seems.



CODE...

////////////////////////////////////////////////////////////////////////////// /* // TriStepduino

//  Arduino stepper board using ULN2803
//  created: 10/18/10
//
//  By:
//  Glasshopper
//
//  Known bugs:
//  None found yet
//
//  Want to add:
//  done, change port reads to use port byte, this will alow precise timming
//  done, allow reset and enable to use the external interupts (pins 2 &3)
//  micro stepping (would need 3 more inputs used: Ms1, Ms2, Ms3 ?)
//  change defines for steps and speed so they are in eeprom and can be changed via serial
//  sleep mode when reset?
//
*/

//////////////////////////////////////////////////////////////////////////////

  1. define version '0.0.1a' // the very first
  2. define DEBUG 0 // not used yet
  3. define SER_CLK 19200 // serial baud rate
  1. define ENABLE_PIN 3 // master pin
  2. define RESET_PIN 2 // reset pin
  1. define X_SPEED 5 // x motor speed (higher = slower)
  2. define X_STEPS 100 // x number of steps on your motor
  3. define Y_SPEED 5 // y motor speed (higher = slower)
  4. define Y_STEPS 100 // y number of steps on your motor
  5. define Z_SPEED 5 // z motor speed (higher = slower)
  6. define Z_STEPS 100 // z number of steps on your motor
  1. define DIR_Z_PIN 19 // z direction pin
  2. define STEP_Z_PIN 18 // z step pin
  1. define DIR_Y_PIN 17 // y direction pin
  2. define STEP_Y_PIN 16 // y step pin
  1. define DIR_X_PIN 15 // x direction pin
  2. define STEP_X_PIN 14 // x step pin
  1. define X_PIN4 13 // x motor pin 4
  2. define X_PIN3 12 // x motor pin 3
  3. define X_PIN2 11 // x motor pin 2
  4. define X_PIN1 10 // x motor pin 1
  1. define Y_PIN4 9 // y motor pin 4
  2. define Y_PIN3 6 // y motor pin 3
  3. define Y_PIN2 7 // y motor pin 2
  4. define Y_PIN1 8 // y motor pin 1
  1. define Z_PIN4 0 // z motor pin 4
  2. define Z_PIN3 1 // z motor pin 3
  3. define Z_PIN2 4 // z motor pin 2
  4. define Z_PIN1 5 // z motor pin 1

////////////////////////////////////////////////////////////////////////////// // Global variables byte checkPort = B00000000; // byte to hold pinc value boolean ena = HIGH; // enable bit, active low boolean Xstp = LOW; // x step strobe bit boolean Xdir = LOW; // x step direction bit boolean Ystp = LOW; // x step strobe bit boolean Ydir = LOW; // x step direction bit boolean Zstp = LOW; // x step strobe bit boolean Zdir = LOW; // x step direction bit

////////////////////////////////////////////////////////////////////////////// void setup() {

 //  Serial.begin(SER_CLK);               // open serial port
 pinMode(ENABLE_PIN, INPUT);          // declare enable pin as input ** ACTIVE LOW **
 digitalWrite(ENABLE_PIN, HIGH);      // turn on internal pullup resistor
 pinMode(DIR_X_PIN, INPUT);           // declare x direction pin as input
 pinMode(STEP_X_PIN, INPUT);          // declare x step pin as input
 pinMode(DIR_Y_PIN, INPUT);           // declare y direction pin as input
 pinMode(STEP_Y_PIN, INPUT);          // declare y step pin as input
 pinMode(DIR_Z_PIN, INPUT);           // declare z direction pin as input
 pinMode(STEP_Z_PIN, INPUT);          // declare z step pin as input
 pinMode(X_PIN1, OUTPUT);             // declare x1 pin as output
 pinMode(X_PIN2, OUTPUT);             // declare x2 pin as output
 pinMode(X_PIN3, OUTPUT);             // declare x3 pin as output
 pinMode(X_PIN4, OUTPUT);             // declare x4 pin as output
 pinMode(Y_PIN1, OUTPUT);             // declare y1 pin as output
 pinMode(Y_PIN2, OUTPUT);             // declare y2 pin as output
 pinMode(Y_PIN3, OUTPUT);             // declare y3 pin as output
 pinMode(Y_PIN4, OUTPUT);             // declare y4 pin as output
 pinMode(Z_PIN1, OUTPUT);             // declare z1 pin as output
 pinMode(Z_PIN2, OUTPUT);             // declare z2 pin as output
 pinMode(Z_PIN3, OUTPUT);             // declare z3 pin as output
 pinMode(Z_PIN4, OUTPUT);             // declare z4 pin as output
 attachInterrupt(0, reset, CHANGE);   // interrupt 0 digital pin 2
 attachInterrupt(1, enableSteps, CHANGE);  // interrupt 1 digital pin 3

}

////////////////////////////////////////////////////////////////////////////// void loop(){

 checkPins();
 if (!ena)  {                         // is the enable bit low?    
   if (Xstp) {                        // is the step bit set?
     if (Xdir)  full_Ccw('X');        // is the direction bit set? then full step counterclockwise
     else full_Cw('X');               // nope, go a full step clockwise
     //      Xstp = 0;                // seems redundant & unneccesary?
     //      Xdir = 0;                // "
   }    
   if (Ystp) {                        // is the step bit set?
     if (Ydir)  full_Ccw('Y');        // is the direction bit set? then full step counterclockwise
     else full_Cw('Y');               // nope, go a full step clockwise
     //      Ystp = 0;
     //      Ydir = 0;
   }    
   if (Zstp) {                        // is the step bit set?
     if (Zdir)  full_Ccw('Z');        // is the direction bit set? then full step counterclockwise
     else full_Cw('Z');               // nope, go a full step clockwise
     //     Zstp = 0;
     //     Zdir = 0;
   }
 }
   else reset();

}

////////////////////////////////////////////////////////////////////////////// // can't use delay(x) or any timers in IRQ routine void enableSteps() { // this is an interupt routhing when the input pin changes state

 ena = digitalRead(ENABLE_PIN);       // Read enablepin  **active Low**
 //  ena = bitRead(PIND, 3);              // read port d pin 3 input register (pin D03)  **active Low**
 if(ena) reset();

}

////////////////////////////////////////////////////////////////////////////// void reset() {

 byte motorPin1, motorPin2, motorPin3, motorPin4;
 motorPin1 = X_PIN1;
 motorPin2 = X_PIN2;
 motorPin3 = X_PIN3;
 motorPin4 = X_PIN4;
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, LOW);
 motorPin1 = Y_PIN1;
 motorPin2 = Y_PIN2;
 motorPin3 = Y_PIN3;
 motorPin4 = Y_PIN4;
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, LOW);
 motorPin1 = Z_PIN1;
 motorPin2 = Z_PIN2;
 motorPin3 = Z_PIN3;
 motorPin4 = Z_PIN4;
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, LOW);

}

////////////////////////////////////////////////////////////////////////////// void checkPins() {

 enableSteps();
 checkPort = PINC;                    // port c input register
 Xstp = bitRead(checkPort, 0);        // if the step line is high set the step bit
 Xdir = bitRead(checkPort, 1);        // if the direction line is high set the direction bit
 Ystp = bitRead(checkPort, 2);        // if the step line is high set the step bit
 Ydir = bitRead(checkPort, 3);        // if the direction line is high set the direction bit
 Zstp = bitRead(checkPort, 4);        // if the step line is high set the step bit
 Zdir = bitRead(checkPort, 5);        // if the direction line is high set the direction bit

}

////////////////////////////////////////////////////////////////////////////// void full_Ccw(char motor) { // one full step clockwise

 byte motorPin1, motorPin2, motorPin3, motorPin4, motorSpeed;
 switch (motor) {
 case 'X':
   motorSpeed = X_SPEED;
   motorPin1 = X_PIN1;
   motorPin2 = X_PIN2;
   motorPin3 = X_PIN3;
   motorPin4 = X_PIN4;
   break;
 case 'Y':
   motorSpeed = Y_SPEED;
   motorPin1 = Y_PIN1;
   motorPin2 = Y_PIN2;
   motorPin3 = Y_PIN3;
   motorPin4 = Y_PIN4;
   break;
 case 'Z':
   motorSpeed = Z_SPEED;
   motorPin1 = Z_PIN1;
   motorPin2 = Z_PIN2;
   motorPin3 = Z_PIN3;
   motorPin4 = Z_PIN4;
   break;
   //   default:
 }
 // 1010
 digitalWrite(motorPin1, HIGH);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, HIGH);
 digitalWrite(motorPin4, LOW);
 delay(motorSpeed);
 // 0110
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, HIGH);
 digitalWrite(motorPin3, HIGH);
 digitalWrite(motorPin4, LOW);
 delay (motorSpeed);
 // 0101
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, HIGH);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, HIGH);
 delay(motorSpeed);
 // 1001
 digitalWrite(motorPin1, HIGH);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, HIGH);
 delay(motorSpeed);

}

////////////////////////////////////////////////////////////////////////////// void full_Cw(char motor) { // one full step counterclockwise

 byte motorPin1, motorPin2, motorPin3, motorPin4, motorSpeed;
 switch (motor) {
 case 'X':
   motorSpeed = X_SPEED;
   motorPin1 = X_PIN1;
   motorPin2 = X_PIN2;
   motorPin3 = X_PIN3;
   motorPin4 = X_PIN4;
   break;
 case 'Y':
   motorSpeed = Y_SPEED;
   motorPin1 = Y_PIN1;
   motorPin2 = Y_PIN2;
   motorPin3 = Y_PIN3;
   motorPin4 = Y_PIN4;
   break;
 case 'Z':
   motorSpeed = Z_SPEED;
   motorPin1 = Z_PIN1;
   motorPin2 = Z_PIN2;
   motorPin3 = Z_PIN3;
   motorPin4 = Z_PIN4;
   break;
   //    default:
 }
 // 0101
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, HIGH);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, HIGH);
 delay(motorSpeed);
 // 0110
 digitalWrite(motorPin1, LOW);
 digitalWrite(motorPin2, HIGH);
 digitalWrite(motorPin3, HIGH);
 digitalWrite(motorPin4, LOW);
 delay (motorSpeed);
 // 1010
 digitalWrite(motorPin1, HIGH);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, HIGH);
 digitalWrite(motorPin4, LOW);
 delay(motorSpeed);
 // 1001
 digitalWrite(motorPin1, HIGH);
 digitalWrite(motorPin2, LOW);
 digitalWrite(motorPin3, LOW);
 digitalWrite(motorPin4, HIGH);
 delay(motorSpeed);

}

//////////////////////////////////////////////////////////////////////////////



File:TriStepduino.pde