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

temperature.cpp 61KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * temperature.cpp - temperature control
  24. */
  25. #include "Marlin.h"
  26. #include "ultralcd.h"
  27. #include "temperature.h"
  28. #include "thermistortables.h"
  29. #include "language.h"
  30. #if ENABLED(BABYSTEPPING)
  31. #include "stepper.h"
  32. #endif
  33. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  34. #include "endstops.h"
  35. #endif
  36. #if ENABLED(USE_WATCHDOG)
  37. #include "watchdog.h"
  38. #endif
  39. #ifdef K1 // Defined in Configuration.h in the PID settings
  40. #define K2 (1.0-K1)
  41. #endif
  42. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  43. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  44. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  45. #else
  46. static void* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE);
  47. static uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN);
  48. #endif
  49. Temperature thermalManager;
  50. // public:
  51. float Temperature::current_temperature[HOTENDS] = { 0.0 },
  52. Temperature::current_temperature_bed = 0.0;
  53. int Temperature::current_temperature_raw[HOTENDS] = { 0 },
  54. Temperature::target_temperature[HOTENDS] = { 0 },
  55. Temperature::current_temperature_bed_raw = 0,
  56. Temperature::target_temperature_bed = 0;
  57. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  58. float Temperature::redundant_temperature = 0.0;
  59. #endif
  60. uint8_t Temperature::soft_pwm_bed;
  61. #if ENABLED(FAN_SOFT_PWM)
  62. uint8_t Temperature::fanSpeedSoftPwm[FAN_COUNT];
  63. #endif
  64. #if ENABLED(PIDTEMP)
  65. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  66. float Temperature::Kp[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kp),
  67. Temperature::Ki[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Ki) * (PID_dT)),
  68. Temperature::Kd[HOTENDS] = ARRAY_BY_HOTENDS1((DEFAULT_Kd) / (PID_dT));
  69. #if ENABLED(PID_EXTRUSION_SCALING)
  70. float Temperature::Kc[HOTENDS] = ARRAY_BY_HOTENDS1(DEFAULT_Kc);
  71. #endif
  72. #else
  73. float Temperature::Kp = DEFAULT_Kp,
  74. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  75. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  76. #if ENABLED(PID_EXTRUSION_SCALING)
  77. float Temperature::Kc = DEFAULT_Kc;
  78. #endif
  79. #endif
  80. #endif
  81. #if ENABLED(PIDTEMPBED)
  82. float Temperature::bedKp = DEFAULT_bedKp,
  83. Temperature::bedKi = ((DEFAULT_bedKi) * PID_dT),
  84. Temperature::bedKd = ((DEFAULT_bedKd) / PID_dT);
  85. #endif
  86. #if ENABLED(BABYSTEPPING)
  87. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  88. #endif
  89. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  90. int Temperature::watch_target_temp[HOTENDS] = { 0 };
  91. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  92. #endif
  93. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  94. int Temperature::watch_target_bed_temp = 0;
  95. millis_t Temperature::watch_bed_next_ms = 0;
  96. #endif
  97. #if ENABLED(PREVENT_COLD_EXTRUSION)
  98. bool Temperature::allow_cold_extrude = false;
  99. float Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  100. #endif
  101. // private:
  102. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  103. int Temperature::redundant_temperature_raw = 0;
  104. float Temperature::redundant_temperature = 0.0;
  105. #endif
  106. volatile bool Temperature::temp_meas_ready = false;
  107. #if ENABLED(PIDTEMP)
  108. float Temperature::temp_iState[HOTENDS] = { 0 },
  109. Temperature::temp_dState[HOTENDS] = { 0 },
  110. Temperature::pTerm[HOTENDS],
  111. Temperature::iTerm[HOTENDS],
  112. Temperature::dTerm[HOTENDS];
  113. #if ENABLED(PID_EXTRUSION_SCALING)
  114. float Temperature::cTerm[HOTENDS];
  115. long Temperature::last_e_position;
  116. long Temperature::lpq[LPQ_MAX_LEN];
  117. int Temperature::lpq_ptr = 0;
  118. #endif
  119. float Temperature::pid_error[HOTENDS];
  120. bool Temperature::pid_reset[HOTENDS];
  121. #endif
  122. #if ENABLED(PIDTEMPBED)
  123. float Temperature::temp_iState_bed = { 0 },
  124. Temperature::temp_dState_bed = { 0 },
  125. Temperature::pTerm_bed,
  126. Temperature::iTerm_bed,
  127. Temperature::dTerm_bed,
  128. Temperature::pid_error_bed;
  129. #else
  130. millis_t Temperature::next_bed_check_ms;
  131. #endif
  132. unsigned long Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 };
  133. unsigned long Temperature::raw_temp_bed_value = 0;
  134. // Init min and max temp with extreme values to prevent false errors during startup
  135. int Temperature::minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP),
  136. Temperature::maxttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP),
  137. Temperature::minttemp[HOTENDS] = { 0 },
  138. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  139. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  140. int Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  141. #endif
  142. #ifdef MILLISECONDS_PREHEAT_TIME
  143. unsigned long Temperature::preheat_end_time[HOTENDS] = { 0 };
  144. #endif
  145. #ifdef BED_MINTEMP
  146. int Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  147. #endif
  148. #ifdef BED_MAXTEMP
  149. int Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  150. #endif
  151. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  152. int Temperature::meas_shift_index; // Index of a delayed sample in buffer
  153. #endif
  154. #if HAS_AUTO_FAN
  155. millis_t Temperature::next_auto_fan_check_ms = 0;
  156. #endif
  157. uint8_t Temperature::soft_pwm[HOTENDS];
  158. #if ENABLED(FAN_SOFT_PWM)
  159. uint8_t Temperature::soft_pwm_fan[FAN_COUNT];
  160. #endif
  161. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  162. int Temperature::current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  163. #endif
  164. #if HAS_PID_HEATING
  165. void Temperature::PID_autotune(float temp, int hotend, int ncycles, bool set_result/*=false*/) {
  166. float input = 0.0;
  167. int cycles = 0;
  168. bool heating = true;
  169. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  170. long t_high = 0, t_low = 0;
  171. long bias, d;
  172. float Ku, Tu;
  173. float workKp = 0, workKi = 0, workKd = 0;
  174. float max = 0, min = 10000;
  175. #if HAS_AUTO_FAN
  176. next_auto_fan_check_ms = temp_ms + 2500UL;
  177. #endif
  178. if (hotend >=
  179. #if ENABLED(PIDTEMP)
  180. HOTENDS
  181. #else
  182. 0
  183. #endif
  184. || hotend <
  185. #if ENABLED(PIDTEMPBED)
  186. -1
  187. #else
  188. 0
  189. #endif
  190. ) {
  191. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  192. return;
  193. }
  194. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  195. disable_all_heaters(); // switch off all heaters.
  196. #if HAS_PID_FOR_BOTH
  197. if (hotend < 0)
  198. soft_pwm_bed = bias = d = (MAX_BED_POWER) >> 1;
  199. else
  200. soft_pwm[hotend] = bias = d = (PID_MAX) >> 1;
  201. #elif ENABLED(PIDTEMP)
  202. soft_pwm[hotend] = bias = d = (PID_MAX) >> 1;
  203. #else
  204. soft_pwm_bed = bias = d = (MAX_BED_POWER) >> 1;
  205. #endif
  206. wait_for_heatup = true;
  207. // PID Tuning loop
  208. while (wait_for_heatup) {
  209. millis_t ms = millis();
  210. if (temp_meas_ready) { // temp sample ready
  211. updateTemperaturesFromRawValues();
  212. input =
  213. #if HAS_PID_FOR_BOTH
  214. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  215. #elif ENABLED(PIDTEMP)
  216. current_temperature[hotend]
  217. #else
  218. current_temperature_bed
  219. #endif
  220. ;
  221. NOLESS(max, input);
  222. NOMORE(min, input);
  223. #if HAS_AUTO_FAN
  224. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  225. checkExtruderAutoFans();
  226. next_auto_fan_check_ms = ms + 2500UL;
  227. }
  228. #endif
  229. if (heating && input > temp) {
  230. if (ELAPSED(ms, t2 + 5000UL)) {
  231. heating = false;
  232. #if HAS_PID_FOR_BOTH
  233. if (hotend < 0)
  234. soft_pwm_bed = (bias - d) >> 1;
  235. else
  236. soft_pwm[hotend] = (bias - d) >> 1;
  237. #elif ENABLED(PIDTEMP)
  238. soft_pwm[hotend] = (bias - d) >> 1;
  239. #elif ENABLED(PIDTEMPBED)
  240. soft_pwm_bed = (bias - d) >> 1;
  241. #endif
  242. t1 = ms;
  243. t_high = t1 - t2;
  244. max = temp;
  245. }
  246. }
  247. if (!heating && input < temp) {
  248. if (ELAPSED(ms, t1 + 5000UL)) {
  249. heating = true;
  250. t2 = ms;
  251. t_low = t2 - t1;
  252. if (cycles > 0) {
  253. long max_pow =
  254. #if HAS_PID_FOR_BOTH
  255. hotend < 0 ? MAX_BED_POWER : PID_MAX
  256. #elif ENABLED(PIDTEMP)
  257. PID_MAX
  258. #else
  259. MAX_BED_POWER
  260. #endif
  261. ;
  262. bias += (d * (t_high - t_low)) / (t_low + t_high);
  263. bias = constrain(bias, 20, max_pow - 20);
  264. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  265. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  266. SERIAL_PROTOCOLPAIR(MSG_D, d);
  267. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  268. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  269. if (cycles > 2) {
  270. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5);
  271. Tu = ((float)(t_low + t_high) * 0.001);
  272. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  273. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  274. workKp = 0.6 * Ku;
  275. workKi = 2 * workKp / Tu;
  276. workKd = workKp * Tu * 0.125;
  277. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  278. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  279. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  280. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  281. /**
  282. workKp = 0.33*Ku;
  283. workKi = workKp/Tu;
  284. workKd = workKp*Tu/3;
  285. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  286. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  287. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  288. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  289. workKp = 0.2*Ku;
  290. workKi = 2*workKp/Tu;
  291. workKd = workKp*Tu/3;
  292. SERIAL_PROTOCOLLNPGM(" No overshoot");
  293. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  294. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  295. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  296. */
  297. }
  298. }
  299. #if HAS_PID_FOR_BOTH
  300. if (hotend < 0)
  301. soft_pwm_bed = (bias + d) >> 1;
  302. else
  303. soft_pwm[hotend] = (bias + d) >> 1;
  304. #elif ENABLED(PIDTEMP)
  305. soft_pwm[hotend] = (bias + d) >> 1;
  306. #else
  307. soft_pwm_bed = (bias + d) >> 1;
  308. #endif
  309. cycles++;
  310. min = temp;
  311. }
  312. }
  313. }
  314. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  315. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  316. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  317. return;
  318. }
  319. // Every 2 seconds...
  320. if (ELAPSED(ms, temp_ms + 2000UL)) {
  321. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  322. print_heaterstates();
  323. SERIAL_EOL;
  324. #endif
  325. temp_ms = ms;
  326. } // every 2 seconds
  327. // Over 2 minutes?
  328. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  329. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  330. return;
  331. }
  332. if (cycles > ncycles) {
  333. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  334. #if HAS_PID_FOR_BOTH
  335. const char* estring = hotend < 0 ? "bed" : "";
  336. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL;
  337. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL;
  338. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL;
  339. #elif ENABLED(PIDTEMP)
  340. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL;
  341. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL;
  342. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL;
  343. #else
  344. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL;
  345. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL;
  346. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL;
  347. #endif
  348. #define _SET_BED_PID() do { \
  349. bedKp = workKp; \
  350. bedKi = scalePID_i(workKi); \
  351. bedKd = scalePID_d(workKd); \
  352. updatePID(); } while(0)
  353. #define _SET_EXTRUDER_PID() do { \
  354. PID_PARAM(Kp, hotend) = workKp; \
  355. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  356. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  357. updatePID(); } while(0)
  358. // Use the result? (As with "M303 U1")
  359. if (set_result) {
  360. #if HAS_PID_FOR_BOTH
  361. if (hotend < 0)
  362. _SET_BED_PID();
  363. else
  364. _SET_EXTRUDER_PID();
  365. #elif ENABLED(PIDTEMP)
  366. _SET_EXTRUDER_PID();
  367. #else
  368. _SET_BED_PID();
  369. #endif
  370. }
  371. return;
  372. }
  373. lcd_update();
  374. }
  375. if (!wait_for_heatup) disable_all_heaters();
  376. }
  377. #endif // HAS_PID_HEATING
  378. /**
  379. * Class and Instance Methods
  380. */
  381. Temperature::Temperature() { }
  382. void Temperature::updatePID() {
  383. #if ENABLED(PIDTEMP)
  384. #if ENABLED(PID_EXTRUSION_SCALING)
  385. last_e_position = 0;
  386. #endif
  387. #endif
  388. }
  389. int Temperature::getHeaterPower(int heater) {
  390. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  391. }
  392. #if HAS_AUTO_FAN
  393. void Temperature::checkExtruderAutoFans() {
  394. const int8_t fanPin[] = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN };
  395. const int fanBit[] = {
  396. 0,
  397. AUTO_1_IS_0 ? 0 : 1,
  398. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  399. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3
  400. };
  401. uint8_t fanState = 0;
  402. HOTEND_LOOP() {
  403. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  404. SBI(fanState, fanBit[e]);
  405. }
  406. uint8_t fanDone = 0;
  407. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  408. int8_t pin = fanPin[f];
  409. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  410. uint8_t newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  411. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  412. digitalWrite(pin, newFanSpeed);
  413. analogWrite(pin, newFanSpeed);
  414. SBI(fanDone, fanBit[f]);
  415. }
  416. }
  417. }
  418. #endif // HAS_AUTO_FAN
  419. //
  420. // Temperature Error Handlers
  421. //
  422. void Temperature::_temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  423. static bool killed = false;
  424. if (IsRunning()) {
  425. SERIAL_ERROR_START;
  426. serialprintPGM(serial_msg);
  427. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  428. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  429. }
  430. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  431. if (!killed) {
  432. Running = false;
  433. killed = true;
  434. kill(lcd_msg);
  435. }
  436. else
  437. disable_all_heaters(); // paranoia
  438. #endif
  439. }
  440. void Temperature::max_temp_error(int8_t e) {
  441. #if HAS_TEMP_BED
  442. _temp_error(e, PSTR(MSG_T_MAXTEMP), e >= 0 ? PSTR(MSG_ERR_MAXTEMP) : PSTR(MSG_ERR_MAXTEMP_BED));
  443. #else
  444. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  445. #if HOTENDS == 1
  446. UNUSED(e);
  447. #endif
  448. #endif
  449. }
  450. void Temperature::min_temp_error(int8_t e) {
  451. #if HAS_TEMP_BED
  452. _temp_error(e, PSTR(MSG_T_MINTEMP), e >= 0 ? PSTR(MSG_ERR_MINTEMP) : PSTR(MSG_ERR_MINTEMP_BED));
  453. #else
  454. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  455. #if HOTENDS == 1
  456. UNUSED(e);
  457. #endif
  458. #endif
  459. }
  460. float Temperature::get_pid_output(int e) {
  461. #if HOTENDS == 1
  462. UNUSED(e);
  463. #define _HOTEND_TEST true
  464. #else
  465. #define _HOTEND_TEST e == active_extruder
  466. #endif
  467. float pid_output;
  468. #if ENABLED(PIDTEMP)
  469. #if DISABLED(PID_OPENLOOP)
  470. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  471. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  472. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  473. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  474. pid_output = BANG_MAX;
  475. pid_reset[HOTEND_INDEX] = true;
  476. }
  477. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0) {
  478. pid_output = 0;
  479. pid_reset[HOTEND_INDEX] = true;
  480. }
  481. else {
  482. if (pid_reset[HOTEND_INDEX]) {
  483. temp_iState[HOTEND_INDEX] = 0.0;
  484. pid_reset[HOTEND_INDEX] = false;
  485. }
  486. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  487. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  488. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  489. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  490. #if ENABLED(PID_EXTRUSION_SCALING)
  491. cTerm[HOTEND_INDEX] = 0;
  492. if (_HOTEND_TEST) {
  493. long e_position = stepper.position(E_AXIS);
  494. if (e_position > last_e_position) {
  495. lpq[lpq_ptr] = e_position - last_e_position;
  496. last_e_position = e_position;
  497. }
  498. else {
  499. lpq[lpq_ptr] = 0;
  500. }
  501. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  502. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  503. pid_output += cTerm[HOTEND_INDEX];
  504. }
  505. #endif // PID_EXTRUSION_SCALING
  506. if (pid_output > PID_MAX) {
  507. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  508. pid_output = PID_MAX;
  509. }
  510. else if (pid_output < 0) {
  511. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  512. pid_output = 0;
  513. }
  514. }
  515. #else
  516. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  517. #endif //PID_OPENLOOP
  518. #if ENABLED(PID_DEBUG)
  519. SERIAL_ECHO_START;
  520. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  521. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  522. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  523. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  524. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  525. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  526. #if ENABLED(PID_EXTRUSION_SCALING)
  527. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  528. #endif
  529. SERIAL_EOL;
  530. #endif //PID_DEBUG
  531. #else /* PID off */
  532. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  533. #endif
  534. return pid_output;
  535. }
  536. #if ENABLED(PIDTEMPBED)
  537. float Temperature::get_pid_output_bed() {
  538. float pid_output;
  539. #if DISABLED(PID_OPENLOOP)
  540. pid_error_bed = target_temperature_bed - current_temperature_bed;
  541. pTerm_bed = bedKp * pid_error_bed;
  542. temp_iState_bed += pid_error_bed;
  543. iTerm_bed = bedKi * temp_iState_bed;
  544. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  545. temp_dState_bed = current_temperature_bed;
  546. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  547. if (pid_output > MAX_BED_POWER) {
  548. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  549. pid_output = MAX_BED_POWER;
  550. }
  551. else if (pid_output < 0) {
  552. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  553. pid_output = 0;
  554. }
  555. #else
  556. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  557. #endif // PID_OPENLOOP
  558. #if ENABLED(PID_BED_DEBUG)
  559. SERIAL_ECHO_START;
  560. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  561. SERIAL_ECHOPGM(": Input ");
  562. SERIAL_ECHO(current_temperature_bed);
  563. SERIAL_ECHOPGM(" Output ");
  564. SERIAL_ECHO(pid_output);
  565. SERIAL_ECHOPGM(" pTerm ");
  566. SERIAL_ECHO(pTerm_bed);
  567. SERIAL_ECHOPGM(" iTerm ");
  568. SERIAL_ECHO(iTerm_bed);
  569. SERIAL_ECHOPGM(" dTerm ");
  570. SERIAL_ECHOLN(dTerm_bed);
  571. #endif //PID_BED_DEBUG
  572. return pid_output;
  573. }
  574. #endif //PIDTEMPBED
  575. /**
  576. * Manage heating activities for extruder hot-ends and a heated bed
  577. * - Acquire updated temperature readings
  578. * - Also resets the watchdog timer
  579. * - Invoke thermal runaway protection
  580. * - Manage extruder auto-fan
  581. * - Apply filament width to the extrusion rate (may move)
  582. * - Update the heated bed PID output value
  583. */
  584. /**
  585. * The following line SOMETIMES results in the dreaded "unable to find a register to spill in class 'POINTER_REGS'"
  586. * compile error.
  587. * thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  588. *
  589. * This is due to a bug in the C++ compiler used by the Arduino IDE from 1.6.10 to at least 1.8.1.
  590. *
  591. * The work around is to add the compiler flag "__attribute__((__optimize__("O2")))" to the declaration for manage_heater()
  592. */
  593. //void Temperature::manage_heater() __attribute__((__optimize__("O2")));
  594. void Temperature::manage_heater() {
  595. if (!temp_meas_ready) return;
  596. updateTemperaturesFromRawValues(); // also resets the watchdog
  597. #if ENABLED(HEATER_0_USES_MAX6675)
  598. if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1)) max_temp_error(0);
  599. if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + 0.01)) min_temp_error(0);
  600. #endif
  601. #if (ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0) || (ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0) || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  602. millis_t ms = millis();
  603. #endif
  604. // Loop through all hotends
  605. HOTEND_LOOP() {
  606. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  607. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  608. #endif
  609. float pid_output = get_pid_output(e);
  610. // Check if temperature is within the correct range
  611. soft_pwm[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  612. // Check if the temperature is failing to increase
  613. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  614. // Is it time to check this extruder's heater?
  615. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) {
  616. // Has it failed to increase enough?
  617. if (degHotend(e) < watch_target_temp[e]) {
  618. // Stop!
  619. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  620. }
  621. else {
  622. // Start again if the target is still far off
  623. start_watching_heater(e);
  624. }
  625. }
  626. #endif // THERMAL_PROTECTION_HOTENDS
  627. // Check if the temperature is failing to increase
  628. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  629. // Is it time to check the bed?
  630. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) {
  631. // Has it failed to increase enough?
  632. if (degBed() < watch_target_bed_temp) {
  633. // Stop!
  634. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  635. }
  636. else {
  637. // Start again if the target is still far off
  638. start_watching_bed();
  639. }
  640. }
  641. #endif // THERMAL_PROTECTION_HOTENDS
  642. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  643. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  644. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  645. }
  646. #endif
  647. } // HOTEND_LOOP
  648. #if HAS_AUTO_FAN
  649. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  650. checkExtruderAutoFans();
  651. next_auto_fan_check_ms = ms + 2500UL;
  652. }
  653. #endif
  654. // Control the extruder rate based on the width sensor
  655. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  656. if (filament_sensor) {
  657. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  658. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  659. // Get the delayed info and add 100 to reconstitute to a percent of
  660. // the nominal filament diameter then square it to get an area
  661. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  662. float vm = pow((measurement_delay[meas_shift_index] + 100.0) * 0.01, 2);
  663. NOLESS(vm, 0.01);
  664. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  665. }
  666. #endif //FILAMENT_WIDTH_SENSOR
  667. #if DISABLED(PIDTEMPBED)
  668. if (PENDING(ms, next_bed_check_ms)) return;
  669. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  670. #endif
  671. #if TEMP_SENSOR_BED != 0
  672. #if HAS_THERMALLY_PROTECTED_BED
  673. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, -1, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  674. #endif
  675. #if ENABLED(PIDTEMPBED)
  676. float pid_output = get_pid_output_bed();
  677. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  678. #elif ENABLED(BED_LIMIT_SWITCHING)
  679. // Check if temperature is within the correct band
  680. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  681. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  682. soft_pwm_bed = 0;
  683. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  684. soft_pwm_bed = MAX_BED_POWER >> 1;
  685. }
  686. else {
  687. soft_pwm_bed = 0;
  688. WRITE_HEATER_BED(LOW);
  689. }
  690. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  691. // Check if temperature is within the correct range
  692. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  693. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  694. }
  695. else {
  696. soft_pwm_bed = 0;
  697. WRITE_HEATER_BED(LOW);
  698. }
  699. #endif
  700. #endif //TEMP_SENSOR_BED != 0
  701. }
  702. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  703. // Derived from RepRap FiveD extruder::getTemperature()
  704. // For hot end temperature measurement.
  705. float Temperature::analog2temp(int raw, uint8_t e) {
  706. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  707. if (e > HOTENDS)
  708. #else
  709. if (e >= HOTENDS)
  710. #endif
  711. {
  712. SERIAL_ERROR_START;
  713. SERIAL_ERROR((int)e);
  714. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  715. kill(PSTR(MSG_KILLED));
  716. return 0.0;
  717. }
  718. #if ENABLED(HEATER_0_USES_MAX6675)
  719. if (e == 0) return 0.25 * raw;
  720. #endif
  721. if (heater_ttbl_map[e] != NULL) {
  722. float celsius = 0;
  723. uint8_t i;
  724. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  725. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  726. if (PGM_RD_W((*tt)[i][0]) > raw) {
  727. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  728. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  729. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  730. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  731. break;
  732. }
  733. }
  734. // Overflow: Set to last value in the table
  735. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  736. return celsius;
  737. }
  738. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  739. }
  740. // Derived from RepRap FiveD extruder::getTemperature()
  741. // For bed temperature measurement.
  742. float Temperature::analog2tempBed(int raw) {
  743. #if ENABLED(BED_USES_THERMISTOR)
  744. float celsius = 0;
  745. byte i;
  746. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  747. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  748. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  749. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  750. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  751. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  752. break;
  753. }
  754. }
  755. // Overflow: Set to last value in the table
  756. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  757. return celsius;
  758. #elif defined(BED_USES_AD595)
  759. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  760. #else
  761. UNUSED(raw);
  762. return 0;
  763. #endif
  764. }
  765. /**
  766. * Get the raw values into the actual temperatures.
  767. * The raw values are created in interrupt context,
  768. * and this function is called from normal context
  769. * as it would block the stepper routine.
  770. */
  771. void Temperature::updateTemperaturesFromRawValues() {
  772. #if ENABLED(HEATER_0_USES_MAX6675)
  773. current_temperature_raw[0] = read_max6675();
  774. #endif
  775. HOTEND_LOOP()
  776. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  777. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  778. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  779. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  780. #endif
  781. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  782. filament_width_meas = analog2widthFil();
  783. #endif
  784. #if ENABLED(USE_WATCHDOG)
  785. // Reset the watchdog after we know we have a temperature measurement.
  786. watchdog_reset();
  787. #endif
  788. CRITICAL_SECTION_START;
  789. temp_meas_ready = false;
  790. CRITICAL_SECTION_END;
  791. }
  792. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  793. // Convert raw Filament Width to millimeters
  794. float Temperature::analog2widthFil() {
  795. return current_raw_filwidth / 16383.0 * 5.0;
  796. //return current_raw_filwidth;
  797. }
  798. // Convert raw Filament Width to a ratio
  799. int Temperature::widthFil_to_size_ratio() {
  800. float temp = filament_width_meas;
  801. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  802. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  803. return filament_width_nominal / temp * 100;
  804. }
  805. #endif
  806. /**
  807. * Initialize the temperature manager
  808. * The manager is implemented by periodic calls to manage_heater()
  809. */
  810. void Temperature::init() {
  811. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  812. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  813. MCUCR = _BV(JTD);
  814. MCUCR = _BV(JTD);
  815. #endif
  816. // Finish init of mult hotend arrays
  817. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  818. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  819. last_e_position = 0;
  820. #endif
  821. #if HAS_HEATER_0
  822. SET_OUTPUT(HEATER_0_PIN);
  823. #endif
  824. #if HAS_HEATER_1
  825. SET_OUTPUT(HEATER_1_PIN);
  826. #endif
  827. #if HAS_HEATER_2
  828. SET_OUTPUT(HEATER_2_PIN);
  829. #endif
  830. #if HAS_HEATER_3
  831. SET_OUTPUT(HEATER_3_PIN);
  832. #endif
  833. #if HAS_HEATER_BED
  834. SET_OUTPUT(HEATER_BED_PIN);
  835. #endif
  836. #if HAS_FAN0
  837. SET_OUTPUT(FAN_PIN);
  838. #if ENABLED(FAST_PWM_FAN)
  839. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  840. #endif
  841. #if ENABLED(FAN_SOFT_PWM)
  842. soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
  843. #endif
  844. #endif
  845. #if HAS_FAN1
  846. SET_OUTPUT(FAN1_PIN);
  847. #if ENABLED(FAST_PWM_FAN)
  848. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  849. #endif
  850. #if ENABLED(FAN_SOFT_PWM)
  851. soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1;
  852. #endif
  853. #endif
  854. #if HAS_FAN2
  855. SET_OUTPUT(FAN2_PIN);
  856. #if ENABLED(FAST_PWM_FAN)
  857. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  858. #endif
  859. #if ENABLED(FAN_SOFT_PWM)
  860. soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1;
  861. #endif
  862. #endif
  863. #if ENABLED(HEATER_0_USES_MAX6675)
  864. OUT_WRITE(SCK_PIN, LOW);
  865. OUT_WRITE(MOSI_PIN, HIGH);
  866. SET_INPUT_PULLUP(MISO_PIN);
  867. OUT_WRITE(SS_PIN, HIGH);
  868. OUT_WRITE(MAX6675_SS, HIGH);
  869. #endif //HEATER_0_USES_MAX6675
  870. #ifdef DIDR2
  871. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  872. #else
  873. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  874. #endif
  875. // Set analog inputs
  876. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  877. DIDR0 = 0;
  878. #ifdef DIDR2
  879. DIDR2 = 0;
  880. #endif
  881. #if HAS_TEMP_0
  882. ANALOG_SELECT(TEMP_0_PIN);
  883. #endif
  884. #if HAS_TEMP_1
  885. ANALOG_SELECT(TEMP_1_PIN);
  886. #endif
  887. #if HAS_TEMP_2
  888. ANALOG_SELECT(TEMP_2_PIN);
  889. #endif
  890. #if HAS_TEMP_3
  891. ANALOG_SELECT(TEMP_3_PIN);
  892. #endif
  893. #if HAS_TEMP_BED
  894. ANALOG_SELECT(TEMP_BED_PIN);
  895. #endif
  896. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  897. ANALOG_SELECT(FILWIDTH_PIN);
  898. #endif
  899. #if HAS_AUTO_FAN_0
  900. #if E0_AUTO_FAN_PIN == FAN1_PIN
  901. SET_OUTPUT(E0_AUTO_FAN_PIN);
  902. #if ENABLED(FAST_PWM_FAN)
  903. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  904. #endif
  905. #else
  906. SET_OUTPUT(E0_AUTO_FAN_PIN);
  907. #endif
  908. #endif
  909. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  910. #if E1_AUTO_FAN_PIN == FAN1_PIN
  911. SET_OUTPUT(E1_AUTO_FAN_PIN);
  912. #if ENABLED(FAST_PWM_FAN)
  913. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  914. #endif
  915. #else
  916. SET_OUTPUT(E1_AUTO_FAN_PIN);
  917. #endif
  918. #endif
  919. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  920. #if E2_AUTO_FAN_PIN == FAN1_PIN
  921. SET_OUTPUT(E2_AUTO_FAN_PIN);
  922. #if ENABLED(FAST_PWM_FAN)
  923. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  924. #endif
  925. #else
  926. SET_OUTPUT(E2_AUTO_FAN_PIN);
  927. #endif
  928. #endif
  929. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  930. #if E3_AUTO_FAN_PIN == FAN1_PIN
  931. SET_OUTPUT(E3_AUTO_FAN_PIN);
  932. #if ENABLED(FAST_PWM_FAN)
  933. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  934. #endif
  935. #else
  936. SET_OUTPUT(E3_AUTO_FAN_PIN);
  937. #endif
  938. #endif
  939. // Use timer0 for temperature measurement
  940. // Interleave temperature interrupt with millies interrupt
  941. OCR0B = 128;
  942. SBI(TIMSK0, OCIE0B);
  943. // Wait for temperature measurement to settle
  944. delay(250);
  945. #define TEMP_MIN_ROUTINE(NR) \
  946. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  947. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  948. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  949. minttemp_raw[NR] += OVERSAMPLENR; \
  950. else \
  951. minttemp_raw[NR] -= OVERSAMPLENR; \
  952. }
  953. #define TEMP_MAX_ROUTINE(NR) \
  954. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  955. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  956. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  957. maxttemp_raw[NR] -= OVERSAMPLENR; \
  958. else \
  959. maxttemp_raw[NR] += OVERSAMPLENR; \
  960. }
  961. #ifdef HEATER_0_MINTEMP
  962. TEMP_MIN_ROUTINE(0);
  963. #endif
  964. #ifdef HEATER_0_MAXTEMP
  965. TEMP_MAX_ROUTINE(0);
  966. #endif
  967. #if HOTENDS > 1
  968. #ifdef HEATER_1_MINTEMP
  969. TEMP_MIN_ROUTINE(1);
  970. #endif
  971. #ifdef HEATER_1_MAXTEMP
  972. TEMP_MAX_ROUTINE(1);
  973. #endif
  974. #if HOTENDS > 2
  975. #ifdef HEATER_2_MINTEMP
  976. TEMP_MIN_ROUTINE(2);
  977. #endif
  978. #ifdef HEATER_2_MAXTEMP
  979. TEMP_MAX_ROUTINE(2);
  980. #endif
  981. #if HOTENDS > 3
  982. #ifdef HEATER_3_MINTEMP
  983. TEMP_MIN_ROUTINE(3);
  984. #endif
  985. #ifdef HEATER_3_MAXTEMP
  986. TEMP_MAX_ROUTINE(3);
  987. #endif
  988. #endif // HOTENDS > 3
  989. #endif // HOTENDS > 2
  990. #endif // HOTENDS > 1
  991. #ifdef BED_MINTEMP
  992. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  993. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  994. bed_minttemp_raw += OVERSAMPLENR;
  995. #else
  996. bed_minttemp_raw -= OVERSAMPLENR;
  997. #endif
  998. }
  999. #endif //BED_MINTEMP
  1000. #ifdef BED_MAXTEMP
  1001. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1002. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1003. bed_maxttemp_raw -= OVERSAMPLENR;
  1004. #else
  1005. bed_maxttemp_raw += OVERSAMPLENR;
  1006. #endif
  1007. }
  1008. #endif //BED_MAXTEMP
  1009. }
  1010. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  1011. /**
  1012. * Start Heating Sanity Check for hotends that are below
  1013. * their target temperature by a configurable margin.
  1014. * This is called when the temperature is set. (M104, M109)
  1015. */
  1016. void Temperature::start_watching_heater(uint8_t e) {
  1017. #if HOTENDS == 1
  1018. UNUSED(e);
  1019. #endif
  1020. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1021. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1022. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1023. }
  1024. else
  1025. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1026. }
  1027. #endif
  1028. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  1029. /**
  1030. * Start Heating Sanity Check for hotends that are below
  1031. * their target temperature by a configurable margin.
  1032. * This is called when the temperature is set. (M140, M190)
  1033. */
  1034. void Temperature::start_watching_bed() {
  1035. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1036. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1037. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1038. }
  1039. else
  1040. watch_bed_next_ms = 0;
  1041. }
  1042. #endif
  1043. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1044. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1045. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1046. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1047. #endif
  1048. #if HAS_THERMALLY_PROTECTED_BED
  1049. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1050. millis_t Temperature::thermal_runaway_bed_timer;
  1051. #endif
  1052. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  1053. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1054. /**
  1055. SERIAL_ECHO_START;
  1056. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1057. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1058. SERIAL_ECHOPAIR(" ; State:", *state);
  1059. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1060. SERIAL_ECHOPAIR(" ; Temperature:", temperature);
  1061. SERIAL_ECHOPAIR(" ; Target Temp:", target_temperature);
  1062. SERIAL_EOL;
  1063. */
  1064. int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1065. // If the target temperature changes, restart
  1066. if (tr_target_temperature[heater_index] != target_temperature) {
  1067. tr_target_temperature[heater_index] = target_temperature;
  1068. *state = target_temperature > 0 ? TRFirstHeating : TRInactive;
  1069. }
  1070. switch (*state) {
  1071. // Inactive state waits for a target temperature to be set
  1072. case TRInactive: break;
  1073. // When first heating, wait for the temperature to be reached then go to Stable state
  1074. case TRFirstHeating:
  1075. if (temperature < tr_target_temperature[heater_index]) break;
  1076. *state = TRStable;
  1077. // While the temperature is stable watch for a bad temperature
  1078. case TRStable:
  1079. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1080. *timer = millis() + period_seconds * 1000UL;
  1081. break;
  1082. }
  1083. else if (PENDING(millis(), *timer)) break;
  1084. *state = TRRunaway;
  1085. case TRRunaway:
  1086. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1087. }
  1088. }
  1089. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1090. void Temperature::disable_all_heaters() {
  1091. HOTEND_LOOP() setTargetHotend(0, e);
  1092. setTargetBed(0);
  1093. // If all heaters go down then for sure our print job has stopped
  1094. print_job_timer.stop();
  1095. #define DISABLE_HEATER(NR) { \
  1096. setTargetHotend(0, NR); \
  1097. soft_pwm[NR] = 0; \
  1098. WRITE_HEATER_ ##NR (LOW); \
  1099. }
  1100. #if HAS_TEMP_HOTEND
  1101. DISABLE_HEATER(0);
  1102. #endif
  1103. #if HOTENDS > 1 && HAS_TEMP_1
  1104. DISABLE_HEATER(1);
  1105. #endif
  1106. #if HOTENDS > 2 && HAS_TEMP_2
  1107. DISABLE_HEATER(2);
  1108. #endif
  1109. #if HOTENDS > 3 && HAS_TEMP_3
  1110. DISABLE_HEATER(3);
  1111. #endif
  1112. #if HAS_TEMP_BED
  1113. target_temperature_bed = 0;
  1114. soft_pwm_bed = 0;
  1115. #if HAS_HEATER_BED
  1116. WRITE_HEATER_BED(LOW);
  1117. #endif
  1118. #endif
  1119. }
  1120. #if ENABLED(HEATER_0_USES_MAX6675)
  1121. #define MAX6675_HEAT_INTERVAL 250u
  1122. #if ENABLED(MAX6675_IS_MAX31855)
  1123. uint32_t max6675_temp = 2000;
  1124. #define MAX6675_ERROR_MASK 7
  1125. #define MAX6675_DISCARD_BITS 18
  1126. #define MAX6675_SPEED_BITS (_BV(SPR1)) // clock ÷ 64
  1127. #else
  1128. uint16_t max6675_temp = 2000;
  1129. #define MAX6675_ERROR_MASK 4
  1130. #define MAX6675_DISCARD_BITS 3
  1131. #define MAX6675_SPEED_BITS (_BV(SPR0)) // clock ÷ 16
  1132. #endif
  1133. int Temperature::read_max6675() {
  1134. static millis_t next_max6675_ms = 0;
  1135. millis_t ms = millis();
  1136. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1137. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1138. CBI(
  1139. #ifdef PRR
  1140. PRR
  1141. #elif defined(PRR0)
  1142. PRR0
  1143. #endif
  1144. , PRSPI);
  1145. SPCR = _BV(MSTR) | _BV(SPE) | MAX6675_SPEED_BITS;
  1146. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1147. // ensure 100ns delay - a bit extra is fine
  1148. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1149. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1150. // Read a big-endian temperature value
  1151. max6675_temp = 0;
  1152. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1153. SPDR = 0;
  1154. for (;!TEST(SPSR, SPIF););
  1155. max6675_temp |= SPDR;
  1156. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1157. }
  1158. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1159. if (max6675_temp & MAX6675_ERROR_MASK) {
  1160. SERIAL_ERROR_START;
  1161. SERIAL_ERRORPGM("Temp measurement error! ");
  1162. #if MAX6675_ERROR_MASK == 7
  1163. SERIAL_ERRORPGM("MAX31855 ");
  1164. if (max6675_temp & 1)
  1165. SERIAL_ERRORLNPGM("Open Circuit");
  1166. else if (max6675_temp & 2)
  1167. SERIAL_ERRORLNPGM("Short to GND");
  1168. else if (max6675_temp & 4)
  1169. SERIAL_ERRORLNPGM("Short to VCC");
  1170. #else
  1171. SERIAL_ERRORLNPGM("MAX6675");
  1172. #endif
  1173. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1174. }
  1175. else
  1176. max6675_temp >>= MAX6675_DISCARD_BITS;
  1177. #if ENABLED(MAX6675_IS_MAX31855)
  1178. // Support negative temperature
  1179. if (max6675_temp & 0x00002000) max6675_temp |= 0xffffc000;
  1180. #endif
  1181. return (int)max6675_temp;
  1182. }
  1183. #endif //HEATER_0_USES_MAX6675
  1184. /**
  1185. * Get raw temperatures
  1186. */
  1187. void Temperature::set_current_temp_raw() {
  1188. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1189. current_temperature_raw[0] = raw_temp_value[0];
  1190. #endif
  1191. #if HAS_TEMP_1
  1192. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1193. redundant_temperature_raw = raw_temp_value[1];
  1194. #else
  1195. current_temperature_raw[1] = raw_temp_value[1];
  1196. #endif
  1197. #if HAS_TEMP_2
  1198. current_temperature_raw[2] = raw_temp_value[2];
  1199. #if HAS_TEMP_3
  1200. current_temperature_raw[3] = raw_temp_value[3];
  1201. #endif
  1202. #endif
  1203. #endif
  1204. current_temperature_bed_raw = raw_temp_bed_value;
  1205. temp_meas_ready = true;
  1206. }
  1207. #if ENABLED(PINS_DEBUGGING)
  1208. /**
  1209. * monitors endstops & Z probe for changes
  1210. *
  1211. * If a change is detected then the LED is toggled and
  1212. * a message is sent out the serial port
  1213. *
  1214. * Yes, we could miss a rapid back & forth change but
  1215. * that won't matter because this is all manual.
  1216. *
  1217. */
  1218. void endstop_monitor() {
  1219. static uint16_t old_endstop_bits_local = 0;
  1220. static uint8_t local_LED_status = 0;
  1221. uint16_t current_endstop_bits_local = 0;
  1222. #if HAS_X_MIN
  1223. if (READ(X_MIN_PIN)) SBI(current_endstop_bits_local, X_MIN);
  1224. #endif
  1225. #if HAS_X_MAX
  1226. if (READ(X_MAX_PIN)) SBI(current_endstop_bits_local, X_MAX);
  1227. #endif
  1228. #if HAS_Y_MIN
  1229. if (READ(Y_MIN_PIN)) SBI(current_endstop_bits_local, Y_MIN);
  1230. #endif
  1231. #if HAS_Y_MAX
  1232. if (READ(Y_MAX_PIN)) SBI(current_endstop_bits_local, Y_MAX);
  1233. #endif
  1234. #if HAS_Z_MIN
  1235. if (READ(Z_MIN_PIN)) SBI(current_endstop_bits_local, Z_MIN);
  1236. #endif
  1237. #if HAS_Z_MAX
  1238. if (READ(Z_MAX_PIN)) SBI(current_endstop_bits_local, Z_MAX);
  1239. #endif
  1240. #if HAS_Z_MIN_PROBE_PIN
  1241. if (READ(Z_MIN_PROBE_PIN)) SBI(current_endstop_bits_local, Z_MIN_PROBE);
  1242. #endif
  1243. #if HAS_Z2_MIN
  1244. if (READ(Z2_MIN_PIN)) SBI(current_endstop_bits_local, Z2_MIN);
  1245. #endif
  1246. #if HAS_Z2_MAX
  1247. if (READ(Z2_MAX_PIN)) SBI(current_endstop_bits_local, Z2_MAX);
  1248. #endif
  1249. uint16_t endstop_change = current_endstop_bits_local ^ old_endstop_bits_local;
  1250. if (endstop_change) {
  1251. #if HAS_X_MIN
  1252. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR("X_MIN:", !!TEST(current_endstop_bits_local, X_MIN));
  1253. #endif
  1254. #if HAS_X_MAX
  1255. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", !!TEST(current_endstop_bits_local, X_MAX));
  1256. #endif
  1257. #if HAS_Y_MIN
  1258. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", !!TEST(current_endstop_bits_local, Y_MIN));
  1259. #endif
  1260. #if HAS_Y_MAX
  1261. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", !!TEST(current_endstop_bits_local, Y_MAX));
  1262. #endif
  1263. #if HAS_Z_MIN
  1264. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", !!TEST(current_endstop_bits_local, Z_MIN));
  1265. #endif
  1266. #if HAS_Z_MAX
  1267. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", !!TEST(current_endstop_bits_local, Z_MAX));
  1268. #endif
  1269. #if HAS_Z_MIN_PROBE_PIN
  1270. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", !!TEST(current_endstop_bits_local, Z_MIN_PROBE));
  1271. #endif
  1272. #if HAS_Z2_MIN
  1273. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", !!TEST(current_endstop_bits_local, Z2_MIN));
  1274. #endif
  1275. #if HAS_Z2_MAX
  1276. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", !!TEST(current_endstop_bits_local, Z2_MAX));
  1277. #endif
  1278. SERIAL_PROTOCOLPGM("\n\n");
  1279. analogWrite(LED_PIN, local_LED_status);
  1280. local_LED_status ^= 255;
  1281. old_endstop_bits_local = current_endstop_bits_local;
  1282. }
  1283. }
  1284. #endif // PINS_DEBUGGING
  1285. /**
  1286. * Timer 0 is shared with millies so don't change the prescaler.
  1287. *
  1288. * This ISR uses the compare method so it runs at the base
  1289. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1290. * in OCR0B above (128 or halfway between OVFs).
  1291. *
  1292. * - Manage PWM to all the heaters and fan
  1293. * - Prepare or Measure one of the raw ADC sensor values
  1294. * - Check new temperature values for MIN/MAX errors (kill on error)
  1295. * - Step the babysteps value for each axis towards 0
  1296. * - For PINS_DEBUGGING, monitor and report endstop pins
  1297. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1298. */
  1299. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1300. volatile bool Temperature::in_temp_isr = false;
  1301. void Temperature::isr() {
  1302. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1303. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1304. if (in_temp_isr) return;
  1305. in_temp_isr = true;
  1306. // Allow UART and stepper ISRs
  1307. CBI(TIMSK0, OCIE0B); //Disable Temperature ISR
  1308. sei();
  1309. static uint8_t temp_count = 0;
  1310. static TempState temp_state = StartupDelay;
  1311. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1312. // avoid multiple loads of pwm_count
  1313. uint8_t pwm_count_tmp = pwm_count;
  1314. // Static members for each heater
  1315. #if ENABLED(SLOW_PWM_HEATERS)
  1316. static uint8_t slow_pwm_count = 0;
  1317. #define ISR_STATICS(n) \
  1318. static uint8_t soft_pwm_ ## n; \
  1319. static uint8_t state_heater_ ## n = 0; \
  1320. static uint8_t state_timer_heater_ ## n = 0
  1321. #else
  1322. #define ISR_STATICS(n) static uint8_t soft_pwm_ ## n = 0
  1323. #endif
  1324. // Statics per heater
  1325. ISR_STATICS(0);
  1326. #if HOTENDS > 1
  1327. ISR_STATICS(1);
  1328. #if HOTENDS > 2
  1329. ISR_STATICS(2);
  1330. #if HOTENDS > 3
  1331. ISR_STATICS(3);
  1332. #endif
  1333. #endif
  1334. #endif
  1335. #if HAS_HEATER_BED
  1336. ISR_STATICS(BED);
  1337. #endif
  1338. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1339. static unsigned long raw_filwidth_value = 0;
  1340. #endif
  1341. #if DISABLED(SLOW_PWM_HEATERS)
  1342. constexpr uint8_t pwm_mask =
  1343. #if ENABLED(SOFT_PWM_DITHER)
  1344. _BV(SOFT_PWM_SCALE) - 1
  1345. #else
  1346. 0
  1347. #endif
  1348. ;
  1349. /**
  1350. * Standard PWM modulation
  1351. */
  1352. if (pwm_count_tmp >= 127) {
  1353. pwm_count_tmp -= 127;
  1354. soft_pwm_0 = (soft_pwm_0 & pwm_mask) + soft_pwm[0];
  1355. WRITE_HEATER_0(soft_pwm_0 > pwm_mask ? HIGH : LOW);
  1356. #if HOTENDS > 1
  1357. soft_pwm_1 = (soft_pwm_1 & pwm_mask) + soft_pwm[1];
  1358. WRITE_HEATER_1(soft_pwm_1 > pwm_mask ? HIGH : LOW);
  1359. #if HOTENDS > 2
  1360. soft_pwm_2 = (soft_pwm_2 & pwm_mask) + soft_pwm[2];
  1361. WRITE_HEATER_2(soft_pwm_2 > pwm_mask ? HIGH : LOW);
  1362. #if HOTENDS > 3
  1363. soft_pwm_3 = (soft_pwm_3 & pwm_mask) + soft_pwm[3];
  1364. WRITE_HEATER_3(soft_pwm_3 > pwm_mask ? HIGH : LOW);
  1365. #endif
  1366. #endif
  1367. #endif
  1368. #if HAS_HEATER_BED
  1369. soft_pwm_BED = (soft_pwm_BED & pwm_mask) + soft_pwm_bed;
  1370. WRITE_HEATER_BED(soft_pwm_BED > pwm_mask ? HIGH : LOW);
  1371. #endif
  1372. #if ENABLED(FAN_SOFT_PWM)
  1373. #if HAS_FAN0
  1374. soft_pwm_fan[0] = (soft_pwm_fan[0] & pwm_mask) + fanSpeedSoftPwm[0] >> 1;
  1375. WRITE_FAN(soft_pwm_fan[0] > pwm_mask ? HIGH : LOW);
  1376. #endif
  1377. #if HAS_FAN1
  1378. soft_pwm_fan[1] = (soft_pwm_fan[1] & pwm_mask) + fanSpeedSoftPwm[1] >> 1;
  1379. WRITE_FAN1(soft_pwm_fan[1] > pwm_mask ? HIGH : LOW);
  1380. #endif
  1381. #if HAS_FAN2
  1382. soft_pwm_fan[2] = (soft_pwm_fan[2] & pwm_mask) + fanSpeedSoftPwm[2] >> 1;
  1383. WRITE_FAN2(soft_pwm_fan[2] > pwm_mask ? HIGH : LOW);
  1384. #endif
  1385. #endif
  1386. }
  1387. else {
  1388. if (soft_pwm_0 <= pwm_count_tmp) WRITE_HEATER_0(0);
  1389. #if HOTENDS > 1
  1390. if (soft_pwm_1 <= pwm_count_tmp) WRITE_HEATER_1(0);
  1391. #endif
  1392. #if HOTENDS > 2
  1393. if (soft_pwm_2 <= pwm_count_tmp) WRITE_HEATER_2(0);
  1394. #endif
  1395. #if HOTENDS > 3
  1396. if (soft_pwm_3 <= pwm_count_tmp) WRITE_HEATER_3(0);
  1397. #endif
  1398. #if HAS_HEATER_BED
  1399. if (soft_pwm_BED <= pwm_count_tmp) WRITE_HEATER_BED(0);
  1400. #endif
  1401. #if ENABLED(FAN_SOFT_PWM)
  1402. #if HAS_FAN0
  1403. if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1404. #endif
  1405. #if HAS_FAN1
  1406. if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1407. #endif
  1408. #if HAS_FAN2
  1409. if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1410. #endif
  1411. #endif
  1412. }
  1413. // SOFT_PWM_SCALE to frequency:
  1414. //
  1415. // 0: 16000000/64/256/128 = 7.6294 Hz
  1416. // 1: / 64 = 15.2588 Hz
  1417. // 2: / 32 = 30.5176 Hz
  1418. // 3: / 16 = 61.0352 Hz
  1419. // 4: / 8 = 122.0703 Hz
  1420. // 5: / 4 = 244.1406 Hz
  1421. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1422. #else // SLOW_PWM_HEATERS
  1423. /**
  1424. * SLOW PWM HEATERS
  1425. *
  1426. * For relay-driven heaters
  1427. */
  1428. #ifndef MIN_STATE_TIME
  1429. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1430. #endif
  1431. // Macros for Slow PWM timer logic
  1432. #define _SLOW_PWM_ROUTINE(NR, src) \
  1433. soft_pwm_ ##NR = src; \
  1434. if (soft_pwm_ ##NR > 0) { \
  1435. if (state_timer_heater_ ##NR == 0) { \
  1436. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1437. state_heater_ ##NR = 1; \
  1438. WRITE_HEATER_ ##NR(1); \
  1439. } \
  1440. } \
  1441. else { \
  1442. if (state_timer_heater_ ##NR == 0) { \
  1443. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1444. state_heater_ ##NR = 0; \
  1445. WRITE_HEATER_ ##NR(0); \
  1446. } \
  1447. }
  1448. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1449. #define PWM_OFF_ROUTINE(NR) \
  1450. if (soft_pwm_ ##NR < slow_pwm_count) { \
  1451. if (state_timer_heater_ ##NR == 0) { \
  1452. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1453. state_heater_ ##NR = 0; \
  1454. WRITE_HEATER_ ##NR (0); \
  1455. } \
  1456. }
  1457. if (slow_pwm_count == 0) {
  1458. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1459. #if HOTENDS > 1
  1460. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1461. #if HOTENDS > 2
  1462. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1463. #if HOTENDS > 3
  1464. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1465. #endif
  1466. #endif
  1467. #endif
  1468. #if HAS_HEATER_BED
  1469. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1470. #endif
  1471. } // slow_pwm_count == 0
  1472. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1473. #if HOTENDS > 1
  1474. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1475. #if HOTENDS > 2
  1476. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1477. #if HOTENDS > 3
  1478. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1479. #endif
  1480. #endif
  1481. #endif
  1482. #if HAS_HEATER_BED
  1483. PWM_OFF_ROUTINE(BED); // BED
  1484. #endif
  1485. #if ENABLED(FAN_SOFT_PWM)
  1486. if (pwm_count_tmp >= 127) {
  1487. pwm_count_tmp = 0;
  1488. #if HAS_FAN0
  1489. soft_pwm_fan[0] = fanSpeedSoftPwm[0] >> 1;
  1490. WRITE_FAN(soft_pwm_fan[0] > 0 ? HIGH : LOW);
  1491. #endif
  1492. #if HAS_FAN1
  1493. soft_pwm_fan[1] = fanSpeedSoftPwm[1] >> 1;
  1494. WRITE_FAN1(soft_pwm_fan[1] > 0 ? HIGH : LOW);
  1495. #endif
  1496. #if HAS_FAN2
  1497. soft_pwm_fan[2] = fanSpeedSoftPwm[2] >> 1;
  1498. WRITE_FAN2(soft_pwm_fan[2] > 0 ? HIGH : LOW);
  1499. #endif
  1500. }
  1501. #if HAS_FAN0
  1502. if (soft_pwm_fan[0] <= pwm_count_tmp) WRITE_FAN(0);
  1503. #endif
  1504. #if HAS_FAN1
  1505. if (soft_pwm_fan[1] <= pwm_count_tmp) WRITE_FAN1(0);
  1506. #endif
  1507. #if HAS_FAN2
  1508. if (soft_pwm_fan[2] <= pwm_count_tmp) WRITE_FAN2(0);
  1509. #endif
  1510. #endif // FAN_SOFT_PWM
  1511. // SOFT_PWM_SCALE to frequency:
  1512. //
  1513. // 0: 16000000/64/256/128 = 7.6294 Hz
  1514. // 1: / 64 = 15.2588 Hz
  1515. // 2: / 32 = 30.5176 Hz
  1516. // 3: / 16 = 61.0352 Hz
  1517. // 4: / 8 = 122.0703 Hz
  1518. // 5: / 4 = 244.1406 Hz
  1519. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1520. // increment slow_pwm_count only every 64th pwm_count,
  1521. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1522. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1523. slow_pwm_count++;
  1524. slow_pwm_count &= 0x7F;
  1525. // EXTRUDER 0
  1526. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1527. #if HOTENDS > 1 // EXTRUDER 1
  1528. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1529. #if HOTENDS > 2 // EXTRUDER 2
  1530. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1531. #if HOTENDS > 3 // EXTRUDER 3
  1532. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1533. #endif
  1534. #endif
  1535. #endif
  1536. #if HAS_HEATER_BED
  1537. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1538. #endif
  1539. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1540. #endif // SLOW_PWM_HEATERS
  1541. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1542. #ifdef MUX5
  1543. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1544. #else
  1545. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1546. #endif
  1547. // Prepare or measure a sensor, each one every 12th frame
  1548. switch (temp_state) {
  1549. case PrepareTemp_0:
  1550. #if HAS_TEMP_0
  1551. START_ADC(TEMP_0_PIN);
  1552. #endif
  1553. lcd_buttons_update();
  1554. temp_state = MeasureTemp_0;
  1555. break;
  1556. case MeasureTemp_0:
  1557. #if HAS_TEMP_0
  1558. raw_temp_value[0] += ADC;
  1559. #endif
  1560. temp_state = PrepareTemp_BED;
  1561. break;
  1562. case PrepareTemp_BED:
  1563. #if HAS_TEMP_BED
  1564. START_ADC(TEMP_BED_PIN);
  1565. #endif
  1566. lcd_buttons_update();
  1567. temp_state = MeasureTemp_BED;
  1568. break;
  1569. case MeasureTemp_BED:
  1570. #if HAS_TEMP_BED
  1571. raw_temp_bed_value += ADC;
  1572. #endif
  1573. temp_state = PrepareTemp_1;
  1574. break;
  1575. case PrepareTemp_1:
  1576. #if HAS_TEMP_1
  1577. START_ADC(TEMP_1_PIN);
  1578. #endif
  1579. lcd_buttons_update();
  1580. temp_state = MeasureTemp_1;
  1581. break;
  1582. case MeasureTemp_1:
  1583. #if HAS_TEMP_1
  1584. raw_temp_value[1] += ADC;
  1585. #endif
  1586. temp_state = PrepareTemp_2;
  1587. break;
  1588. case PrepareTemp_2:
  1589. #if HAS_TEMP_2
  1590. START_ADC(TEMP_2_PIN);
  1591. #endif
  1592. lcd_buttons_update();
  1593. temp_state = MeasureTemp_2;
  1594. break;
  1595. case MeasureTemp_2:
  1596. #if HAS_TEMP_2
  1597. raw_temp_value[2] += ADC;
  1598. #endif
  1599. temp_state = PrepareTemp_3;
  1600. break;
  1601. case PrepareTemp_3:
  1602. #if HAS_TEMP_3
  1603. START_ADC(TEMP_3_PIN);
  1604. #endif
  1605. lcd_buttons_update();
  1606. temp_state = MeasureTemp_3;
  1607. break;
  1608. case MeasureTemp_3:
  1609. #if HAS_TEMP_3
  1610. raw_temp_value[3] += ADC;
  1611. #endif
  1612. temp_state = Prepare_FILWIDTH;
  1613. break;
  1614. case Prepare_FILWIDTH:
  1615. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1616. START_ADC(FILWIDTH_PIN);
  1617. #endif
  1618. lcd_buttons_update();
  1619. temp_state = Measure_FILWIDTH;
  1620. break;
  1621. case Measure_FILWIDTH:
  1622. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1623. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1624. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1625. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1626. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1627. }
  1628. #endif
  1629. temp_state = PrepareTemp_0;
  1630. temp_count++;
  1631. break;
  1632. case StartupDelay:
  1633. temp_state = PrepareTemp_0;
  1634. break;
  1635. // default:
  1636. // SERIAL_ERROR_START;
  1637. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1638. // break;
  1639. } // switch(temp_state)
  1640. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1641. temp_count = 0;
  1642. // Update the raw values if they've been read. Else we could be updating them during reading.
  1643. if (!temp_meas_ready) set_current_temp_raw();
  1644. // Filament Sensor - can be read any time since IIR filtering is used
  1645. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1646. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1647. #endif
  1648. ZERO(raw_temp_value);
  1649. raw_temp_bed_value = 0;
  1650. int constexpr temp_dir[] = {
  1651. #if ENABLED(HEATER_0_USES_MAX6675)
  1652. 0
  1653. #elif HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1654. -1
  1655. #else
  1656. 1
  1657. #endif
  1658. #if HAS_TEMP_1 && HOTENDS > 1
  1659. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1660. , -1
  1661. #else
  1662. , 1
  1663. #endif
  1664. #endif
  1665. #if HAS_TEMP_2 && HOTENDS > 2
  1666. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1667. , -1
  1668. #else
  1669. , 1
  1670. #endif
  1671. #endif
  1672. #if HAS_TEMP_3 && HOTENDS > 3
  1673. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1674. , -1
  1675. #else
  1676. , 1
  1677. #endif
  1678. #endif
  1679. };
  1680. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1681. const int tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1682. if (rawtemp > maxttemp_raw[e] * tdir && target_temperature[e] > 0.0f) max_temp_error(e);
  1683. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && target_temperature[e] > 0.0f) {
  1684. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1685. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1686. #endif
  1687. min_temp_error(e);
  1688. }
  1689. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1690. else
  1691. consecutive_low_temperature_error[e] = 0;
  1692. #endif
  1693. }
  1694. #if HAS_TEMP_BED
  1695. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1696. #define GEBED <=
  1697. #else
  1698. #define GEBED >=
  1699. #endif
  1700. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && target_temperature_bed > 0.0f) max_temp_error(-1);
  1701. if (bed_minttemp_raw GEBED current_temperature_bed_raw && target_temperature_bed > 0.0f) min_temp_error(-1);
  1702. #endif
  1703. } // temp_count >= OVERSAMPLENR
  1704. #if ENABLED(BABYSTEPPING)
  1705. LOOP_XYZ(axis) {
  1706. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1707. if (curTodo > 0) {
  1708. stepper.babystep((AxisEnum)axis,/*fwd*/true);
  1709. babystepsTodo[axis]--; //fewer to do next time
  1710. }
  1711. else if (curTodo < 0) {
  1712. stepper.babystep((AxisEnum)axis,/*fwd*/false);
  1713. babystepsTodo[axis]++; //fewer to do next time
  1714. }
  1715. }
  1716. #endif //BABYSTEPPING
  1717. #if ENABLED(PINS_DEBUGGING)
  1718. extern bool endstop_monitor_flag;
  1719. // run the endstop monitor at 15Hz
  1720. static uint8_t endstop_monitor_count = 16; // offset this check from the others
  1721. if (endstop_monitor_flag) {
  1722. endstop_monitor_count += _BV(1); // 15 Hz
  1723. endstop_monitor_count &= 0x7F;
  1724. if (!endstop_monitor_count) endstop_monitor(); // report changes in endstop status
  1725. }
  1726. #endif
  1727. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1728. extern volatile uint8_t e_hit;
  1729. if (e_hit && ENDSTOPS_ENABLED) {
  1730. endstops.update(); // call endstop update routine
  1731. e_hit--;
  1732. }
  1733. #endif
  1734. cli();
  1735. in_temp_isr = false;
  1736. SBI(TIMSK0, OCIE0B); //re-enable Temperature ISR
  1737. }