Skip to content

Instantly share code, notes, and snippets.

@ghent360
Created May 14, 2018 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghent360/190116d56bb31505e4a4f1cce7453332 to your computer and use it in GitHub Desktop.
Save ghent360/190116d56bb31505e4a4f1cce7453332 to your computer and use it in GitHub Desktop.
tmc2130 test program
/**
* Author Teemu Mäntykallio
* Initializes the library and turns the motor in alternating directions.
*/
#include <SPI.h>
#define EN_PIN PA5 // Nano v3: 16 Mega: 38 //enable (CFG6)
#define DIR_PIN PA9 // 19 55 //direction
#define STEP_PIN PC9 // 18 54 //step
#define CS_PIN PB9 // 17 64 //chip select
#define FAN_PIN PC6
#define MOT_CLOCK PB1
bool dir = true;
#include <TMC2130Stepper.h>
TMC2130Stepper driver = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN);
void status()
{
uint32_t drv_status = driver.DRV_STATUS();
uint8_t response = driver.status_response;
Serial.print("Status ");
Serial.println(drv_status, HEX);
Serial.print("Response ");
Serial.println(response, HEX);
}
void setup() {
pinMode(FAN_PIN, OUTPUT);
SPI.setMOSI(PC1);
SPI.setMISO(PC2);
SPI.setSCLK(PB10);
Serial.begin(9600);
while(!Serial);
Serial.println("Start...");
driver.begin(); // Initiate pins and registeries
driver.rms_current(900, 0.5, 0.15); // 900mA, hold current 450mA, sense resistor 0.15 ohm
driver.stealthChop(1); // Enable extremely quiet stepping
status();
digitalWrite(EN_PIN, LOW);
digitalWrite(FAN_PIN, HIGH);
}
void checkOT() {
if (driver.checkOT()) {
digitalWrite(EN_PIN, HIGH);
Serial.println("Driver temperature warning.");
status();
do {
int i;
while (driver.checkOT()) {
Serial.println("c");
delay(500);
}
// Check warning is gone for 2s
for (i = 0; i < 4; i++) {
Serial.println("C");
if (driver.checkOT()) break;
delay(500);
}
if (i >= 4) break;
} while (true);
digitalWrite(EN_PIN, LOW);
}
}
void loop() {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(10);
uint32_t ms = millis();
static uint32_t last_time = 0;
uint32_t elapsed = (ms - last_time);
if ((elapsed / 200) & 1) {
checkOT();
}
if (elapsed > 2000) {
status();
checkOT();
if (dir) {
Serial.println("Dir -> 0");
driver.shaft_dir(0);
} else {
Serial.println("Dir -> 1");
driver.shaft_dir(1);
}
dir = !dir;
last_time = ms;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment