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

temperature.cpp 26KB

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