Created
March 1, 2017 09:59
-
-
Save teemuatlut/2bc6d85732311087e3e5285e04c36cb5 to your computer and use it in GitHub Desktop.
Plot stallGuard value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```cpp | |
/* | |
Trinamic TMC2130 Example | |
*/ | |
#include <SPI.h> | |
// Note: You also have to connect GND, 5V and VM. | |
// A connection diagram can be found in the schematics. | |
#define EN_PIN 38 // Nano v3: 16 Mega: 38 //enable (CFG6) | |
#define DIR_PIN 55 // 19 55 //direction | |
#define STEP_PIN 54 // 18 54 //step | |
#define CS_PIN 40 // 17 64 //chip select | |
#define MOSI_PIN MOSI //51 //SDI/MOSI (ICSP: 4, Uno: 11, Mega: 51) | |
#define MISO_PIN MISO //50 //SDO/MISO (ICSP: 1, Uno: 12, Mega: 50) | |
#define SCK_PIN SCK //52 //CLK/SCK (ICSP: 3, Uno: 13, Mega: 52) | |
//TMC2130 registers | |
#define _WRITE 0x80 //write flag | |
#define REG_GCONF 0x00 | |
#define REG_GSTAT 0x01 | |
#define REG_IHOLD_IRUN 0x10 | |
#define REG_CHOPCONF 0x6C | |
#define REG_COOLCONF 0x6D | |
#define REG_DCCTRL 0x6E | |
#define REG_DRVSTATUS 0x6F | |
#define REG_TCOOLTHRS 0x14 | |
#define REG_TPWMTHRS 0x13 | |
#define REG_TSTEP 0x12 | |
#define NUMBER_OF_STEPS 14000 | |
int stepCount = 0; | |
#include <TMC2130Stepper.h> | |
TMC2130Stepper TMC2130 = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN); | |
uint8_t tmc_write(uint8_t cmd, uint32_t data) | |
{ | |
uint8_t s; | |
digitalWrite(CS_PIN, LOW); | |
s = SPI.transfer(cmd); | |
SPI.transfer((data>>24UL)&0xFF)&0xFF; | |
SPI.transfer((data>>16UL)&0xFF)&0xFF; | |
SPI.transfer((data>> 8UL)&0xFF)&0xFF; | |
SPI.transfer((data>> 0UL)&0xFF)&0xFF; | |
digitalWrite(CS_PIN, HIGH); | |
return s; | |
} | |
uint8_t tmc_read(uint8_t cmd, uint32_t *data) | |
{ | |
uint8_t s; | |
tmc_write(cmd, 0UL); //set read address | |
digitalWrite(CS_PIN, LOW); | |
s = SPI.transfer(cmd); | |
*data = SPI.transfer(0x00)&0xFF; | |
*data <<=8; | |
*data |= SPI.transfer(0x00)&0xFF; | |
*data <<=8; | |
*data |= SPI.transfer(0x00)&0xFF; | |
*data <<=8; | |
*data |= SPI.transfer(0x00)&0xFF; | |
digitalWrite(CS_PIN, HIGH); | |
return s; | |
} | |
void setup() | |
{ | |
delay(1500); | |
pinMode(12, OUTPUT); | |
digitalWrite(12, HIGH); | |
delay(500); | |
cli();//stop interrupts | |
//set timer1 interrupt at 1Hz | |
TCCR1A = 0;// set entire TCCR1A register to 0 | |
TCCR1B = 0;// same for TCCR1B | |
TCNT1 = 0;//initialize counter value to 0 | |
// set compare match register for 1hz increments | |
OCR1A = 256;// = (16*10^6) / (1*1024) - 1 (must be <65536) | |
// turn on CTC mode | |
TCCR1B |= (1 << WGM12); | |
// Set CS12 and CS10 bits for 1024 prescaler | |
TCCR1B |= (1 << CS11);// | (1 << CS10); | |
// enable timer compare interrupt | |
TIMSK1 |= (1 << OCIE1A); | |
sei();//allow interrupts | |
//set pins | |
pinMode(EN_PIN, OUTPUT); | |
pinMode(DIR_PIN, OUTPUT); | |
pinMode(STEP_PIN, OUTPUT); | |
digitalWrite(EN_PIN, HIGH); //deactivate driver (LOW active) | |
digitalWrite(DIR_PIN, LOW); //LOW or HIGH | |
digitalWrite(STEP_PIN, LOW); | |
pinMode(CS_PIN, OUTPUT); | |
pinMode(MOSI_PIN, OUTPUT); | |
pinMode(MISO_PIN, INPUT); | |
pinMode(SCK_PIN, OUTPUT); | |
digitalWrite(CS_PIN, HIGH); | |
digitalWrite(MOSI_PIN, LOW); | |
digitalWrite(MISO_PIN, HIGH); | |
digitalWrite(SCK_PIN, LOW); | |
//init serial port | |
Serial.begin(250000); //init serial port and set baudrate | |
while(!Serial); //wait for serial port to connect (needed for Leonardo only) | |
Serial.println("\nStart..."); | |
//init SPI | |
SPI.setDataMode(SPI_MODE3); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setClockDivider(SPI_CLOCK_DIV128); | |
SPI.begin(); | |
//set TMC2130 config | |
tmc_write(_WRITE|REG_IHOLD_IRUN, 0x10UL<<0 | 0x10UL<<8); //IHOLD=0x10, IRUN=0x10 | |
tmc_write(_WRITE|REG_CHOPCONF, 0x4UL<<24 | 0x1UL<<15 | 0x8UL<<0); //16 microsteps, MRES=0, TBL=1=24, TOFF=8 | |
TMC2130.coolstep_min_speed(1048575); | |
TMC2130.diag1_stall(1); | |
TMC2130.diag1_active_high(1); | |
TMC2130.sg_stall_value(15); | |
TMC2130.sg_filter(1); | |
digitalWrite(EN_PIN, LOW); | |
} | |
ISR(TIMER1_COMPA_vect){ | |
digitalWrite(STEP_PIN, !analogRead(STEP_PIN)); | |
if (++stepCount >= NUMBER_OF_STEPS) { | |
//digitalWrite(DIR_PIN, !analogRead(DIR_PIN)); // Change direction | |
stepCount = 0; | |
} | |
} | |
void loop() | |
{ | |
static uint32_t last_time=0; | |
uint32_t ms = millis(); | |
uint32_t data; | |
uint8_t s; | |
if((ms-last_time) > 100) //run every 1s | |
{ | |
last_time = ms; | |
s = tmc_read(REG_DRVSTATUS, &data); | |
Serial.println(data & 0x3FF, DEC); | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment