My first question is about daisy chaining 2 boards. How can this work when there is only 1 chip select line?
Next, I am having a problem with getting the MOVE command to work. This is with a Teensy 4.0 using SPI #2.
My test sketch is supposed to move the stepper motor forward 50 steps, and then reverse for 50 steps. Instead it seems to get stuck moving one direction for the other for a LLOOONNNGGG time. Before deciding to change directions.
I am also occasionally seeing a status register value of D000, which doesn’t make sense because I am not using the sync input.
My sketch is attached. Am I missing something?
#include <SparkFunAutoDriver.h>
#include <SparkFundSPINConstants.h>
#include <SPI.h>
// Create our AutoDriver instances. The parameters are the position in the chain of
// boards (with board 0 being located at the end of the chain, farthest from the
// controlling processor), CS pin, and reset pin.
#define motorCS 36
#define motorReset 31
#define motorBusy 32
#define NUM_BOARDS 2
// Numbering starts from the board farthest from the
// controller and counts up from 0. Thus, board "4" is the
// board which is plugged directly into the controller
// and board "0" is the farthest away.
AutoDriver RAmotor(0, motorCS, motorReset);
AutoDriver DECmotor(1, motorCS, motorReset);
#define SPISPEED 1000000
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello world");
// Start by setting up the SPI port and pins. The
// Autodriver library does not do this for you!
pinMode(motorCS, OUTPUT);
digitalWrite(motorCS, HIGH); // tied low is also OK.
pinMode(motorBusy, INPUT);
pinMode(motorReset, OUTPUT);
digitalWrite(motorReset, LOW);
delay(100); // LOW at least 4 clock cycles of onboard clock. 100 microseconds is enough
digitalWrite(motorReset, HIGH); // now reset to default values
delay(500);
SPI2.setBitOrder(MSBFIRST);
SPI2.setClockDivider(SPI_CLOCK_DIV4);
SPI2.setDataMode(SPI_MODE3);
SPI2.begin(); //start the spi-bus
// Configure the boards to the settings we wish to use
// for them. See below for more information about the
// settings applied.
configureBoards();
}
// For ease of reading, we're just going to configure all the boards
// to the same settings. It's working okay for me.
void configureBoards()
{
Serial.println("Configuring motors...");
// Before we do anything, we need to tell each board which SPI
// port we're using. Most of the time, there's only the one,
// but it's possible for some larger Arduino boards to have more
// than one, so don't take it for granted.
DECmotor.SPIPortConnect(&SPI2);
// RAmotor.SPIPortConnect(&SPI2);
DECmotor.getParam(CONFIG);
Serial.println(CONFIG,HEX);
// Set the Overcurrent Threshold to 6A. The OC detect circuit
// is quite sensitive; even if the current is only momentarily
// exceeded during acceleration or deceleration, the driver
// will shutdown. This is a per channel value; it's useful to
// consider worst case, which is startup. These motors have a
// 1.8 ohm static winding resistance; at 12V, the current will
// exceed this limit, so we need to use the KVAL settings (see
// below) to trim that value back to a safe level.
DECmotor.setOCThreshold(OC_1125mA );
// RAmotor.setOCThreshold(OC_6000mA );
delay(100);
// KVAL is a modifier that sets the effective voltage applied
// to the motor. KVAL/255 * Vsupply = effective motor voltage.
// This lets us hammer the motor harder during some phases
// than others, and to use a higher voltage to achieve better
// torqure performance even if a motor isn't rated for such a
// high current. This system has 3V motors and a 12V supply.
DECmotor.setRunKVAL(128); // 128/255 * 12V = 6V
delay(100);
DECmotor.setAccKVAL(128); // 192/255 * 12V = 9V
delay(100);
DECmotor.setDecKVAL(128);
delay(100);
DECmotor.setHoldKVAL(128); // 32/255 * 12V = 1.5V
delay(100);
// RAmotor.setRunKVAL(192); // 128/255 * 12V = 6V
delay(100);
// RAmotor.setAccKVAL(255); // 192/255 * 12V = 9V
// RAmotor.setDecKVAL(255);
delay(100);
// RAmotor.setHoldKVAL(128); // 32/255 * 12V = 1.5V
delay(100);
// When a move command is issued, max speed is the speed the
// motor tops out at while completing the move, in steps/s
delay(100);
DECmotor.setMaxSpeed(50);
delay(100);
// RAmotor.setMaxSpeed(5);
DECmotor.setMinSpeed(0.0);
delay(100);
// RAmotor.setMinSpeed(0.0);
DECmotor.setLoSpdOpt(0);
delay(100);
// RAmotor.setLoSpdOpt(0);
delay(100);
// Acceleration and deceleration in steps/s/s. Increasing this
// value makes the motor reach its full speed more quickly,
// at the cost of possibly causing it to slip and miss steps.
DECmotor.setAcc(1);
delay(100);
DECmotor.setDec(1);
DECmotor.setSwitchMode(SW_HARD_STOP);
delay(100);
DECmotor.setOscMode( EXT_16MHZ_XTAL_DRIVE );
// RAmotor.setAcc(40);
delay(100);
// RAmotor.setDec(40);
// RAmotor.setSwitchMode(SW_HARD_STOP);
delay(100);
// RAmotor.setOscMode( EXT_16MHZ_XTAL_DRIVE );
delay(100);
DECmotor.configStepMode(STEP_FS_128);
// RAmotor.configStepMode(STEP_FS_128);
delay(100);
DECmotor.configSyncPin(BUSY_PIN, SYNC_FS);
// RAmotor.getParam(CONFIG);
DECmotor.softStop();
while (DECmotor.busyCheck()){}
int status = DECmotor.getStatus();
Serial.println(status,HEX);
Serial.println();
delay(2000);
}
void motorMove(int direction)
{
int param;
param = DECmotor.getParam(CONFIG);
Serial.println(param,HEX);
if (direction)
{
Serial.println();
Serial.print("Forward ");
}
else
Serial.print("Reverse ");
delay(500);
while (DECmotor.busyCheck()==1){}
DECmotor.move(direction, 50);
while (DECmotor.busyCheck()==1){}
DECmotor.hardStop();
int status = DECmotor.getStatus();
Serial.println(status,HEX);
}
int i=0;
void loop() {
int status = DECmotor.getStatus();
Serial.println(status,HEX);
while (DECmotor.busyCheck()==1){}
motorMove(i);
if (i==0)
i=1;
else
i=0;
}