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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  1. /*
  2. temperature.cpp - temperature control
  3. Part of Marlin
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Marlin.h"
  17. #include "ultralcd.h"
  18. #include "temperature.h"
  19. #include "language.h"
  20. #include "Sd2PinMap.h"
  21. #if ENABLED(USE_WATCHDOG)
  22. #include "watchdog.h"
  23. #endif
  24. //===========================================================================
  25. //================================== macros =================================
  26. //===========================================================================
  27. #ifdef K1 // Defined in Configuration.h in the PID settings
  28. #define K2 (1.0-K1)
  29. #endif
  30. #if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMP)
  31. #define PID_dT ((OVERSAMPLENR * 12.0)/(F_CPU / 64.0 / 256.0))
  32. #endif
  33. //===========================================================================
  34. //============================= public variables ============================
  35. //===========================================================================
  36. int target_temperature[4] = { 0 };
  37. int target_temperature_bed = 0;
  38. int current_temperature_raw[4] = { 0 };
  39. float current_temperature[4] = { 0.0 };
  40. int current_temperature_bed_raw = 0;
  41. float current_temperature_bed = 0.0;
  42. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  43. int redundant_temperature_raw = 0;
  44. float redundant_temperature = 0.0;
  45. #endif
  46. #if ENABLED(PIDTEMPBED)
  47. float bedKp = DEFAULT_bedKp;
  48. float bedKi = (DEFAULT_bedKi* PID_dT);
  49. float bedKd = (DEFAULT_bedKd / PID_dT);
  50. #endif //PIDTEMPBED
  51. #if ENABLED(FAN_SOFT_PWM)
  52. unsigned char fanSpeedSoftPwm[FAN_COUNT];
  53. #endif
  54. unsigned char soft_pwm_bed;
  55. #if ENABLED(BABYSTEPPING)
  56. volatile int babystepsTodo[3] = { 0 };
  57. #endif
  58. #if ENABLED(FILAMENT_SENSOR)
  59. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  60. #endif
  61. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
  62. enum TRState { TRReset, TRInactive, TRFirstHeating, TRStable, TRRunaway };
  63. void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
  64. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  65. static TRState thermal_runaway_state_machine[4] = { TRReset, TRReset, TRReset, TRReset };
  66. static millis_t thermal_runaway_timer[4]; // = {0,0,0,0};
  67. #endif
  68. #if ENABLED(THERMAL_PROTECTION_BED) && TEMP_SENSOR_BED != 0
  69. static TRState thermal_runaway_bed_state_machine = TRReset;
  70. static millis_t thermal_runaway_bed_timer;
  71. #endif
  72. #endif
  73. //===========================================================================
  74. //============================ private variables ============================
  75. //===========================================================================
  76. static volatile bool temp_meas_ready = false;
  77. #if ENABLED(PIDTEMP)
  78. //static cannot be external:
  79. static float temp_iState[EXTRUDERS] = { 0 };
  80. static float temp_dState[EXTRUDERS] = { 0 };
  81. static float pTerm[EXTRUDERS];
  82. static float iTerm[EXTRUDERS];
  83. static float dTerm[EXTRUDERS];
  84. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  85. static float cTerm[EXTRUDERS];
  86. static long last_position[EXTRUDERS];
  87. static long lpq[LPQ_MAX_LEN];
  88. static int lpq_ptr = 0;
  89. #endif
  90. //int output;
  91. static float pid_error[EXTRUDERS];
  92. static float temp_iState_min[EXTRUDERS];
  93. static float temp_iState_max[EXTRUDERS];
  94. static bool pid_reset[EXTRUDERS];
  95. #endif //PIDTEMP
  96. #if ENABLED(PIDTEMPBED)
  97. //static cannot be external:
  98. static float temp_iState_bed = { 0 };
  99. static float temp_dState_bed = { 0 };
  100. static float pTerm_bed;
  101. static float iTerm_bed;
  102. static float dTerm_bed;
  103. //int output;
  104. static float pid_error_bed;
  105. static float temp_iState_min_bed;
  106. static float temp_iState_max_bed;
  107. #else //PIDTEMPBED
  108. static millis_t next_bed_check_ms;
  109. #endif //PIDTEMPBED
  110. static unsigned char soft_pwm[EXTRUDERS];
  111. #if ENABLED(FAN_SOFT_PWM)
  112. static unsigned char soft_pwm_fan[FAN_COUNT];
  113. #endif
  114. #if HAS_AUTO_FAN
  115. static millis_t next_auto_fan_check_ms;
  116. #endif
  117. #if ENABLED(PIDTEMP)
  118. #if ENABLED(PID_PARAMS_PER_EXTRUDER)
  119. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kp);
  120. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Ki) * (PID_dT));
  121. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Kd) / (PID_dT));
  122. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  123. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kc);
  124. #endif // PID_ADD_EXTRUSION_RATE
  125. #else //PID_PARAMS_PER_EXTRUDER
  126. float Kp = DEFAULT_Kp;
  127. float Ki = (DEFAULT_Ki) * (PID_dT);
  128. float Kd = (DEFAULT_Kd) / (PID_dT);
  129. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  130. float Kc = DEFAULT_Kc;
  131. #endif // PID_ADD_EXTRUSION_RATE
  132. #endif // PID_PARAMS_PER_EXTRUDER
  133. #endif //PIDTEMP
  134. // Init min and max temp with extreme values to prevent false errors during startup
  135. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  136. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  137. static int minttemp[EXTRUDERS] = { 0 };
  138. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(16383);
  139. #ifdef BED_MINTEMP
  140. static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  141. #endif
  142. #ifdef BED_MAXTEMP
  143. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  144. #endif
  145. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  146. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  147. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  148. #else
  149. static void* heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE);
  150. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN);
  151. #endif
  152. static float analog2temp(int raw, uint8_t e);
  153. static float analog2tempBed(int raw);
  154. static void updateTemperaturesFromRawValues();
  155. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  156. int watch_target_temp[EXTRUDERS] = { 0 };
  157. millis_t watch_heater_next_ms[EXTRUDERS] = { 0 };
  158. #endif
  159. #ifndef SOFT_PWM_SCALE
  160. #define SOFT_PWM_SCALE 0
  161. #endif
  162. #if ENABLED(FILAMENT_SENSOR)
  163. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  164. #endif
  165. #if ENABLED(HEATER_0_USES_MAX6675)
  166. static int read_max6675();
  167. #endif
  168. //===========================================================================
  169. //================================ Functions ================================
  170. //===========================================================================
  171. void PID_autotune(float temp, int extruder, int ncycles) {
  172. float input = 0.0;
  173. int cycles = 0;
  174. bool heating = true;
  175. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  176. long t_high = 0, t_low = 0;
  177. long bias, d;
  178. float Ku, Tu;
  179. float Kp = 0, Ki = 0, Kd = 0;
  180. float max = 0, min = 10000;
  181. #if HAS_AUTO_FAN
  182. millis_t next_auto_fan_check_ms = temp_ms + 2500;
  183. #endif
  184. if (extruder >= EXTRUDERS
  185. #if !HAS_TEMP_BED
  186. || extruder < 0
  187. #endif
  188. ) {
  189. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  190. return;
  191. }
  192. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  193. disable_all_heaters(); // switch off all heaters.
  194. if (extruder < 0)
  195. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  196. else
  197. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  198. // PID Tuning loop
  199. for (;;) {
  200. millis_t ms = millis();
  201. if (temp_meas_ready) { // temp sample ready
  202. updateTemperaturesFromRawValues();
  203. input = (extruder < 0) ? current_temperature_bed : current_temperature[extruder];
  204. max = max(max, input);
  205. min = min(min, input);
  206. #if HAS_AUTO_FAN
  207. if (ms > next_auto_fan_check_ms) {
  208. checkExtruderAutoFans();
  209. next_auto_fan_check_ms = ms + 2500;
  210. }
  211. #endif
  212. if (heating && input > temp) {
  213. if (ms > t2 + 5000) {
  214. heating = false;
  215. if (extruder < 0)
  216. soft_pwm_bed = (bias - d) >> 1;
  217. else
  218. soft_pwm[extruder] = (bias - d) >> 1;
  219. t1 = ms;
  220. t_high = t1 - t2;
  221. max = temp;
  222. }
  223. }
  224. if (!heating && input < temp) {
  225. if (ms > t1 + 5000) {
  226. heating = true;
  227. t2 = ms;
  228. t_low = t2 - t1;
  229. if (cycles > 0) {
  230. long max_pow = extruder < 0 ? MAX_BED_POWER : PID_MAX;
  231. bias += (d * (t_high - t_low)) / (t_low + t_high);
  232. bias = constrain(bias, 20, max_pow - 20);
  233. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  234. SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
  235. SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
  236. SERIAL_PROTOCOLPGM(MSG_T_MIN); SERIAL_PROTOCOL(min);
  237. SERIAL_PROTOCOLPGM(MSG_T_MAX); SERIAL_PROTOCOLLN(max);
  238. if (cycles > 2) {
  239. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  240. Tu = ((float)(t_low + t_high) / 1000.0);
  241. SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
  242. SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
  243. Kp = 0.6 * Ku;
  244. Ki = 2 * Kp / Tu;
  245. Kd = Kp * Tu / 8;
  246. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  247. SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(Kp);
  248. SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(Ki);
  249. SERIAL_PROTOCOLPGM(MSG_KD); SERIAL_PROTOCOLLN(Kd);
  250. /*
  251. Kp = 0.33*Ku;
  252. Ki = Kp/Tu;
  253. Kd = Kp*Tu/3;
  254. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  255. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  256. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  257. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  258. Kp = 0.2*Ku;
  259. Ki = 2*Kp/Tu;
  260. Kd = Kp*Tu/3;
  261. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  262. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  263. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  264. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  265. */
  266. }
  267. }
  268. if (extruder < 0)
  269. soft_pwm_bed = (bias + d) >> 1;
  270. else
  271. soft_pwm[extruder] = (bias + d) >> 1;
  272. cycles++;
  273. min = temp;
  274. }
  275. }
  276. }
  277. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  278. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  279. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  280. return;
  281. }
  282. // Every 2 seconds...
  283. if (ms > temp_ms + 2000) {
  284. #if HAS_TEMP_0 || HAS_TEMP_BED || ENABLED(HEATER_0_USES_MAX6675)
  285. print_heaterstates();
  286. SERIAL_EOL;
  287. #endif
  288. temp_ms = ms;
  289. } // every 2 seconds
  290. // Over 2 minutes?
  291. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  292. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  293. return;
  294. }
  295. if (cycles > ncycles) {
  296. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  297. const char* estring = extruder < 0 ? "bed" : "";
  298. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kp "); SERIAL_PROTOCOLLN(Kp);
  299. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Ki "); SERIAL_PROTOCOLLN(Ki);
  300. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kd "); SERIAL_PROTOCOLLN(Kd);
  301. return;
  302. }
  303. lcd_update();
  304. }
  305. }
  306. void updatePID() {
  307. #if ENABLED(PIDTEMP)
  308. for (int e = 0; e < EXTRUDERS; e++) {
  309. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki,e);
  310. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  311. last_position[e] = 0;
  312. #endif
  313. }
  314. #endif
  315. #if ENABLED(PIDTEMPBED)
  316. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  317. #endif
  318. }
  319. int getHeaterPower(int heater) {
  320. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  321. }
  322. #if HAS_AUTO_FAN
  323. void setExtruderAutoFanState(int pin, bool state) {
  324. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  325. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  326. digitalWrite(pin, newFanSpeed);
  327. analogWrite(pin, newFanSpeed);
  328. }
  329. void checkExtruderAutoFans() {
  330. uint8_t fanState = 0;
  331. // which fan pins need to be turned on?
  332. #if HAS_AUTO_FAN_0
  333. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  334. fanState |= 1;
  335. #endif
  336. #if HAS_AUTO_FAN_1
  337. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
  338. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  339. fanState |= 1;
  340. else
  341. fanState |= 2;
  342. }
  343. #endif
  344. #if HAS_AUTO_FAN_2
  345. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
  346. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  347. fanState |= 1;
  348. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  349. fanState |= 2;
  350. else
  351. fanState |= 4;
  352. }
  353. #endif
  354. #if HAS_AUTO_FAN_3
  355. if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
  356. if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  357. fanState |= 1;
  358. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  359. fanState |= 2;
  360. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
  361. fanState |= 4;
  362. else
  363. fanState |= 8;
  364. }
  365. #endif
  366. // update extruder auto fan states
  367. #if HAS_AUTO_FAN_0
  368. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  369. #endif
  370. #if HAS_AUTO_FAN_1
  371. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  372. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  373. #endif
  374. #if HAS_AUTO_FAN_2
  375. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  376. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  377. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  378. #endif
  379. #if HAS_AUTO_FAN_3
  380. if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  381. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN
  382. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  383. setExtruderAutoFanState(EXTRUDER_3_AUTO_FAN_PIN, (fanState & 8) != 0);
  384. #endif
  385. }
  386. #endif // HAS_AUTO_FAN
  387. //
  388. // Temperature Error Handlers
  389. //
  390. inline void _temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  391. static bool killed = false;
  392. if (IsRunning()) {
  393. SERIAL_ERROR_START;
  394. serialprintPGM(serial_msg);
  395. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  396. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  397. }
  398. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  399. if (!killed) {
  400. Running = false;
  401. killed = true;
  402. kill(lcd_msg);
  403. }
  404. else
  405. disable_all_heaters(); // paranoia
  406. #endif
  407. }
  408. void max_temp_error(uint8_t e) {
  409. _temp_error(e, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  410. }
  411. void min_temp_error(uint8_t e) {
  412. _temp_error(e, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  413. }
  414. float get_pid_output(int e) {
  415. float pid_output;
  416. #if ENABLED(PIDTEMP)
  417. #if DISABLED(PID_OPENLOOP)
  418. pid_error[e] = target_temperature[e] - current_temperature[e];
  419. dTerm[e] = K2 * PID_PARAM(Kd, e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
  420. temp_dState[e] = current_temperature[e];
  421. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  422. pid_output = BANG_MAX;
  423. pid_reset[e] = true;
  424. }
  425. else if (pid_error[e] < -(PID_FUNCTIONAL_RANGE) || target_temperature[e] == 0) {
  426. pid_output = 0;
  427. pid_reset[e] = true;
  428. }
  429. else {
  430. if (pid_reset[e]) {
  431. temp_iState[e] = 0.0;
  432. pid_reset[e] = false;
  433. }
  434. pTerm[e] = PID_PARAM(Kp, e) * pid_error[e];
  435. temp_iState[e] += pid_error[e];
  436. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  437. iTerm[e] = PID_PARAM(Ki, e) * temp_iState[e];
  438. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  439. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  440. cTerm[e] = 0;
  441. if (e == active_extruder) {
  442. long e_position = st_get_position(E_AXIS);
  443. if (e_position > last_position[e]) {
  444. lpq[lpq_ptr++] = e_position - last_position[e];
  445. last_position[e] = e_position;
  446. }
  447. else {
  448. lpq[lpq_ptr++] = 0;
  449. }
  450. if (lpq_ptr >= lpq_len) lpq_ptr = 0;
  451. cTerm[e] = (lpq[lpq_ptr] / axis_steps_per_unit[E_AXIS]) * PID_PARAM(Kc, e);
  452. pid_output += cTerm[e];
  453. }
  454. #endif //PID_ADD_EXTRUSION_RATE
  455. if (pid_output > PID_MAX) {
  456. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  457. pid_output = PID_MAX;
  458. }
  459. else if (pid_output < 0) {
  460. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  461. pid_output = 0;
  462. }
  463. }
  464. #else
  465. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  466. #endif //PID_OPENLOOP
  467. #if ENABLED(PID_DEBUG)
  468. SERIAL_ECHO_START;
  469. SERIAL_ECHOPAIR(MSG_PID_DEBUG, e);
  470. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[e]);
  471. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  472. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[e]);
  473. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[e]);
  474. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[e]);
  475. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  476. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[e]);
  477. #endif
  478. SERIAL_EOL;
  479. #endif //PID_DEBUG
  480. #else /* PID off */
  481. pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
  482. #endif
  483. return pid_output;
  484. }
  485. #if ENABLED(PIDTEMPBED)
  486. float get_pid_output_bed() {
  487. float pid_output;
  488. #if DISABLED(PID_OPENLOOP)
  489. pid_error_bed = target_temperature_bed - current_temperature_bed;
  490. pTerm_bed = bedKp * pid_error_bed;
  491. temp_iState_bed += pid_error_bed;
  492. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  493. iTerm_bed = bedKi * temp_iState_bed;
  494. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  495. temp_dState_bed = current_temperature_bed;
  496. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  497. if (pid_output > MAX_BED_POWER) {
  498. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  499. pid_output = MAX_BED_POWER;
  500. }
  501. else if (pid_output < 0) {
  502. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  503. pid_output = 0;
  504. }
  505. #else
  506. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  507. #endif // PID_OPENLOOP
  508. #if ENABLED(PID_BED_DEBUG)
  509. SERIAL_ECHO_START;
  510. SERIAL_ECHO(" PID_BED_DEBUG ");
  511. SERIAL_ECHO(": Input ");
  512. SERIAL_ECHO(current_temperature_bed);
  513. SERIAL_ECHO(" Output ");
  514. SERIAL_ECHO(pid_output);
  515. SERIAL_ECHO(" pTerm ");
  516. SERIAL_ECHO(pTerm_bed);
  517. SERIAL_ECHO(" iTerm ");
  518. SERIAL_ECHO(iTerm_bed);
  519. SERIAL_ECHO(" dTerm ");
  520. SERIAL_ECHOLN(dTerm_bed);
  521. #endif //PID_BED_DEBUG
  522. return pid_output;
  523. }
  524. #endif
  525. /**
  526. * Manage heating activities for extruder hot-ends and a heated bed
  527. * - Acquire updated temperature readings
  528. * - Invoke thermal runaway protection
  529. * - Manage extruder auto-fan
  530. * - Apply filament width to the extrusion rate (may move)
  531. * - Update the heated bed PID output value
  532. */
  533. void manage_heater() {
  534. if (!temp_meas_ready) return;
  535. updateTemperaturesFromRawValues();
  536. #if ENABLED(HEATER_0_USES_MAX6675)
  537. float ct = current_temperature[0];
  538. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  539. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  540. #endif
  541. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  542. millis_t ms = millis();
  543. #endif
  544. // Loop through all extruders
  545. for (int e = 0; e < EXTRUDERS; e++) {
  546. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  547. 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);
  548. #endif
  549. float pid_output = get_pid_output(e);
  550. // Check if temperature is within the correct range
  551. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  552. // Check if the temperature is failing to increase
  553. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  554. // Is it time to check this extruder's heater?
  555. if (watch_heater_next_ms[e] && ms > watch_heater_next_ms[e]) {
  556. // Has it failed to increase enough?
  557. if (degHotend(e) < watch_target_temp[e]) {
  558. // Stop!
  559. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  560. }
  561. else {
  562. // Start again if the target is still far off
  563. start_watching_heater(e);
  564. }
  565. }
  566. #endif // THERMAL_PROTECTION_HOTENDS
  567. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  568. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  569. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  570. }
  571. #endif
  572. } // Extruders Loop
  573. #if HAS_AUTO_FAN
  574. if (ms > next_auto_fan_check_ms) { // only need to check fan state very infrequently
  575. checkExtruderAutoFans();
  576. next_auto_fan_check_ms = ms + 2500;
  577. }
  578. #endif
  579. // Control the extruder rate based on the width sensor
  580. #if ENABLED(FILAMENT_SENSOR)
  581. if (filament_sensor) {
  582. meas_shift_index = delay_index1 - meas_delay_cm;
  583. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  584. // Get the delayed info and add 100 to reconstitute to a percent of
  585. // the nominal filament diameter then square it to get an area
  586. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  587. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  588. NOLESS(vm, 0.01);
  589. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  590. }
  591. #endif //FILAMENT_SENSOR
  592. #if DISABLED(PIDTEMPBED)
  593. if (ms < next_bed_check_ms) return;
  594. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  595. #endif
  596. #if TEMP_SENSOR_BED != 0
  597. #if ENABLED(THERMAL_PROTECTION_BED)
  598. 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);
  599. #endif
  600. #if ENABLED(PIDTEMPBED)
  601. float pid_output = get_pid_output_bed();
  602. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  603. #elif ENABLED(BED_LIMIT_SWITCHING)
  604. // Check if temperature is within the correct band
  605. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  606. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  607. soft_pwm_bed = 0;
  608. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  609. soft_pwm_bed = MAX_BED_POWER >> 1;
  610. }
  611. else {
  612. soft_pwm_bed = 0;
  613. WRITE_HEATER_BED(LOW);
  614. }
  615. #else // BED_LIMIT_SWITCHING
  616. // Check if temperature is within the correct range
  617. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  618. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  619. }
  620. else {
  621. soft_pwm_bed = 0;
  622. WRITE_HEATER_BED(LOW);
  623. }
  624. #endif
  625. #endif //TEMP_SENSOR_BED != 0
  626. }
  627. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  628. // Derived from RepRap FiveD extruder::getTemperature()
  629. // For hot end temperature measurement.
  630. static float analog2temp(int raw, uint8_t e) {
  631. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  632. if (e > EXTRUDERS)
  633. #else
  634. if (e >= EXTRUDERS)
  635. #endif
  636. {
  637. SERIAL_ERROR_START;
  638. SERIAL_ERROR((int)e);
  639. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  640. kill(PSTR(MSG_KILLED));
  641. return 0.0;
  642. }
  643. #if ENABLED(HEATER_0_USES_MAX6675)
  644. if (e == 0) return 0.25 * raw;
  645. #endif
  646. if (heater_ttbl_map[e] != NULL) {
  647. float celsius = 0;
  648. uint8_t i;
  649. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  650. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  651. if (PGM_RD_W((*tt)[i][0]) > raw) {
  652. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  653. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  654. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  655. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  656. break;
  657. }
  658. }
  659. // Overflow: Set to last value in the table
  660. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  661. return celsius;
  662. }
  663. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  664. }
  665. // Derived from RepRap FiveD extruder::getTemperature()
  666. // For bed temperature measurement.
  667. static float analog2tempBed(int raw) {
  668. #if ENABLED(BED_USES_THERMISTOR)
  669. float celsius = 0;
  670. byte i;
  671. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  672. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  673. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  674. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  675. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  676. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  677. break;
  678. }
  679. }
  680. // Overflow: Set to last value in the table
  681. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  682. return celsius;
  683. #elif defined(BED_USES_AD595)
  684. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  685. #else
  686. UNUSED(raw);
  687. return 0;
  688. #endif
  689. }
  690. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  691. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  692. static void updateTemperaturesFromRawValues() {
  693. #if ENABLED(HEATER_0_USES_MAX6675)
  694. current_temperature_raw[0] = read_max6675();
  695. #endif
  696. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  697. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  698. }
  699. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  700. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  701. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  702. #endif
  703. #if HAS_FILAMENT_SENSOR
  704. filament_width_meas = analog2widthFil();
  705. #endif
  706. #if ENABLED(USE_WATCHDOG)
  707. // Reset the watchdog after we know we have a temperature measurement.
  708. watchdog_reset();
  709. #endif
  710. CRITICAL_SECTION_START;
  711. temp_meas_ready = false;
  712. CRITICAL_SECTION_END;
  713. }
  714. #if ENABLED(FILAMENT_SENSOR)
  715. // Convert raw Filament Width to millimeters
  716. float analog2widthFil() {
  717. return current_raw_filwidth / 16383.0 * 5.0;
  718. //return current_raw_filwidth;
  719. }
  720. // Convert raw Filament Width to a ratio
  721. int widthFil_to_size_ratio() {
  722. float temp = filament_width_meas;
  723. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  724. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  725. return filament_width_nominal / temp * 100;
  726. }
  727. #endif
  728. /**
  729. * Initialize the temperature manager
  730. * The manager is implemented by periodic calls to manage_heater()
  731. */
  732. void tp_init() {
  733. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  734. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  735. MCUCR = _BV(JTD);
  736. MCUCR = _BV(JTD);
  737. #endif
  738. // Finish init of mult extruder arrays
  739. for (int e = 0; e < EXTRUDERS; e++) {
  740. // populate with the first value
  741. maxttemp[e] = maxttemp[0];
  742. #if ENABLED(PIDTEMP)
  743. temp_iState_min[e] = 0.0;
  744. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  745. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  746. last_position[e] = 0;
  747. #endif
  748. #endif //PIDTEMP
  749. #if ENABLED(PIDTEMPBED)
  750. temp_iState_min_bed = 0.0;
  751. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  752. #endif //PIDTEMPBED
  753. }
  754. #if HAS_HEATER_0
  755. SET_OUTPUT(HEATER_0_PIN);
  756. #endif
  757. #if HAS_HEATER_1
  758. SET_OUTPUT(HEATER_1_PIN);
  759. #endif
  760. #if HAS_HEATER_2
  761. SET_OUTPUT(HEATER_2_PIN);
  762. #endif
  763. #if HAS_HEATER_3
  764. SET_OUTPUT(HEATER_3_PIN);
  765. #endif
  766. #if HAS_HEATER_BED
  767. SET_OUTPUT(HEATER_BED_PIN);
  768. #endif
  769. #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM)
  770. #if HAS_FAN0
  771. SET_OUTPUT(FAN_PIN);
  772. #if ENABLED(FAST_PWM_FAN)
  773. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  774. #endif
  775. #if ENABLED(FAN_SOFT_PWM)
  776. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  777. #endif
  778. #endif
  779. #if HAS_FAN1
  780. SET_OUTPUT(FAN1_PIN);
  781. #if ENABLED(FAST_PWM_FAN)
  782. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  783. #endif
  784. #if ENABLED(FAN_SOFT_PWM)
  785. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  786. #endif
  787. #endif
  788. #if HAS_FAN2
  789. SET_OUTPUT(FAN2_PIN);
  790. #if ENABLED(FAST_PWM_FAN)
  791. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  792. #endif
  793. #if ENABLED(FAN_SOFT_PWM)
  794. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  795. #endif
  796. #endif
  797. #endif // FAST_PWM_FAN || FAN_SOFT_PWM
  798. #if ENABLED(HEATER_0_USES_MAX6675)
  799. #if DISABLED(SDSUPPORT)
  800. OUT_WRITE(SCK_PIN, LOW);
  801. OUT_WRITE(MOSI_PIN, HIGH);
  802. OUT_WRITE(MISO_PIN, HIGH);
  803. #else
  804. pinMode(SS_PIN, OUTPUT);
  805. digitalWrite(SS_PIN, HIGH);
  806. #endif
  807. OUT_WRITE(MAX6675_SS, HIGH);
  808. #endif //HEATER_0_USES_MAX6675
  809. #ifdef DIDR2
  810. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  811. #else
  812. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  813. #endif
  814. // Set analog inputs
  815. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  816. DIDR0 = 0;
  817. #ifdef DIDR2
  818. DIDR2 = 0;
  819. #endif
  820. #if HAS_TEMP_0
  821. ANALOG_SELECT(TEMP_0_PIN);
  822. #endif
  823. #if HAS_TEMP_1
  824. ANALOG_SELECT(TEMP_1_PIN);
  825. #endif
  826. #if HAS_TEMP_2
  827. ANALOG_SELECT(TEMP_2_PIN);
  828. #endif
  829. #if HAS_TEMP_3
  830. ANALOG_SELECT(TEMP_3_PIN);
  831. #endif
  832. #if HAS_TEMP_BED
  833. ANALOG_SELECT(TEMP_BED_PIN);
  834. #endif
  835. #if HAS_FILAMENT_SENSOR
  836. ANALOG_SELECT(FILWIDTH_PIN);
  837. #endif
  838. #if HAS_AUTO_FAN_0
  839. pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT);
  840. #endif
  841. #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  842. pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT);
  843. #endif
  844. #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  845. pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT);
  846. #endif
  847. #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  848. pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT);
  849. #endif
  850. // Use timer0 for temperature measurement
  851. // Interleave temperature interrupt with millies interrupt
  852. OCR0B = 128;
  853. SBI(TIMSK0, OCIE0B);
  854. // Wait for temperature measurement to settle
  855. delay(250);
  856. #define TEMP_MIN_ROUTINE(NR) \
  857. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  858. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  859. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  860. minttemp_raw[NR] += OVERSAMPLENR; \
  861. else \
  862. minttemp_raw[NR] -= OVERSAMPLENR; \
  863. }
  864. #define TEMP_MAX_ROUTINE(NR) \
  865. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  866. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  867. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  868. maxttemp_raw[NR] -= OVERSAMPLENR; \
  869. else \
  870. maxttemp_raw[NR] += OVERSAMPLENR; \
  871. }
  872. #ifdef HEATER_0_MINTEMP
  873. TEMP_MIN_ROUTINE(0);
  874. #endif
  875. #ifdef HEATER_0_MAXTEMP
  876. TEMP_MAX_ROUTINE(0);
  877. #endif
  878. #if EXTRUDERS > 1
  879. #ifdef HEATER_1_MINTEMP
  880. TEMP_MIN_ROUTINE(1);
  881. #endif
  882. #ifdef HEATER_1_MAXTEMP
  883. TEMP_MAX_ROUTINE(1);
  884. #endif
  885. #if EXTRUDERS > 2
  886. #ifdef HEATER_2_MINTEMP
  887. TEMP_MIN_ROUTINE(2);
  888. #endif
  889. #ifdef HEATER_2_MAXTEMP
  890. TEMP_MAX_ROUTINE(2);
  891. #endif
  892. #if EXTRUDERS > 3
  893. #ifdef HEATER_3_MINTEMP
  894. TEMP_MIN_ROUTINE(3);
  895. #endif
  896. #ifdef HEATER_3_MAXTEMP
  897. TEMP_MAX_ROUTINE(3);
  898. #endif
  899. #endif // EXTRUDERS > 3
  900. #endif // EXTRUDERS > 2
  901. #endif // EXTRUDERS > 1
  902. #ifdef BED_MINTEMP
  903. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  904. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  905. bed_minttemp_raw += OVERSAMPLENR;
  906. #else
  907. bed_minttemp_raw -= OVERSAMPLENR;
  908. #endif
  909. }
  910. #endif //BED_MINTEMP
  911. #ifdef BED_MAXTEMP
  912. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  913. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  914. bed_maxttemp_raw -= OVERSAMPLENR;
  915. #else
  916. bed_maxttemp_raw += OVERSAMPLENR;
  917. #endif
  918. }
  919. #endif //BED_MAXTEMP
  920. }
  921. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  922. /**
  923. * Start Heating Sanity Check for hotends that are below
  924. * their target temperature by a configurable margin.
  925. * This is called when the temperature is set. (M104, M109)
  926. */
  927. void start_watching_heater(int e) {
  928. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  929. watch_target_temp[e] = degHotend(e) + WATCH_TEMP_INCREASE;
  930. watch_heater_next_ms[e] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  931. }
  932. else
  933. watch_heater_next_ms[e] = 0;
  934. }
  935. #endif
  936. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
  937. void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  938. static float tr_target_temperature[EXTRUDERS + 1] = { 0.0 };
  939. /*
  940. SERIAL_ECHO_START;
  941. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  942. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHOPGM(heater_id);
  943. SERIAL_ECHOPGM(" ; State:");
  944. SERIAL_ECHOPGM(*state);
  945. SERIAL_ECHOPGM(" ; Timer:");
  946. SERIAL_ECHOPGM(*timer);
  947. SERIAL_ECHOPGM(" ; Temperature:");
  948. SERIAL_ECHOPGM(temperature);
  949. SERIAL_ECHOPGM(" ; Target Temp:");
  950. SERIAL_ECHOPGM(target_temperature);
  951. SERIAL_EOL;
  952. */
  953. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  954. // If the target temperature changes, restart
  955. if (tr_target_temperature[heater_index] != target_temperature)
  956. *state = TRReset;
  957. switch (*state) {
  958. case TRReset:
  959. *timer = 0;
  960. *state = TRInactive;
  961. // Inactive state waits for a target temperature to be set
  962. case TRInactive:
  963. if (target_temperature > 0) {
  964. tr_target_temperature[heater_index] = target_temperature;
  965. *state = TRFirstHeating;
  966. }
  967. break;
  968. // When first heating, wait for the temperature to be reached then go to Stable state
  969. case TRFirstHeating:
  970. if (temperature >= tr_target_temperature[heater_index]) *state = TRStable;
  971. break;
  972. // While the temperature is stable watch for a bad temperature
  973. case TRStable:
  974. // If the temperature is over the target (-hysteresis) restart the timer
  975. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
  976. *timer = millis();
  977. // If the timer goes too long without a reset, trigger shutdown
  978. else if (millis() > *timer + period_seconds * 1000UL)
  979. *state = TRRunaway;
  980. break;
  981. case TRRunaway:
  982. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  983. }
  984. }
  985. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  986. void disable_all_heaters() {
  987. for (int i = 0; i < EXTRUDERS; i++) setTargetHotend(0, i);
  988. setTargetBed(0);
  989. // If all heaters go down then for sure our print job has stopped
  990. print_job_stop(true);
  991. #define DISABLE_HEATER(NR) { \
  992. setTargetHotend(NR, 0); \
  993. soft_pwm[NR] = 0; \
  994. WRITE_HEATER_ ## NR (LOW); \
  995. }
  996. #if HAS_TEMP_0 || ENABLED(HEATER_0_USES_MAX6675)
  997. setTargetHotend(0, 0);
  998. soft_pwm[0] = 0;
  999. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  1000. #endif
  1001. #if EXTRUDERS > 1 && HAS_TEMP_1
  1002. DISABLE_HEATER(1);
  1003. #endif
  1004. #if EXTRUDERS > 2 && HAS_TEMP_2
  1005. DISABLE_HEATER(2);
  1006. #endif
  1007. #if EXTRUDERS > 3 && HAS_TEMP_3
  1008. DISABLE_HEATER(3);
  1009. #endif
  1010. #if HAS_TEMP_BED
  1011. target_temperature_bed = 0;
  1012. soft_pwm_bed = 0;
  1013. #if HAS_HEATER_BED
  1014. WRITE_HEATER_BED(LOW);
  1015. #endif
  1016. #endif
  1017. }
  1018. #if ENABLED(HEATER_0_USES_MAX6675)
  1019. #define MAX6675_HEAT_INTERVAL 250u
  1020. #if ENABLED(MAX6675_IS_MAX31855)
  1021. unsigned long max6675_temp = 2000;
  1022. #define MAX6675_READ_BYTES 4
  1023. #define MAX6675_ERROR_MASK 7
  1024. #define MAX6675_DISCARD_BITS 18
  1025. #else
  1026. unsigned int max6675_temp = 2000;
  1027. #define MAX6675_READ_BYTES 2
  1028. #define MAX6675_ERROR_MASK 4
  1029. #define MAX6675_DISCARD_BITS 3
  1030. #endif
  1031. static millis_t next_max6675_ms = 0;
  1032. static int read_max6675() {
  1033. millis_t ms = millis();
  1034. if (ms < next_max6675_ms) return (int)max6675_temp;
  1035. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1036. CBI(
  1037. #ifdef PRR
  1038. PRR
  1039. #elif defined(PRR0)
  1040. PRR0
  1041. #endif
  1042. , PRSPI);
  1043. SPCR = _BV(MSTR) | _BV(SPE) | _BV(SPR0);
  1044. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1045. // ensure 100ns delay - a bit extra is fine
  1046. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1047. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1048. // Read a big-endian temperature value
  1049. max6675_temp = 0;
  1050. for (uint8_t i = MAX6675_READ_BYTES; i--;) {
  1051. SPDR = 0;
  1052. for (;!TEST(SPSR, SPIF););
  1053. max6675_temp |= SPDR;
  1054. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1055. }
  1056. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1057. if (max6675_temp & MAX6675_ERROR_MASK)
  1058. max6675_temp = 4000; // thermocouple open
  1059. else
  1060. max6675_temp >>= MAX6675_DISCARD_BITS;
  1061. return (int)max6675_temp;
  1062. }
  1063. #endif //HEATER_0_USES_MAX6675
  1064. /**
  1065. * Stages in the ISR loop
  1066. */
  1067. enum TempState {
  1068. PrepareTemp_0,
  1069. MeasureTemp_0,
  1070. PrepareTemp_BED,
  1071. MeasureTemp_BED,
  1072. PrepareTemp_1,
  1073. MeasureTemp_1,
  1074. PrepareTemp_2,
  1075. MeasureTemp_2,
  1076. PrepareTemp_3,
  1077. MeasureTemp_3,
  1078. Prepare_FILWIDTH,
  1079. Measure_FILWIDTH,
  1080. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1081. };
  1082. static unsigned long raw_temp_value[4] = { 0 };
  1083. static unsigned long raw_temp_bed_value = 0;
  1084. static void set_current_temp_raw() {
  1085. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1086. current_temperature_raw[0] = raw_temp_value[0];
  1087. #endif
  1088. #if HAS_TEMP_1
  1089. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1090. redundant_temperature_raw = raw_temp_value[1];
  1091. #else
  1092. current_temperature_raw[1] = raw_temp_value[1];
  1093. #endif
  1094. #if HAS_TEMP_2
  1095. current_temperature_raw[2] = raw_temp_value[2];
  1096. #if HAS_TEMP_3
  1097. current_temperature_raw[3] = raw_temp_value[3];
  1098. #endif
  1099. #endif
  1100. #endif
  1101. current_temperature_bed_raw = raw_temp_bed_value;
  1102. temp_meas_ready = true;
  1103. }
  1104. /**
  1105. * Timer 0 is shared with millies
  1106. * - Manage PWM to all the heaters and fan
  1107. * - Update the raw temperature values
  1108. * - Check new temperature values for MIN/MAX errors
  1109. * - Step the babysteps value for each axis towards 0
  1110. */
  1111. ISR(TIMER0_COMPB_vect) {
  1112. static unsigned char temp_count = 0;
  1113. static TempState temp_state = StartupDelay;
  1114. static unsigned char pwm_count = _BV(SOFT_PWM_SCALE);
  1115. // Static members for each heater
  1116. #if ENABLED(SLOW_PWM_HEATERS)
  1117. static unsigned char slow_pwm_count = 0;
  1118. #define ISR_STATICS(n) \
  1119. static unsigned char soft_pwm_ ## n; \
  1120. static unsigned char state_heater_ ## n = 0; \
  1121. static unsigned char state_timer_heater_ ## n = 0
  1122. #else
  1123. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1124. #endif
  1125. // Statics per heater
  1126. ISR_STATICS(0);
  1127. #if (EXTRUDERS > 1) || ENABLED(HEATERS_PARALLEL)
  1128. ISR_STATICS(1);
  1129. #if EXTRUDERS > 2
  1130. ISR_STATICS(2);
  1131. #if EXTRUDERS > 3
  1132. ISR_STATICS(3);
  1133. #endif
  1134. #endif
  1135. #endif
  1136. #if HAS_HEATER_BED
  1137. ISR_STATICS(BED);
  1138. #endif
  1139. #if HAS_FILAMENT_SENSOR
  1140. static unsigned long raw_filwidth_value = 0;
  1141. #endif
  1142. #if DISABLED(SLOW_PWM_HEATERS)
  1143. /**
  1144. * standard PWM modulation
  1145. */
  1146. if (pwm_count == 0) {
  1147. soft_pwm_0 = soft_pwm[0];
  1148. if (soft_pwm_0 > 0) {
  1149. WRITE_HEATER_0(1);
  1150. }
  1151. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1152. #if EXTRUDERS > 1
  1153. soft_pwm_1 = soft_pwm[1];
  1154. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1155. #if EXTRUDERS > 2
  1156. soft_pwm_2 = soft_pwm[2];
  1157. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1158. #if EXTRUDERS > 3
  1159. soft_pwm_3 = soft_pwm[3];
  1160. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1161. #endif
  1162. #endif
  1163. #endif
  1164. #if HAS_HEATER_BED
  1165. soft_pwm_BED = soft_pwm_bed;
  1166. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1167. #endif
  1168. #if ENABLED(FAN_SOFT_PWM)
  1169. #if HAS_FAN0
  1170. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1171. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1172. #endif
  1173. #if HAS_FAN1
  1174. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1175. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1176. #endif
  1177. #if HAS_FAN2
  1178. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1179. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1180. #endif
  1181. #endif
  1182. }
  1183. if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
  1184. #if EXTRUDERS > 1
  1185. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1186. #if EXTRUDERS > 2
  1187. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1188. #if EXTRUDERS > 3
  1189. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1190. #endif
  1191. #endif
  1192. #endif
  1193. #if HAS_HEATER_BED
  1194. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1195. #endif
  1196. #if ENABLED(FAN_SOFT_PWM)
  1197. #if HAS_FAN0
  1198. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1199. #endif
  1200. #if HAS_FAN1
  1201. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1202. #endif
  1203. #if HAS_FAN2
  1204. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1205. #endif
  1206. #endif
  1207. pwm_count += _BV(SOFT_PWM_SCALE);
  1208. pwm_count &= 0x7f;
  1209. #else // SLOW_PWM_HEATERS
  1210. /*
  1211. * SLOW PWM HEATERS
  1212. *
  1213. * for heaters drived by relay
  1214. */
  1215. #ifndef MIN_STATE_TIME
  1216. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1217. #endif
  1218. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1219. #define _SLOW_PWM_ROUTINE(NR, src) \
  1220. soft_pwm_ ## NR = src; \
  1221. if (soft_pwm_ ## NR > 0) { \
  1222. if (state_timer_heater_ ## NR == 0) { \
  1223. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1224. state_heater_ ## NR = 1; \
  1225. WRITE_HEATER_ ## NR(1); \
  1226. } \
  1227. } \
  1228. else { \
  1229. if (state_timer_heater_ ## NR == 0) { \
  1230. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1231. state_heater_ ## NR = 0; \
  1232. WRITE_HEATER_ ## NR(0); \
  1233. } \
  1234. }
  1235. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1236. #define PWM_OFF_ROUTINE(NR) \
  1237. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1238. if (state_timer_heater_ ## NR == 0) { \
  1239. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1240. state_heater_ ## NR = 0; \
  1241. WRITE_HEATER_ ## NR (0); \
  1242. } \
  1243. }
  1244. if (slow_pwm_count == 0) {
  1245. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1246. #if EXTRUDERS > 1
  1247. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1248. #if EXTRUDERS > 2
  1249. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1250. #if EXTRUDERS > 3
  1251. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1252. #endif
  1253. #endif
  1254. #endif
  1255. #if HAS_HEATER_BED
  1256. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1257. #endif
  1258. } // slow_pwm_count == 0
  1259. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1260. #if EXTRUDERS > 1
  1261. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1262. #if EXTRUDERS > 2
  1263. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1264. #if EXTRUDERS > 3
  1265. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1266. #endif
  1267. #endif
  1268. #endif
  1269. #if HAS_HEATER_BED
  1270. PWM_OFF_ROUTINE(BED); // BED
  1271. #endif
  1272. #if ENABLED(FAN_SOFT_PWM)
  1273. if (pwm_count == 0) {
  1274. #if HAS_FAN0
  1275. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1276. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1277. #endif
  1278. #if HAS_FAN1
  1279. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1280. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1281. #endif
  1282. #if HAS_FAN2
  1283. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1284. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1285. #endif
  1286. }
  1287. #if HAS_FAN0
  1288. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1289. #endif
  1290. #if HAS_FAN1
  1291. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1292. #endif
  1293. #if HAS_FAN2
  1294. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1295. #endif
  1296. #endif //FAN_SOFT_PWM
  1297. pwm_count += _BV(SOFT_PWM_SCALE);
  1298. pwm_count &= 0x7f;
  1299. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1300. if ((pwm_count % 64) == 0) {
  1301. slow_pwm_count++;
  1302. slow_pwm_count &= 0x7f;
  1303. // EXTRUDER 0
  1304. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1305. #if EXTRUDERS > 1 // EXTRUDER 1
  1306. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1307. #if EXTRUDERS > 2 // EXTRUDER 2
  1308. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1309. #if EXTRUDERS > 3 // EXTRUDER 3
  1310. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1311. #endif
  1312. #endif
  1313. #endif
  1314. #if HAS_HEATER_BED
  1315. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1316. #endif
  1317. } // (pwm_count % 64) == 0
  1318. #endif // SLOW_PWM_HEATERS
  1319. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1320. #ifdef MUX5
  1321. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1322. #else
  1323. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1324. #endif
  1325. // Prepare or measure a sensor, each one every 12th frame
  1326. switch (temp_state) {
  1327. case PrepareTemp_0:
  1328. #if HAS_TEMP_0
  1329. START_ADC(TEMP_0_PIN);
  1330. #endif
  1331. lcd_buttons_update();
  1332. temp_state = MeasureTemp_0;
  1333. break;
  1334. case MeasureTemp_0:
  1335. #if HAS_TEMP_0
  1336. raw_temp_value[0] += ADC;
  1337. #endif
  1338. temp_state = PrepareTemp_BED;
  1339. break;
  1340. case PrepareTemp_BED:
  1341. #if HAS_TEMP_BED
  1342. START_ADC(TEMP_BED_PIN);
  1343. #endif
  1344. lcd_buttons_update();
  1345. temp_state = MeasureTemp_BED;
  1346. break;
  1347. case MeasureTemp_BED:
  1348. #if HAS_TEMP_BED
  1349. raw_temp_bed_value += ADC;
  1350. #endif
  1351. temp_state = PrepareTemp_1;
  1352. break;
  1353. case PrepareTemp_1:
  1354. #if HAS_TEMP_1
  1355. START_ADC(TEMP_1_PIN);
  1356. #endif
  1357. lcd_buttons_update();
  1358. temp_state = MeasureTemp_1;
  1359. break;
  1360. case MeasureTemp_1:
  1361. #if HAS_TEMP_1
  1362. raw_temp_value[1] += ADC;
  1363. #endif
  1364. temp_state = PrepareTemp_2;
  1365. break;
  1366. case PrepareTemp_2:
  1367. #if HAS_TEMP_2
  1368. START_ADC(TEMP_2_PIN);
  1369. #endif
  1370. lcd_buttons_update();
  1371. temp_state = MeasureTemp_2;
  1372. break;
  1373. case MeasureTemp_2:
  1374. #if HAS_TEMP_2
  1375. raw_temp_value[2] += ADC;
  1376. #endif
  1377. temp_state = PrepareTemp_3;
  1378. break;
  1379. case PrepareTemp_3:
  1380. #if HAS_TEMP_3
  1381. START_ADC(TEMP_3_PIN);
  1382. #endif
  1383. lcd_buttons_update();
  1384. temp_state = MeasureTemp_3;
  1385. break;
  1386. case MeasureTemp_3:
  1387. #if HAS_TEMP_3
  1388. raw_temp_value[3] += ADC;
  1389. #endif
  1390. temp_state = Prepare_FILWIDTH;
  1391. break;
  1392. case Prepare_FILWIDTH:
  1393. #if HAS_FILAMENT_SENSOR
  1394. START_ADC(FILWIDTH_PIN);
  1395. #endif
  1396. lcd_buttons_update();
  1397. temp_state = Measure_FILWIDTH;
  1398. break;
  1399. case Measure_FILWIDTH:
  1400. #if HAS_FILAMENT_SENSOR
  1401. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1402. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1403. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1404. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1405. }
  1406. #endif
  1407. temp_state = PrepareTemp_0;
  1408. temp_count++;
  1409. break;
  1410. case StartupDelay:
  1411. temp_state = PrepareTemp_0;
  1412. break;
  1413. // default:
  1414. // SERIAL_ERROR_START;
  1415. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1416. // break;
  1417. } // switch(temp_state)
  1418. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1419. // Update the raw values if they've been read. Else we could be updating them during reading.
  1420. if (!temp_meas_ready) set_current_temp_raw();
  1421. // Filament Sensor - can be read any time since IIR filtering is used
  1422. #if HAS_FILAMENT_SENSOR
  1423. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1424. #endif
  1425. temp_count = 0;
  1426. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1427. raw_temp_bed_value = 0;
  1428. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1429. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1430. #define GE0 <=
  1431. #else
  1432. #define GE0 >=
  1433. #endif
  1434. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1435. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1436. #endif
  1437. #if HAS_TEMP_1 && EXTRUDERS > 1
  1438. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1439. #define GE1 <=
  1440. #else
  1441. #define GE1 >=
  1442. #endif
  1443. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1444. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1445. #endif // TEMP_SENSOR_1
  1446. #if HAS_TEMP_2 && EXTRUDERS > 2
  1447. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1448. #define GE2 <=
  1449. #else
  1450. #define GE2 >=
  1451. #endif
  1452. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1453. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1454. #endif // TEMP_SENSOR_2
  1455. #if HAS_TEMP_3 && EXTRUDERS > 3
  1456. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1457. #define GE3 <=
  1458. #else
  1459. #define GE3 >=
  1460. #endif
  1461. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1462. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1463. #endif // TEMP_SENSOR_3
  1464. #if HAS_TEMP_BED
  1465. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1466. #define GEBED <=
  1467. #else
  1468. #define GEBED >=
  1469. #endif
  1470. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) _temp_error(-1, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP_BED));
  1471. if (bed_minttemp_raw GEBED current_temperature_bed_raw) _temp_error(-1, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP_BED));
  1472. #endif
  1473. } // temp_count >= OVERSAMPLENR
  1474. #if ENABLED(BABYSTEPPING)
  1475. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1476. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1477. if (curTodo > 0) {
  1478. babystep(axis,/*fwd*/true);
  1479. babystepsTodo[axis]--; //fewer to do next time
  1480. }
  1481. else if (curTodo < 0) {
  1482. babystep(axis,/*fwd*/false);
  1483. babystepsTodo[axis]++; //fewer to do next time
  1484. }
  1485. }
  1486. #endif //BABYSTEPPING
  1487. }
  1488. #if ENABLED(PIDTEMP)
  1489. // Apply the scale factors to the PID values
  1490. float scalePID_i(float i) { return i * PID_dT; }
  1491. float unscalePID_i(float i) { return i / PID_dT; }
  1492. float scalePID_d(float d) { return d / PID_dT; }
  1493. float unscalePID_d(float d) { return d * PID_dT; }
  1494. #endif //PIDTEMP