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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 volatile 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 PID_autotune(float temp)
  117. {
  118. float input;
  119. int cycles=0;
  120. bool heating = true;
  121. soft_pwm[0] = 255>>1;
  122. unsigned long temp_millis = millis();
  123. unsigned long t1=temp_millis;
  124. unsigned long t2=temp_millis;
  125. long t_high;
  126. long t_low;
  127. long bias=127;
  128. long d = 127;
  129. float Ku, Tu;
  130. float Kp, Ki, Kd;
  131. float max, min;
  132. SERIAL_ECHOLN("PID Autotune start");
  133. for(;;) {
  134. if(temp_meas_ready == true) { // temp sample ready
  135. CRITICAL_SECTION_START;
  136. temp_meas_ready = false;
  137. CRITICAL_SECTION_END;
  138. input = analog2temp(current_raw[0], 0);
  139. max=max(max,input);
  140. min=min(min,input);
  141. if(heating == true && input > temp) {
  142. if(millis() - t2 > 5000) {
  143. heating=false;
  144. soft_pwm[0] = (bias - d) >> 1;
  145. t1=millis();
  146. t_high=t1 - t2;
  147. max=temp;
  148. }
  149. }
  150. if(heating == false && input < temp) {
  151. if(millis() - t1 > 5000) {
  152. heating=true;
  153. t2=millis();
  154. t_low=t2 - t1;
  155. if(cycles > 0) {
  156. bias += (d*(t_high - t_low))/(t_low + t_high);
  157. bias = constrain(bias, 20 ,235);
  158. if(bias > 127) d = 254 - bias;
  159. else d = bias;
  160. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  161. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  162. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  163. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  164. if(cycles > 2) {
  165. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  166. Tu = ((float)(t_low + t_high)/1000.0);
  167. Kp = 0.6*Ku;
  168. Ki = 2*Kp/Tu;
  169. Kd = Kp*Tu/8;
  170. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  171. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  172. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  173. }
  174. }
  175. soft_pwm[0] = (bias + d) >> 1;
  176. cycles++;
  177. min=temp;
  178. }
  179. }
  180. }
  181. if(input > (temp + 20)) {
  182. SERIAL_PROTOCOLLNPGM("PID Autotune failed !, Temperature to high");
  183. return;
  184. }
  185. if(millis() - temp_millis > 2000) {
  186. temp_millis = millis();
  187. SERIAL_PROTOCOLPGM("ok T:");
  188. SERIAL_PROTOCOL(degHotend(0));
  189. SERIAL_PROTOCOLPGM(" @:");
  190. SERIAL_PROTOCOLLN(getHeaterPower(0));
  191. }
  192. LCD_STATUS;
  193. }
  194. }
  195. void updatePID()
  196. {
  197. #ifdef PIDTEMP
  198. for(int e = 0; e < EXTRUDERS; e++) {
  199. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  200. }
  201. #endif
  202. }
  203. int getHeaterPower(int heater) {
  204. return soft_pwm[heater];
  205. }
  206. void manage_heater()
  207. {
  208. #ifdef USE_WATCHDOG
  209. wd_reset();
  210. #endif
  211. float pid_input;
  212. float pid_output;
  213. if(temp_meas_ready != true) //better readability
  214. return;
  215. CRITICAL_SECTION_START;
  216. temp_meas_ready = false;
  217. CRITICAL_SECTION_END;
  218. for(int e = 0; e < EXTRUDERS; e++)
  219. {
  220. #ifdef PIDTEMP
  221. pid_input = analog2temp(current_raw[e], e);
  222. #ifndef PID_OPENLOOP
  223. pid_error[e] = pid_setpoint[e] - pid_input;
  224. if(pid_error[e] > 10) {
  225. pid_output = PID_MAX;
  226. pid_reset[e] = true;
  227. }
  228. else if(pid_error[e] < -10) {
  229. pid_output = 0;
  230. pid_reset[e] = true;
  231. }
  232. else {
  233. if(pid_reset[e] == true) {
  234. temp_iState[e] = 0.0;
  235. pid_reset[e] = false;
  236. }
  237. pTerm[e] = Kp * pid_error[e];
  238. temp_iState[e] += pid_error[e];
  239. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  240. iTerm[e] = Ki * temp_iState[e];
  241. //K1 defined in Configuration.h in the PID settings
  242. #define K2 (1.0-K1)
  243. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  244. temp_dState[e] = pid_input;
  245. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  246. }
  247. #endif //PID_OPENLOOP
  248. #ifdef PID_DEBUG
  249. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  250. #endif //PID_DEBUG
  251. #else /* PID off */
  252. pid_output = 0;
  253. if(current_raw[e] < target_raw[e]) {
  254. pid_output = PID_MAX;
  255. }
  256. #endif
  257. // Check if temperature is within the correct range
  258. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  259. {
  260. //analogWrite(heater_pin_map[e], pid_output);
  261. soft_pwm[e] = (int)pid_output >> 1;
  262. }
  263. else {
  264. //analogWrite(heater_pin_map[e], 0);
  265. soft_pwm[e] = 0;
  266. }
  267. } // End extruder for loop
  268. #ifdef WATCHPERIOD
  269. if(watchmillis && millis() - watchmillis > WATCHPERIOD){
  270. if(watch_oldtemp[0] >= degHotend(active_extruder)){
  271. setTargetHotend(0,active_extruder);
  272. LCD_MESSAGEPGM("Heating failed");
  273. SERIAL_ECHO_START;
  274. SERIAL_ECHOLN("Heating failed");
  275. }else{
  276. watchmillis = 0;
  277. }
  278. }
  279. #endif
  280. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  281. return;
  282. previous_millis_bed_heater = millis();
  283. #if TEMP_BED_PIN > -1
  284. #ifndef BED_LIMIT_SWITCHING
  285. // Check if temperature is within the correct range
  286. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  287. if(current_raw_bed >= target_raw_bed)
  288. {
  289. WRITE(HEATER_BED_PIN,LOW);
  290. }
  291. else
  292. {
  293. WRITE(HEATER_BED_PIN,HIGH);
  294. }
  295. }
  296. else {
  297. WRITE(HEATER_BED_PIN,LOW);
  298. }
  299. #else //#ifdef BED_LIMIT_SWITCHING
  300. // Check if temperature is within the correct band
  301. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  302. if(current_raw_bed > target_bed_high_temp)
  303. {
  304. WRITE(HEATER_BED_PIN,LOW);
  305. }
  306. else
  307. if(current_raw_bed <= target_bed_low_temp)
  308. {
  309. WRITE(HEATER_BED_PIN,HIGH);
  310. }
  311. }
  312. else {
  313. WRITE(HEATER_BED_PIN,LOW);
  314. }
  315. #endif
  316. #endif
  317. }
  318. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  319. // Takes hot end temperature value as input and returns corresponding raw value.
  320. // For a thermistor, it uses the RepRap thermistor temp table.
  321. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  322. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  323. int temp2analog(int celsius, uint8_t e) {
  324. if(e >= EXTRUDERS)
  325. {
  326. SERIAL_ERROR_START;
  327. SERIAL_ERROR((int)e);
  328. SERIAL_ERRORLNPGM(" - Invalid extruder number!");
  329. kill();
  330. }
  331. #ifdef HEATER_0_USES_MAX6675
  332. if (e == 0)
  333. {
  334. return celsius * 4;
  335. }
  336. #endif
  337. if(heater_ttbl_map[e] != 0)
  338. {
  339. int raw = 0;
  340. byte i;
  341. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  342. for (i=1; i<heater_ttbllen_map[e]; i++)
  343. {
  344. if (PGM_RD_W((*tt)[i][1]) < celsius)
  345. {
  346. raw = PGM_RD_W((*tt)[i-1][0]) +
  347. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  348. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  349. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  350. break;
  351. }
  352. }
  353. // Overflow: Set to last value in the table
  354. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  355. return (1023 * OVERSAMPLENR) - raw;
  356. }
  357. return ((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  358. }
  359. // Takes bed temperature value as input and returns corresponding raw value.
  360. // For a thermistor, it uses the RepRap thermistor temp table.
  361. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  362. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  363. int temp2analogBed(int celsius) {
  364. #ifdef BED_USES_THERMISTOR
  365. int raw = 0;
  366. byte i;
  367. for (i=1; i<bedtemptable_len; i++)
  368. {
  369. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  370. {
  371. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  372. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  373. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  374. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  375. break;
  376. }
  377. }
  378. // Overflow: Set to last value in the table
  379. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  380. return (1023 * OVERSAMPLENR) - raw;
  381. #elif defined BED_USES_AD595
  382. return lround(((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  383. #else
  384. #warning No heater-type defined for the bed.
  385. return 0;
  386. #endif
  387. }
  388. // Derived from RepRap FiveD extruder::getTemperature()
  389. // For hot end temperature measurement.
  390. float analog2temp(int raw, uint8_t e) {
  391. if(e >= EXTRUDERS)
  392. {
  393. SERIAL_ERROR_START;
  394. SERIAL_ERROR((int)e);
  395. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  396. kill();
  397. }
  398. #ifdef HEATER_0_USES_MAX6675
  399. if (e == 0)
  400. {
  401. return 0.25 * raw;
  402. }
  403. #endif
  404. if(heater_ttbl_map[e] != 0)
  405. {
  406. float celsius = 0;
  407. byte i;
  408. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  409. raw = (1023 * OVERSAMPLENR) - raw;
  410. for (i=1; i<heater_ttbllen_map[e]; i++)
  411. {
  412. if (PGM_RD_W((*tt)[i][0]) > raw)
  413. {
  414. celsius = PGM_RD_W((*tt)[i-1][1]) +
  415. (raw - PGM_RD_W((*tt)[i-1][0])) *
  416. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  417. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  418. break;
  419. }
  420. }
  421. // Overflow: Set to last value in the table
  422. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  423. return celsius;
  424. }
  425. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  426. }
  427. // Derived from RepRap FiveD extruder::getTemperature()
  428. // For bed temperature measurement.
  429. float analog2tempBed(int raw) {
  430. #ifdef BED_USES_THERMISTOR
  431. float celsius = 0;
  432. byte i;
  433. raw = (1023 * OVERSAMPLENR) - raw;
  434. for (i=1; i<bedtemptable_len; i++)
  435. {
  436. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  437. {
  438. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  439. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  440. (float)(PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  441. (float)(PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  442. break;
  443. }
  444. }
  445. // Overflow: Set to last value in the table
  446. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  447. return celsius;
  448. #elif defined BED_USES_AD595
  449. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  450. #else
  451. #warning No heater-type defined for the bed.
  452. return 0;
  453. #endif
  454. }
  455. void tp_init()
  456. {
  457. // Finish init of mult extruder arrays
  458. for(int e = 0; e < EXTRUDERS; e++) {
  459. // populate with the first value
  460. #ifdef WATCHPERIOD
  461. watch_raw[e] = watch_raw[0];
  462. #endif
  463. maxttemp[e] = maxttemp[0];
  464. #ifdef PIDTEMP
  465. temp_iState_min[e] = 0.0;
  466. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  467. #endif //PIDTEMP
  468. }
  469. #if (HEATER_0_PIN > -1)
  470. SET_OUTPUT(HEATER_0_PIN);
  471. #endif
  472. #if (HEATER_1_PIN > -1)
  473. SET_OUTPUT(HEATER_1_PIN);
  474. #endif
  475. #if (HEATER_2_PIN > -1)
  476. SET_OUTPUT(HEATER_2_PIN);
  477. #endif
  478. #if (HEATER_BED_PIN > -1)
  479. SET_OUTPUT(HEATER_BED_PIN);
  480. #endif
  481. #if (FAN_PIN > -1)
  482. SET_OUTPUT(FAN_PIN);
  483. #endif
  484. #ifdef HEATER_0_USES_MAX6675
  485. #ifndef SDSUPPORT
  486. SET_OUTPUT(MAX_SCK_PIN);
  487. WRITE(MAX_SCK_PIN,0);
  488. SET_OUTPUT(MAX_MOSI_PIN);
  489. WRITE(MAX_MOSI_PIN,1);
  490. SET_INPUT(MAX_MISO_PIN);
  491. WRITE(MAX_MISO_PIN,1);
  492. #endif
  493. SET_OUTPUT(MAX6675_SS);
  494. WRITE(MAX6675_SS,1);
  495. #endif
  496. // Set analog inputs
  497. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  498. DIDR0 = 0;
  499. #ifdef DIDR2
  500. DIDR2 = 0;
  501. #endif
  502. #if (TEMP_0_PIN > -1)
  503. #if TEMP_0_PIN < 8
  504. DIDR0 |= 1 << TEMP_0_PIN;
  505. #else
  506. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  507. #endif
  508. #endif
  509. #if (TEMP_1_PIN > -1)
  510. #if TEMP_1_PIN < 8
  511. DIDR0 |= 1<<TEMP_1_PIN;
  512. #else
  513. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  514. #endif
  515. #endif
  516. #if (TEMP_2_PIN > -1)
  517. #if TEMP_2_PIN < 8
  518. DIDR0 |= 1 << TEMP_2_PIN;
  519. #else
  520. DIDR2 = 1<<(TEMP_2_PIN - 8);
  521. #endif
  522. #endif
  523. #if (TEMP_BED_PIN > -1)
  524. #if TEMP_BED_PIN < 8
  525. DIDR0 |= 1<<TEMP_BED_PIN;
  526. #else
  527. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  528. #endif
  529. #endif
  530. // Use timer0 for temperature measurement
  531. // Interleave temperature interrupt with millies interrupt
  532. OCR0B = 128;
  533. TIMSK0 |= (1<<OCIE0B);
  534. // Wait for temperature measurement to settle
  535. delay(250);
  536. #ifdef HEATER_0_MINTEMP
  537. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  538. #endif //MINTEMP
  539. #ifdef HEATER_0_MAXTEMP
  540. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  541. #endif //MAXTEMP
  542. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  543. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  544. #endif // MINTEMP 1
  545. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  546. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  547. #endif //MAXTEMP 1
  548. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  549. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  550. #endif //MINTEMP 2
  551. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  552. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  553. #endif //MAXTEMP 2
  554. #ifdef BED_MINTEMP
  555. bed_minttemp = temp2analogBed(BED_MINTEMP);
  556. #endif //BED_MINTEMP
  557. #ifdef BED_MAXTEMP
  558. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  559. #endif //BED_MAXTEMP
  560. }
  561. void setWatch()
  562. {
  563. #ifdef WATCHPERIOD
  564. int t = 0;
  565. for (int e = 0; e < EXTRUDERS; e++)
  566. {
  567. if(isHeatingHotend(e))
  568. watch_oldtemp[0] = degHotend(0);
  569. {
  570. t = max(t,millis());
  571. watch_raw[e] = current_raw[e];
  572. }
  573. }
  574. watchmillis = t;
  575. #endif
  576. }
  577. void disable_heater()
  578. {
  579. for(int i=0;i<EXTRUDERS;i++)
  580. setTargetHotend(0,i);
  581. setTargetBed(0);
  582. #if TEMP_0_PIN > -1
  583. target_raw[0]=0;
  584. soft_pwm[0]=0;
  585. #if HEATER_0_PIN > -1
  586. digitalWrite(HEATER_0_PIN,LOW);
  587. #endif
  588. #endif
  589. #if TEMP_1_PIN > -1
  590. target_raw[1]=0;
  591. soft_pwm[1]=0;
  592. #if HEATER_1_PIN > -1
  593. digitalWrite(HEATER_1_PIN,LOW);
  594. #endif
  595. #endif
  596. #if TEMP_2_PIN > -1
  597. target_raw[2]=0;
  598. soft_pwm[2]=0;
  599. #if HEATER_2_PIN > -1
  600. digitalWrite(HEATER_2_PIN,LOW);
  601. #endif
  602. #endif
  603. #if TEMP_BED_PIN > -1
  604. target_raw_bed=0;
  605. #if HEATER_BED_PIN > -1
  606. digitalWrite(HEATER_BED_PIN,LOW);
  607. #endif
  608. #endif
  609. }
  610. void max_temp_error(uint8_t e) {
  611. digitalWrite(heater_pin_map[e], 0);
  612. SERIAL_ERROR_START;
  613. SERIAL_ERRORLN(e);
  614. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  615. }
  616. void min_temp_error(uint8_t e) {
  617. digitalWrite(heater_pin_map[e], 0);
  618. SERIAL_ERROR_START;
  619. SERIAL_ERRORLN(e);
  620. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  621. }
  622. void bed_max_temp_error(void) {
  623. digitalWrite(HEATER_BED_PIN, 0);
  624. SERIAL_ERROR_START;
  625. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  626. }
  627. #define HEAT_INTERVAL 250
  628. #ifdef HEATER_0_USES_MAX6675
  629. long max6675_previous_millis = -HEAT_INTERVAL;
  630. int max6675_temp = 2000;
  631. int read_max6675()
  632. {
  633. if (millis() - max6675_previous_millis < HEAT_INTERVAL)
  634. return max6675_temp;
  635. max6675_previous_millis = millis();
  636. max6675_temp = 0;
  637. #ifdef PRR
  638. PRR &= ~(1<<PRSPI);
  639. #elif defined PRR0
  640. PRR0 &= ~(1<<PRSPI);
  641. #endif
  642. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  643. // enable TT_MAX6675
  644. WRITE(MAX6675_SS, 0);
  645. // ensure 100ns delay - a bit extra is fine
  646. delay(1);
  647. // read MSB
  648. SPDR = 0;
  649. for (;(SPSR & (1<<SPIF)) == 0;);
  650. max6675_temp = SPDR;
  651. max6675_temp <<= 8;
  652. // read LSB
  653. SPDR = 0;
  654. for (;(SPSR & (1<<SPIF)) == 0;);
  655. max6675_temp |= SPDR;
  656. // disable TT_MAX6675
  657. WRITE(MAX6675_SS, 1);
  658. if (max6675_temp & 4)
  659. {
  660. // thermocouple open
  661. max6675_temp = 2000;
  662. }
  663. else
  664. {
  665. max6675_temp = max6675_temp >> 3;
  666. }
  667. return max6675_temp;
  668. }
  669. #endif
  670. // Timer 0 is shared with millies
  671. ISR(TIMER0_COMPB_vect)
  672. {
  673. //these variables are only accesible from the ISR, but static, so they don't loose their value
  674. static unsigned char temp_count = 0;
  675. static unsigned long raw_temp_0_value = 0;
  676. static unsigned long raw_temp_1_value = 0;
  677. static unsigned long raw_temp_2_value = 0;
  678. static unsigned long raw_temp_bed_value = 0;
  679. static unsigned char temp_state = 0;
  680. static unsigned char pwm_count = 1;
  681. static unsigned char soft_pwm_0;
  682. static unsigned char soft_pwm_1;
  683. static unsigned char soft_pwm_2;
  684. if(pwm_count == 0){
  685. soft_pwm_0 = soft_pwm[0];
  686. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  687. #if EXTRUDERS > 1
  688. soft_pwm_1 = soft_pwm[1];
  689. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  690. #endif
  691. #if EXTRUDERS > 2
  692. soft_pwm_2 = soft_pwm[2];
  693. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  694. #endif
  695. }
  696. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  697. #if EXTRUDERS > 1
  698. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  699. #endif
  700. #if EXTRUDERS > 2
  701. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  702. #endif
  703. pwm_count++;
  704. pwm_count &= 0x7f;
  705. switch(temp_state) {
  706. case 0: // Prepare TEMP_0
  707. #if (TEMP_0_PIN > -1)
  708. #if TEMP_0_PIN > 7
  709. ADCSRB = 1<<MUX5;
  710. #else
  711. ADCSRB = 0;
  712. #endif
  713. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  714. ADCSRA |= 1<<ADSC; // Start conversion
  715. #endif
  716. #ifdef ULTIPANEL
  717. buttons_check();
  718. #endif
  719. temp_state = 1;
  720. break;
  721. case 1: // Measure TEMP_0
  722. #if (TEMP_0_PIN > -1)
  723. raw_temp_0_value += ADC;
  724. #endif
  725. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  726. raw_temp_0_value = read_max6675();
  727. #endif
  728. temp_state = 2;
  729. break;
  730. case 2: // Prepare TEMP_BED
  731. #if (TEMP_BED_PIN > -1)
  732. #if TEMP_BED_PIN > 7
  733. ADCSRB = 1<<MUX5;
  734. #endif
  735. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  736. ADCSRA |= 1<<ADSC; // Start conversion
  737. #endif
  738. #ifdef ULTIPANEL
  739. buttons_check();
  740. #endif
  741. temp_state = 3;
  742. break;
  743. case 3: // Measure TEMP_BED
  744. #if (TEMP_BED_PIN > -1)
  745. raw_temp_bed_value += ADC;
  746. #endif
  747. temp_state = 4;
  748. break;
  749. case 4: // Prepare TEMP_1
  750. #if (TEMP_1_PIN > -1)
  751. #if TEMP_1_PIN > 7
  752. ADCSRB = 1<<MUX5;
  753. #else
  754. ADCSRB = 0;
  755. #endif
  756. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  757. ADCSRA |= 1<<ADSC; // Start conversion
  758. #endif
  759. #ifdef ULTIPANEL
  760. buttons_check();
  761. #endif
  762. temp_state = 5;
  763. break;
  764. case 5: // Measure TEMP_1
  765. #if (TEMP_1_PIN > -1)
  766. raw_temp_1_value += ADC;
  767. #endif
  768. temp_state = 6;
  769. break;
  770. case 6: // Prepare TEMP_2
  771. #if (TEMP_2_PIN > -1)
  772. #if TEMP_2_PIN > 7
  773. ADCSRB = 1<<MUX5;
  774. #else
  775. ADCSRB = 0;
  776. #endif
  777. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  778. ADCSRA |= 1<<ADSC; // Start conversion
  779. #endif
  780. #ifdef ULTIPANEL
  781. buttons_check();
  782. #endif
  783. temp_state = 7;
  784. break;
  785. case 7: // Measure TEMP_2
  786. #if (TEMP_2_PIN > -1)
  787. raw_temp_2_value += ADC;
  788. #endif
  789. temp_state = 0;
  790. temp_count++;
  791. break;
  792. // default:
  793. // SERIAL_ERROR_START;
  794. // SERIAL_ERRORLNPGM("Temp measurement error!");
  795. // break;
  796. }
  797. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  798. {
  799. #ifdef HEATER_0_USES_AD595
  800. current_raw[0] = raw_temp_0_value;
  801. #else
  802. current_raw[0] = 16383 - raw_temp_0_value;
  803. #endif
  804. #if EXTRUDERS > 1
  805. #ifdef HEATER_1_USES_AD595 || defined HEATER_0_USES_MAX6675
  806. current_raw[1] = raw_temp_1_value;
  807. #else
  808. current_raw[1] = 16383 - raw_temp_1_value;
  809. #endif
  810. #endif
  811. #if EXTRUDERS > 2
  812. #ifdef HEATER_2_USES_AD595
  813. current_raw[2] = raw_temp_2_value;
  814. #else
  815. current_raw[2] = 16383 - raw_temp_2_value;
  816. #endif
  817. #endif
  818. #ifdef BED_USES_AD595
  819. current_raw_bed = raw_temp_bed_value;
  820. #else
  821. current_raw_bed = 16383 - raw_temp_bed_value;
  822. #endif
  823. temp_meas_ready = true;
  824. temp_count = 0;
  825. raw_temp_0_value = 0;
  826. raw_temp_1_value = 0;
  827. raw_temp_2_value = 0;
  828. raw_temp_bed_value = 0;
  829. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  830. if(current_raw[e] >= maxttemp[e]) {
  831. target_raw[e] = 0;
  832. max_temp_error(e);
  833. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  834. {
  835. kill();;
  836. }
  837. #endif
  838. }
  839. if(current_raw[e] <= minttemp[e]) {
  840. target_raw[e] = 0;
  841. min_temp_error(e);
  842. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  843. {
  844. kill();
  845. }
  846. #endif
  847. }
  848. }
  849. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  850. if(current_raw_bed >= bed_maxttemp) {
  851. target_raw_bed = 0;
  852. bed_max_temp_error();
  853. kill();
  854. }
  855. #endif
  856. }
  857. }