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

temperature.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 <avr/pgmspace.h>
  24. #include "fastio.h"
  25. #include "Configuration.h"
  26. #include "pins.h"
  27. #include "Marlin.h"
  28. #include "ultralcd.h"
  29. #include "temperature.h"
  30. #include "watchdog.h"
  31. //===========================================================================
  32. //=============================public variables============================
  33. //===========================================================================
  34. int target_raw[EXTRUDERS] = { 0 };
  35. int target_raw_bed = 0;
  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;
  43. float Kd=DEFAULT_Kd;
  44. #ifdef PID_ADD_EXTRUSION_RATE
  45. float Kc=DEFAULT_Kc;
  46. #endif
  47. #endif //PIDTEMP
  48. //===========================================================================
  49. //=============================private variables============================
  50. //===========================================================================
  51. static 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. static int watch_raw[EXTRUDERS] = { -1000 }; // the first value used for all
  72. static int watch_oldtemp[3] = {0,0,0};
  73. static 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 updatePID()
  117. {
  118. #ifdef PIDTEMP
  119. for(int e = 0; e < EXTRUDERS; e++) {
  120. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  121. }
  122. #endif
  123. }
  124. int getHeaterPower(int heater) {
  125. return soft_pwm[heater];
  126. }
  127. void manage_heater()
  128. {
  129. #ifdef USE_WATCHDOG
  130. wd_reset();
  131. #endif
  132. float pid_input;
  133. float pid_output;
  134. if(temp_meas_ready != true) //better readability
  135. return;
  136. CRITICAL_SECTION_START;
  137. temp_meas_ready = false;
  138. CRITICAL_SECTION_END;
  139. for(int e = 0; e < EXTRUDERS; e++)
  140. {
  141. #ifdef PIDTEMP
  142. pid_input = analog2temp(current_raw[e], e);
  143. #ifndef PID_OPENLOOP
  144. pid_error[e] = pid_setpoint[e] - pid_input;
  145. if(pid_error[e] > 10) {
  146. pid_output = PID_MAX;
  147. pid_reset[e] = true;
  148. }
  149. else if(pid_error[e] < -10) {
  150. pid_output = 0;
  151. pid_reset[e] = true;
  152. }
  153. else {
  154. if(pid_reset[e] == true) {
  155. temp_iState[e] = 0.0;
  156. pid_reset[e] = false;
  157. }
  158. pTerm[e] = Kp * pid_error[e];
  159. temp_iState[e] += pid_error[e];
  160. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  161. iTerm[e] = Ki * temp_iState[e];
  162. //K1 defined in Configuration.h in the PID settings
  163. #define K2 (1.0-K1)
  164. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  165. temp_dState[e] = pid_input;
  166. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  167. }
  168. #endif //PID_OPENLOOP
  169. #ifdef PID_DEBUG
  170. SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
  171. #endif //PID_DEBUG
  172. #else /* PID off */
  173. pid_output = 0;
  174. if(current_raw[e] < target_raw[e]) {
  175. pid_output = PID_MAX;
  176. }
  177. #endif
  178. // Check if temperature is within the correct range
  179. if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e]))
  180. {
  181. //analogWrite(heater_pin_map[e], pid_output);
  182. soft_pwm[e] = (int)pid_output >> 1;
  183. }
  184. else {
  185. //analogWrite(heater_pin_map[e], 0);
  186. soft_pwm[e] = 0;
  187. }
  188. } // End extruder for loop
  189. #ifdef WATCHPERIOD
  190. if(watchmillis && millis() - watchmillis > WATCHPERIOD){
  191. if(watch_oldtemp[TEMPSENSOR_HOTEND_0] >= degHotend(active_extruder)){
  192. setTargetHotend(0,active_extruder);
  193. LCD_MESSAGEPGM("Heating failed");
  194. SERIAL_ECHO_START;
  195. SERIAL_ECHOLN("Heating failed");
  196. }else{
  197. watchmillis = 0;
  198. }
  199. }
  200. #endif
  201. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  202. return;
  203. previous_millis_bed_heater = millis();
  204. #if TEMP_BED_PIN > -1
  205. // Check if temperature is within the correct range
  206. if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
  207. if(current_raw_bed >= target_raw_bed)
  208. {
  209. WRITE(HEATER_BED_PIN,LOW);
  210. }
  211. else
  212. {
  213. WRITE(HEATER_BED_PIN,HIGH);
  214. }
  215. }
  216. else {
  217. WRITE(HEATER_BED_PIN,LOW);
  218. }
  219. #endif
  220. }
  221. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  222. // Takes hot end temperature value as input and returns corresponding raw value.
  223. // For a thermistor, it uses the RepRap thermistor temp table.
  224. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  225. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  226. int temp2analog(int celsius, uint8_t e) {
  227. if(e >= EXTRUDERS)
  228. {
  229. SERIAL_ERROR_START;
  230. SERIAL_ERROR((int)e);
  231. SERIAL_ERRORLNPGM(" - Invalid extruder number!");
  232. kill();
  233. }
  234. if(heater_ttbl_map[e] != 0)
  235. {
  236. int raw = 0;
  237. byte i;
  238. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  239. for (i=1; i<heater_ttbllen_map[e]; i++)
  240. {
  241. if (PGM_RD_W((*tt)[i][1]) < celsius)
  242. {
  243. raw = PGM_RD_W((*tt)[i-1][0]) +
  244. (celsius - PGM_RD_W((*tt)[i-1][1])) *
  245. (PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
  246. (PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
  247. break;
  248. }
  249. }
  250. // Overflow: Set to last value in the table
  251. if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
  252. return (1023 * OVERSAMPLENR) - raw;
  253. }
  254. return celsius * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
  255. }
  256. // Takes bed temperature value as input and returns corresponding raw value.
  257. // For a thermistor, it uses the RepRap thermistor temp table.
  258. // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
  259. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
  260. int temp2analogBed(int celsius) {
  261. #ifdef BED_USES_THERMISTOR
  262. int raw = 0;
  263. byte i;
  264. for (i=1; i<bedtemptable_len; i++)
  265. {
  266. if (PGM_RD_W(bedtemptable[i][1]) < celsius)
  267. {
  268. raw = PGM_RD_W(bedtemptable[i-1][0]) +
  269. (celsius - PGM_RD_W(bedtemptable[i-1][1])) *
  270. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
  271. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
  272. break;
  273. }
  274. }
  275. // Overflow: Set to last value in the table
  276. if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
  277. return (1023 * OVERSAMPLENR) - raw;
  278. #elif defined BED_USES_AD595
  279. return lround(celsius * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
  280. #else
  281. #warning No heater-type defined for the bed.
  282. return 0;
  283. #endif
  284. }
  285. // Derived from RepRap FiveD extruder::getTemperature()
  286. // For hot end temperature measurement.
  287. float analog2temp(int raw, uint8_t e) {
  288. if(e >= EXTRUDERS)
  289. {
  290. SERIAL_ERROR_START;
  291. SERIAL_ERROR((int)e);
  292. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  293. kill();
  294. }
  295. if(heater_ttbl_map[e] != 0)
  296. {
  297. float celsius = 0;
  298. byte i;
  299. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  300. raw = (1023 * OVERSAMPLENR) - raw;
  301. for (i=1; i<heater_ttbllen_map[e]; i++)
  302. {
  303. if (PGM_RD_W((*tt)[i][0]) > raw)
  304. {
  305. celsius = PGM_RD_W((*tt)[i-1][1]) +
  306. (raw - PGM_RD_W((*tt)[i-1][0])) *
  307. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  308. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  309. break;
  310. }
  311. }
  312. // Overflow: Set to last value in the table
  313. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  314. return celsius;
  315. }
  316. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  317. }
  318. // Derived from RepRap FiveD extruder::getTemperature()
  319. // For bed temperature measurement.
  320. float analog2tempBed(int raw) {
  321. #ifdef BED_USES_THERMISTOR
  322. int celsius = 0;
  323. byte i;
  324. raw = (1023 * OVERSAMPLENR) - raw;
  325. for (i=1; i<bedtemptable_len; i++)
  326. {
  327. if (PGM_RD_W(bedtemptable[i][0]) > raw)
  328. {
  329. celsius = PGM_RD_W(bedtemptable[i-1][1]) +
  330. (raw - PGM_RD_W(bedtemptable[i-1][0])) *
  331. (PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1])) /
  332. (PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0]));
  333. break;
  334. }
  335. }
  336. // Overflow: Set to last value in the table
  337. if (i == bedtemptable_len) celsius = PGM_RD_W(bedtemptable[i-1][1]);
  338. return celsius;
  339. #elif defined BED_USES_AD595
  340. return raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR;
  341. #else
  342. #warning No heater-type defined for the bed.
  343. #endif
  344. return 0;
  345. }
  346. void tp_init()
  347. {
  348. // Finish init of mult extruder arrays
  349. for(int e = 0; e < EXTRUDERS; e++) {
  350. // populate with the first value
  351. #ifdef WATCHPERIOD
  352. watch_raw[e] = watch_raw[0];
  353. #endif
  354. maxttemp[e] = maxttemp[0];
  355. #ifdef PIDTEMP
  356. temp_iState_min[e] = 0.0;
  357. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  358. #endif //PIDTEMP
  359. }
  360. #if (HEATER_0_PIN > -1)
  361. SET_OUTPUT(HEATER_0_PIN);
  362. #endif
  363. #if (HEATER_1_PIN > -1)
  364. SET_OUTPUT(HEATER_1_PIN);
  365. #endif
  366. #if (HEATER_2_PIN > -1)
  367. SET_OUTPUT(HEATER_2_PIN);
  368. #endif
  369. #if (HEATER_BED_PIN > -1)
  370. SET_OUTPUT(HEATER_BED_PIN);
  371. #endif
  372. #if (FAN_PIN > -1)
  373. SET_OUTPUT(FAN_PIN);
  374. #endif
  375. // Set analog inputs
  376. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  377. DIDR0 = 0;
  378. #ifdef DIDR2
  379. DIDR2 = 0;
  380. #endif
  381. #if (TEMP_0_PIN > -1)
  382. #if TEMP_0_PIN < 8
  383. DIDR0 |= 1 << TEMP_0_PIN;
  384. #else
  385. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  386. #endif
  387. #endif
  388. #if (TEMP_1_PIN > -1)
  389. #if TEMP_1_PIN < 8
  390. DIDR0 |= 1<<TEMP_1_PIN;
  391. #else
  392. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  393. #endif
  394. #endif
  395. #if (TEMP_2_PIN > -1)
  396. #if TEMP_2_PIN < 8
  397. DIDR0 |= 1 << TEMP_2_PIN;
  398. #else
  399. DIDR2 = 1<<(TEMP_2_PIN - 8);
  400. #endif
  401. #endif
  402. #if (TEMP_BED_PIN > -1)
  403. #if TEMP_BED_PIN < 8
  404. DIDR0 |= 1<<TEMP_BED_PIN;
  405. #else
  406. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  407. #endif
  408. #endif
  409. // Use timer0 for temperature measurement
  410. // Interleave temperature interrupt with millies interrupt
  411. OCR0B = 128;
  412. TIMSK0 |= (1<<OCIE0B);
  413. // Wait for temperature measurement to settle
  414. delay(250);
  415. #ifdef HEATER_0_MINTEMP
  416. minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0);
  417. #endif //MINTEMP
  418. #ifdef HEATER_0_MAXTEMP
  419. maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0);
  420. #endif //MAXTEMP
  421. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  422. minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1);
  423. #endif // MINTEMP 1
  424. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  425. maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1);
  426. #endif //MAXTEMP 1
  427. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  428. minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2);
  429. #endif //MINTEMP 2
  430. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  431. maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2);
  432. #endif //MAXTEMP 2
  433. #ifdef BED_MINTEMP
  434. bed_minttemp = temp2analogBed(BED_MINTEMP);
  435. #endif //BED_MINTEMP
  436. #ifdef BED_MAXTEMP
  437. bed_maxttemp = temp2analogBed(BED_MAXTEMP);
  438. #endif //BED_MAXTEMP
  439. }
  440. void setWatch()
  441. {
  442. #ifdef WATCHPERIOD
  443. int t = 0;
  444. for (int e = 0; e < EXTRUDERS; e++)
  445. {
  446. if(isHeatingHotend(e))
  447. watch_oldtemp[TEMPSENSOR_HOTEND_0] = degHotend(0);
  448. {
  449. t = max(t,millis());
  450. watch_raw[e] = current_raw[e];
  451. }
  452. }
  453. watchmillis = t;
  454. #endif
  455. }
  456. void disable_heater()
  457. {
  458. #if TEMP_0_PIN > -1
  459. target_raw[0]=0;
  460. soft_pwm[0]=0;
  461. #if HEATER_0_PIN > -1
  462. digitalWrite(HEATER_0_PIN,LOW);
  463. #endif
  464. #endif
  465. #if TEMP_1_PIN > -1
  466. target_raw[1]=0;
  467. soft_pwm[1]=0;
  468. #if HEATER_1_PIN > -1
  469. digitalWrite(HEATER_1_PIN,LOW);
  470. #endif
  471. #endif
  472. #if TEMP_2_PIN > -1
  473. target_raw[2]=0;
  474. soft_pwm[2]=0;
  475. #if HEATER_2_PIN > -1
  476. digitalWrite(HEATER_2_PIN,LOW);
  477. #endif
  478. #endif
  479. #if TEMP_BED_PIN > -1
  480. target_raw_bed=0;
  481. #if HEATER_BED_PIN > -1
  482. digitalWrite(HEATER_BED_PIN,LOW);
  483. #endif
  484. #endif
  485. }
  486. void max_temp_error(uint8_t e) {
  487. digitalWrite(heater_pin_map[e], 0);
  488. SERIAL_ERROR_START;
  489. SERIAL_ERRORLN(e);
  490. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  491. }
  492. void min_temp_error(uint8_t e) {
  493. digitalWrite(heater_pin_map[e], 0);
  494. SERIAL_ERROR_START;
  495. SERIAL_ERRORLN(e);
  496. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  497. }
  498. void bed_max_temp_error(void) {
  499. digitalWrite(HEATER_BED_PIN, 0);
  500. SERIAL_ERROR_START;
  501. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  502. }
  503. // Timer 0 is shared with millies
  504. ISR(TIMER0_COMPB_vect)
  505. {
  506. //these variables are only accesible from the ISR, but static, so they don't loose their value
  507. static unsigned char temp_count = 0;
  508. static unsigned long raw_temp_0_value = 0;
  509. static unsigned long raw_temp_1_value = 0;
  510. static unsigned long raw_temp_2_value = 0;
  511. static unsigned long raw_temp_bed_value = 0;
  512. static unsigned char temp_state = 0;
  513. static unsigned char pwm_count = 1;
  514. static unsigned char soft_pwm_0;
  515. static unsigned char soft_pwm_1;
  516. static unsigned char soft_pwm_2;
  517. if(pwm_count == 0){
  518. soft_pwm_0 = soft_pwm[0];
  519. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  520. #if EXTRUDERS > 1
  521. soft_pwm_1 = soft_pwm[1];
  522. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  523. #endif
  524. #if EXTRUDERS > 2
  525. soft_pwm_2 = soft_pwm[2];
  526. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  527. #endif
  528. }
  529. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  530. #if EXTRUDERS > 1
  531. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  532. #endif
  533. #if EXTRUDERS > 2
  534. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  535. #endif
  536. pwm_count++;
  537. pwm_count &= 0x7f;
  538. switch(temp_state) {
  539. case 0: // Prepare TEMP_0
  540. #if (TEMP_0_PIN > -1)
  541. #if TEMP_0_PIN > 7
  542. ADCSRB = 1<<MUX5;
  543. #else
  544. ADCSRB = 0;
  545. #endif
  546. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  547. ADCSRA |= 1<<ADSC; // Start conversion
  548. #endif
  549. #ifdef ULTIPANEL
  550. buttons_check();
  551. #endif
  552. temp_state = 1;
  553. break;
  554. case 1: // Measure TEMP_0
  555. #if (TEMP_0_PIN > -1)
  556. raw_temp_0_value += ADC;
  557. #endif
  558. temp_state = 2;
  559. break;
  560. case 2: // Prepare TEMP_BED
  561. #if (TEMP_BED_PIN > -1)
  562. #if TEMP_BED_PIN > 7
  563. ADCSRB = 1<<MUX5;
  564. #endif
  565. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  566. ADCSRA |= 1<<ADSC; // Start conversion
  567. #endif
  568. #ifdef ULTIPANEL
  569. buttons_check();
  570. #endif
  571. temp_state = 3;
  572. break;
  573. case 3: // Measure TEMP_BED
  574. #if (TEMP_BED_PIN > -1)
  575. raw_temp_bed_value += ADC;
  576. #endif
  577. temp_state = 4;
  578. break;
  579. case 4: // Prepare TEMP_1
  580. #if (TEMP_1_PIN > -1)
  581. #if TEMP_1_PIN > 7
  582. ADCSRB = 1<<MUX5;
  583. #else
  584. ADCSRB = 0;
  585. #endif
  586. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  587. ADCSRA |= 1<<ADSC; // Start conversion
  588. #endif
  589. #ifdef ULTIPANEL
  590. buttons_check();
  591. #endif
  592. temp_state = 5;
  593. break;
  594. case 5: // Measure TEMP_1
  595. #if (TEMP_1_PIN > -1)
  596. raw_temp_1_value += ADC;
  597. #endif
  598. temp_state = 6;
  599. break;
  600. case 6: // Prepare TEMP_2
  601. #if (TEMP_2_PIN > -1)
  602. #if TEMP_2_PIN > 7
  603. ADCSRB = 1<<MUX5;
  604. #else
  605. ADCSRB = 0;
  606. #endif
  607. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  608. ADCSRA |= 1<<ADSC; // Start conversion
  609. #endif
  610. #ifdef ULTIPANEL
  611. buttons_check();
  612. #endif
  613. temp_state = 7;
  614. break;
  615. case 7: // Measure TEMP_2
  616. #if (TEMP_2_PIN > -1)
  617. raw_temp_2_value += ADC;
  618. #endif
  619. temp_state = 0;
  620. temp_count++;
  621. break;
  622. // default:
  623. // SERIAL_ERROR_START;
  624. // SERIAL_ERRORLNPGM("Temp measurement error!");
  625. // break;
  626. }
  627. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  628. {
  629. #ifdef HEATER_0_USES_AD595
  630. current_raw[0] = raw_temp_0_value;
  631. #else
  632. current_raw[0] = 16383 - raw_temp_0_value;
  633. #endif
  634. #if EXTRUDERS > 1
  635. #ifdef HEATER_1_USES_AD595
  636. current_raw[1] = raw_temp_1_value;
  637. #else
  638. current_raw[1] = 16383 - raw_temp_1_value;
  639. #endif
  640. #endif
  641. #if EXTRUDERS > 2
  642. #ifdef HEATER_2_USES_AD595
  643. current_raw[2] = raw_temp_2_value;
  644. #else
  645. current_raw[2] = 16383 - raw_temp_2_value;
  646. #endif
  647. #endif
  648. #ifdef BED_USES_AD595
  649. current_raw_bed = raw_temp_bed_value;
  650. #else
  651. current_raw_bed = 16383 - raw_temp_bed_value;
  652. #endif
  653. temp_meas_ready = true;
  654. temp_count = 0;
  655. raw_temp_0_value = 0;
  656. raw_temp_1_value = 0;
  657. raw_temp_2_value = 0;
  658. raw_temp_bed_value = 0;
  659. for(unsigned char e = 0; e < EXTRUDERS; e++) {
  660. if(current_raw[e] >= maxttemp[e]) {
  661. target_raw[e] = 0;
  662. max_temp_error(e);
  663. kill();;
  664. }
  665. if(current_raw[e] <= minttemp[e]) {
  666. target_raw[e] = 0;
  667. min_temp_error(e);
  668. kill();
  669. }
  670. }
  671. #if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1)
  672. if(current_raw_bed >= bed_maxttemp) {
  673. target_raw_bed = 0;
  674. bed_max_temp_error();
  675. kill();
  676. }
  677. #endif
  678. }
  679. }