Hello,
I'm using a L6470 stepper driver to run a motor. I'm running the example code given by Sparkfun. From what I understand the code is written for the motor to move back and forth, but mine just run in one direction continuously. Could someone please help me figure out what is wrong. Part of the code is given below. (I've only hooked up 4,6,10,11,12,13 and GND pins of arduino and not any LEDs or switches). How would you modify the coding for the motor to run a specific number of steps and stop?
Thank you !!!
//dSPIN_main.ino - Contains the setup() and loop() functions.
float testSpeed = 10;
void setup()
{
// Standard serial port initialization for debugging.
Serial.begin(9600);
// pin configurations for the test jig
pinMode(STAT1, OUTPUT);
pinMode(STAT2, OUTPUT);
pinMode(SWITCH, INPUT);
digitalWrite(STAT1, LOW);
digitalWrite(STAT2, LOW);
// dSPIN_init() is implemented in the dSPIN_support.ino file. It includes
// all the necessary port setup and SPI setup to allow the Arduino to
// control the dSPIN chip and relies entirely upon the pin redefinitions
// in dSPIN_example.ino
dSPIN_init();
// First things first: let's check communications. The CONFIG register should
// power up to 0x2E88, so we can use that to check the communications.
// On the test jig, this causes an LED to light up.
if (dSPIN_GetParam(dSPIN_CONFIG) == 0x2E88) digitalWrite(STAT1, HIGH);
.
// First, let's set the step mode register:
// - dSPIN_SYNC_EN controls whether the BUSY/SYNC pin reflects the step
// frequency or the BUSY status of the chip. We want it to be the BUSY
// status.
// - dSPIN_STEP_SEL_x is the microstepping rate- we'll go full step.
// - dSPIN_SYNC_SEL_x is the ratio of (micro)steps to toggles on the
// BUSY/SYNC pin (when that pin is used for SYNC). Make it 1:1, despite
// not using that pin.
dSPIN_SetParam(dSPIN_STEP_MODE, !dSPIN_SYNC_EN | dSPIN_STEP_SEL_1_32 | dSPIN_SYNC_SEL_1);
dSPIN_SetParam(dSPIN_MAX_SPEED, MaxSpdCalc(200));
dSPIN_SetParam(dSPIN_FS_SPD, FSCalc(400));
dSPIN_SetParam(dSPIN_ACC, 0xfff);
// Configure the overcurrent detection threshold. The constants for this are defined
// in the dSPIN_example.ino file.
dSPIN_SetParam(dSPIN_OCD_TH, dSPIN_OCD_TH_6000mA);
// Set up the CONFIG register as follows:
// PWM frequency divisor = 1
// PWM frequency multiplier = 2 (62.5kHz PWM frequency)
// Slew rate is 290V/us
// Do NOT shut down bridges on overcurrent
// Disable motor voltage compensation
// Hard stop on switch low
// 16MHz internal oscillator, nothing on output
dSPIN_SetParam(dSPIN_CONFIG,
dSPIN_CONFIG_PWM_DIV_1 | dSPIN_CONFIG_PWM_MUL_2 | dSPIN_CONFIG_SR_290V_us
| dSPIN_CONFIG_OC_SD_DISABLE | dSPIN_CONFIG_VS_COMP_DISABLE
| dSPIN_CONFIG_SW_HARD_STOP | dSPIN_CONFIG_INT_16MHZ);
dSPIN_SetParam(dSPIN_KVAL_RUN, 0x3F);
dSPIN_GetStatus();
}
// Test jig behavior- rotate one full revolution forward, then one full revolution
// backwards, then slowly tick forwards until the hard stop button is pressed.
void loop()
{
// 200 steps is one revolution on a 1.8 deg/step motor.
dSPIN_Run(FWD, SpdCalc(400));
while(1);
while (digitalRead(dSPIN_BUSYN) == LOW); // Until the movement completes, the
// BUSYN pin will be low.
dSPIN_SoftStop(); // Inserting a soft stop between
// motions ensures that the driver
// will execute the next motion with
// the right speed.
while (digitalRead(dSPIN_BUSYN) == LOW); // Wait for the soft stop to complete.
delay(500); // Pause. Not necessary for proper operation.
dSPIN_Move(REV, 200); // Now do it again, but backwards.
while (digitalRead(dSPIN_BUSYN) == LOW);
dSPIN_SoftStop();
while (digitalRead(dSPIN_BUSYN) == LOW);
delay(500);
dSPIN_Run(FWD, SpdCalc(testSpeed)); // Now we'll test the hard stop switch...
while (digitalRead(SWITCH) == HIGH); // The dSPIN will stop on its own; this is
// so the Arduino knows the button has been
// pressed.
while (digitalRead(dSPIN_BUSYN) == LOW);
delay(50);
// Finally check to see if the motor has actually stopped. If so, light the second
// LED and wait forever, until the board is removed and the test jig is reset.
if (dSPIN_GetParam(dSPIN_SPEED) == 0) digitalWrite(STAT2, HIGH);
while(1);
}