My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. temperature.c - temperature control
  3. Part of Marlin
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program 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. This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include <avr/pgmspace.h>
  24. #include "fastio.h"
  25. #include "Configuration.h"
  26. #include "pins.h"
  27. #include "Marlin.h"
  28. #include "ultralcd.h"
  29. #include "temperature.h"
  30. #include "watchdog.h"
  31. //===========================================================================
  32. //=============================public variables============================
  33. //===========================================================================
  34. int target_raw[EXTRUDERS] = { 0 };
  35. int target_raw_bed = 0;
  36. int current_raw[EXTRUDERS] = { 0 };
  37. int current_raw_bed = 0;
  38. #ifdef PIDTEMP
  39. // used external
  40. float pid_setpoint[EXTRUDERS] = { 0.0 };
  41. float Kp=DEFAULT_Kp;
  42. float Ki=DEFAULT_Ki;
  43. float Kd=DEFAULT_Kd;
  44. #ifdef PID_ADD_EXTRUSION_RATE
  45. float Kc=DEFAULT_Kc;
  46. #endif
  47. #endif //PIDTEMP
  48. //===========================================================================
  49. //=============================private variables============================
  50. //===========================================================================
  51. static bool temp_meas_ready = false;
  52. static unsigned long previous_millis_bed_heater;
  53. //static unsigned long previous_millis_heater;
  54. #ifdef PIDTEMP
  55. //static cannot be external:
  56. static float temp_iState[EXTRUDERS] = { 0 };
  57. static float temp_dState[EXTRUDERS] = { 0 };
  58. static float pTerm[EXTRUDERS];
  59. static float iTerm[EXTRUDERS];
  60. static float dTerm[EXTRUDERS];
  61. //int output;
  62. static float pid_error[EXTRUDERS];
  63. static float temp_iState_min[EXTRUDERS];
  64. static float temp_iState_max[EXTRUDERS];
  65. // static float pid_input[EXTRUDERS];
  66. // static float pid_output[EXTRUDERS];
  67. static bool pid_reset[EXTRUDERS];
  68. #endif //PIDTEMP
  69. static unsigned char soft_pwm[EXTRUDERS];
  70. #ifdef WATCHPERIOD
  71. static int watch_raw[EXTRUDERS] = { -1000 }; // the first value used for all
  72. static unsigned long watchmillis = 0;
  73. #endif //WATCHPERIOD
  74. // Init min and max temp with extreme values to prevent false errors during startup
  75. static int minttemp[EXTRUDERS] = { 0 };
  76. static int maxttemp[EXTRUDERS] = { 16383 }; // the first value used for all
  77. static int bed_minttemp = 0;
  78. static int bed_maxttemp = 16383;
  79. static int heater_pin_map[EXTRUDERS] = { HEATER_0_PIN
  80. #if EXTRUDERS > 1
  81. , HEATER_1_PIN
  82. #endif
  83. #if EXTRUDERS > 2
  84. , HEATER_2_PIN
  85. #endif
  86. #if EXTRUDERS > 3
  87. #error Unsupported number of extruders
  88. #endif
  89. };
  90. static void *heater_ttbl_map[EXTRUDERS] = { (void *)heater_0_temptable
  91. #if EXTRUDERS > 1
  92. , (void *)heater_1_temptable
  93. #endif
  94. #if EXTRUDERS > 2
  95. , (void *)heater_2_temptable
  96. #endif
  97. #if EXTRUDERS > 3
  98. #error Unsupported number of extruders
  99. #endif
  100. };
  101. static int heater_ttbllen_map[EXTRUDERS] = { heater_0_temptable_len
  102. #if EXTRUDERS > 1
  103. , heater_1_temptable_len
  104. #endif
  105. #if EXTRUDERS > 2
  106. , heater_2_temptable_len
  107. #endif
  108. #if EXTRUDERS > 3
  109. #error Unsupported number of extruders
  110. #endif
  111. };
  112. //===========================================================================
  113. //============================= functions ============================
  114. //===========================================================================
  115. void updatePID()
  116. {
  117. #ifdef PIDTEMP
  118. for(int e = 0; e < EXTRUDERS; e++) {
  119. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  120. }
  121. #endif
  122. }
  123. int getHeaterPower(int heater) {
  124. return soft_pwm[heater];
  125. }
  126. void manage_heater()
  127. {
  128. #ifdef USE_WATCHDOG
  129. wd_reset();
  130. #endif
  131. float pid_input;
  132. float pid_output;
  133. if(temp_meas_ready != true) //better readability
  134. return;
  135. CRITICAL_SECTION_START;
  136. temp_meas_ready = false;
  137. CRITICAL_SECTION_END;
  138. for(int e = 0; e < EXTRUDERS; e++)
  139. {
  140. #ifdef PIDTEMP
  141. pid_input = analog2temp(current_raw[e], e);
  142. #ifndef PID_OPENLOOP
  143. pid_error[e] = pid_setpoint[e] - pid_input;
  144. if(pid_error[e] > 10) {
  145. pid_output = PID_MAX;
  146. pid_reset[e] = true;
  147. }
  148. else if(pid_error[e] < -10) {
  149. pid_output = 0;
  150. pid_reset[e] = true;
  151. }
  152. else {
  153. if(pid_reset[e] == true) {
  154. temp_iState[e] = 0.0;
  155. pid_reset[e] = false;
  156. }
  157. pTerm[e] = Kp * pid_error[e];
  158. temp_iState[e] += pid_error[e];
  159. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  160. iTerm[e] = Ki * temp_iState[e];
  161. //K1 defined in Configuration.h in the PID settings
  162. #define K2 (1.0-K1)
  163. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  164. temp_dState[e] = pid_input;
  165. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  166. }
  167. #endif //PID_OPENLOOP
  168. #ifdef PID_DEBUG
  169. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  170. #endif //PID_DEBUG
  171. #else /* PID off */
  172. pid_output = 0;
  173. if(current_raw[e] < target_raw[e]) {
  174. pid_output = PID_MAX;
  175. }
  176. #endif
  177. // Check if temperature is within the correct range
  178. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  179. {
  180. //analogWrite(heater_pin_map[e], pid_output);
  181. soft_pwm[e] = (int)pid_output >> 1;
  182. }
  183. else {
  184. //analogWrite(heater_pin_map[e], 0);
  185. soft_pwm[e] = 0;
  186. }
  187. } // End extruder for loop
  188. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  189. return;
  190. previous_millis_bed_heater = millis();
  191. #if TEMP_BED_PIN > -1
  192. // Check if temperature is within the correct range
  193. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  194. if(current_raw_bed >= target_raw_bed)
  195. {
  196. WRITE(HEATER_BED_PIN,LOW);
  197. }
  198. else
  199. {
  200. WRITE(HEATER_BED_PIN,HIGH);
  201. }
  202. }
  203. else {
  204. WRITE(HEATER_BED_PIN,LOW);
  205. }
  206. #endif
  207. }
  208. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  209. // Takes hot end temperature value as input and returns corresponding raw value.
  210. // For a thermistor, it uses the RepRap thermistor temp table.
  211. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  212. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  213. int temp2analog(int celsius, uint8_t e) {
  214. if(e >= EXTRUDERS)
  215. {
  216. SERIAL_ERROR_START;
  217. SERIAL_ERROR((int)e);
  218. SERIAL_ERRORLNPGM(" - Invalid extruder number!");
  219. kill();
  220. }
  221. if(heater_ttbl_map[e] != 0)
  222. {
  223. int raw = 0;
  224. byte i;
  225. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  226. for (i=1; i<heater_ttbllen_map[e]; i++)
  227. {
  228. if (PGM_RD_W((*tt)[i][1]) < celsius)
  229. {
  230. raw = PGM_RD_W((*tt)[i-1][0]) +
  231. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  232. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  233. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  234. break;
  235. }
  236. }
  237. // Overflow: Set to last value in the table
  238. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  239. return (1023 * OVERSAMPLENR) - raw;
  240. }
  241. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  242. }
  243. // Takes bed temperature value as input and returns corresponding raw value.
  244. // For a thermistor, it uses the RepRap thermistor temp table.
  245. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  246. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  247. int temp2analogBed(int celsius) {
  248. #ifdef BED_USES_THERMISTOR
  249. int raw = 0;
  250. byte i;
  251. for (i=1; i<bedtemptable_len; i++)
  252. {
  253. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  254. {
  255. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  256. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  257. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  258. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  259. break;
  260. }
  261. }
  262. // Overflow: Set to last value in the table
  263. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  264. return (1023 * OVERSAMPLENR) - raw;
  265. #elif defined BED_USES_AD595
  266. return lround(celsius * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  267. #else
  268. #warning No heater-type defined for the bed.
  269. return 0;
  270. #endif
  271. }
  272. // Derived from RepRap FiveD extruder::getTemperature()
  273. // For hot end temperature measurement.
  274. float analog2temp(int raw, uint8_t e) {
  275. if(e >= EXTRUDERS)
  276. {
  277. SERIAL_ERROR_START;
  278. SERIAL_ERROR((int)e);
  279. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  280. kill();
  281. }
  282. if(heater_ttbl_map[e] != 0)
  283. {
  284. float celsius = 0;
  285. byte i;
  286. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  287. raw = (1023 * OVERSAMPLENR) - raw;
  288. for (i=1; i<heater_ttbllen_map[e]; i++)
  289. {
  290. if (PGM_RD_W((*tt)[i][0]) > raw)
  291. {
  292. celsius = PGM_RD_W((*tt)[i-1][1]) +
  293. (raw - PGM_RD_W((*tt)[i-1][0])) *
  294. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  295. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  296. break;
  297. }
  298. }
  299. // Overflow: Set to last value in the table
  300. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  301. return celsius;
  302. }
  303. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  304. }
  305. // Derived from RepRap FiveD extruder::getTemperature()
  306. // For bed temperature measurement.
  307. float analog2tempBed(int raw) {
  308. #ifdef BED_USES_THERMISTOR
  309. int celsius = 0;
  310. byte i;
  311. raw = (1023 * OVERSAMPLENR) - raw;
  312. for (i=1; i<bedtemptable_len; i++)
  313. {
  314. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  315. {
  316. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  317. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  318. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  319. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  320. break;
  321. }
  322. }
  323. // Overflow: Set to last value in the table
  324. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  325. return celsius;
  326. #elif defined BED_USES_AD595
  327. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  328. #else
  329. #warning No heater-type defined for the bed.
  330. #endif
  331. return 0;
  332. }
  333. void tp_init()
  334. {
  335. // Finish init of mult extruder arrays
  336. for(int e = 0; e < EXTRUDERS; e++) {
  337. // populate with the first value
  338. #ifdef WATCHPERIOD
  339. watch_raw[e] = watch_raw[0];
  340. #endif
  341. maxttemp[e] = maxttemp[0];
  342. #ifdef PIDTEMP
  343. temp_iState_min[e] = 0.0;
  344. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  345. #endif //PIDTEMP
  346. }
  347. #if (HEATER_0_PIN > -1)
  348. SET_OUTPUT(HEATER_0_PIN);
  349. #endif
  350. #if (HEATER_1_PIN > -1)
  351. SET_OUTPUT(HEATER_1_PIN);
  352. #endif
  353. #if (HEATER_2_PIN > -1)
  354. SET_OUTPUT(HEATER_2_PIN);
  355. #endif
  356. #if (HEATER_BED_PIN > -1)
  357. SET_OUTPUT(HEATER_BED_PIN);
  358. #endif
  359. #if (FAN_PIN > -1)
  360. SET_OUTPUT(FAN_PIN);
  361. #endif
  362. // Set analog inputs
  363. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  364. DIDR0 = 0;
  365. #ifdef DIDR2
  366. DIDR2 = 0;
  367. #endif
  368. #if (TEMP_0_PIN > -1)
  369. #if TEMP_0_PIN < 8
  370. DIDR0 |= 1 << TEMP_0_PIN;
  371. #else
  372. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  373. #endif
  374. #endif
  375. #if (TEMP_1_PIN > -1)
  376. #if TEMP_1_PIN < 8
  377. DIDR0 |= 1<<TEMP_1_PIN;
  378. #else
  379. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  380. #endif
  381. #endif
  382. #if (TEMP_2_PIN > -1)
  383. #if TEMP_2_PIN < 8
  384. DIDR0 |= 1 << TEMP_2_PIN;
  385. #else
  386. DIDR2 = 1<<(TEMP_2_PIN - 8);
  387. #endif
  388. #endif
  389. #if (TEMP_BED_PIN > -1)
  390. #if TEMP_BED_PIN < 8
  391. DIDR0 |= 1<<TEMP_BED_PIN;
  392. #else
  393. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  394. #endif
  395. #endif
  396. // Use timer0 for temperature measurement
  397. // Interleave temperature interrupt with millies interrupt
  398. OCR0B = 128;
  399. TIMSK0 |= (1<<OCIE0B);
  400. // Wait for temperature measurement to settle
  401. delay(250);
  402. #ifdef HEATER_0_MINTEMP
  403. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  404. #endif //MINTEMP
  405. #ifdef HEATER_0_MAXTEMP
  406. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  407. #endif //MAXTEMP
  408. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  409. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  410. #endif // MINTEMP 1
  411. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  412. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  413. #endif //MAXTEMP 1
  414. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  415. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  416. #endif //MINTEMP 2
  417. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  418. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  419. #endif //MAXTEMP 2
  420. #ifdef BED_MINTEMP
  421. bed_minttemp = temp2analogBed(BED_MINTEMP);
  422. #endif //BED_MINTEMP
  423. #ifdef BED_MAXTEMP
  424. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  425. #endif //BED_MAXTEMP
  426. }
  427. void setWatch()
  428. {
  429. #ifdef WATCHPERIOD
  430. int t = 0;
  431. for (int e = 0; e < EXTRUDERS; e++)
  432. {
  433. if(isHeatingHotend(e))
  434. {
  435. t = max(t,millis());
  436. watch_raw[e] = current_raw[e];
  437. }
  438. }
  439. watchmillis = t;
  440. #endif
  441. }
  442. void disable_heater()
  443. {
  444. #if TEMP_0_PIN > -1
  445. target_raw[0]=0;
  446. soft_pwm[0]=0;
  447. #if HEATER_0_PIN > -1
  448. digitalWrite(HEATER_0_PIN,LOW);
  449. #endif
  450. #endif
  451. #if TEMP_1_PIN > -1
  452. target_raw[1]=0;
  453. soft_pwm[1]=0;
  454. #if HEATER_1_PIN > -1
  455. digitalWrite(HEATER_1_PIN,LOW);
  456. #endif
  457. #endif
  458. #if TEMP_2_PIN > -1
  459. target_raw[2]=0;
  460. soft_pwm[2]=0;
  461. #if HEATER_2_PIN > -1
  462. digitalWrite(HEATER_2_PIN,LOW);
  463. #endif
  464. #endif
  465. #if TEMP_BED_PIN > -1
  466. target_raw_bed=0;
  467. #if HEATER_BED_PIN > -1
  468. digitalWrite(HEATER_BED_PIN,LOW);
  469. #endif
  470. #endif
  471. }
  472. void max_temp_error(uint8_t e) {
  473. digitalWrite(heater_pin_map[e], 0);
  474. SERIAL_ERROR_START;
  475. SERIAL_ERRORLN(e);
  476. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  477. }
  478. void min_temp_error(uint8_t e) {
  479. digitalWrite(heater_pin_map[e], 0);
  480. SERIAL_ERROR_START;
  481. SERIAL_ERRORLN(e);
  482. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  483. }
  484. void bed_max_temp_error(void) {
  485. digitalWrite(HEATER_BED_PIN, 0);
  486. SERIAL_ERROR_START;
  487. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  488. }
  489. // Timer 0 is shared with millies
  490. ISR(TIMER0_COMPB_vect)
  491. {
  492. //these variables are only accesible from the ISR, but static, so they don't loose their value
  493. static unsigned char temp_count = 0;
  494. static unsigned long raw_temp_0_value = 0;
  495. static unsigned long raw_temp_1_value = 0;
  496. static unsigned long raw_temp_2_value = 0;
  497. static unsigned long raw_temp_bed_value = 0;
  498. static unsigned char temp_state = 0;
  499. static unsigned char pwm_count = 1;
  500. static unsigned char soft_pwm_0;
  501. static unsigned char soft_pwm_1;
  502. static unsigned char soft_pwm_2;
  503. if(pwm_count == 0){
  504. soft_pwm_0 = soft_pwm[0];
  505. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  506. #if EXTRUDERS > 1
  507. soft_pwm_1 = soft_pwm[1];
  508. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  509. #endif
  510. #if EXTRUDERS > 2
  511. soft_pwm_2 = soft_pwm[2];
  512. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  513. #endif
  514. }
  515. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  516. #if EXTRUDERS > 1
  517. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  518. #endif
  519. #if EXTRUDERS > 2
  520. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  521. #endif
  522. pwm_count++;
  523. pwm_count &= 0x7f;
  524. switch(temp_state) {
  525. case 0: // Prepare TEMP_0
  526. #if (TEMP_0_PIN > -1)
  527. #if TEMP_0_PIN > 7
  528. ADCSRB = 1<<MUX5;
  529. #else
  530. ADCSRB = 0;
  531. #endif
  532. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  533. ADCSRA |= 1<<ADSC; // Start conversion
  534. #endif
  535. #ifdef ULTIPANEL
  536. buttons_check();
  537. #endif
  538. temp_state = 1;
  539. break;
  540. case 1: // Measure TEMP_0
  541. #if (TEMP_0_PIN > -1)
  542. raw_temp_0_value += ADC;
  543. #endif
  544. temp_state = 2;
  545. break;
  546. case 2: // Prepare TEMP_BED
  547. #if (TEMP_BED_PIN > -1)
  548. #if TEMP_BED_PIN > 7
  549. ADCSRB = 1<<MUX5;
  550. #endif
  551. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  552. ADCSRA |= 1<<ADSC; // Start conversion
  553. #endif
  554. #ifdef ULTIPANEL
  555. buttons_check();
  556. #endif
  557. temp_state = 3;
  558. break;
  559. case 3: // Measure TEMP_BED
  560. #if (TEMP_BED_PIN > -1)
  561. raw_temp_bed_value += ADC;
  562. #endif
  563. temp_state = 4;
  564. break;
  565. case 4: // Prepare TEMP_1
  566. #if (TEMP_1_PIN > -1)
  567. #if TEMP_1_PIN > 7
  568. ADCSRB = 1<<MUX5;
  569. #else
  570. ADCSRB = 0;
  571. #endif
  572. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  573. ADCSRA |= 1<<ADSC; // Start conversion
  574. #endif
  575. #ifdef ULTIPANEL
  576. buttons_check();
  577. #endif
  578. temp_state = 5;
  579. break;
  580. case 5: // Measure TEMP_1
  581. #if (TEMP_1_PIN > -1)
  582. raw_temp_1_value += ADC;
  583. #endif
  584. temp_state = 6;
  585. break;
  586. case 6: // Prepare TEMP_2
  587. #if (TEMP_2_PIN > -1)
  588. #if TEMP_2_PIN > 7
  589. ADCSRB = 1<<MUX5;
  590. #else
  591. ADCSRB = 0;
  592. #endif
  593. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  594. ADCSRA |= 1<<ADSC; // Start conversion
  595. #endif
  596. #ifdef ULTIPANEL
  597. buttons_check();
  598. #endif
  599. temp_state = 7;
  600. break;
  601. case 7: // Measure TEMP_2
  602. #if (TEMP_2_PIN > -1)
  603. raw_temp_2_value += ADC;
  604. #endif
  605. temp_state = 0;
  606. temp_count++;
  607. break;
  608. // default:
  609. // SERIAL_ERROR_START;
  610. // SERIAL_ERRORLNPGM("Temp measurement error!");
  611. // break;
  612. }
  613. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  614. {
  615. #ifdef HEATER_0_USES_AD595
  616. current_raw[0] = raw_temp_0_value;
  617. #else
  618. current_raw[0] = 16383 - raw_temp_0_value;
  619. #endif
  620. #if EXTRUDERS > 1
  621. #ifdef HEATER_1_USES_AD595
  622. current_raw[1] = raw_temp_1_value;
  623. #else
  624. current_raw[1] = 16383 - raw_temp_1_value;
  625. #endif
  626. #endif
  627. #if EXTRUDERS > 2
  628. #ifdef HEATER_2_USES_AD595
  629. current_raw[2] = raw_temp_2_value;
  630. #else
  631. current_raw[2] = 16383 - raw_temp_2_value;
  632. #endif
  633. #endif
  634. #ifdef BED_USES_AD595
  635. current_raw_bed = raw_temp_bed_value;
  636. #else
  637. current_raw_bed = 16383 - raw_temp_bed_value;
  638. #endif
  639. temp_meas_ready = true;
  640. temp_count = 0;
  641. raw_temp_0_value = 0;
  642. raw_temp_1_value = 0;
  643. raw_temp_2_value = 0;
  644. raw_temp_bed_value = 0;
  645. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  646. if(current_raw[e] >= maxttemp[e]) {
  647. target_raw[e] = 0;
  648. max_temp_error(e);
  649. kill();;
  650. }
  651. if(current_raw[e] <= minttemp[e]) {
  652. target_raw[e] = 0;
  653. min_temp_error(e);
  654. kill();
  655. }
  656. }
  657. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  658. if(current_raw_bed >= bed_maxttemp) {
  659. target_raw_bed = 0;
  660. bed_max_temp_error();
  661. kill();
  662. }
  663. #endif
  664. }
  665. }