Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test half-duplex serial with TMC2209
#include <TMCStepper.h>
#define MAX_SPEED 20 // In timer value
#define MIN_SPEED 300
#define STALL_VALUE 100 // [0..255]
#define EN_PIN PC1 // Enable
#define DIR_PIN PC4 // Direction
#define STEP_PIN PE12 // Step
#define SERIAL_RX -1 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SERIAL_TX PD8 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define R_SENSE 0.091f// Match to your driver
// Select your stepper driver type
//TMC2209Stepper driver(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS);
HardwareSerial Serial_xyz(-1, PD8);
TMC2209Stepper driverx(&Serial_xyz, R_SENSE, 0);
TMC2209Stepper drivery(&Serial_xyz, R_SENSE, 1);
TMC2209Stepper driverz(&Serial_xyz, R_SENSE, 2);
HardwareSerial Serial_e(-1, PC6);
TMC2209Stepper drivere0(&Serial_e, R_SENSE, 0);
TMC2209Stepper drivere1(&Serial_e, R_SENSE, 1);
void setup() {
Serial.begin(115200); // Init serial port and set baudrate
while (!Serial); // Wait for serial port to connect
Serial.println("\nStart...");
Serial_xyz.begin(115200);
Serial_e.begin(115200);
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
// disable steppers
digitalWrite(EN_PIN, LOW);
delay(500);
driverx.senddelay(14);
delay(10);
drivery.senddelay(14);
delay(10);
driverz.senddelay(14);
delay(10);
drivere0.senddelay(14);
delay(10);
drivere1.senddelay(14);
delay(10);
drvSetupX();
}
void drvSetupX() {
driverx.begin(); // SPI: Init CS pins and possible SW SPI pins
// UART: Init SW UART (if selected) with default 115200 baudrate
driverx.toff(5); // Enables driver in software
driverx.rms_current(600); // Set motor RMS current
driverx.microsteps(16); // Set microsteps to 1/16th
//driverx.en_pwm_mode(true); // Toggle stealthChop on TMC2130/2160/5130/5160
//driverx.en_spreadCycle(false); // Toggle spreadCycle on TMC2208/2209/2224
driverx.pwm_autoscale(true); // Needed for stealthChop
}
void loop1() {
Serial.print("Driver X version ");
Serial.println(driverx.version());
Serial.print("Driver X status 0x");
Serial.println(driverx.DRV_STATUS(), HEX);
Serial.print("Driver Y version ");
Serial.println(drivery.version());
Serial.print("Driver Y status 0x");
Serial.println(drivery.DRV_STATUS(), HEX);
Serial.print("Driver Z version ");
Serial.println(driverz.version());
Serial.print("Driver Z status 0x");
Serial.println(driverz.DRV_STATUS(), HEX);
Serial.print("Driver E0 version ");
Serial.println(drivere0.version());
Serial.print("Driver E0 status 0x");
Serial.println(drivere0.DRV_STATUS(), HEX);
Serial.print("Driver E1 version ");
Serial.println(drivere1.version());
Serial.print("Driver E1 status 0x");
Serial.println(drivere1.DRV_STATUS(), HEX);
delay(1000);
}
void loop() {
digitalWrite(STEP_PIN, 0);
delayMicroseconds(160);
digitalWrite(STEP_PIN, 1);
delayMicroseconds(140);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment