My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

stepper.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. stepper.c - stepper motor driver: executes motion plans using stepper motors
  3. Part of Grbl
  4. Copyright (c) 2009-2011 Simen Svale Skogsrud
  5. Grbl is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Grbl is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  17. and Philipp Tiefenbacher. */
  18. #include "stepper.h"
  19. #include "Configuration.h"
  20. #include "Marlin.h"
  21. #include "planner.h"
  22. #include "pins.h"
  23. #include "fastio.h"
  24. #include "temperature.h"
  25. #include "ultralcd.h"
  26. #include "speed_lookuptable.h"
  27. //===========================================================================
  28. //=============================public variables ============================
  29. //===========================================================================
  30. block_t *current_block; // A pointer to the block currently being traced
  31. //===========================================================================
  32. //=============================private variables ============================
  33. //===========================================================================
  34. //static makes it inpossible to be called from outside of this file by extern.!
  35. // Variables used by The Stepper Driver Interrupt
  36. static unsigned char out_bits; // The next stepping-bits to be output
  37. static long counter_x, // Counter variables for the bresenham line tracer
  38. counter_y,
  39. counter_z,
  40. counter_e;
  41. static unsigned long step_events_completed; // The number of step events executed in the current block
  42. #ifdef ADVANCE
  43. static long advance_rate, advance, final_advance = 0;
  44. static short old_advance = 0;
  45. #endif
  46. static short e_steps;
  47. static unsigned char busy = false; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
  48. static long acceleration_time, deceleration_time;
  49. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  50. static unsigned short acc_step_rate; // needed for deccelaration start point
  51. static char step_loops;
  52. volatile long endstops_trigsteps[3]={0,0,0};
  53. volatile long endstops_stepsTotal,endstops_stepsDone;
  54. static volatile bool endstops_hit=false;
  55. // if DEBUG_STEPS is enabled, M114 can be used to compare two methods of determining the X,Y,Z position of the printer.
  56. // for debugging purposes only, should be disabled by default
  57. #ifdef DEBUG_STEPS
  58. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  59. volatile int count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  60. #endif
  61. //===========================================================================
  62. //=============================functions ============================
  63. //===========================================================================
  64. // intRes = intIn1 * intIn2 >> 16
  65. // uses:
  66. // r26 to store 0
  67. // r27 to store the byte 1 of the 24 bit result
  68. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  69. asm volatile ( \
  70. "clr r26 \n\t" \
  71. "mul %A1, %B2 \n\t" \
  72. "movw %A0, r0 \n\t" \
  73. "mul %A1, %A2 \n\t" \
  74. "add %A0, r1 \n\t" \
  75. "adc %B0, r26 \n\t" \
  76. "lsr r0 \n\t" \
  77. "adc %A0, r26 \n\t" \
  78. "adc %B0, r26 \n\t" \
  79. "clr r1 \n\t" \
  80. : \
  81. "=&r" (intRes) \
  82. : \
  83. "d" (charIn1), \
  84. "d" (intIn2) \
  85. : \
  86. "r26" \
  87. )
  88. // intRes = longIn1 * longIn2 >> 24
  89. // uses:
  90. // r26 to store 0
  91. // r27 to store the byte 1 of the 48bit result
  92. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  93. asm volatile ( \
  94. "clr r26 \n\t" \
  95. "mul %A1, %B2 \n\t" \
  96. "mov r27, r1 \n\t" \
  97. "mul %B1, %C2 \n\t" \
  98. "movw %A0, r0 \n\t" \
  99. "mul %C1, %C2 \n\t" \
  100. "add %B0, r0 \n\t" \
  101. "mul %C1, %B2 \n\t" \
  102. "add %A0, r0 \n\t" \
  103. "adc %B0, r1 \n\t" \
  104. "mul %A1, %C2 \n\t" \
  105. "add r27, r0 \n\t" \
  106. "adc %A0, r1 \n\t" \
  107. "adc %B0, r26 \n\t" \
  108. "mul %B1, %B2 \n\t" \
  109. "add r27, r0 \n\t" \
  110. "adc %A0, r1 \n\t" \
  111. "adc %B0, r26 \n\t" \
  112. "mul %C1, %A2 \n\t" \
  113. "add r27, r0 \n\t" \
  114. "adc %A0, r1 \n\t" \
  115. "adc %B0, r26 \n\t" \
  116. "mul %B1, %A2 \n\t" \
  117. "add r27, r1 \n\t" \
  118. "adc %A0, r26 \n\t" \
  119. "adc %B0, r26 \n\t" \
  120. "lsr r27 \n\t" \
  121. "adc %A0, r26 \n\t" \
  122. "adc %B0, r26 \n\t" \
  123. "clr r1 \n\t" \
  124. : \
  125. "=&r" (intRes) \
  126. : \
  127. "d" (longIn1), \
  128. "d" (longIn2) \
  129. : \
  130. "r26" , "r27" \
  131. )
  132. // Some useful constants
  133. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  134. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  135. inline void endstops_triggered(const unsigned long &stepstaken)
  136. {
  137. //this will only work if there is no bufferig
  138. //however, if you perform a move at which the endstops should be triggered, and wait for it to complete, i.e. by blocking command, it should work
  139. //yes, it uses floats, but: if endstops are triggered, thats hopefully not critical anymore anyways.
  140. //endstops_triggerpos;
  141. if(endstops_hit) //hitting a second time while the first hit is not reported
  142. return;
  143. if(current_block == NULL)
  144. return;
  145. endstops_stepsTotal=current_block->step_event_count;
  146. endstops_stepsDone=stepstaken;
  147. endstops_trigsteps[0]=current_block->steps_x;
  148. endstops_trigsteps[1]=current_block->steps_y;
  149. endstops_trigsteps[2]=current_block->steps_z;
  150. endstops_hit=true;
  151. }
  152. void checkHitEndstops()
  153. {
  154. if( !endstops_hit)
  155. return;
  156. float endstops_triggerpos[3]={0,0,0};
  157. float ratiodone=endstops_stepsDone/float(endstops_stepsTotal); //ratio of current_block thas was performed
  158. endstops_triggerpos[0]=current_position[0]-(endstops_trigsteps[0]*ratiodone)/float(axis_steps_per_unit[0]);
  159. endstops_triggerpos[1]=current_position[1]-(endstops_trigsteps[1]*ratiodone)/float(axis_steps_per_unit[1]);
  160. endstops_triggerpos[2]=current_position[2]-(endstops_trigsteps[2]*ratiodone)/float(axis_steps_per_unit[2]);
  161. SERIAL_ECHO_START;
  162. SERIAL_ECHOPGM("endstops hit: ");
  163. SERIAL_ECHOPAIR(" X:",endstops_triggerpos[0]);
  164. SERIAL_ECHOPAIR(" Y:",endstops_triggerpos[1]);
  165. SERIAL_ECHOPAIR(" Z:",endstops_triggerpos[2]);
  166. SERIAL_ECHOLN("");
  167. endstops_hit=false;
  168. }
  169. void endstops_hit_on_purpose()
  170. {
  171. endstops_hit=false;
  172. }
  173. // __________________________
  174. // /| |\ _________________ ^
  175. // / | | \ /| |\ |
  176. // / | | \ / | | \ s
  177. // / | | | | | \ p
  178. // / | | | | | \ e
  179. // +-----+------------------------+---+--+---------------+----+ e
  180. // | BLOCK 1 | BLOCK 2 | d
  181. //
  182. // time ----->
  183. //
  184. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  185. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  186. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  187. // The slope of acceleration is calculated with the leib ramp alghorithm.
  188. void st_wake_up() {
  189. // TCNT1 = 0;
  190. if(busy == false)
  191. ENABLE_STEPPER_DRIVER_INTERRUPT();
  192. }
  193. inline unsigned short calc_timer(unsigned short step_rate) {
  194. unsigned short timer;
  195. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  196. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  197. step_rate = (step_rate >> 2)&0x3fff;
  198. step_loops = 4;
  199. }
  200. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  201. step_rate = (step_rate >> 1)&0x7fff;
  202. step_loops = 2;
  203. }
  204. else {
  205. step_loops = 1;
  206. }
  207. if(step_rate < 32) step_rate = 32;
  208. step_rate -= 32; // Correct for minimal speed
  209. if(step_rate >= (8*256)){ // higher step rate
  210. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  211. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  212. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  213. MultiU16X8toH16(timer, tmp_step_rate, gain);
  214. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  215. }
  216. else { // lower step rates
  217. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  218. table_address += ((step_rate)>>1) & 0xfffc;
  219. timer = (unsigned short)pgm_read_word_near(table_address);
  220. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  221. }
  222. if(timer < 100) { timer = 100; Serial.print("Steprate to high : "); Serial.println(step_rate); }//(20kHz this should never happen)
  223. return timer;
  224. }
  225. // Initializes the trapezoid generator from the current block. Called whenever a new
  226. // block begins.
  227. inline void trapezoid_generator_reset() {
  228. #ifdef ADVANCE
  229. advance = current_block->initial_advance;
  230. final_advance = current_block->final_advance;
  231. #endif
  232. deceleration_time = 0;
  233. // step_rate to timer interval
  234. acc_step_rate = current_block->initial_rate;
  235. acceleration_time = calc_timer(acc_step_rate);
  236. OCR1A = acceleration_time;
  237. }
  238. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  239. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  240. ISR(TIMER1_COMPA_vect)
  241. {
  242. if(busy){
  243. SERIAL_ERROR_START
  244. SERIAL_ERROR(*(unsigned short *)OCR1A);
  245. SERIAL_ERRORLNPGM(" ISR overtaking itself.");
  246. return;
  247. } // The busy-flag is used to avoid reentering this interrupt
  248. busy = true;
  249. sei(); // Re enable interrupts (normally disabled while inside an interrupt handler)
  250. // If there is no current block, attempt to pop one from the buffer
  251. if (current_block == NULL) {
  252. // Anything in the buffer?
  253. current_block = plan_get_current_block();
  254. if (current_block != NULL) {
  255. trapezoid_generator_reset();
  256. counter_x = -(current_block->step_event_count >> 1);
  257. counter_y = counter_x;
  258. counter_z = counter_x;
  259. counter_e = counter_x;
  260. step_events_completed = 0;
  261. // #ifdef ADVANCE
  262. e_steps = 0;
  263. // #endif
  264. }
  265. else {
  266. // DISABLE_STEPPER_DRIVER_INTERRUPT();
  267. }
  268. }
  269. if (current_block != NULL) {
  270. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  271. out_bits = current_block->direction_bits;
  272. // Set direction en check limit switches
  273. if ((out_bits & (1<<X_AXIS)) != 0) { // -direction
  274. WRITE(X_DIR_PIN, INVERT_X_DIR);
  275. #ifdef DEBUG_STEPS
  276. count_direction[X_AXIS]=-1;
  277. #endif
  278. #if X_MIN_PIN > -1
  279. if(READ(X_MIN_PIN) != ENDSTOPS_INVERTING) {
  280. // endstops_triggered(step_events_completed);
  281. step_events_completed = current_block->step_event_count;
  282. }
  283. #endif
  284. }
  285. else { // +direction
  286. WRITE(X_DIR_PIN,!INVERT_X_DIR);
  287. #ifdef DEBUG_STEPS
  288. count_direction[X_AXIS]=1;
  289. #endif
  290. #if X_MAX_PIN > -1
  291. if((READ(X_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_x >0)){
  292. // endstops_triggered(step_events_completed);
  293. step_events_completed = current_block->step_event_count;
  294. }
  295. #endif
  296. }
  297. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  298. WRITE(Y_DIR_PIN,INVERT_Y_DIR);
  299. #ifdef DEBUG_STEPS
  300. count_direction[Y_AXIS]=-1;
  301. #endif
  302. #if Y_MIN_PIN > -1
  303. if(READ(Y_MIN_PIN) != ENDSTOPS_INVERTING) {
  304. // endstops_triggered(step_events_completed);
  305. step_events_completed = current_block->step_event_count;
  306. }
  307. #endif
  308. }
  309. else { // +direction
  310. WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
  311. #ifdef DEBUG_STEPS
  312. count_direction[Y_AXIS]=1;
  313. #endif
  314. #if Y_MAX_PIN > -1
  315. if((READ(Y_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_y >0)){
  316. // endstops_triggered(step_events_completed);
  317. step_events_completed = current_block->step_event_count;
  318. }
  319. #endif
  320. }
  321. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  322. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  323. #ifdef DEBUG_STEPS
  324. count_direction[Z_AXIS]=-1;
  325. #endif
  326. #if Z_MIN_PIN > -1
  327. if(READ(Z_MIN_PIN) != ENDSTOPS_INVERTING) {
  328. // endstops_triggered(step_events_completed);
  329. step_events_completed = current_block->step_event_count;
  330. }
  331. #endif
  332. }
  333. else { // +direction
  334. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  335. #ifdef DEBUG_STEPS
  336. count_direction[Z_AXIS]=1;
  337. #endif
  338. #if Z_MAX_PIN > -1
  339. if((READ(Z_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_z >0)){
  340. // endstops_triggered(step_events_completed);
  341. step_events_completed = current_block->step_event_count;
  342. }
  343. #endif
  344. }
  345. #ifndef ADVANCE
  346. if ((out_bits & (1<<E_AXIS)) != 0) // -direction
  347. WRITE(E_DIR_PIN,INVERT_E_DIR);
  348. else // +direction
  349. WRITE(E_DIR_PIN,!INVERT_E_DIR);
  350. #endif //!ADVANCE
  351. for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  352. /*
  353. counter_e += current_block->steps_e;
  354. if (counter_e > 0) {
  355. counter_e -= current_block->step_event_count;
  356. if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
  357. CRITICAL_SECTION_START;
  358. e_steps--;
  359. CRITICAL_SECTION_END;
  360. }
  361. else {
  362. CRITICAL_SECTION_START;
  363. e_steps++;
  364. CRITICAL_SECTION_END;
  365. }
  366. }
  367. */
  368. /*
  369. // Do E steps + advance steps
  370. CRITICAL_SECTION_START;
  371. e_steps += ((advance >> 16) - old_advance);
  372. CRITICAL_SECTION_END;
  373. old_advance = advance >> 16;
  374. */
  375. counter_x += current_block->steps_x;
  376. if (counter_x > 0) {
  377. WRITE(X_STEP_PIN, HIGH);
  378. counter_x -= current_block->step_event_count;
  379. WRITE(X_STEP_PIN, LOW);
  380. #ifdef DEBUG_STEPS
  381. count_position[X_AXIS]+=count_direction[X_AXIS];
  382. #endif
  383. }
  384. counter_y += current_block->steps_y;
  385. if (counter_y > 0) {
  386. WRITE(Y_STEP_PIN, HIGH);
  387. counter_y -= current_block->step_event_count;
  388. WRITE(Y_STEP_PIN, LOW);
  389. #ifdef DEBUG_STEPS
  390. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  391. #endif
  392. }
  393. counter_z += current_block->steps_z;
  394. if (counter_z > 0) {
  395. WRITE(Z_STEP_PIN, HIGH);
  396. counter_z -= current_block->step_event_count;
  397. WRITE(Z_STEP_PIN, LOW);
  398. #ifdef DEBUG_STEPS
  399. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  400. #endif
  401. }
  402. #ifndef ADVANCE
  403. counter_e += current_block->steps_e;
  404. if (counter_e > 0) {
  405. WRITE(E_STEP_PIN, HIGH);
  406. counter_e -= current_block->step_event_count;
  407. WRITE(E_STEP_PIN, LOW);
  408. }
  409. #endif //!ADVANCE
  410. step_events_completed += 1;
  411. if(step_events_completed >= current_block->step_event_count) break;
  412. }
  413. // Calculare new timer value
  414. unsigned short timer;
  415. unsigned short step_rate;
  416. if (step_events_completed <= current_block->accelerate_until) {
  417. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  418. acc_step_rate += current_block->initial_rate;
  419. // upper limit
  420. if(acc_step_rate > current_block->nominal_rate)
  421. acc_step_rate = current_block->nominal_rate;
  422. // step_rate to timer interval
  423. timer = calc_timer(acc_step_rate);
  424. #ifdef ADVANCE
  425. advance += advance_rate;
  426. #endif
  427. acceleration_time += timer;
  428. OCR1A = timer;
  429. }
  430. else if (step_events_completed > current_block->decelerate_after) {
  431. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  432. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  433. step_rate = current_block->final_rate;
  434. }
  435. else {
  436. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  437. }
  438. // lower limit
  439. if(step_rate < current_block->final_rate)
  440. step_rate = current_block->final_rate;
  441. // step_rate to timer interval
  442. timer = calc_timer(step_rate);
  443. #ifdef ADVANCE
  444. advance -= advance_rate;
  445. if(advance < final_advance)
  446. advance = final_advance;
  447. #endif //ADVANCE
  448. deceleration_time += timer;
  449. OCR1A = timer;
  450. }
  451. else {
  452. timer = calc_timer(current_block->nominal_rate);
  453. OCR1A = timer;
  454. }
  455. // If current block is finished, reset pointer
  456. if (step_events_completed >= current_block->step_event_count) {
  457. current_block = NULL;
  458. plan_discard_current_block();
  459. }
  460. }
  461. cli(); // disable interrupts
  462. busy=false;
  463. }
  464. #ifdef ADVANCE
  465. unsigned char old_OCR0A;
  466. // Timer interrupt for E. e_steps is set in the main routine;
  467. // Timer 0 is shared with millies
  468. ISR(TIMER0_COMPA_vect)
  469. {
  470. // Critical section needed because Timer 1 interrupt has higher priority.
  471. // The pin set functions are placed on trategic position to comply with the stepper driver timing.
  472. WRITE(E_STEP_PIN, LOW);
  473. // Set E direction (Depends on E direction + advance)
  474. if (e_steps < 0) {
  475. WRITE(E_DIR_PIN,INVERT_E_DIR);
  476. e_steps++;
  477. WRITE(E_STEP_PIN, HIGH);
  478. }
  479. if (e_steps > 0) {
  480. WRITE(E_DIR_PIN,!INVERT_E_DIR);
  481. e_steps--;
  482. WRITE(E_STEP_PIN, HIGH);
  483. }
  484. old_OCR0A += 25; // 10kHz interrupt
  485. OCR0A = old_OCR0A;
  486. }
  487. #endif // ADVANCE
  488. void st_init()
  489. {
  490. //Initialize Dir Pins
  491. #if X_DIR_PIN > -1
  492. SET_OUTPUT(X_DIR_PIN);
  493. #endif
  494. #if Y_DIR_PIN > -1
  495. SET_OUTPUT(Y_DIR_PIN);
  496. #endif
  497. #if Z_DIR_PIN > -1
  498. SET_OUTPUT(Z_DIR_PIN);
  499. #endif
  500. #if E_DIR_PIN > -1
  501. SET_OUTPUT(E_DIR_PIN);
  502. #endif
  503. //Initialize Enable Pins - steppers default to disabled.
  504. #if (X_ENABLE_PIN > -1)
  505. SET_OUTPUT(X_ENABLE_PIN);
  506. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  507. #endif
  508. #if (Y_ENABLE_PIN > -1)
  509. SET_OUTPUT(Y_ENABLE_PIN);
  510. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  511. #endif
  512. #if (Z_ENABLE_PIN > -1)
  513. SET_OUTPUT(Z_ENABLE_PIN);
  514. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  515. #endif
  516. #if (E_ENABLE_PIN > -1)
  517. SET_OUTPUT(E_ENABLE_PIN);
  518. if(!E_ENABLE_ON) WRITE(E_ENABLE_PIN,HIGH);
  519. #endif
  520. //endstops and pullups
  521. #ifdef ENDSTOPPULLUPS
  522. #if X_MIN_PIN > -1
  523. SET_INPUT(X_MIN_PIN);
  524. WRITE(X_MIN_PIN,HIGH);
  525. #endif
  526. #if X_MAX_PIN > -1
  527. SET_INPUT(X_MAX_PIN);
  528. WRITE(X_MAX_PIN,HIGH);
  529. #endif
  530. #if Y_MIN_PIN > -1
  531. SET_INPUT(Y_MIN_PIN);
  532. WRITE(Y_MIN_PIN,HIGH);
  533. #endif
  534. #if Y_MAX_PIN > -1
  535. SET_INPUT(Y_MAX_PIN);
  536. WRITE(Y_MAX_PIN,HIGH);
  537. #endif
  538. #if Z_MIN_PIN > -1
  539. SET_INPUT(Z_MIN_PIN);
  540. WRITE(Z_MIN_PIN,HIGH);
  541. #endif
  542. #if Z_MAX_PIN > -1
  543. SET_INPUT(Z_MAX_PIN);
  544. WRITE(Z_MAX_PIN,HIGH);
  545. #endif
  546. #else //ENDSTOPPULLUPS
  547. #if X_MIN_PIN > -1
  548. SET_INPUT(X_MIN_PIN);
  549. #endif
  550. #if X_MAX_PIN > -1
  551. SET_INPUT(X_MAX_PIN);
  552. #endif
  553. #if Y_MIN_PIN > -1
  554. SET_INPUT(Y_MIN_PIN);
  555. #endif
  556. #if Y_MAX_PIN > -1
  557. SET_INPUT(Y_MAX_PIN);
  558. #endif
  559. #if Z_MIN_PIN > -1
  560. SET_INPUT(Z_MIN_PIN);
  561. #endif
  562. #if Z_MAX_PIN > -1
  563. SET_INPUT(Z_MAX_PIN);
  564. #endif
  565. #endif //ENDSTOPPULLUPS
  566. //Initialize Step Pins
  567. #if (X_STEP_PIN > -1)
  568. SET_OUTPUT(X_STEP_PIN);
  569. #endif
  570. #if (Y_STEP_PIN > -1)
  571. SET_OUTPUT(Y_STEP_PIN);
  572. #endif
  573. #if (Z_STEP_PIN > -1)
  574. SET_OUTPUT(Z_STEP_PIN);
  575. #endif
  576. #if (E_STEP_PIN > -1)
  577. SET_OUTPUT(E_STEP_PIN);
  578. #endif
  579. // waveform generation = 0100 = CTC
  580. TCCR1B &= ~(1<<WGM13);
  581. TCCR1B |= (1<<WGM12);
  582. TCCR1A &= ~(1<<WGM11);
  583. TCCR1A &= ~(1<<WGM10);
  584. // output mode = 00 (disconnected)
  585. TCCR1A &= ~(3<<COM1A0);
  586. TCCR1A &= ~(3<<COM1B0);
  587. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10); // 2MHz timer
  588. OCR1A = 0x4000;
  589. TCNT1 = 0;
  590. ENABLE_STEPPER_DRIVER_INTERRUPT();
  591. #ifdef ADVANCE
  592. e_steps = 0;
  593. TIMSK0 |= (1<<OCIE0A);
  594. #endif //ADVANCE
  595. sei();
  596. }
  597. // Block until all buffered steps are executed
  598. void st_synchronize()
  599. {
  600. while(plan_get_current_block()) {
  601. manage_heater();
  602. manage_inactivity(1);
  603. LCD_STATUS;
  604. }
  605. }