My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

temperature.cpp 19KB

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