My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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_raw[EXTRUDERS] = { 0 };
  31. int target_raw_bed = 0;
  32. #ifdef BED_LIMIT_SWITCHING
  33. int target_bed_low_temp =0;
  34. int target_bed_high_temp =0;
  35. #endif
  36. int current_raw[EXTRUDERS] = { 0 };
  37. int current_raw_bed = 0;
  38. #ifdef PIDTEMP
  39. // used external
  40. float pid_setpoint[EXTRUDERS] = { 0.0 };
  41. float Kp=DEFAULT_Kp;
  42. float Ki=(DEFAULT_Ki*PID_dT);
  43. float Kd=(DEFAULT_Kd/PID_dT);
  44. #ifdef PID_ADD_EXTRUSION_RATE
  45. float Kc=DEFAULT_Kc;
  46. #endif
  47. #endif //PIDTEMP
  48. //===========================================================================
  49. //=============================private variables============================
  50. //===========================================================================
  51. static volatile bool temp_meas_ready = false;
  52. static unsigned long previous_millis_bed_heater;
  53. //static unsigned long previous_millis_heater;
  54. #ifdef PIDTEMP
  55. //static cannot be external:
  56. static float temp_iState[EXTRUDERS] = { 0 };
  57. static float temp_dState[EXTRUDERS] = { 0 };
  58. static float pTerm[EXTRUDERS];
  59. static float iTerm[EXTRUDERS];
  60. static float dTerm[EXTRUDERS];
  61. //int output;
  62. static float pid_error[EXTRUDERS];
  63. static float temp_iState_min[EXTRUDERS];
  64. static float temp_iState_max[EXTRUDERS];
  65. // static float pid_input[EXTRUDERS];
  66. // static float pid_output[EXTRUDERS];
  67. static bool pid_reset[EXTRUDERS];
  68. #endif //PIDTEMP
  69. static unsigned char soft_pwm[EXTRUDERS];
  70. #ifdef WATCHPERIOD
  71. int watch_raw[EXTRUDERS] = { -1000 }; // the first value used for all
  72. int watch_oldtemp[3] = {0,0,0};
  73. unsigned long watchmillis = 0;
  74. #endif //WATCHPERIOD
  75. // Init min and max temp with extreme values to prevent false errors during startup
  76. static int minttemp[EXTRUDERS] = { 0 };
  77. static int maxttemp[EXTRUDERS] = { 16383 }; // the first value used for all
  78. static int bed_minttemp = 0;
  79. static int bed_maxttemp = 16383;
  80. static void *heater_ttbl_map[EXTRUDERS] = { (void *)heater_0_temptable
  81. #if EXTRUDERS > 1
  82. , (void *)heater_1_temptable
  83. #endif
  84. #if EXTRUDERS > 2
  85. , (void *)heater_2_temptable
  86. #endif
  87. #if EXTRUDERS > 3
  88. #error Unsupported number of extruders
  89. #endif
  90. };
  91. static int heater_ttbllen_map[EXTRUDERS] = { heater_0_temptable_len
  92. #if EXTRUDERS > 1
  93. , heater_1_temptable_len
  94. #endif
  95. #if EXTRUDERS > 2
  96. , heater_2_temptable_len
  97. #endif
  98. #if EXTRUDERS > 3
  99. #error Unsupported number of extruders
  100. #endif
  101. };
  102. //===========================================================================
  103. //============================= functions ============================
  104. //===========================================================================
  105. void PID_autotune(float temp)
  106. {
  107. float input;
  108. int cycles=0;
  109. bool heating = true;
  110. unsigned long temp_millis = millis();
  111. unsigned long t1=temp_millis;
  112. unsigned long t2=temp_millis;
  113. long t_high;
  114. long t_low;
  115. long bias=127;
  116. long d = 127;
  117. float Ku, Tu;
  118. float Kp, Ki, Kd;
  119. float max, min;
  120. SERIAL_ECHOLN("PID Autotune start");
  121. disable_heater(); // switch off all heaters.
  122. soft_pwm[0] = 255>>1;
  123. for(;;) {
  124. if(temp_meas_ready == true) { // temp sample ready
  125. CRITICAL_SECTION_START;
  126. temp_meas_ready = false;
  127. CRITICAL_SECTION_END;
  128. input = analog2temp(current_raw[0], 0);
  129. max=max(max,input);
  130. min=min(min,input);
  131. if(heating == true && input > temp) {
  132. if(millis() - t2 > 5000) {
  133. heating=false;
  134. soft_pwm[0] = (bias - d) >> 1;
  135. t1=millis();
  136. t_high=t1 - t2;
  137. max=temp;
  138. }
  139. }
  140. if(heating == false && input < temp) {
  141. if(millis() - t1 > 5000) {
  142. heating=true;
  143. t2=millis();
  144. t_low=t2 - t1;
  145. if(cycles > 0) {
  146. bias += (d*(t_high - t_low))/(t_low + t_high);
  147. bias = constrain(bias, 20 ,235);
  148. if(bias > 127) d = 254 - bias;
  149. else d = bias;
  150. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  151. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  152. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  153. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  154. if(cycles > 2) {
  155. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  156. Tu = ((float)(t_low + t_high)/1000.0);
  157. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  158. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  159. Kp = 0.6*Ku;
  160. Ki = 2*Kp/Tu;
  161. Kd = Kp*Tu/8;
  162. SERIAL_PROTOCOLLNPGM(" Clasic PID ")
  163. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  164. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  165. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  166. /*
  167. Kp = 0.33*Ku;
  168. Ki = Kp/Tu;
  169. Kd = Kp*Tu/3;
  170. SERIAL_PROTOCOLLNPGM(" Some overshoot ")
  171. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  172. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  173. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  174. Kp = 0.2*Ku;
  175. Ki = 2*Kp/Tu;
  176. Kd = Kp*Tu/3;
  177. SERIAL_PROTOCOLLNPGM(" No overshoot ")
  178. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  179. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  180. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  181. */
  182. }
  183. }
  184. soft_pwm[0] = (bias + d) >> 1;
  185. cycles++;
  186. min=temp;
  187. }
  188. }
  189. }
  190. if(input > (temp + 20)) {
  191. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature to high");
  192. return;
  193. }
  194. if(millis() - temp_millis > 2000) {
  195. temp_millis = millis();
  196. SERIAL_PROTOCOLPGM("ok T:");
  197. SERIAL_PROTOCOL(degHotend(0));
  198. SERIAL_PROTOCOLPGM(" @:");
  199. SERIAL_PROTOCOLLN(getHeaterPower(0));
  200. }
  201. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  202. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  203. return;
  204. }
  205. if(cycles > 5) {
  206. SERIAL_PROTOCOLLNPGM("PID Autotune finished ! Place the Kp, Ki and Kd constants in the configuration.h");
  207. return;
  208. }
  209. LCD_STATUS;
  210. }
  211. }
  212. void updatePID()
  213. {
  214. #ifdef PIDTEMP
  215. for(int e = 0; e < EXTRUDERS; e++) {
  216. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  217. }
  218. #endif
  219. }
  220. int getHeaterPower(int heater) {
  221. return soft_pwm[heater];
  222. }
  223. void manage_heater()
  224. {
  225. #ifdef USE_WATCHDOG
  226. wd_reset();
  227. #endif
  228. float pid_input;
  229. float pid_output;
  230. if(temp_meas_ready != true) //better readability
  231. return;
  232. CRITICAL_SECTION_START;
  233. temp_meas_ready = false;
  234. CRITICAL_SECTION_END;
  235. for(int e = 0; e < EXTRUDERS; e++)
  236. {
  237. #ifdef PIDTEMP
  238. pid_input = analog2temp(current_raw[e], e);
  239. #ifndef PID_OPENLOOP
  240. pid_error[e] = pid_setpoint[e] - pid_input;
  241. if(pid_error[e] > 10) {
  242. pid_output = PID_MAX;
  243. pid_reset[e] = true;
  244. }
  245. else if(pid_error[e] < -10) {
  246. pid_output = 0;
  247. pid_reset[e] = true;
  248. }
  249. else {
  250. if(pid_reset[e] == true) {
  251. temp_iState[e] = 0.0;
  252. pid_reset[e] = false;
  253. }
  254. pTerm[e] = Kp * pid_error[e];
  255. temp_iState[e] += pid_error[e];
  256. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  257. iTerm[e] = Ki * temp_iState[e];
  258. //K1 defined in Configuration.h in the PID settings
  259. #define K2 (1.0-K1)
  260. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  261. temp_dState[e] = pid_input;
  262. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  263. }
  264. #endif //PID_OPENLOOP
  265. #ifdef PID_DEBUG
  266. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  267. #endif //PID_DEBUG
  268. #else /* PID off */
  269. pid_output = 0;
  270. if(current_raw[e] < target_raw[e]) {
  271. pid_output = PID_MAX;
  272. }
  273. #endif
  274. // Check if temperature is within the correct range
  275. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  276. {
  277. soft_pwm[e] = (int)pid_output >> 1;
  278. }
  279. else {
  280. soft_pwm[e] = 0;
  281. }
  282. } // End extruder for loop
  283. #ifdef WATCHPERIOD
  284. if(watchmillis && millis() - watchmillis > WATCHPERIOD){
  285. if(watch_oldtemp[0] >= degHotend(active_extruder)){
  286. setTargetHotend(0,active_extruder);
  287. LCD_MESSAGEPGM("Heating failed");
  288. SERIAL_ECHO_START;
  289. SERIAL_ECHOLN("Heating failed");
  290. }else{
  291. watchmillis = 0;
  292. }
  293. }
  294. #endif
  295. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  296. return;
  297. previous_millis_bed_heater = millis();
  298. #if TEMP_BED_PIN > -1
  299. #ifndef BED_LIMIT_SWITCHING
  300. // Check if temperature is within the correct range
  301. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  302. if(current_raw_bed >= target_raw_bed)
  303. {
  304. WRITE(HEATER_BED_PIN,LOW);
  305. }
  306. else
  307. {
  308. WRITE(HEATER_BED_PIN,HIGH);
  309. }
  310. }
  311. else {
  312. WRITE(HEATER_BED_PIN,LOW);
  313. }
  314. #else //#ifdef BED_LIMIT_SWITCHING
  315. // Check if temperature is within the correct band
  316. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  317. if(current_raw_bed > target_bed_high_temp)
  318. {
  319. WRITE(HEATER_BED_PIN,LOW);
  320. }
  321. else
  322. if(current_raw_bed <= target_bed_low_temp)
  323. {
  324. WRITE(HEATER_BED_PIN,HIGH);
  325. }
  326. }
  327. else {
  328. WRITE(HEATER_BED_PIN,LOW);
  329. }
  330. #endif
  331. #endif
  332. }
  333. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  334. // Takes hot end temperature value as input and returns corresponding raw value.
  335. // For a thermistor, it uses the RepRap thermistor temp table.
  336. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  337. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  338. int temp2analog(int celsius, uint8_t e) {
  339. if(e >= EXTRUDERS)
  340. {
  341. SERIAL_ERROR_START;
  342. SERIAL_ERROR((int)e);
  343. SERIAL_ERRORLNPGM(" - Invalid extruder number!");
  344. kill();
  345. }
  346. #ifdef HEATER_0_USES_MAX6675
  347. if (e == 0)
  348. {
  349. return celsius * 4;
  350. }
  351. #endif
  352. if(heater_ttbl_map[e] != 0)
  353. {
  354. int raw = 0;
  355. byte i;
  356. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  357. for (i=1; i<heater_ttbllen_map[e]; i++)
  358. {
  359. if (PGM_RD_W((*tt)[i][1]) < celsius)
  360. {
  361. raw = PGM_RD_W((*tt)[i-1][0]) +
  362. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  363. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  364. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  365. break;
  366. }
  367. }
  368. // Overflow: Set to last value in the table
  369. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  370. return (1023 * OVERSAMPLENR) - raw;
  371. }
  372. return ((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  373. }
  374. // Takes bed temperature value as input and returns corresponding raw value.
  375. // For a thermistor, it uses the RepRap thermistor temp table.
  376. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  377. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  378. int temp2analogBed(int celsius) {
  379. #ifdef BED_USES_THERMISTOR
  380. int raw = 0;
  381. byte i;
  382. for (i=1; i<bedtemptable_len; i++)
  383. {
  384. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  385. {
  386. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  387. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  388. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  389. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  390. break;
  391. }
  392. }
  393. // Overflow: Set to last value in the table
  394. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  395. return (1023 * OVERSAMPLENR) - raw;
  396. #elif defined BED_USES_AD595
  397. return lround(((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  398. #else
  399. #warning No heater-type defined for the bed.
  400. return 0;
  401. #endif
  402. }
  403. // Derived from RepRap FiveD extruder::getTemperature()
  404. // For hot end temperature measurement.
  405. float analog2temp(int raw, uint8_t e) {
  406. if(e >= EXTRUDERS)
  407. {
  408. SERIAL_ERROR_START;
  409. SERIAL_ERROR((int)e);
  410. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  411. kill();
  412. }
  413. #ifdef HEATER_0_USES_MAX6675
  414. if (e == 0)
  415. {
  416. return 0.25 * raw;
  417. }
  418. #endif
  419. if(heater_ttbl_map[e] != 0)
  420. {
  421. float celsius = 0;
  422. byte i;
  423. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  424. raw = (1023 * OVERSAMPLENR) - raw;
  425. for (i=1; i<heater_ttbllen_map[e]; i++)
  426. {
  427. if (PGM_RD_W((*tt)[i][0]) > raw)
  428. {
  429. celsius = PGM_RD_W((*tt)[i-1][1]) +
  430. (raw - PGM_RD_W((*tt)[i-1][0])) *
  431. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  432. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  433. break;
  434. }
  435. }
  436. // Overflow: Set to last value in the table
  437. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  438. return celsius;
  439. }
  440. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  441. }
  442. // Derived from RepRap FiveD extruder::getTemperature()
  443. // For bed temperature measurement.
  444. float analog2tempBed(int raw) {
  445. #ifdef BED_USES_THERMISTOR
  446. float celsius = 0;
  447. byte i;
  448. raw = (1023 * OVERSAMPLENR) - raw;
  449. for (i=1; i<bedtemptable_len; i++)
  450. {
  451. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  452. {
  453. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  454. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  455. (float)(PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  456. (float)(PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  457. break;
  458. }
  459. }
  460. // Overflow: Set to last value in the table
  461. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  462. return celsius;
  463. #elif defined BED_USES_AD595
  464. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  465. #else
  466. #warning No heater-type defined for the bed.
  467. return 0;
  468. #endif
  469. }
  470. void tp_init()
  471. {
  472. // Finish init of mult extruder arrays
  473. for(int e = 0; e < EXTRUDERS; e++) {
  474. // populate with the first value
  475. #ifdef WATCHPERIOD
  476. watch_raw[e] = watch_raw[0];
  477. #endif
  478. maxttemp[e] = maxttemp[0];
  479. #ifdef PIDTEMP
  480. temp_iState_min[e] = 0.0;
  481. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  482. #endif //PIDTEMP
  483. }
  484. #if (HEATER_0_PIN > -1)
  485. SET_OUTPUT(HEATER_0_PIN);
  486. #endif
  487. #if (HEATER_1_PIN > -1)
  488. SET_OUTPUT(HEATER_1_PIN);
  489. #endif
  490. #if (HEATER_2_PIN > -1)
  491. SET_OUTPUT(HEATER_2_PIN);
  492. #endif
  493. #if (HEATER_BED_PIN > -1)
  494. SET_OUTPUT(HEATER_BED_PIN);
  495. #endif
  496. #if (FAN_PIN > -1)
  497. SET_OUTPUT(FAN_PIN);
  498. #endif
  499. #ifdef HEATER_0_USES_MAX6675
  500. #ifndef SDSUPPORT
  501. SET_OUTPUT(MAX_SCK_PIN);
  502. WRITE(MAX_SCK_PIN,0);
  503. SET_OUTPUT(MAX_MOSI_PIN);
  504. WRITE(MAX_MOSI_PIN,1);
  505. SET_INPUT(MAX_MISO_PIN);
  506. WRITE(MAX_MISO_PIN,1);
  507. #endif
  508. SET_OUTPUT(MAX6675_SS);
  509. WRITE(MAX6675_SS,1);
  510. #endif
  511. // Set analog inputs
  512. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  513. DIDR0 = 0;
  514. #ifdef DIDR2
  515. DIDR2 = 0;
  516. #endif
  517. #if (TEMP_0_PIN > -1)
  518. #if TEMP_0_PIN < 8
  519. DIDR0 |= 1 << TEMP_0_PIN;
  520. #else
  521. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  522. #endif
  523. #endif
  524. #if (TEMP_1_PIN > -1)
  525. #if TEMP_1_PIN < 8
  526. DIDR0 |= 1<<TEMP_1_PIN;
  527. #else
  528. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  529. #endif
  530. #endif
  531. #if (TEMP_2_PIN > -1)
  532. #if TEMP_2_PIN < 8
  533. DIDR0 |= 1 << TEMP_2_PIN;
  534. #else
  535. DIDR2 = 1<<(TEMP_2_PIN - 8);
  536. #endif
  537. #endif
  538. #if (TEMP_BED_PIN > -1)
  539. #if TEMP_BED_PIN < 8
  540. DIDR0 |= 1<<TEMP_BED_PIN;
  541. #else
  542. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  543. #endif
  544. #endif
  545. // Use timer0 for temperature measurement
  546. // Interleave temperature interrupt with millies interrupt
  547. OCR0B = 128;
  548. TIMSK0 |= (1<<OCIE0B);
  549. // Wait for temperature measurement to settle
  550. delay(250);
  551. #ifdef HEATER_0_MINTEMP
  552. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  553. #endif //MINTEMP
  554. #ifdef HEATER_0_MAXTEMP
  555. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  556. #endif //MAXTEMP
  557. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  558. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  559. #endif // MINTEMP 1
  560. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  561. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  562. #endif //MAXTEMP 1
  563. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  564. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  565. #endif //MINTEMP 2
  566. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  567. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  568. #endif //MAXTEMP 2
  569. #ifdef BED_MINTEMP
  570. bed_minttemp = temp2analogBed(BED_MINTEMP);
  571. #endif //BED_MINTEMP
  572. #ifdef BED_MAXTEMP
  573. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  574. #endif //BED_MAXTEMP
  575. }
  576. void setWatch()
  577. {
  578. #ifdef WATCHPERIOD
  579. int t = 0;
  580. for (int e = 0; e < EXTRUDERS; e++)
  581. {
  582. if(isHeatingHotend(e))
  583. watch_oldtemp[0] = degHotend(0);
  584. {
  585. t = max(t,millis());
  586. watch_raw[e] = current_raw[e];
  587. }
  588. }
  589. watchmillis = t;
  590. #endif
  591. }
  592. void disable_heater()
  593. {
  594. for(int i=0;i<EXTRUDERS;i++)
  595. setTargetHotend(0,i);
  596. setTargetBed(0);
  597. #if TEMP_0_PIN > -1
  598. target_raw[0]=0;
  599. soft_pwm[0]=0;
  600. #if HEATER_0_PIN > -1
  601. WRITE(HEATER_0_PIN,LOW);
  602. #endif
  603. #endif
  604. #if TEMP_1_PIN > -1
  605. target_raw[1]=0;
  606. soft_pwm[1]=0;
  607. #if HEATER_1_PIN > -1
  608. WRITE(HEATER_1_PIN,LOW);
  609. #endif
  610. #endif
  611. #if TEMP_2_PIN > -1
  612. target_raw[2]=0;
  613. soft_pwm[2]=0;
  614. #if HEATER_2_PIN > -1
  615. WRITE(HEATER_2_PIN,LOW);
  616. #endif
  617. #endif
  618. #if TEMP_BED_PIN > -1
  619. target_raw_bed=0;
  620. #if HEATER_BED_PIN > -1
  621. WRITE(HEATER_BED_PIN,LOW);
  622. #endif
  623. #endif
  624. }
  625. void max_temp_error(uint8_t e) {
  626. disable_heater();
  627. if(IsStopped() == false) {
  628. SERIAL_ERROR_START;
  629. SERIAL_ERRORLN((int)e);
  630. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  631. }
  632. }
  633. void min_temp_error(uint8_t e) {
  634. disable_heater();
  635. if(IsStopped() == false) {
  636. SERIAL_ERROR_START;
  637. SERIAL_ERRORLN((int)e);
  638. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  639. }
  640. }
  641. void bed_max_temp_error(void) {
  642. WRITE(HEATER_BED_PIN, 0);
  643. if(IsStopped() == false) {
  644. SERIAL_ERROR_START;
  645. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  646. }
  647. }
  648. #define HEAT_INTERVAL 250
  649. #ifdef HEATER_0_USES_MAX6675
  650. long max6675_previous_millis = -HEAT_INTERVAL;
  651. int max6675_temp = 2000;
  652. int read_max6675()
  653. {
  654. if (millis() - max6675_previous_millis < HEAT_INTERVAL)
  655. return max6675_temp;
  656. max6675_previous_millis = millis();
  657. max6675_temp = 0;
  658. #ifdef PRR
  659. PRR &= ~(1<<PRSPI);
  660. #elif defined PRR0
  661. PRR0 &= ~(1<<PRSPI);
  662. #endif
  663. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  664. // enable TT_MAX6675
  665. WRITE(MAX6675_SS, 0);
  666. // ensure 100ns delay - a bit extra is fine
  667. delay(1);
  668. // read MSB
  669. SPDR = 0;
  670. for (;(SPSR & (1<<SPIF)) == 0;);
  671. max6675_temp = SPDR;
  672. max6675_temp <<= 8;
  673. // read LSB
  674. SPDR = 0;
  675. for (;(SPSR & (1<<SPIF)) == 0;);
  676. max6675_temp |= SPDR;
  677. // disable TT_MAX6675
  678. WRITE(MAX6675_SS, 1);
  679. if (max6675_temp & 4)
  680. {
  681. // thermocouple open
  682. max6675_temp = 2000;
  683. }
  684. else
  685. {
  686. max6675_temp = max6675_temp >> 3;
  687. }
  688. return max6675_temp;
  689. }
  690. #endif
  691. // Timer 0 is shared with millies
  692. ISR(TIMER0_COMPB_vect)
  693. {
  694. //these variables are only accesible from the ISR, but static, so they don't loose their value
  695. static unsigned char temp_count = 0;
  696. static unsigned long raw_temp_0_value = 0;
  697. static unsigned long raw_temp_1_value = 0;
  698. static unsigned long raw_temp_2_value = 0;
  699. static unsigned long raw_temp_bed_value = 0;
  700. static unsigned char temp_state = 0;
  701. static unsigned char pwm_count = 1;
  702. static unsigned char soft_pwm_0;
  703. static unsigned char soft_pwm_1;
  704. static unsigned char soft_pwm_2;
  705. if(pwm_count == 0){
  706. soft_pwm_0 = soft_pwm[0];
  707. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  708. #if EXTRUDERS > 1
  709. soft_pwm_1 = soft_pwm[1];
  710. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  711. #endif
  712. #if EXTRUDERS > 2
  713. soft_pwm_2 = soft_pwm[2];
  714. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  715. #endif
  716. }
  717. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  718. #if EXTRUDERS > 1
  719. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  720. #endif
  721. #if EXTRUDERS > 2
  722. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  723. #endif
  724. pwm_count++;
  725. pwm_count &= 0x7f;
  726. switch(temp_state) {
  727. case 0: // Prepare TEMP_0
  728. #if (TEMP_0_PIN > -1)
  729. #if TEMP_0_PIN > 7
  730. ADCSRB = 1<<MUX5;
  731. #else
  732. ADCSRB = 0;
  733. #endif
  734. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  735. ADCSRA |= 1<<ADSC; // Start conversion
  736. #endif
  737. #ifdef ULTIPANEL
  738. buttons_check();
  739. #endif
  740. temp_state = 1;
  741. break;
  742. case 1: // Measure TEMP_0
  743. #if (TEMP_0_PIN > -1)
  744. raw_temp_0_value += ADC;
  745. #endif
  746. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  747. raw_temp_0_value = read_max6675();
  748. #endif
  749. temp_state = 2;
  750. break;
  751. case 2: // Prepare TEMP_BED
  752. #if (TEMP_BED_PIN > -1)
  753. #if TEMP_BED_PIN > 7
  754. ADCSRB = 1<<MUX5;
  755. #endif
  756. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  757. ADCSRA |= 1<<ADSC; // Start conversion
  758. #endif
  759. #ifdef ULTIPANEL
  760. buttons_check();
  761. #endif
  762. temp_state = 3;
  763. break;
  764. case 3: // Measure TEMP_BED
  765. #if (TEMP_BED_PIN > -1)
  766. raw_temp_bed_value += ADC;
  767. #endif
  768. temp_state = 4;
  769. break;
  770. case 4: // Prepare TEMP_1
  771. #if (TEMP_1_PIN > -1)
  772. #if TEMP_1_PIN > 7
  773. ADCSRB = 1<<MUX5;
  774. #else
  775. ADCSRB = 0;
  776. #endif
  777. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  778. ADCSRA |= 1<<ADSC; // Start conversion
  779. #endif
  780. #ifdef ULTIPANEL
  781. buttons_check();
  782. #endif
  783. temp_state = 5;
  784. break;
  785. case 5: // Measure TEMP_1
  786. #if (TEMP_1_PIN > -1)
  787. raw_temp_1_value += ADC;
  788. #endif
  789. temp_state = 6;
  790. break;
  791. case 6: // Prepare TEMP_2
  792. #if (TEMP_2_PIN > -1)
  793. #if TEMP_2_PIN > 7
  794. ADCSRB = 1<<MUX5;
  795. #else
  796. ADCSRB = 0;
  797. #endif
  798. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  799. ADCSRA |= 1<<ADSC; // Start conversion
  800. #endif
  801. #ifdef ULTIPANEL
  802. buttons_check();
  803. #endif
  804. temp_state = 7;
  805. break;
  806. case 7: // Measure TEMP_2
  807. #if (TEMP_2_PIN > -1)
  808. raw_temp_2_value += ADC;
  809. #endif
  810. temp_state = 0;
  811. temp_count++;
  812. break;
  813. // default:
  814. // SERIAL_ERROR_START;
  815. // SERIAL_ERRORLNPGM("Temp measurement error!");
  816. // break;
  817. }
  818. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  819. {
  820. #if defined(HEATER_0_USES_AD595) || defined(HEATER_0_USES_MAX6675)
  821. current_raw[0] = raw_temp_0_value;
  822. #else
  823. current_raw[0] = 16383 - raw_temp_0_value;
  824. #endif
  825. #if EXTRUDERS > 1
  826. #ifdef HEATER_1_USES_AD595
  827. current_raw[1] = raw_temp_1_value;
  828. #else
  829. current_raw[1] = 16383 - raw_temp_1_value;
  830. #endif
  831. #endif
  832. #if EXTRUDERS > 2
  833. #ifdef HEATER_2_USES_AD595
  834. current_raw[2] = raw_temp_2_value;
  835. #else
  836. current_raw[2] = 16383 - raw_temp_2_value;
  837. #endif
  838. #endif
  839. #ifdef BED_USES_AD595
  840. current_raw_bed = raw_temp_bed_value;
  841. #else
  842. current_raw_bed = 16383 - raw_temp_bed_value;
  843. #endif
  844. temp_meas_ready = true;
  845. temp_count = 0;
  846. raw_temp_0_value = 0;
  847. raw_temp_1_value = 0;
  848. raw_temp_2_value = 0;
  849. raw_temp_bed_value = 0;
  850. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  851. if(current_raw[e] >= maxttemp[e]) {
  852. target_raw[e] = 0;
  853. max_temp_error(e);
  854. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  855. {
  856. Stop();;
  857. }
  858. #endif
  859. }
  860. if(current_raw[e] <= minttemp[e]) {
  861. target_raw[e] = 0;
  862. min_temp_error(e);
  863. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  864. {
  865. Stop();
  866. }
  867. #endif
  868. }
  869. }
  870. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  871. if(current_raw_bed >= bed_maxttemp) {
  872. target_raw_bed = 0;
  873. bed_max_temp_error();
  874. Stop();
  875. }
  876. #endif
  877. }
  878. }