stepper.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 "Marlin.h"
  19. #include "stepper.h"
  20. #include "planner.h"
  21. #include "temperature.h"
  22. #include "ultralcd.h"
  23. #include "language.h"
  24. #include "cardreader.h"
  25. #include "speed_lookuptable.h"
  26. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  27. #include <SPI.h>
  28. #endif
  29. //===========================================================================
  30. //=============================public variables ============================
  31. //===========================================================================
  32. block_t *current_block; // A pointer to the block currently being traced
  33. //===========================================================================
  34. //=============================private variables ============================
  35. //===========================================================================
  36. //static makes it inpossible to be called from outside of this file by extern.!
  37. // Variables used by The Stepper Driver Interrupt
  38. static unsigned char out_bits; // The next stepping-bits to be output
  39. static int32_t counter_x, // Counter variables for the bresenham line tracer
  40. counter_y,
  41. counter_z,
  42. counter_e;
  43. volatile static uint32_t step_events_completed; // The number of step events executed in the current block
  44. static int32_t acceleration_time, deceleration_time;
  45. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  46. static uint16_t acc_step_rate; // needed for deccelaration start point
  47. static uint8_t step_loops;
  48. static uint16_t OCR1A_nominal;
  49. static uint8_t step_loops_nominal;
  50. volatile long endstops_trigsteps[3]={0,0,0};
  51. volatile long endstops_stepsTotal,endstops_stepsDone;
  52. static volatile bool endstop_x_hit=false;
  53. static volatile bool endstop_y_hit=false;
  54. static volatile bool endstop_z_hit=false;
  55. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  56. bool abort_on_endstop_hit = false;
  57. #endif
  58. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  59. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  60. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  61. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  62. #endif
  63. static bool old_x_min_endstop=false;
  64. static bool old_x_max_endstop=false;
  65. static bool old_y_min_endstop=false;
  66. static bool old_y_max_endstop=false;
  67. static bool old_z_min_endstop=false;
  68. static bool old_z_max_endstop=false;
  69. static bool check_endstops = true;
  70. static bool check_z_endstop = false;
  71. int8_t SilentMode;
  72. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  73. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  74. //===========================================================================
  75. //=============================functions ============================
  76. //===========================================================================
  77. #define CHECK_ENDSTOPS if(check_endstops)
  78. // intRes = intIn1 * intIn2 >> 16
  79. // uses:
  80. // r26 to store 0
  81. // r27 to store the byte 1 of the 24 bit result
  82. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  83. asm volatile ( \
  84. "clr r26 \n\t" \
  85. "mul %A1, %B2 \n\t" \
  86. "movw %A0, r0 \n\t" \
  87. "mul %A1, %A2 \n\t" \
  88. "add %A0, r1 \n\t" \
  89. "adc %B0, r26 \n\t" \
  90. "lsr r0 \n\t" \
  91. "adc %A0, r26 \n\t" \
  92. "adc %B0, r26 \n\t" \
  93. "clr r1 \n\t" \
  94. : \
  95. "=&r" (intRes) \
  96. : \
  97. "d" (charIn1), \
  98. "d" (intIn2) \
  99. : \
  100. "r26" \
  101. )
  102. // intRes = longIn1 * longIn2 >> 24
  103. // uses:
  104. // r26 to store 0
  105. // r27 to store the byte 1 of the 48bit result
  106. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  107. asm volatile ( \
  108. "clr r26 \n\t" \
  109. "mul %A1, %B2 \n\t" \
  110. "mov r27, r1 \n\t" \
  111. "mul %B1, %C2 \n\t" \
  112. "movw %A0, r0 \n\t" \
  113. "mul %C1, %C2 \n\t" \
  114. "add %B0, r0 \n\t" \
  115. "mul %C1, %B2 \n\t" \
  116. "add %A0, r0 \n\t" \
  117. "adc %B0, r1 \n\t" \
  118. "mul %A1, %C2 \n\t" \
  119. "add r27, r0 \n\t" \
  120. "adc %A0, r1 \n\t" \
  121. "adc %B0, r26 \n\t" \
  122. "mul %B1, %B2 \n\t" \
  123. "add r27, r0 \n\t" \
  124. "adc %A0, r1 \n\t" \
  125. "adc %B0, r26 \n\t" \
  126. "mul %C1, %A2 \n\t" \
  127. "add r27, r0 \n\t" \
  128. "adc %A0, r1 \n\t" \
  129. "adc %B0, r26 \n\t" \
  130. "mul %B1, %A2 \n\t" \
  131. "add r27, r1 \n\t" \
  132. "adc %A0, r26 \n\t" \
  133. "adc %B0, r26 \n\t" \
  134. "lsr r27 \n\t" \
  135. "adc %A0, r26 \n\t" \
  136. "adc %B0, r26 \n\t" \
  137. "clr r1 \n\t" \
  138. : \
  139. "=&r" (intRes) \
  140. : \
  141. "d" (longIn1), \
  142. "d" (longIn2) \
  143. : \
  144. "r26" , "r27" \
  145. )
  146. // Some useful constants
  147. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  148. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  149. void checkHitEndstops()
  150. {
  151. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  152. SERIAL_ECHO_START;
  153. SERIAL_ECHORPGM(MSG_ENDSTOPS_HIT);
  154. if(endstop_x_hit) {
  155. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  156. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("X")));
  157. }
  158. if(endstop_y_hit) {
  159. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  160. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("Y")));
  161. }
  162. if(endstop_z_hit) {
  163. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  164. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT,PSTR("Z")));
  165. }
  166. SERIAL_ECHOLN("");
  167. endstop_x_hit=false;
  168. endstop_y_hit=false;
  169. endstop_z_hit=false;
  170. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  171. if (abort_on_endstop_hit)
  172. {
  173. card.sdprinting = false;
  174. card.closefile();
  175. quickStop();
  176. setTargetHotend0(0);
  177. setTargetHotend1(0);
  178. setTargetHotend2(0);
  179. }
  180. #endif
  181. }
  182. }
  183. bool endstops_hit_on_purpose()
  184. {
  185. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  186. endstop_x_hit=false;
  187. endstop_y_hit=false;
  188. endstop_z_hit=false;
  189. return hit;
  190. }
  191. bool endstop_z_hit_on_purpose()
  192. {
  193. bool hit = endstop_z_hit;
  194. endstop_z_hit=false;
  195. return hit;
  196. }
  197. bool enable_endstops(bool check)
  198. {
  199. bool old = check_endstops;
  200. check_endstops = check;
  201. return old;
  202. }
  203. bool enable_z_endstop(bool check)
  204. {
  205. bool old = check_z_endstop;
  206. check_z_endstop = check;
  207. endstop_z_hit=false;
  208. return old;
  209. }
  210. // __________________________
  211. // /| |\ _________________ ^
  212. // / | | \ /| |\ |
  213. // / | | \ / | | \ s
  214. // / | | | | | \ p
  215. // / | | | | | \ e
  216. // +-----+------------------------+---+--+---------------+----+ e
  217. // | BLOCK 1 | BLOCK 2 | d
  218. //
  219. // time ----->
  220. //
  221. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  222. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  223. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  224. // The slope of acceleration is calculated with the leib ramp alghorithm.
  225. void st_wake_up() {
  226. // TCNT1 = 0;
  227. ENABLE_STEPPER_DRIVER_INTERRUPT();
  228. }
  229. void step_wait(){
  230. for(int8_t i=0; i < 6; i++){
  231. }
  232. }
  233. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  234. unsigned short timer;
  235. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  236. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  237. step_rate = (step_rate >> 2)&0x3fff;
  238. step_loops = 4;
  239. }
  240. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  241. step_rate = (step_rate >> 1)&0x7fff;
  242. step_loops = 2;
  243. }
  244. else {
  245. step_loops = 1;
  246. }
  247. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  248. step_rate -= (F_CPU/500000); // Correct for minimal speed
  249. if(step_rate >= (8*256)){ // higher step rate
  250. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  251. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  252. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  253. MultiU16X8toH16(timer, tmp_step_rate, gain);
  254. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  255. }
  256. else { // lower step rates
  257. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  258. table_address += ((step_rate)>>1) & 0xfffc;
  259. timer = (unsigned short)pgm_read_word_near(table_address);
  260. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  261. }
  262. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  263. return timer;
  264. }
  265. // Initializes the trapezoid generator from the current block. Called whenever a new
  266. // block begins.
  267. FORCE_INLINE void trapezoid_generator_reset() {
  268. deceleration_time = 0;
  269. // step_rate to timer interval
  270. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  271. // make a note of the number of step loops required at nominal speed
  272. step_loops_nominal = step_loops;
  273. acc_step_rate = current_block->initial_rate;
  274. acceleration_time = calc_timer(acc_step_rate);
  275. OCR1A = acceleration_time;
  276. // SERIAL_ECHO_START;
  277. // SERIAL_ECHOPGM("advance :");
  278. // SERIAL_ECHO(current_block->advance/256.0);
  279. // SERIAL_ECHOPGM("advance rate :");
  280. // SERIAL_ECHO(current_block->advance_rate/256.0);
  281. // SERIAL_ECHOPGM("initial advance :");
  282. // SERIAL_ECHO(current_block->initial_advance/256.0);
  283. // SERIAL_ECHOPGM("final advance :");
  284. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  285. }
  286. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  287. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  288. ISR(TIMER1_COMPA_vect)
  289. {
  290. // If there is no current block, attempt to pop one from the buffer
  291. if (current_block == NULL) {
  292. // Anything in the buffer?
  293. current_block = plan_get_current_block();
  294. if (current_block != NULL) {
  295. // The busy flag is set by the plan_get_current_block() call.
  296. // current_block->busy = true;
  297. trapezoid_generator_reset();
  298. counter_x = -(current_block->step_event_count >> 1);
  299. counter_y = counter_x;
  300. counter_z = counter_x;
  301. counter_e = counter_x;
  302. step_events_completed = 0;
  303. #ifdef Z_LATE_ENABLE
  304. if(current_block->steps_z > 0) {
  305. enable_z();
  306. OCR1A = 2000; //1ms wait
  307. return;
  308. }
  309. #endif
  310. }
  311. else {
  312. OCR1A=2000; // 1kHz.
  313. }
  314. }
  315. if (current_block != NULL) {
  316. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  317. out_bits = current_block->direction_bits;
  318. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  319. if((out_bits & (1<<X_AXIS))!=0){
  320. WRITE(X_DIR_PIN, INVERT_X_DIR);
  321. count_direction[X_AXIS]=-1;
  322. }
  323. else{
  324. WRITE(X_DIR_PIN, !INVERT_X_DIR);
  325. count_direction[X_AXIS]=1;
  326. }
  327. if((out_bits & (1<<Y_AXIS))!=0){
  328. WRITE(Y_DIR_PIN, INVERT_Y_DIR);
  329. #ifdef Y_DUAL_STEPPER_DRIVERS
  330. WRITE(Y2_DIR_PIN, !(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  331. #endif
  332. count_direction[Y_AXIS]=-1;
  333. }
  334. else{
  335. WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
  336. #ifdef Y_DUAL_STEPPER_DRIVERS
  337. WRITE(Y2_DIR_PIN, (INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  338. #endif
  339. count_direction[Y_AXIS]=1;
  340. }
  341. // Set direction en check limit switches
  342. #ifndef COREXY
  343. if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
  344. #else
  345. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) { //-X occurs for -A and -B
  346. #endif
  347. CHECK_ENDSTOPS
  348. {
  349. {
  350. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  351. bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  352. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  353. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  354. endstop_x_hit=true;
  355. step_events_completed = current_block->step_event_count;
  356. }
  357. old_x_min_endstop = x_min_endstop;
  358. #endif
  359. }
  360. }
  361. }
  362. else { // +direction
  363. CHECK_ENDSTOPS
  364. {
  365. {
  366. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  367. bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  368. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  369. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  370. endstop_x_hit=true;
  371. step_events_completed = current_block->step_event_count;
  372. }
  373. old_x_max_endstop = x_max_endstop;
  374. #endif
  375. }
  376. }
  377. }
  378. #ifndef COREXY
  379. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  380. #else
  381. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) { // -Y occurs for -A and +B
  382. #endif
  383. CHECK_ENDSTOPS
  384. {
  385. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  386. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  387. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  388. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  389. endstop_y_hit=true;
  390. step_events_completed = current_block->step_event_count;
  391. }
  392. old_y_min_endstop = y_min_endstop;
  393. #endif
  394. }
  395. }
  396. else { // +direction
  397. CHECK_ENDSTOPS
  398. {
  399. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  400. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  401. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  402. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  403. endstop_y_hit=true;
  404. step_events_completed = current_block->step_event_count;
  405. }
  406. old_y_max_endstop = y_max_endstop;
  407. #endif
  408. }
  409. }
  410. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  411. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  412. #ifdef Z_DUAL_STEPPER_DRIVERS
  413. WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
  414. #endif
  415. count_direction[Z_AXIS]=-1;
  416. if(check_endstops && ! check_z_endstop)
  417. {
  418. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  419. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  420. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  421. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  422. endstop_z_hit=true;
  423. step_events_completed = current_block->step_event_count;
  424. }
  425. old_z_min_endstop = z_min_endstop;
  426. #endif
  427. }
  428. }
  429. else { // +direction
  430. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  431. #ifdef Z_DUAL_STEPPER_DRIVERS
  432. WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
  433. #endif
  434. count_direction[Z_AXIS]=1;
  435. CHECK_ENDSTOPS
  436. {
  437. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  438. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  439. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  440. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  441. endstop_z_hit=true;
  442. step_events_completed = current_block->step_event_count;
  443. }
  444. old_z_max_endstop = z_max_endstop;
  445. #endif
  446. }
  447. }
  448. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  449. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  450. if(check_z_endstop) {
  451. // Check the Z min end-stop no matter what.
  452. // Good for searching for the center of an induction target.
  453. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  454. if(z_min_endstop && old_z_min_endstop) {
  455. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  456. endstop_z_hit=true;
  457. step_events_completed = current_block->step_event_count;
  458. }
  459. old_z_min_endstop = z_min_endstop;
  460. }
  461. #endif
  462. if ((out_bits & (1<<E_AXIS)) != 0) { // -direction
  463. REV_E_DIR();
  464. count_direction[E_AXIS]=-1;
  465. }
  466. else { // +direction
  467. NORM_E_DIR();
  468. count_direction[E_AXIS]=1;
  469. }
  470. for(uint8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  471. #ifndef AT90USB
  472. MSerial.checkRx(); // Check for serial chars.
  473. #endif
  474. counter_x += current_block->steps_x;
  475. if (counter_x > 0) {
  476. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  477. counter_x -= current_block->step_event_count;
  478. count_position[X_AXIS]+=count_direction[X_AXIS];
  479. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  480. }
  481. counter_y += current_block->steps_y;
  482. if (counter_y > 0) {
  483. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  484. #ifdef Y_DUAL_STEPPER_DRIVERS
  485. WRITE(Y2_STEP_PIN, !INVERT_Y_STEP_PIN);
  486. #endif
  487. counter_y -= current_block->step_event_count;
  488. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  489. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  490. #ifdef Y_DUAL_STEPPER_DRIVERS
  491. WRITE(Y2_STEP_PIN, INVERT_Y_STEP_PIN);
  492. #endif
  493. }
  494. counter_z += current_block->steps_z;
  495. if (counter_z > 0) {
  496. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  497. #ifdef Z_DUAL_STEPPER_DRIVERS
  498. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  499. #endif
  500. counter_z -= current_block->step_event_count;
  501. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  502. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  503. #ifdef Z_DUAL_STEPPER_DRIVERS
  504. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  505. #endif
  506. }
  507. counter_e += current_block->steps_e;
  508. if (counter_e > 0) {
  509. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  510. counter_e -= current_block->step_event_count;
  511. count_position[E_AXIS]+=count_direction[E_AXIS];
  512. WRITE_E_STEP(INVERT_E_STEP_PIN);
  513. }
  514. step_events_completed += 1;
  515. if(step_events_completed >= current_block->step_event_count) break;
  516. }
  517. // Calculare new timer value
  518. unsigned short timer;
  519. unsigned short step_rate;
  520. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  521. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  522. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  523. acc_step_rate += current_block->initial_rate;
  524. // upper limit
  525. if(acc_step_rate > current_block->nominal_rate)
  526. acc_step_rate = current_block->nominal_rate;
  527. // step_rate to timer interval
  528. timer = calc_timer(acc_step_rate);
  529. OCR1A = timer;
  530. acceleration_time += timer;
  531. }
  532. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  533. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  534. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  535. step_rate = current_block->final_rate;
  536. }
  537. else {
  538. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  539. }
  540. // lower limit
  541. if(step_rate < current_block->final_rate)
  542. step_rate = current_block->final_rate;
  543. // step_rate to timer interval
  544. timer = calc_timer(step_rate);
  545. OCR1A = timer;
  546. deceleration_time += timer;
  547. }
  548. else {
  549. OCR1A = OCR1A_nominal;
  550. // ensure we're running at the correct step rate, even if we just came off an acceleration
  551. step_loops = step_loops_nominal;
  552. }
  553. // If current block is finished, reset pointer
  554. if (step_events_completed >= current_block->step_event_count) {
  555. current_block = NULL;
  556. plan_discard_current_block();
  557. }
  558. }
  559. }
  560. void st_init()
  561. {
  562. digipot_init(); //Initialize Digipot Motor Current
  563. microstep_init(); //Initialize Microstepping Pins
  564. //Initialize Dir Pins
  565. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  566. SET_OUTPUT(X_DIR_PIN);
  567. #endif
  568. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  569. SET_OUTPUT(X2_DIR_PIN);
  570. #endif
  571. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  572. SET_OUTPUT(Y_DIR_PIN);
  573. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  574. SET_OUTPUT(Y2_DIR_PIN);
  575. #endif
  576. #endif
  577. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  578. SET_OUTPUT(Z_DIR_PIN);
  579. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  580. SET_OUTPUT(Z2_DIR_PIN);
  581. #endif
  582. #endif
  583. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  584. SET_OUTPUT(E0_DIR_PIN);
  585. #endif
  586. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  587. SET_OUTPUT(E1_DIR_PIN);
  588. #endif
  589. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  590. SET_OUTPUT(E2_DIR_PIN);
  591. #endif
  592. //Initialize Enable Pins - steppers default to disabled.
  593. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  594. SET_OUTPUT(X_ENABLE_PIN);
  595. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  596. #endif
  597. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  598. SET_OUTPUT(X2_ENABLE_PIN);
  599. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  600. #endif
  601. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  602. SET_OUTPUT(Y_ENABLE_PIN);
  603. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  604. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  605. SET_OUTPUT(Y2_ENABLE_PIN);
  606. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  607. #endif
  608. #endif
  609. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  610. SET_OUTPUT(Z_ENABLE_PIN);
  611. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  612. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  613. SET_OUTPUT(Z2_ENABLE_PIN);
  614. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  615. #endif
  616. #endif
  617. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  618. SET_OUTPUT(E0_ENABLE_PIN);
  619. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  620. #endif
  621. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  622. SET_OUTPUT(E1_ENABLE_PIN);
  623. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  624. #endif
  625. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  626. SET_OUTPUT(E2_ENABLE_PIN);
  627. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  628. #endif
  629. //endstops and pullups
  630. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  631. SET_INPUT(X_MIN_PIN);
  632. #ifdef ENDSTOPPULLUP_XMIN
  633. WRITE(X_MIN_PIN,HIGH);
  634. #endif
  635. #endif
  636. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  637. SET_INPUT(Y_MIN_PIN);
  638. #ifdef ENDSTOPPULLUP_YMIN
  639. WRITE(Y_MIN_PIN,HIGH);
  640. #endif
  641. #endif
  642. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  643. SET_INPUT(Z_MIN_PIN);
  644. #ifdef ENDSTOPPULLUP_ZMIN
  645. WRITE(Z_MIN_PIN,HIGH);
  646. #endif
  647. #endif
  648. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  649. SET_INPUT(X_MAX_PIN);
  650. #ifdef ENDSTOPPULLUP_XMAX
  651. WRITE(X_MAX_PIN,HIGH);
  652. #endif
  653. #endif
  654. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  655. SET_INPUT(Y_MAX_PIN);
  656. #ifdef ENDSTOPPULLUP_YMAX
  657. WRITE(Y_MAX_PIN,HIGH);
  658. #endif
  659. #endif
  660. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  661. SET_INPUT(Z_MAX_PIN);
  662. #ifdef ENDSTOPPULLUP_ZMAX
  663. WRITE(Z_MAX_PIN,HIGH);
  664. #endif
  665. #endif
  666. //Initialize Step Pins
  667. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  668. SET_OUTPUT(X_STEP_PIN);
  669. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  670. disable_x();
  671. #endif
  672. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  673. SET_OUTPUT(X2_STEP_PIN);
  674. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  675. disable_x();
  676. #endif
  677. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  678. SET_OUTPUT(Y_STEP_PIN);
  679. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  680. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  681. SET_OUTPUT(Y2_STEP_PIN);
  682. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  683. #endif
  684. disable_y();
  685. #endif
  686. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  687. SET_OUTPUT(Z_STEP_PIN);
  688. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  689. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  690. SET_OUTPUT(Z2_STEP_PIN);
  691. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  692. #endif
  693. disable_z();
  694. #endif
  695. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  696. SET_OUTPUT(E0_STEP_PIN);
  697. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  698. disable_e0();
  699. #endif
  700. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  701. SET_OUTPUT(E1_STEP_PIN);
  702. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  703. disable_e1();
  704. #endif
  705. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  706. SET_OUTPUT(E2_STEP_PIN);
  707. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  708. disable_e2();
  709. #endif
  710. // waveform generation = 0100 = CTC
  711. TCCR1B &= ~(1<<WGM13);
  712. TCCR1B |= (1<<WGM12);
  713. TCCR1A &= ~(1<<WGM11);
  714. TCCR1A &= ~(1<<WGM10);
  715. // output mode = 00 (disconnected)
  716. TCCR1A &= ~(3<<COM1A0);
  717. TCCR1A &= ~(3<<COM1B0);
  718. // Set the timer pre-scaler
  719. // Generally we use a divider of 8, resulting in a 2MHz timer
  720. // frequency on a 16MHz MCU. If you are going to change this, be
  721. // sure to regenerate speed_lookuptable.h with
  722. // create_speed_lookuptable.py
  723. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  724. OCR1A = 0x4000;
  725. TCNT1 = 0;
  726. ENABLE_STEPPER_DRIVER_INTERRUPT();
  727. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  728. sei();
  729. }
  730. // Block until all buffered steps are executed
  731. void st_synchronize()
  732. {
  733. while( blocks_queued()) {
  734. manage_heater();
  735. // Vojtech: Don't disable motors inside the planner!
  736. manage_inactivity(true);
  737. lcd_update();
  738. }
  739. }
  740. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  741. {
  742. CRITICAL_SECTION_START;
  743. count_position[X_AXIS] = x;
  744. count_position[Y_AXIS] = y;
  745. count_position[Z_AXIS] = z;
  746. count_position[E_AXIS] = e;
  747. CRITICAL_SECTION_END;
  748. }
  749. void st_set_e_position(const long &e)
  750. {
  751. CRITICAL_SECTION_START;
  752. count_position[E_AXIS] = e;
  753. CRITICAL_SECTION_END;
  754. }
  755. long st_get_position(uint8_t axis)
  756. {
  757. long count_pos;
  758. CRITICAL_SECTION_START;
  759. count_pos = count_position[axis];
  760. CRITICAL_SECTION_END;
  761. return count_pos;
  762. }
  763. float st_get_position_mm(uint8_t axis)
  764. {
  765. float steper_position_in_steps = st_get_position(axis);
  766. return steper_position_in_steps / axis_steps_per_unit[axis];
  767. }
  768. void finishAndDisableSteppers()
  769. {
  770. st_synchronize();
  771. disable_x();
  772. disable_y();
  773. disable_z();
  774. disable_e0();
  775. disable_e1();
  776. disable_e2();
  777. }
  778. void quickStop()
  779. {
  780. DISABLE_STEPPER_DRIVER_INTERRUPT();
  781. while (blocks_queued()) plan_discard_current_block();
  782. current_block = NULL;
  783. ENABLE_STEPPER_DRIVER_INTERRUPT();
  784. }
  785. #ifdef BABYSTEPPING
  786. void babystep(const uint8_t axis,const bool direction)
  787. {
  788. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  789. //store initial pin states
  790. switch(axis)
  791. {
  792. case X_AXIS:
  793. {
  794. enable_x();
  795. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  796. //setup new step
  797. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  798. //perform step
  799. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  800. {
  801. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  802. }
  803. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  804. //get old pin state back.
  805. WRITE(X_DIR_PIN,old_x_dir_pin);
  806. }
  807. break;
  808. case Y_AXIS:
  809. {
  810. enable_y();
  811. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  812. //setup new step
  813. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  814. //perform step
  815. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  816. {
  817. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  818. }
  819. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  820. //get old pin state back.
  821. WRITE(Y_DIR_PIN,old_y_dir_pin);
  822. }
  823. break;
  824. case Z_AXIS:
  825. {
  826. enable_z();
  827. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  828. //setup new step
  829. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  830. #ifdef Z_DUAL_STEPPER_DRIVERS
  831. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  832. #endif
  833. //perform step
  834. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  835. #ifdef Z_DUAL_STEPPER_DRIVERS
  836. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  837. #endif
  838. //wait a tiny bit
  839. {
  840. volatile float x=1./float(axis+1); //absolutely useless
  841. }
  842. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  843. #ifdef Z_DUAL_STEPPER_DRIVERS
  844. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  845. #endif
  846. //get old pin state back.
  847. WRITE(Z_DIR_PIN,old_z_dir_pin);
  848. #ifdef Z_DUAL_STEPPER_DRIVERS
  849. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  850. #endif
  851. }
  852. break;
  853. default: break;
  854. }
  855. }
  856. #endif //BABYSTEPPING
  857. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  858. {
  859. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  860. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  861. SPI.transfer(address); // send in the address and value via SPI:
  862. SPI.transfer(value);
  863. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  864. //delay(10);
  865. #endif
  866. }
  867. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  868. {
  869. do
  870. {
  871. *value = eeprom_read_byte((unsigned char*)pos);
  872. pos++;
  873. value++;
  874. }while(--size);
  875. }
  876. void digipot_init() //Initialize Digipot Motor Current
  877. {
  878. EEPROM_read_st(EEPROM_SILENT,(uint8_t*)&SilentMode,sizeof(SilentMode));
  879. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  880. if(SilentMode == 0){
  881. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT_LOUD;
  882. }else{
  883. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  884. }
  885. SPI.begin();
  886. pinMode(DIGIPOTSS_PIN, OUTPUT);
  887. for(int i=0;i<=4;i++)
  888. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  889. digipot_current(i,digipot_motor_current[i]);
  890. #endif
  891. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  892. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  893. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  894. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  895. if((SilentMode == 0) || (farm_mode) ){
  896. motor_current_setting[0] = motor_current_setting_loud[0];
  897. motor_current_setting[1] = motor_current_setting_loud[1];
  898. motor_current_setting[2] = motor_current_setting_loud[2];
  899. }else{
  900. motor_current_setting[0] = motor_current_setting_silent[0];
  901. motor_current_setting[1] = motor_current_setting_silent[1];
  902. motor_current_setting[2] = motor_current_setting_silent[2];
  903. }
  904. digipot_current(0, motor_current_setting[0]);
  905. digipot_current(1, motor_current_setting[1]);
  906. digipot_current(2, motor_current_setting[2]);
  907. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  908. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  909. #endif
  910. }
  911. void digipot_current(uint8_t driver, int current)
  912. {
  913. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  914. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  915. digitalPotWrite(digipot_ch[driver], current);
  916. #endif
  917. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  918. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  919. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  920. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  921. #endif
  922. }
  923. void microstep_init()
  924. {
  925. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  926. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  927. pinMode(E1_MS1_PIN,OUTPUT);
  928. pinMode(E1_MS2_PIN,OUTPUT);
  929. #endif
  930. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  931. pinMode(X_MS1_PIN,OUTPUT);
  932. pinMode(X_MS2_PIN,OUTPUT);
  933. pinMode(Y_MS1_PIN,OUTPUT);
  934. pinMode(Y_MS2_PIN,OUTPUT);
  935. pinMode(Z_MS1_PIN,OUTPUT);
  936. pinMode(Z_MS2_PIN,OUTPUT);
  937. pinMode(E0_MS1_PIN,OUTPUT);
  938. pinMode(E0_MS2_PIN,OUTPUT);
  939. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  940. #endif
  941. }
  942. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  943. {
  944. if(ms1 > -1) switch(driver)
  945. {
  946. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  947. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  948. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  949. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  950. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  951. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  952. #endif
  953. }
  954. if(ms2 > -1) switch(driver)
  955. {
  956. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  957. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  958. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  959. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  960. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  961. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  962. #endif
  963. }
  964. }
  965. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  966. {
  967. switch(stepping_mode)
  968. {
  969. case 1: microstep_ms(driver,MICROSTEP1); break;
  970. case 2: microstep_ms(driver,MICROSTEP2); break;
  971. case 4: microstep_ms(driver,MICROSTEP4); break;
  972. case 8: microstep_ms(driver,MICROSTEP8); break;
  973. case 16: microstep_ms(driver,MICROSTEP16); break;
  974. }
  975. }
  976. void microstep_readings()
  977. {
  978. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  979. SERIAL_PROTOCOLPGM("X: ");
  980. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  981. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  982. SERIAL_PROTOCOLPGM("Y: ");
  983. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  984. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  985. SERIAL_PROTOCOLPGM("Z: ");
  986. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  987. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  988. SERIAL_PROTOCOLPGM("E0: ");
  989. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  990. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  991. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  992. SERIAL_PROTOCOLPGM("E1: ");
  993. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  994. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  995. #endif
  996. }