My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

temperature.cpp 21KB

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