My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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