My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. //===========================================================================
  28. //=============================public variables============================
  29. //===========================================================================
  30. int target_temperature[EXTRUDERS] = { 0 };
  31. int target_temperature_bed = 0;
  32. int current_temperature_raw[EXTRUDERS] = { 0 };
  33. float current_temperature[EXTRUDERS] = { 0 };
  34. int current_temperature_bed_raw = 0;
  35. float current_temperature_bed = 0;
  36. #ifdef PIDTEMP
  37. float Kp=DEFAULT_Kp;
  38. float Ki=(DEFAULT_Ki*PID_dT);
  39. float Kd=(DEFAULT_Kd/PID_dT);
  40. #ifdef PID_ADD_EXTRUSION_RATE
  41. float Kc=DEFAULT_Kc;
  42. #endif
  43. #endif //PIDTEMP
  44. #ifdef PIDTEMPBED
  45. float bedKp=DEFAULT_bedKp;
  46. float bedKi=(DEFAULT_bedKi*PID_dT);
  47. float bedKd=(DEFAULT_bedKd/PID_dT);
  48. #endif //PIDTEMPBED
  49. //===========================================================================
  50. //=============================private variables============================
  51. //===========================================================================
  52. static volatile bool temp_meas_ready = false;
  53. #ifdef PIDTEMP
  54. //static cannot be external:
  55. static float temp_iState[EXTRUDERS] = { 0 };
  56. static float temp_dState[EXTRUDERS] = { 0 };
  57. static float pTerm[EXTRUDERS];
  58. static float iTerm[EXTRUDERS];
  59. static float dTerm[EXTRUDERS];
  60. //int output;
  61. static float pid_error[EXTRUDERS];
  62. static float temp_iState_min[EXTRUDERS];
  63. static float temp_iState_max[EXTRUDERS];
  64. // static float pid_input[EXTRUDERS];
  65. // static float pid_output[EXTRUDERS];
  66. static bool pid_reset[EXTRUDERS];
  67. #endif //PIDTEMP
  68. #ifdef PIDTEMPBED
  69. //static cannot be external:
  70. static float temp_iState_bed = { 0 };
  71. static float temp_dState_bed = { 0 };
  72. static float pTerm_bed;
  73. static float iTerm_bed;
  74. static float dTerm_bed;
  75. //int output;
  76. static float pid_error_bed;
  77. static float temp_iState_min_bed;
  78. static float temp_iState_max_bed;
  79. #else //PIDTEMPBED
  80. static unsigned long previous_millis_bed_heater;
  81. #endif //PIDTEMPBED
  82. static unsigned char soft_pwm[EXTRUDERS];
  83. static unsigned char soft_pwm_bed;
  84. #ifdef FAN_SOFT_PWM
  85. static unsigned char soft_pwm_fan;
  86. #endif
  87. #if EXTRUDER_0_AUTO_FAN_PIN > 0 || EXTRUDER_1_AUTO_FAN_PIN > 0 || EXTRUDER_2_AUTO_FAN_PIN > 0
  88. static unsigned long extruder_autofan_last_check;
  89. #endif
  90. #if EXTRUDERS > 3
  91. # error Unsupported number of extruders
  92. #elif EXTRUDERS > 2
  93. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  94. #elif EXTRUDERS > 1
  95. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  96. #else
  97. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  98. #endif
  99. // Init min and max temp with extreme values to prevent false errors during startup
  100. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  101. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  102. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  103. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  104. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  105. #ifdef BED_MAXTEMP
  106. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  107. #endif
  108. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  109. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  110. static float analog2temp(int raw, uint8_t e);
  111. static float analog2tempBed(int raw);
  112. static void updateTemperaturesFromRawValues();
  113. #ifdef WATCH_TEMP_PERIOD
  114. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  115. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  116. #endif //WATCH_TEMP_PERIOD
  117. //===========================================================================
  118. //============================= functions ============================
  119. //===========================================================================
  120. void PID_autotune(float temp, int extruder, int ncycles)
  121. {
  122. float input = 0.0;
  123. int cycles=0;
  124. bool heating = true;
  125. unsigned long temp_millis = millis();
  126. unsigned long t1=temp_millis;
  127. unsigned long t2=temp_millis;
  128. long t_high = 0;
  129. long t_low = 0;
  130. long bias, d;
  131. float Ku, Tu;
  132. float Kp, Ki, Kd;
  133. float max = 0, min = 10000;
  134. if ((extruder > EXTRUDERS)
  135. #if (TEMP_BED_PIN <= -1)
  136. ||(extruder < 0)
  137. #endif
  138. ){
  139. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  140. return;
  141. }
  142. SERIAL_ECHOLN("PID Autotune start");
  143. disable_heater(); // switch off all heaters.
  144. if (extruder<0)
  145. {
  146. soft_pwm_bed = (MAX_BED_POWER)/2;
  147. bias = d = (MAX_BED_POWER)/2;
  148. }
  149. else
  150. {
  151. soft_pwm[extruder] = (PID_MAX)/2;
  152. bias = d = (PID_MAX)/2;
  153. }
  154. for(;;) {
  155. if(temp_meas_ready == true) { // temp sample ready
  156. updateTemperaturesFromRawValues();
  157. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  158. max=max(max,input);
  159. min=min(min,input);
  160. if(heating == true && input > temp) {
  161. if(millis() - t2 > 5000) {
  162. heating=false;
  163. if (extruder<0)
  164. soft_pwm_bed = (bias - d) >> 1;
  165. else
  166. soft_pwm[extruder] = (bias - d) >> 1;
  167. t1=millis();
  168. t_high=t1 - t2;
  169. max=temp;
  170. }
  171. }
  172. if(heating == false && input < temp) {
  173. if(millis() - t1 > 5000) {
  174. heating=true;
  175. t2=millis();
  176. t_low=t2 - t1;
  177. if(cycles > 0) {
  178. bias += (d*(t_high - t_low))/(t_low + t_high);
  179. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  180. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  181. else d = bias;
  182. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  183. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  184. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  185. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  186. if(cycles > 2) {
  187. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  188. Tu = ((float)(t_low + t_high)/1000.0);
  189. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  190. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  191. Kp = 0.6*Ku;
  192. Ki = 2*Kp/Tu;
  193. Kd = Kp*Tu/8;
  194. SERIAL_PROTOCOLLNPGM(" Clasic PID ")
  195. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  196. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  197. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  198. /*
  199. Kp = 0.33*Ku;
  200. Ki = Kp/Tu;
  201. Kd = Kp*Tu/3;
  202. SERIAL_PROTOCOLLNPGM(" Some overshoot ")
  203. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  204. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  205. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  206. Kp = 0.2*Ku;
  207. Ki = 2*Kp/Tu;
  208. Kd = Kp*Tu/3;
  209. SERIAL_PROTOCOLLNPGM(" No overshoot ")
  210. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  211. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  212. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  213. */
  214. }
  215. }
  216. if (extruder<0)
  217. soft_pwm_bed = (bias + d) >> 1;
  218. else
  219. soft_pwm[extruder] = (bias + d) >> 1;
  220. cycles++;
  221. min=temp;
  222. }
  223. }
  224. }
  225. if(input > (temp + 20)) {
  226. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature to high");
  227. return;
  228. }
  229. if(millis() - temp_millis > 2000) {
  230. int p;
  231. if (extruder<0){
  232. p=soft_pwm_bed;
  233. SERIAL_PROTOCOLPGM("ok B:");
  234. }else{
  235. p=soft_pwm[extruder];
  236. SERIAL_PROTOCOLPGM("ok T:");
  237. }
  238. SERIAL_PROTOCOL(input);
  239. SERIAL_PROTOCOLPGM(" @:");
  240. SERIAL_PROTOCOLLN(p);
  241. temp_millis = millis();
  242. }
  243. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  244. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  245. return;
  246. }
  247. if(cycles > ncycles) {
  248. SERIAL_PROTOCOLLNPGM("PID Autotune finished ! Place the Kp, Ki and Kd constants in the configuration.h");
  249. return;
  250. }
  251. lcd_update();
  252. }
  253. }
  254. void updatePID()
  255. {
  256. #ifdef PIDTEMP
  257. for(int e = 0; e < EXTRUDERS; e++) {
  258. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  259. }
  260. #endif
  261. #ifdef PIDTEMPBED
  262. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  263. #endif
  264. }
  265. int getHeaterPower(int heater) {
  266. if (heater<0)
  267. return soft_pwm_bed;
  268. return soft_pwm[heater];
  269. }
  270. #if EXTRUDER_0_AUTO_FAN_PIN > 0 || EXTRUDER_1_AUTO_FAN_PIN > 0 || EXTRUDER_2_AUTO_FAN_PIN > 0
  271. void setExtruderAutoFanState(int pin, bool state)
  272. {
  273. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  274. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  275. pinMode(pin, OUTPUT);
  276. digitalWrite(pin, newFanSpeed);
  277. analogWrite(pin, newFanSpeed);
  278. }
  279. void checkExtruderAutoFans()
  280. {
  281. uint8_t fanState = 0;
  282. // which fan pins need to be turned on?
  283. #if EXTRUDER_0_AUTO_FAN_PIN > 0
  284. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  285. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  286. #endif
  287. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  288. fanState |= 1;
  289. #endif
  290. #if EXTRUDER_1_AUTO_FAN_PIN > 0
  291. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  292. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  293. #endif
  294. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  295. {
  296. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  297. fanState |= 1;
  298. else
  299. fanState |= 2;
  300. }
  301. #endif
  302. #if EXTRUDER_2_AUTO_FAN_PIN > 0
  303. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  304. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  305. #endif
  306. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  307. {
  308. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  309. fanState |= 1;
  310. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  311. fanState |= 2;
  312. else
  313. fanState |= 4;
  314. }
  315. #endif
  316. // update extruder auto fan states
  317. #if EXTRUDER_0_AUTO_FAN_PIN > 0
  318. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  319. #endif
  320. #if EXTRUDER_1_AUTO_FAN_PIN > 0
  321. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  322. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  323. #endif
  324. #if EXTRUDER_2_AUTO_FAN_PIN > 0
  325. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  326. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  327. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  328. #endif
  329. }
  330. #endif // any extruder auto fan pins set
  331. void manage_heater()
  332. {
  333. float pid_input;
  334. float pid_output;
  335. if(temp_meas_ready != true) //better readability
  336. return;
  337. updateTemperaturesFromRawValues();
  338. for(int e = 0; e < EXTRUDERS; e++)
  339. {
  340. #ifdef PIDTEMP
  341. pid_input = current_temperature[e];
  342. #ifndef PID_OPENLOOP
  343. pid_error[e] = target_temperature[e] - pid_input;
  344. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  345. pid_output = BANG_MAX;
  346. pid_reset[e] = true;
  347. }
  348. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  349. pid_output = 0;
  350. pid_reset[e] = true;
  351. }
  352. else {
  353. if(pid_reset[e] == true) {
  354. temp_iState[e] = 0.0;
  355. pid_reset[e] = false;
  356. }
  357. pTerm[e] = Kp * pid_error[e];
  358. temp_iState[e] += pid_error[e];
  359. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  360. iTerm[e] = Ki * temp_iState[e];
  361. //K1 defined in Configuration.h in the PID settings
  362. #define K2 (1.0-K1)
  363. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  364. temp_dState[e] = pid_input;
  365. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  366. }
  367. #else
  368. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  369. #endif //PID_OPENLOOP
  370. #ifdef PID_DEBUG
  371. SERIAL_ECHO_START(" PIDDEBUG ");
  372. SERIAL_ECHO(e);
  373. SERIAL_ECHO(": Input ");
  374. SERIAL_ECHO(pid_input);
  375. SERIAL_ECHO(" Output ");
  376. SERIAL_ECHO(pid_output);
  377. SERIAL_ECHO(" pTerm ");
  378. SERIAL_ECHO(pTerm[e]);
  379. SERIAL_ECHO(" iTerm ");
  380. SERIAL_ECHO(iTerm[e]);
  381. SERIAL_ECHO(" dTerm ");
  382. SERIAL_ECHOLN(dTerm[e]);
  383. #endif //PID_DEBUG
  384. #else /* PID off */
  385. pid_output = 0;
  386. if(current_temperature[e] < target_temperature[e]) {
  387. pid_output = PID_MAX;
  388. }
  389. #endif
  390. // Check if temperature is within the correct range
  391. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  392. {
  393. soft_pwm[e] = (int)pid_output >> 1;
  394. }
  395. else {
  396. soft_pwm[e] = 0;
  397. }
  398. #ifdef WATCH_TEMP_PERIOD
  399. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  400. {
  401. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  402. {
  403. setTargetHotend(0, e);
  404. LCD_MESSAGEPGM("Heating failed");
  405. SERIAL_ECHO_START;
  406. SERIAL_ECHOLN("Heating failed");
  407. }else{
  408. watchmillis[e] = 0;
  409. }
  410. }
  411. #endif
  412. } // End extruder for loop
  413. #if EXTRUDER_0_AUTO_FAN_PIN > 0 || EXTRUDER_1_AUTO_FAN_PIN > 0 || EXTRUDER_2_AUTO_FAN_PIN > 0
  414. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  415. {
  416. checkExtruderAutoFans();
  417. extruder_autofan_last_check = millis();
  418. }
  419. #endif
  420. #ifndef PIDTEMPBED
  421. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  422. return;
  423. previous_millis_bed_heater = millis();
  424. #endif
  425. #if TEMP_SENSOR_BED != 0
  426. #ifdef PIDTEMPBED
  427. pid_input = current_temperature_bed;
  428. #ifndef PID_OPENLOOP
  429. pid_error_bed = target_temperature_bed - pid_input;
  430. pTerm_bed = bedKp * pid_error_bed;
  431. temp_iState_bed += pid_error_bed;
  432. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  433. iTerm_bed = bedKi * temp_iState_bed;
  434. //K1 defined in Configuration.h in the PID settings
  435. #define K2 (1.0-K1)
  436. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  437. temp_dState_bed = pid_input;
  438. pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
  439. #else
  440. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  441. #endif //PID_OPENLOOP
  442. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  443. {
  444. soft_pwm_bed = (int)pid_output >> 1;
  445. }
  446. else {
  447. soft_pwm_bed = 0;
  448. }
  449. #elif !defined(BED_LIMIT_SWITCHING)
  450. // Check if temperature is within the correct range
  451. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  452. {
  453. if(current_temperature_bed >= target_temperature_bed)
  454. {
  455. soft_pwm_bed = 0;
  456. }
  457. else
  458. {
  459. soft_pwm_bed = MAX_BED_POWER>>1;
  460. }
  461. }
  462. else
  463. {
  464. soft_pwm_bed = 0;
  465. WRITE(HEATER_BED_PIN,LOW);
  466. }
  467. #else //#ifdef BED_LIMIT_SWITCHING
  468. // Check if temperature is within the correct band
  469. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  470. {
  471. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  472. {
  473. soft_pwm_bed = 0;
  474. }
  475. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  476. {
  477. soft_pwm_bed = MAX_BED_POWER>>1;
  478. }
  479. }
  480. else
  481. {
  482. soft_pwm_bed = 0;
  483. WRITE(HEATER_BED_PIN,LOW);
  484. }
  485. #endif
  486. #endif
  487. }
  488. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  489. // Derived from RepRap FiveD extruder::getTemperature()
  490. // For hot end temperature measurement.
  491. static float analog2temp(int raw, uint8_t e) {
  492. if(e >= EXTRUDERS)
  493. {
  494. SERIAL_ERROR_START;
  495. SERIAL_ERROR((int)e);
  496. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  497. kill();
  498. }
  499. #ifdef HEATER_0_USES_MAX6675
  500. if (e == 0)
  501. {
  502. return 0.25 * raw;
  503. }
  504. #endif
  505. if(heater_ttbl_map[e] != NULL)
  506. {
  507. float celsius = 0;
  508. uint8_t i;
  509. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  510. for (i=1; i<heater_ttbllen_map[e]; i++)
  511. {
  512. if (PGM_RD_W((*tt)[i][0]) > raw)
  513. {
  514. celsius = PGM_RD_W((*tt)[i-1][1]) +
  515. (raw - PGM_RD_W((*tt)[i-1][0])) *
  516. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  517. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  518. break;
  519. }
  520. }
  521. // Overflow: Set to last value in the table
  522. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  523. return celsius;
  524. }
  525. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  526. }
  527. // Derived from RepRap FiveD extruder::getTemperature()
  528. // For bed temperature measurement.
  529. static float analog2tempBed(int raw) {
  530. #ifdef BED_USES_THERMISTOR
  531. float celsius = 0;
  532. byte i;
  533. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  534. {
  535. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  536. {
  537. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  538. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  539. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  540. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  541. break;
  542. }
  543. }
  544. // Overflow: Set to last value in the table
  545. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  546. return celsius;
  547. #elif defined BED_USES_AD595
  548. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  549. #else
  550. return 0;
  551. #endif
  552. }
  553. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  554. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  555. static void updateTemperaturesFromRawValues()
  556. {
  557. for(uint8_t e=0;e<EXTRUDERS;e++)
  558. {
  559. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  560. }
  561. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  562. //Reset the watchdog after we know we have a temperature measurement.
  563. watchdog_reset();
  564. CRITICAL_SECTION_START;
  565. temp_meas_ready = false;
  566. CRITICAL_SECTION_END;
  567. }
  568. void tp_init()
  569. {
  570. // Finish init of mult extruder arrays
  571. for(int e = 0; e < EXTRUDERS; e++) {
  572. // populate with the first value
  573. maxttemp[e] = maxttemp[0];
  574. #ifdef PIDTEMP
  575. temp_iState_min[e] = 0.0;
  576. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  577. #endif //PIDTEMP
  578. #ifdef PIDTEMPBED
  579. temp_iState_min_bed = 0.0;
  580. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  581. #endif //PIDTEMPBED
  582. }
  583. #if (HEATER_0_PIN > -1)
  584. SET_OUTPUT(HEATER_0_PIN);
  585. #endif
  586. #if (HEATER_1_PIN > -1)
  587. SET_OUTPUT(HEATER_1_PIN);
  588. #endif
  589. #if (HEATER_2_PIN > -1)
  590. SET_OUTPUT(HEATER_2_PIN);
  591. #endif
  592. #if (HEATER_BED_PIN > -1)
  593. SET_OUTPUT(HEATER_BED_PIN);
  594. #endif
  595. #if (FAN_PIN > -1)
  596. SET_OUTPUT(FAN_PIN);
  597. #ifdef FAST_PWM_FAN
  598. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  599. #endif
  600. #ifdef FAN_SOFT_PWM
  601. soft_pwm_fan=(unsigned char)fanSpeed;
  602. #endif
  603. #endif
  604. #ifdef HEATER_0_USES_MAX6675
  605. #ifndef SDSUPPORT
  606. SET_OUTPUT(MAX_SCK_PIN);
  607. WRITE(MAX_SCK_PIN,0);
  608. SET_OUTPUT(MAX_MOSI_PIN);
  609. WRITE(MAX_MOSI_PIN,1);
  610. SET_INPUT(MAX_MISO_PIN);
  611. WRITE(MAX_MISO_PIN,1);
  612. #endif
  613. SET_OUTPUT(MAX6675_SS);
  614. WRITE(MAX6675_SS,1);
  615. #endif
  616. // Set analog inputs
  617. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  618. DIDR0 = 0;
  619. #ifdef DIDR2
  620. DIDR2 = 0;
  621. #endif
  622. #if (TEMP_0_PIN > -1)
  623. #if TEMP_0_PIN < 8
  624. DIDR0 |= 1 << TEMP_0_PIN;
  625. #else
  626. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  627. #endif
  628. #endif
  629. #if (TEMP_1_PIN > -1)
  630. #if TEMP_1_PIN < 8
  631. DIDR0 |= 1<<TEMP_1_PIN;
  632. #else
  633. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  634. #endif
  635. #endif
  636. #if (TEMP_2_PIN > -1)
  637. #if TEMP_2_PIN < 8
  638. DIDR0 |= 1 << TEMP_2_PIN;
  639. #else
  640. DIDR2 = 1<<(TEMP_2_PIN - 8);
  641. #endif
  642. #endif
  643. #if (TEMP_BED_PIN > -1)
  644. #if TEMP_BED_PIN < 8
  645. DIDR0 |= 1<<TEMP_BED_PIN;
  646. #else
  647. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  648. #endif
  649. #endif
  650. // Use timer0 for temperature measurement
  651. // Interleave temperature interrupt with millies interrupt
  652. OCR0B = 128;
  653. TIMSK0 |= (1<<OCIE0B);
  654. // Wait for temperature measurement to settle
  655. delay(250);
  656. #ifdef HEATER_0_MINTEMP
  657. minttemp[0] = HEATER_0_MINTEMP;
  658. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  659. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  660. minttemp_raw[0] += OVERSAMPLENR;
  661. #else
  662. minttemp_raw[0] -= OVERSAMPLENR;
  663. #endif
  664. }
  665. #endif //MINTEMP
  666. #ifdef HEATER_0_MAXTEMP
  667. maxttemp[0] = HEATER_0_MAXTEMP;
  668. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  669. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  670. maxttemp_raw[0] -= OVERSAMPLENR;
  671. #else
  672. maxttemp_raw[0] += OVERSAMPLENR;
  673. #endif
  674. }
  675. #endif //MAXTEMP
  676. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  677. minttemp[1] = HEATER_1_MINTEMP;
  678. while(analog2temp(minttemp_raw[1], 1) > HEATER_1_MINTEMP) {
  679. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  680. minttemp_raw[1] += OVERSAMPLENR;
  681. #else
  682. minttemp_raw[1] -= OVERSAMPLENR;
  683. #endif
  684. }
  685. #endif // MINTEMP 1
  686. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  687. maxttemp[1] = HEATER_1_MAXTEMP;
  688. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  689. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  690. maxttemp_raw[1] -= OVERSAMPLENR;
  691. #else
  692. maxttemp_raw[1] += OVERSAMPLENR;
  693. #endif
  694. }
  695. #endif //MAXTEMP 1
  696. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  697. minttemp[2] = HEATER_2_MINTEMP;
  698. while(analog2temp(minttemp_raw[2], 2) > HEATER_2_MINTEMP) {
  699. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  700. minttemp_raw[2] += OVERSAMPLENR;
  701. #else
  702. minttemp_raw[2] -= OVERSAMPLENR;
  703. #endif
  704. }
  705. #endif //MINTEMP 2
  706. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  707. maxttemp[2] = HEATER_2_MAXTEMP;
  708. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  709. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  710. maxttemp_raw[2] -= OVERSAMPLENR;
  711. #else
  712. maxttemp_raw[2] += OVERSAMPLENR;
  713. #endif
  714. }
  715. #endif //MAXTEMP 2
  716. #ifdef BED_MINTEMP
  717. /* No bed MINTEMP error implemented?!? */ /*
  718. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  719. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  720. bed_minttemp_raw += OVERSAMPLENR;
  721. #else
  722. bed_minttemp_raw -= OVERSAMPLENR;
  723. #endif
  724. }
  725. */
  726. #endif //BED_MINTEMP
  727. #ifdef BED_MAXTEMP
  728. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  729. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  730. bed_maxttemp_raw -= OVERSAMPLENR;
  731. #else
  732. bed_maxttemp_raw += OVERSAMPLENR;
  733. #endif
  734. }
  735. #endif //BED_MAXTEMP
  736. }
  737. void setWatch()
  738. {
  739. #ifdef WATCH_TEMP_PERIOD
  740. for (int e = 0; e < EXTRUDERS; e++)
  741. {
  742. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  743. {
  744. watch_start_temp[e] = degHotend(e);
  745. watchmillis[e] = millis();
  746. }
  747. }
  748. #endif
  749. }
  750. void disable_heater()
  751. {
  752. for(int i=0;i<EXTRUDERS;i++)
  753. setTargetHotend(0,i);
  754. setTargetBed(0);
  755. #if TEMP_0_PIN > -1
  756. target_temperature[0]=0;
  757. soft_pwm[0]=0;
  758. #if HEATER_0_PIN > -1
  759. WRITE(HEATER_0_PIN,LOW);
  760. #endif
  761. #endif
  762. #if TEMP_1_PIN > -1
  763. target_temperature[1]=0;
  764. soft_pwm[1]=0;
  765. #if HEATER_1_PIN > -1
  766. WRITE(HEATER_1_PIN,LOW);
  767. #endif
  768. #endif
  769. #if TEMP_2_PIN > -1
  770. target_temperature[2]=0;
  771. soft_pwm[2]=0;
  772. #if HEATER_2_PIN > -1
  773. WRITE(HEATER_2_PIN,LOW);
  774. #endif
  775. #endif
  776. #if TEMP_BED_PIN > -1
  777. target_temperature_bed=0;
  778. soft_pwm_bed=0;
  779. #if HEATER_BED_PIN > -1
  780. WRITE(HEATER_BED_PIN,LOW);
  781. #endif
  782. #endif
  783. }
  784. void max_temp_error(uint8_t e) {
  785. disable_heater();
  786. if(IsStopped() == false) {
  787. SERIAL_ERROR_START;
  788. SERIAL_ERRORLN((int)e);
  789. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  790. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  791. }
  792. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  793. Stop();
  794. #endif
  795. }
  796. void min_temp_error(uint8_t e) {
  797. disable_heater();
  798. if(IsStopped() == false) {
  799. SERIAL_ERROR_START;
  800. SERIAL_ERRORLN((int)e);
  801. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  802. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  803. }
  804. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  805. Stop();
  806. #endif
  807. }
  808. void bed_max_temp_error(void) {
  809. #if HEATER_BED_PIN > -1
  810. WRITE(HEATER_BED_PIN, 0);
  811. #endif
  812. if(IsStopped() == false) {
  813. SERIAL_ERROR_START;
  814. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  815. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  816. }
  817. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  818. Stop();
  819. #endif
  820. }
  821. #ifdef HEATER_0_USES_MAX6675
  822. #define MAX6675_HEAT_INTERVAL 250
  823. long max6675_previous_millis = -HEAT_INTERVAL;
  824. int max6675_temp = 2000;
  825. int read_max6675()
  826. {
  827. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  828. return max6675_temp;
  829. max6675_previous_millis = millis();
  830. max6675_temp = 0;
  831. #ifdef PRR
  832. PRR &= ~(1<<PRSPI);
  833. #elif defined PRR0
  834. PRR0 &= ~(1<<PRSPI);
  835. #endif
  836. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  837. // enable TT_MAX6675
  838. WRITE(MAX6675_SS, 0);
  839. // ensure 100ns delay - a bit extra is fine
  840. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  841. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  842. // read MSB
  843. SPDR = 0;
  844. for (;(SPSR & (1<<SPIF)) == 0;);
  845. max6675_temp = SPDR;
  846. max6675_temp <<= 8;
  847. // read LSB
  848. SPDR = 0;
  849. for (;(SPSR & (1<<SPIF)) == 0;);
  850. max6675_temp |= SPDR;
  851. // disable TT_MAX6675
  852. WRITE(MAX6675_SS, 1);
  853. if (max6675_temp & 4)
  854. {
  855. // thermocouple open
  856. max6675_temp = 2000;
  857. }
  858. else
  859. {
  860. max6675_temp = max6675_temp >> 3;
  861. }
  862. return max6675_temp;
  863. }
  864. #endif
  865. // Timer 0 is shared with millies
  866. ISR(TIMER0_COMPB_vect)
  867. {
  868. //these variables are only accesible from the ISR, but static, so they don't loose their value
  869. static unsigned char temp_count = 0;
  870. static unsigned long raw_temp_0_value = 0;
  871. static unsigned long raw_temp_1_value = 0;
  872. static unsigned long raw_temp_2_value = 0;
  873. static unsigned long raw_temp_bed_value = 0;
  874. static unsigned char temp_state = 0;
  875. static unsigned char pwm_count = 1;
  876. static unsigned char soft_pwm_0;
  877. #if EXTRUDERS > 1
  878. static unsigned char soft_pwm_1;
  879. #endif
  880. #if EXTRUDERS > 2
  881. static unsigned char soft_pwm_2;
  882. #endif
  883. #if HEATER_BED_PIN > -1
  884. static unsigned char soft_pwm_b;
  885. #endif
  886. if(pwm_count == 0){
  887. soft_pwm_0 = soft_pwm[0];
  888. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  889. #if EXTRUDERS > 1
  890. soft_pwm_1 = soft_pwm[1];
  891. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  892. #endif
  893. #if EXTRUDERS > 2
  894. soft_pwm_2 = soft_pwm[2];
  895. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  896. #endif
  897. #if HEATER_BED_PIN > -1
  898. soft_pwm_b = soft_pwm_bed;
  899. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1);
  900. #endif
  901. #ifdef FAN_SOFT_PWM
  902. soft_pwm_fan =(unsigned char) fanSpeed;
  903. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1);
  904. #endif
  905. }
  906. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  907. #if EXTRUDERS > 1
  908. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  909. #endif
  910. #if EXTRUDERS > 2
  911. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  912. #endif
  913. #if HEATER_BED_PIN > -1
  914. if(soft_pwm_b <= pwm_count) WRITE(HEATER_BED_PIN,0);
  915. #endif
  916. #ifdef FAN_SOFT_PWM
  917. if(soft_pwm_fan <= pwm_count) WRITE(FAN_PIN,0);
  918. #endif
  919. pwm_count++;
  920. pwm_count &= 0x7f;
  921. switch(temp_state) {
  922. case 0: // Prepare TEMP_0
  923. #if (TEMP_0_PIN > -1)
  924. #if TEMP_0_PIN > 7
  925. ADCSRB = 1<<MUX5;
  926. #else
  927. ADCSRB = 0;
  928. #endif
  929. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  930. ADCSRA |= 1<<ADSC; // Start conversion
  931. #endif
  932. lcd_buttons_update();
  933. temp_state = 1;
  934. break;
  935. case 1: // Measure TEMP_0
  936. #if (TEMP_0_PIN > -1)
  937. raw_temp_0_value += ADC;
  938. #endif
  939. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  940. raw_temp_0_value = read_max6675();
  941. #endif
  942. temp_state = 2;
  943. break;
  944. case 2: // Prepare TEMP_BED
  945. #if (TEMP_BED_PIN > -1)
  946. #if TEMP_BED_PIN > 7
  947. ADCSRB = 1<<MUX5;
  948. #else
  949. ADCSRB = 0;
  950. #endif
  951. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  952. ADCSRA |= 1<<ADSC; // Start conversion
  953. #endif
  954. lcd_buttons_update();
  955. temp_state = 3;
  956. break;
  957. case 3: // Measure TEMP_BED
  958. #if (TEMP_BED_PIN > -1)
  959. raw_temp_bed_value += ADC;
  960. #endif
  961. temp_state = 4;
  962. break;
  963. case 4: // Prepare TEMP_1
  964. #if (TEMP_1_PIN > -1)
  965. #if TEMP_1_PIN > 7
  966. ADCSRB = 1<<MUX5;
  967. #else
  968. ADCSRB = 0;
  969. #endif
  970. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  971. ADCSRA |= 1<<ADSC; // Start conversion
  972. #endif
  973. lcd_buttons_update();
  974. temp_state = 5;
  975. break;
  976. case 5: // Measure TEMP_1
  977. #if (TEMP_1_PIN > -1)
  978. raw_temp_1_value += ADC;
  979. #endif
  980. temp_state = 6;
  981. break;
  982. case 6: // Prepare TEMP_2
  983. #if (TEMP_2_PIN > -1)
  984. #if TEMP_2_PIN > 7
  985. ADCSRB = 1<<MUX5;
  986. #else
  987. ADCSRB = 0;
  988. #endif
  989. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  990. ADCSRA |= 1<<ADSC; // Start conversion
  991. #endif
  992. lcd_buttons_update();
  993. temp_state = 7;
  994. break;
  995. case 7: // Measure TEMP_2
  996. #if (TEMP_2_PIN > -1)
  997. raw_temp_2_value += ADC;
  998. #endif
  999. temp_state = 0;
  1000. temp_count++;
  1001. break;
  1002. // default:
  1003. // SERIAL_ERROR_START;
  1004. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1005. // break;
  1006. }
  1007. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  1008. {
  1009. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1010. {
  1011. current_temperature_raw[0] = raw_temp_0_value;
  1012. #if EXTRUDERS > 1
  1013. current_temperature_raw[1] = raw_temp_1_value;
  1014. #endif
  1015. #if EXTRUDERS > 2
  1016. current_temperature_raw[2] = raw_temp_2_value;
  1017. #endif
  1018. current_temperature_bed_raw = raw_temp_bed_value;
  1019. }
  1020. temp_meas_ready = true;
  1021. temp_count = 0;
  1022. raw_temp_0_value = 0;
  1023. raw_temp_1_value = 0;
  1024. raw_temp_2_value = 0;
  1025. raw_temp_bed_value = 0;
  1026. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1027. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1028. #else
  1029. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1030. #endif
  1031. max_temp_error(0);
  1032. }
  1033. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1034. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1035. #else
  1036. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1037. #endif
  1038. min_temp_error(0);
  1039. }
  1040. #if EXTRUDERS > 1
  1041. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1042. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1043. #else
  1044. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1045. #endif
  1046. max_temp_error(1);
  1047. }
  1048. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1049. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1050. #else
  1051. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1052. #endif
  1053. min_temp_error(1);
  1054. }
  1055. #endif
  1056. #if EXTRUDERS > 2
  1057. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1058. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1059. #else
  1060. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1061. #endif
  1062. max_temp_error(2);
  1063. }
  1064. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1065. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1066. #else
  1067. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1068. #endif
  1069. min_temp_error(2);
  1070. }
  1071. #endif
  1072. /* No bed MINTEMP error? */
  1073. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1074. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1075. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1076. #else
  1077. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1078. #endif
  1079. target_temperature_bed = 0;
  1080. bed_max_temp_error();
  1081. }
  1082. #endif
  1083. }
  1084. }
  1085. #ifdef PIDTEMP
  1086. // Apply the scale factors to the PID values
  1087. float scalePID_i(float i)
  1088. {
  1089. return i*PID_dT;
  1090. }
  1091. float unscalePID_i(float i)
  1092. {
  1093. return i/PID_dT;
  1094. }
  1095. float scalePID_d(float d)
  1096. {
  1097. return d/PID_dT;
  1098. }
  1099. float unscalePID_d(float d)
  1100. {
  1101. return d*PID_dT;
  1102. }
  1103. #endif //PIDTEMP