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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. int watch_raw[EXTRUDERS] = { -1000 }; // the first value used for all
  72. int watch_oldtemp[3] = {0,0,0};
  73. 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[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. #ifdef HEATER_0_USES_MAX6675
  253. if (e == 0)
  254. {
  255. return celsius * 4;
  256. }
  257. #endif
  258. if(heater_ttbl_map[e] != 0)
  259. {
  260. int raw = 0;
  261. byte i;
  262. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  263. for (i=1; i<heater_ttbllen_map[e]; i++)
  264. {
  265. if (PGM_RD_W((*tt)[i][1]) < celsius)
  266. {
  267. raw = PGM_RD_W((*tt)[i-1][0]) +
  268. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  269. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  270. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  271. break;
  272. }
  273. }
  274. // Overflow: Set to last value in the table
  275. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  276. return (1023 * OVERSAMPLENR) - raw;
  277. }
  278. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  279. }
  280. // Takes bed temperature value as input and returns corresponding raw value.
  281. // For a thermistor, it uses the RepRap thermistor temp table.
  282. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  283. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  284. int temp2analogBed(int celsius) {
  285. #ifdef BED_USES_THERMISTOR
  286. int raw = 0;
  287. byte i;
  288. for (i=1; i<bedtemptable_len; i++)
  289. {
  290. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  291. {
  292. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  293. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  294. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  295. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  296. break;
  297. }
  298. }
  299. // Overflow: Set to last value in the table
  300. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  301. return (1023 * OVERSAMPLENR) - raw;
  302. #elif defined BED_USES_AD595
  303. return lround(celsius * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  304. #else
  305. #warning No heater-type defined for the bed.
  306. return 0;
  307. #endif
  308. }
  309. // Derived from RepRap FiveD extruder::getTemperature()
  310. // For hot end temperature measurement.
  311. float analog2temp(int raw, uint8_t e) {
  312. if(e >= EXTRUDERS)
  313. {
  314. SERIAL_ERROR_START;
  315. SERIAL_ERROR((int)e);
  316. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  317. kill();
  318. }
  319. #ifdef HEATER_0_USES_MAX6675
  320. if (e == 0)
  321. {
  322. return 0.25 * raw;
  323. }
  324. #endif
  325. if(heater_ttbl_map[e] != 0)
  326. {
  327. float celsius = 0;
  328. byte i;
  329. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  330. raw = (1023 * OVERSAMPLENR) - raw;
  331. for (i=1; i<heater_ttbllen_map[e]; i++)
  332. {
  333. if (PGM_RD_W((*tt)[i][0]) > raw)
  334. {
  335. celsius = PGM_RD_W((*tt)[i-1][1]) +
  336. (raw - PGM_RD_W((*tt)[i-1][0])) *
  337. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  338. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  339. break;
  340. }
  341. }
  342. // Overflow: Set to last value in the table
  343. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  344. return celsius;
  345. }
  346. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  347. }
  348. // Derived from RepRap FiveD extruder::getTemperature()
  349. // For bed temperature measurement.
  350. float analog2tempBed(int raw) {
  351. #ifdef BED_USES_THERMISTOR
  352. int celsius = 0;
  353. byte i;
  354. raw = (1023 * OVERSAMPLENR) - raw;
  355. for (i=1; i<bedtemptable_len; i++)
  356. {
  357. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  358. {
  359. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  360. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  361. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  362. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  363. break;
  364. }
  365. }
  366. // Overflow: Set to last value in the table
  367. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  368. return celsius;
  369. #elif defined BED_USES_AD595
  370. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  371. #else
  372. #warning No heater-type defined for the bed.
  373. #endif
  374. return 0;
  375. }
  376. void tp_init()
  377. {
  378. // Finish init of mult extruder arrays
  379. for(int e = 0; e < EXTRUDERS; e++) {
  380. // populate with the first value
  381. #ifdef WATCHPERIOD
  382. watch_raw[e] = watch_raw[0];
  383. #endif
  384. maxttemp[e] = maxttemp[0];
  385. #ifdef PIDTEMP
  386. temp_iState_min[e] = 0.0;
  387. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  388. #endif //PIDTEMP
  389. }
  390. #if (HEATER_0_PIN > -1)
  391. SET_OUTPUT(HEATER_0_PIN);
  392. #endif
  393. #if (HEATER_1_PIN > -1)
  394. SET_OUTPUT(HEATER_1_PIN);
  395. #endif
  396. #if (HEATER_2_PIN > -1)
  397. SET_OUTPUT(HEATER_2_PIN);
  398. #endif
  399. #if (HEATER_BED_PIN > -1)
  400. SET_OUTPUT(HEATER_BED_PIN);
  401. #endif
  402. #if (FAN_PIN > -1)
  403. SET_OUTPUT(FAN_PIN);
  404. #endif
  405. #ifdef HEATER_0_USES_MAX6675
  406. #ifndef SDSUPPORT
  407. SET_OUTPUT(MAX_SCK_PIN);
  408. WRITE(MAX_SCK_PIN,0);
  409. SET_OUTPUT(MAX_MOSI_PIN);
  410. WRITE(MAX_MOSI_PIN,1);
  411. SET_INPUT(MAX_MISO_PIN);
  412. WRITE(MAX_MISO_PIN,1);
  413. #endif
  414. SET_OUTPUT(MAX6675_SS);
  415. WRITE(MAX6675_SS,1);
  416. #endif
  417. // Set analog inputs
  418. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  419. DIDR0 = 0;
  420. #ifdef DIDR2
  421. DIDR2 = 0;
  422. #endif
  423. #if (TEMP_0_PIN > -1)
  424. #if TEMP_0_PIN < 8
  425. DIDR0 |= 1 << TEMP_0_PIN;
  426. #else
  427. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  428. #endif
  429. #endif
  430. #if (TEMP_1_PIN > -1)
  431. #if TEMP_1_PIN < 8
  432. DIDR0 |= 1<<TEMP_1_PIN;
  433. #else
  434. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  435. #endif
  436. #endif
  437. #if (TEMP_2_PIN > -1)
  438. #if TEMP_2_PIN < 8
  439. DIDR0 |= 1 << TEMP_2_PIN;
  440. #else
  441. DIDR2 = 1<<(TEMP_2_PIN - 8);
  442. #endif
  443. #endif
  444. #if (TEMP_BED_PIN > -1)
  445. #if TEMP_BED_PIN < 8
  446. DIDR0 |= 1<<TEMP_BED_PIN;
  447. #else
  448. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  449. #endif
  450. #endif
  451. // Use timer0 for temperature measurement
  452. // Interleave temperature interrupt with millies interrupt
  453. OCR0B = 128;
  454. TIMSK0 |= (1<<OCIE0B);
  455. // Wait for temperature measurement to settle
  456. delay(250);
  457. #ifdef HEATER_0_MINTEMP
  458. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  459. #endif //MINTEMP
  460. #ifdef HEATER_0_MAXTEMP
  461. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  462. #endif //MAXTEMP
  463. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  464. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  465. #endif // MINTEMP 1
  466. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  467. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  468. #endif //MAXTEMP 1
  469. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  470. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  471. #endif //MINTEMP 2
  472. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  473. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  474. #endif //MAXTEMP 2
  475. #ifdef BED_MINTEMP
  476. bed_minttemp = temp2analogBed(BED_MINTEMP);
  477. #endif //BED_MINTEMP
  478. #ifdef BED_MAXTEMP
  479. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  480. #endif //BED_MAXTEMP
  481. }
  482. void setWatch()
  483. {
  484. #ifdef WATCHPERIOD
  485. int t = 0;
  486. for (int e = 0; e < EXTRUDERS; e++)
  487. {
  488. if(isHeatingHotend(e))
  489. watch_oldtemp[0] = degHotend(0);
  490. {
  491. t = max(t,millis());
  492. watch_raw[e] = current_raw[e];
  493. }
  494. }
  495. watchmillis = t;
  496. #endif
  497. }
  498. void disable_heater()
  499. {
  500. for(int i=0;i<EXTRUDERS;i++)
  501. setTargetHotend(0,i);
  502. setTargetBed(0);
  503. #if TEMP_0_PIN > -1
  504. target_raw[0]=0;
  505. soft_pwm[0]=0;
  506. #if HEATER_0_PIN > -1
  507. digitalWrite(HEATER_0_PIN,LOW);
  508. #endif
  509. #endif
  510. #if TEMP_1_PIN > -1
  511. target_raw[1]=0;
  512. soft_pwm[1]=0;
  513. #if HEATER_1_PIN > -1
  514. digitalWrite(HEATER_1_PIN,LOW);
  515. #endif
  516. #endif
  517. #if TEMP_2_PIN > -1
  518. target_raw[2]=0;
  519. soft_pwm[2]=0;
  520. #if HEATER_2_PIN > -1
  521. digitalWrite(HEATER_2_PIN,LOW);
  522. #endif
  523. #endif
  524. #if TEMP_BED_PIN > -1
  525. target_raw_bed=0;
  526. #if HEATER_BED_PIN > -1
  527. digitalWrite(HEATER_BED_PIN,LOW);
  528. #endif
  529. #endif
  530. }
  531. void max_temp_error(uint8_t e) {
  532. digitalWrite(heater_pin_map[e], 0);
  533. SERIAL_ERROR_START;
  534. SERIAL_ERRORLN(e);
  535. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  536. }
  537. void min_temp_error(uint8_t e) {
  538. digitalWrite(heater_pin_map[e], 0);
  539. SERIAL_ERROR_START;
  540. SERIAL_ERRORLN(e);
  541. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  542. }
  543. void bed_max_temp_error(void) {
  544. digitalWrite(HEATER_BED_PIN, 0);
  545. SERIAL_ERROR_START;
  546. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  547. }
  548. #define HEAT_INTERVAL 250
  549. #ifdef HEATER_0_USES_MAX6675
  550. long max6675_previous_millis = -HEAT_INTERVAL;
  551. int max6675_temp = 2000;
  552. int read_max6675()
  553. {
  554. if (millis() - max6675_previous_millis < HEAT_INTERVAL)
  555. return max6675_temp;
  556. max6675_previous_millis = millis();
  557. max6675_temp = 0;
  558. #ifdef PRR
  559. PRR &= ~(1<<PRSPI);
  560. #elif defined PRR0
  561. PRR0 &= ~(1<<PRSPI);
  562. #endif
  563. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  564. // enable TT_MAX6675
  565. WRITE(MAX6675_SS, 0);
  566. // ensure 100ns delay - a bit extra is fine
  567. delay(1);
  568. // read MSB
  569. SPDR = 0;
  570. for (;(SPSR & (1<<SPIF)) == 0;);
  571. max6675_temp = SPDR;
  572. max6675_temp <<= 8;
  573. // read LSB
  574. SPDR = 0;
  575. for (;(SPSR & (1<<SPIF)) == 0;);
  576. max6675_temp |= SPDR;
  577. // disable TT_MAX6675
  578. WRITE(MAX6675_SS, 1);
  579. if (max6675_temp & 4)
  580. {
  581. // thermocouple open
  582. max6675_temp = 2000;
  583. }
  584. else
  585. {
  586. max6675_temp = max6675_temp >> 3;
  587. }
  588. return max6675_temp;
  589. }
  590. #endif
  591. // Timer 0 is shared with millies
  592. ISR(TIMER0_COMPB_vect)
  593. {
  594. //these variables are only accesible from the ISR, but static, so they don't loose their value
  595. static unsigned char temp_count = 0;
  596. static unsigned long raw_temp_0_value = 0;
  597. static unsigned long raw_temp_1_value = 0;
  598. static unsigned long raw_temp_2_value = 0;
  599. static unsigned long raw_temp_bed_value = 0;
  600. static unsigned char temp_state = 0;
  601. static unsigned char pwm_count = 1;
  602. static unsigned char soft_pwm_0;
  603. static unsigned char soft_pwm_1;
  604. static unsigned char soft_pwm_2;
  605. if(pwm_count == 0){
  606. soft_pwm_0 = soft_pwm[0];
  607. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  608. #if EXTRUDERS > 1
  609. soft_pwm_1 = soft_pwm[1];
  610. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  611. #endif
  612. #if EXTRUDERS > 2
  613. soft_pwm_2 = soft_pwm[2];
  614. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  615. #endif
  616. }
  617. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  618. #if EXTRUDERS > 1
  619. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  620. #endif
  621. #if EXTRUDERS > 2
  622. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  623. #endif
  624. pwm_count++;
  625. pwm_count &= 0x7f;
  626. switch(temp_state) {
  627. case 0: // Prepare TEMP_0
  628. #if (TEMP_0_PIN > -1)
  629. #if TEMP_0_PIN > 7
  630. ADCSRB = 1<<MUX5;
  631. #else
  632. ADCSRB = 0;
  633. #endif
  634. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  635. ADCSRA |= 1<<ADSC; // Start conversion
  636. #endif
  637. #ifdef ULTIPANEL
  638. buttons_check();
  639. #endif
  640. temp_state = 1;
  641. break;
  642. case 1: // Measure TEMP_0
  643. #if (TEMP_0_PIN > -1)
  644. raw_temp_0_value += ADC;
  645. #endif
  646. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  647. raw_temp_0_value = read_max6675();
  648. #endif
  649. temp_state = 2;
  650. break;
  651. case 2: // Prepare TEMP_BED
  652. #if (TEMP_BED_PIN > -1)
  653. #if TEMP_BED_PIN > 7
  654. ADCSRB = 1<<MUX5;
  655. #endif
  656. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  657. ADCSRA |= 1<<ADSC; // Start conversion
  658. #endif
  659. #ifdef ULTIPANEL
  660. buttons_check();
  661. #endif
  662. temp_state = 3;
  663. break;
  664. case 3: // Measure TEMP_BED
  665. #if (TEMP_BED_PIN > -1)
  666. raw_temp_bed_value += ADC;
  667. #endif
  668. temp_state = 4;
  669. break;
  670. case 4: // Prepare TEMP_1
  671. #if (TEMP_1_PIN > -1)
  672. #if TEMP_1_PIN > 7
  673. ADCSRB = 1<<MUX5;
  674. #else
  675. ADCSRB = 0;
  676. #endif
  677. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  678. ADCSRA |= 1<<ADSC; // Start conversion
  679. #endif
  680. #ifdef ULTIPANEL
  681. buttons_check();
  682. #endif
  683. temp_state = 5;
  684. break;
  685. case 5: // Measure TEMP_1
  686. #if (TEMP_1_PIN > -1)
  687. raw_temp_1_value += ADC;
  688. #endif
  689. temp_state = 6;
  690. break;
  691. case 6: // Prepare TEMP_2
  692. #if (TEMP_2_PIN > -1)
  693. #if TEMP_2_PIN > 7
  694. ADCSRB = 1<<MUX5;
  695. #else
  696. ADCSRB = 0;
  697. #endif
  698. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  699. ADCSRA |= 1<<ADSC; // Start conversion
  700. #endif
  701. #ifdef ULTIPANEL
  702. buttons_check();
  703. #endif
  704. temp_state = 7;
  705. break;
  706. case 7: // Measure TEMP_2
  707. #if (TEMP_2_PIN > -1)
  708. raw_temp_2_value += ADC;
  709. #endif
  710. temp_state = 0;
  711. temp_count++;
  712. break;
  713. // default:
  714. // SERIAL_ERROR_START;
  715. // SERIAL_ERRORLNPGM("Temp measurement error!");
  716. // break;
  717. }
  718. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  719. {
  720. #ifdef HEATER_0_USES_AD595
  721. current_raw[0] = raw_temp_0_value;
  722. #else
  723. current_raw[0] = 16383 - raw_temp_0_value;
  724. #endif
  725. #if EXTRUDERS > 1
  726. #ifdef HEATER_1_USES_AD595 || defined HEATER_0_USES_MAX6675
  727. current_raw[1] = raw_temp_1_value;
  728. #else
  729. current_raw[1] = 16383 - raw_temp_1_value;
  730. #endif
  731. #endif
  732. #if EXTRUDERS > 2
  733. #ifdef HEATER_2_USES_AD595
  734. current_raw[2] = raw_temp_2_value;
  735. #else
  736. current_raw[2] = 16383 - raw_temp_2_value;
  737. #endif
  738. #endif
  739. #ifdef BED_USES_AD595
  740. current_raw_bed = raw_temp_bed_value;
  741. #else
  742. current_raw_bed = 16383 - raw_temp_bed_value;
  743. #endif
  744. temp_meas_ready = true;
  745. temp_count = 0;
  746. raw_temp_0_value = 0;
  747. raw_temp_1_value = 0;
  748. raw_temp_2_value = 0;
  749. raw_temp_bed_value = 0;
  750. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  751. if(current_raw[e] >= maxttemp[e]) {
  752. target_raw[e] = 0;
  753. max_temp_error(e);
  754. kill();;
  755. }
  756. if(current_raw[e] <= minttemp[e]) {
  757. target_raw[e] = 0;
  758. min_temp_error(e);
  759. kill();
  760. }
  761. }
  762. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  763. if(current_raw_bed >= bed_maxttemp) {
  764. target_raw_bed = 0;
  765. bed_max_temp_error();
  766. kill();
  767. }
  768. #endif
  769. }
  770. }