creat1001

TB67S109AFTG stepper driver test code

Mar 30th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. /*
  2.    Scott Beasley - 2020
  3.    This is a test program for the cheap knockoff TB67S109AFTG
  4.    stepper driver boards that pose as TB6600 drivers. These drivers  
  5.    have an issue with losing or gaining steps.  The ones I have tested
  6.    have step issues whilst changing direction. The move following the
  7.    direction change will lose/gain steps.  I have noticed as well that
  8.    the pulse delay needs to change as the current is set to a higher
  9.    selection on the driver.   The driver also will only output no more
  10.    than 1.2amps per my testing.  Your mileage may vary.
  11.  
  12.    UPDATE:
  13.    The issue may be with the ENA function of the chip being slow to change
  14.    state.  If I enable the driver and leave it that way, no steps seem to be
  15.    lost.
  16. */
  17.  
  18. // Direct io macros.
  19. #define SET_BIT(p,n) ((p) |= (1 << (n)))
  20. #define CLR_BIT(p,n) ((p) &= (~(1) << (n)))
  21. #define TOGGLE_BIT(p,n) ((p) ^= ((1) << (n)))
  22.  
  23. // Arduino pins
  24. #define STEPPIN 12   // Connected to the PUL + on driver
  25. #define DIRPIN 6     // Connected to the DIR + on the driver
  26. #define ENAPIN 10    // Connected to the ENA + on the driver
  27. // DIR -, PUL - and ENA - are connected to the Arduino GND
  28.  
  29. // 328p ports
  30. #define STEP_OUT PB4
  31. #define DIR_OUT PD6
  32. #define ENA_OUT PB2
  33.  
  34. // Globals
  35. //const int PULSEDELAY = 750; // Delay for 0.5-0.7a
  36. const int PULSEDELAY = 850; // Delay for 1-1.2a
  37. //const int PULSEDELAY = 450; // Pulse delay for current setting of 1.5-1.7a
  38.  
  39. void setup() {
  40.   // put your setup code here, to run once:
  41.   pinMode(STEPPIN,OUTPUT);
  42.   pinMode(DIRPIN,OUTPUT);
  43.   pinMode(ENAPIN,OUTPUT);
  44.   SET_BIT (PORTB, ENA_OUT);
  45. }
  46.  
  47. void loop() {
  48.   forward(200);
  49.   delay(500);
  50.   forward(200);
  51.   delay(500);
  52.   TOGGLE_BIT (PORTD, DIR_OUT); //SET DIRECTION  
  53.   reverse(400);
  54.   delay(1000);
  55.   TOGGLE_BIT (PORTD, DIR_OUT); //REVERSE DIRECTION    
  56. }
  57.  
  58. void pulse (void) {
  59.     SET_BIT (PORTB, STEP_OUT);
  60.     delayMicroseconds(PULSEDELAY);
  61.     CLR_BIT (PORTB, STEP_OUT);
  62.     delayMicroseconds(PULSEDELAY);
  63. }
  64.  
  65. void forward(int steps){
  66.   for(int i=0;i<steps;i++){
  67.       pulse ();
  68.   }
  69. }
  70.  
  71. void reverse(int steps){
  72.   for(int i=0;i<steps;i++){
  73.       pulse ();
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment