My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 68KB

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