My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

temperature.cpp 67KB

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