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

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