@ -115,14 +115,16 @@ static volatile bool temp_meas_ready = false;
static unsigned long extruder_autofan_last_check ;
# endif
# if EXTRUDERS > 3
# if EXTRUDERS > 4
# error Unsupported number of extruders
# elif EXTRUDERS > 3
# define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3, v4 }
# elif EXTRUDERS > 2
# define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
# define ARRAY_BY_EXTRUDERS(v1, v2, v3 , v4 ) { v1, v2, v3 }
# elif EXTRUDERS > 1
# define ARRAY_BY_EXTRUDERS(v1, v2, v3 ) { v1, v2 }
# define ARRAY_BY_EXTRUDERS(v1, v2, v3 , v4 ) { v1, v2 }
# else
# define ARRAY_BY_EXTRUDERS(v1, v2, v3 ) { v1 }
# define ARRAY_BY_EXTRUDERS(v1, v2, v3 , v4 ) { v1 }
# endif
# ifdef PIDTEMP
@ -144,10 +146,10 @@ static volatile bool temp_meas_ready = false;
# endif //PIDTEMP
// Init min and max temp with extreme values to prevent false errors during startup
static int minttemp_raw [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP ) ;
static int maxttemp_raw [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP ) ;
static int minttemp [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 0 , 0 , 0 ) ;
static int maxttemp [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 16383 , 16383 , 16383 ) ;
static int minttemp_raw [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP , HEATER_3_RAW_LO_TEMP ) ;
static int maxttemp_raw [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP , HEATER_3_RAW_HI_TEMP ) ;
static int minttemp [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 0 , 0 , 0 , 0 ) ;
static int maxttemp [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 16383 , 16383 , 16383 , 16383 ) ;
//static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
# ifdef BED_MAXTEMP
static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP ;
@ -157,8 +159,8 @@ static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
static void * heater_ttbl_map [ 2 ] = { ( void * ) HEATER_0_TEMPTABLE , ( void * ) HEATER_1_TEMPTABLE } ;
static uint8_t heater_ttbllen_map [ 2 ] = { HEATER_0_TEMPTABLE_LEN , HEATER_1_TEMPTABLE_LEN } ;
# else
static void * heater_ttbl_map [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( ( void * ) HEATER_0_TEMPTABLE , ( void * ) HEATER_1_TEMPTABLE , ( void * ) HEATER_2_TEMPTABLE ) ;
static uint8_t heater_ttbllen_map [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( HEATER_0_TEMPTABLE_LEN , HEATER_1_TEMPTABLE_LEN , HEATER_2_TEMPTABLE_LEN ) ;
static void * heater_ttbl_map [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( ( void * ) HEATER_0_TEMPTABLE , ( void * ) HEATER_1_TEMPTABLE , ( void * ) HEATER_2_TEMPTABLE , ( void * ) HEATER_3_TEMPTABLE ) ;
static uint8_t heater_ttbllen_map [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( HEATER_0_TEMPTABLE_LEN , HEATER_1_TEMPTABLE_LEN , HEATER_2_TEMPTABLE_LEN , HEATER_3_TEMPTABLE_LEN ) ;
# endif
static float analog2temp ( int raw , uint8_t e ) ;
@ -166,8 +168,8 @@ static float analog2tempBed(int raw);
static void updateTemperaturesFromRawValues ( ) ;
# ifdef WATCH_TEMP_PERIOD
int watch_start_temp [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 0 , 0 , 0 );
unsigned long watchmillis [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 0 , 0 , 0 );
int watch_start_temp [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 0 , 0 , 0 ,0 );
unsigned long watchmillis [ EXTRUDERS ] = ARRAY_BY_EXTRUDERS ( 0 , 0 , 0 ,0 );
# endif //WATCH_TEMP_PERIOD
# ifndef SOFT_PWM_SCALE
@ -200,7 +202,8 @@ void PID_autotune(float temp, int extruder, int ncycles)
# if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
( defined ( EXTRUDER_1_AUTO_FAN_PIN ) & & EXTRUDER_1_AUTO_FAN_PIN > - 1 ) | | \
( defined ( EXTRUDER_2_AUTO_FAN_PIN ) & & EXTRUDER_2_AUTO_FAN_PIN > - 1 )
( defined ( EXTRUDER_2_AUTO_FAN_PIN ) & & EXTRUDER_2_AUTO_FAN_PIN > - 1 ) | | \
( defined ( EXTRUDER_3_AUTO_FAN_PIN ) & & EXTRUDER_3_AUTO_FAN_PIN > - 1 )
unsigned long extruder_autofan_last_check = millis ( ) ;
# endif
@ -243,7 +246,8 @@ void PID_autotune(float temp, int extruder, int ncycles)
# if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
( defined ( EXTRUDER_1_AUTO_FAN_PIN ) & & EXTRUDER_1_AUTO_FAN_PIN > - 1 ) | | \
( defined ( EXTRUDER_2_AUTO_FAN_PIN ) & & EXTRUDER_2_AUTO_FAN_PIN > - 1 )
( defined ( EXTRUDER_2_AUTO_FAN_PIN ) & & EXTRUDER_2_AUTO_FAN_PIN > - 1 ) | | \
( defined ( EXTRUDER_3_AUTO_FAN_PIN ) & & EXTRUDER_3_AUTO_FAN_PIN > - 1 )
if ( millis ( ) - extruder_autofan_last_check > 2500 ) {
checkExtruderAutoFans ( ) ;
extruder_autofan_last_check = millis ( ) ;
@ -420,6 +424,19 @@ void checkExtruderAutoFans()
fanState | = 4 ;
}
# endif
# if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
if ( current_temperature [ 3 ] > EXTRUDER_AUTO_FAN_TEMPERATURE )
{
if ( EXTRUDER_3_AUTO_FAN_PIN = = EXTRUDER_0_AUTO_FAN_PIN )
fanState | = 1 ;
else if ( EXTRUDER_3_AUTO_FAN_PIN = = EXTRUDER_1_AUTO_FAN_PIN )
fanState | = 2 ;
else if ( EXTRUDER_3_AUTO_FAN_PIN = = EXTRUDER_2_AUTO_FAN_PIN )
fanState | = 4 ;
else
fanState | = 8 ;
}
# endif
// update extruder auto fan states
# if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
@ -434,6 +451,12 @@ void checkExtruderAutoFans()
& & EXTRUDER_2_AUTO_FAN_PIN ! = EXTRUDER_1_AUTO_FAN_PIN )
setExtruderAutoFanState ( EXTRUDER_2_AUTO_FAN_PIN , ( fanState & 4 ) ! = 0 ) ;
# endif
# if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
if ( EXTRUDER_3_AUTO_FAN_PIN ! = EXTRUDER_0_AUTO_FAN_PIN
& & EXTRUDER_3_AUTO_FAN_PIN ! = EXTRUDER_1_AUTO_FAN_PIN )
& & EXTRUDER_3_AUTO_FAN_PIN ! = EXTRUDER_0_AUTO_FAN_PIN )
setExtruderAutoFanState ( EXTRUDER_3_AUTO_FAN_PIN , ( fanState & 8 ) ! = 0 ) ;
# endif
}
# endif // any extruder auto fan pins set
@ -837,6 +860,9 @@ void tp_init()
# if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
SET_OUTPUT ( HEATER_2_PIN ) ;
# endif
# if defined(HEATER_3_PIN) && (HEATER_3_PIN > -1)
SET_OUTPUT ( HEATER_3_PIN ) ;
# endif
# if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
SET_OUTPUT ( HEATER_BED_PIN ) ;
# endif
@ -897,6 +923,13 @@ void tp_init()
DIDR2 | = 1 < < ( TEMP_2_PIN - 8 ) ;
# endif
# endif
# if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
# if TEMP_3_PIN < 8
DIDR0 | = 1 < < TEMP_3_PIN ;
# else
DIDR2 | = 1 < < ( TEMP_3_PIN - 8 ) ;
# endif
# endif
# if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
# if TEMP_BED_PIN < 8
DIDR0 | = 1 < < TEMP_BED_PIN ;
@ -987,6 +1020,28 @@ void tp_init()
}
# endif //MAXTEMP 2
# if (EXTRUDERS > 3) && defined(HEATER_3_MINTEMP)
minttemp [ 3 ] = HEATER_3_MINTEMP ;
while ( analog2temp ( minttemp_raw [ 3 ] , 3 ) < HEATER_3_MINTEMP ) {
# if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
minttemp_raw [ 3 ] + = OVERSAMPLENR ;
# else
minttemp_raw [ 3 ] - = OVERSAMPLENR ;
# endif
}
# endif //MINTEMP 3
# if (EXTRUDERS > 3) && defined(HEATER_3_MAXTEMP)
maxttemp [ 3 ] = HEATER_3_MAXTEMP ;
while ( analog2temp ( maxttemp_raw [ 3 ] , 3 ) > HEATER_3_MAXTEMP ) {
# if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
maxttemp_raw [ 3 ] - = OVERSAMPLENR ;
# else
maxttemp_raw [ 3 ] + = OVERSAMPLENR ;
# endif
}
# endif // MAXTEMP 3
# ifdef BED_MINTEMP
/* No bed MINTEMP error implemented?!? */ /*
while ( analog2tempBed ( bed_minttemp_raw ) < BED_MINTEMP ) {
@ -1075,6 +1130,7 @@ void thermal_runaway_protection(int *state, unsigned long *timer, float temperat
disable_e0 ( ) ;
disable_e1 ( ) ;
disable_e2 ( ) ;
disable_e3 ( ) ;
manage_heater ( ) ;
lcd_update ( ) ;
}
@ -1113,6 +1169,15 @@ void disable_heater()
# endif
# endif
# if defined(TEMP_3_PIN) && TEMP_3_PIN > -1 && EXTRUDERS > 3
target_temperature [ 3 ] = 0 ;
soft_pwm [ 3 ] = 0 ;
# if defined(HEATER_3_PIN) && HEATER_3_PIN > -1
WRITE ( HEATER_3_PIN , LOW ) ;
# endif
# endif
# if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
target_temperature_bed = 0 ;
soft_pwm_bed = 0 ;
@ -1227,8 +1292,9 @@ ISR(TIMER0_COMPB_vect)
static unsigned long raw_temp_0_value = 0 ;
static unsigned long raw_temp_1_value = 0 ;
static unsigned long raw_temp_2_value = 0 ;
static unsigned long raw_temp_3_value = 0 ;
static unsigned long raw_temp_bed_value = 0 ;
static unsigned char temp_state = 1 0 ;
static unsigned char temp_state = 1 2 ;
static unsigned char pwm_count = ( 1 < < SOFT_PWM_SCALE ) ;
static unsigned char soft_pwm_0 ;
# ifdef SLOW_PWM_HEATERS
@ -1236,6 +1302,7 @@ ISR(TIMER0_COMPB_vect)
static unsigned char state_heater_0 = 0 ;
static unsigned char state_timer_heater_0 = 0 ;
# endif
# if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
static unsigned char soft_pwm_1 ;
# ifdef SLOW_PWM_HEATERS
@ -1250,6 +1317,14 @@ ISR(TIMER0_COMPB_vect)
static unsigned char state_timer_heater_2 = 0 ;
# endif
# endif
# if EXTRUDERS > 3
static unsigned char soft_pwm_3 ;
# ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_3 = 0 ;
static unsigned char state_timer_heater_3 = 0 ;
# endif
# endif
# if HEATER_BED_PIN > -1
static unsigned char soft_pwm_b ;
# ifdef SLOW_PWM_HEATERS
@ -1283,6 +1358,12 @@ ISR(TIMER0_COMPB_vect)
soft_pwm_2 = soft_pwm [ 2 ] ;
if ( soft_pwm_2 > 0 ) WRITE ( HEATER_2_PIN , 1 ) ; else WRITE ( HEATER_2_PIN , 0 ) ;
# endif
# if EXTRUDERS > 3
soft_pwm_3 = soft_pwm [ 3 ] ;
if ( soft_pwm_3 > 0 ) WRITE ( HEATER_3_PIN , 1 ) ; else WRITE ( HEATER_3_PIN , 0 ) ;
# endif
# if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
soft_pwm_b = soft_pwm_bed ;
if ( soft_pwm_b > 0 ) WRITE ( HEATER_BED_PIN , 1 ) ; else WRITE ( HEATER_BED_PIN , 0 ) ;
@ -1298,12 +1379,17 @@ ISR(TIMER0_COMPB_vect)
WRITE ( HEATER_1_PIN , 0 ) ;
# endif
}
# if EXTRUDERS > 1
if ( soft_pwm_1 < pwm_count ) WRITE ( HEATER_1_PIN , 0 ) ;
# endif
# if EXTRUDERS > 2
if ( soft_pwm_2 < pwm_count ) WRITE ( HEATER_2_PIN , 0 ) ;
# endif
# if EXTRUDERS > 3
if ( soft_pwm_3 < pwm_count ) WRITE ( HEATER_3_PIN , 0 ) ;
# endif
# if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
if ( soft_pwm_b < pwm_count ) WRITE ( HEATER_BED_PIN , 0 ) ;
# endif
@ -1406,6 +1492,32 @@ ISR(TIMER0_COMPB_vect)
}
# endif
# if EXTRUDERS > 3
// EXTRUDER 3
soft_pwm_3 = soft_pwm [ 3 ] ;
if ( soft_pwm_3 > 0 ) {
// turn ON heather only if the minimum time is up
if ( state_timer_heater_3 = = 0 ) {
// if change state set timer
if ( state_heater_3 = = 0 ) {
state_timer_heater_3 = MIN_STATE_TIME ;
}
state_heater_3 = 1 ;
WRITE ( HEATER_3_PIN , 1 ) ;
}
} else {
// turn OFF heather only if the minimum time is up
if ( state_timer_heater_3 = = 0 ) {
// if change state set timer
if ( state_heater_3 = = 1 ) {
state_timer_heater_3 = MIN_STATE_TIME ;
}
state_heater_3 = 0 ;
WRITE ( HEATER_3_PIN , 0 ) ;
}
}
# endif
# if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// BED
soft_pwm_b = soft_pwm_bed ;
@ -1479,6 +1591,21 @@ ISR(TIMER0_COMPB_vect)
}
# endif
# if EXTRUDERS > 3
// EXTRUDER 3
if ( soft_pwm_3 < slow_pwm_count ) {
// turn OFF heather only if the minimum time is up
if ( state_timer_heater_3 = = 0 ) {
// if change state set timer
if ( state_heater_3 = = 1 ) {
state_timer_heater_3 = MIN_STATE_TIME ;
}
state_heater_3 = 0 ;
WRITE ( HEATER_3_PIN , 0 ) ;
}
}
# endif
# if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// BED
if ( soft_pwm_b < slow_pwm_count ) {
@ -1527,6 +1654,12 @@ ISR(TIMER0_COMPB_vect)
state_timer_heater_2 - - ;
# endif
# if EXTRUDERS > 3
// Extruder 3
if ( state_timer_heater_3 > 0 )
state_timer_heater_3 - - ;
# endif
# if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// Bed
if ( state_timer_heater_b > 0 )
@ -1614,10 +1747,28 @@ ISR(TIMER0_COMPB_vect)
# if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
raw_temp_2_value + = ADC ;
# endif
temp_state = 8 ; //change so that Filament Width is also measured
temp_state = 8 ;
break ;
case 8 : //Prepare FILWIDTH
case 8 : // Prepare TEMP_3
# if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
# if TEMP_3_PIN > 7
ADCSRB = 1 < < MUX5 ;
# else
ADCSRB = 0 ;
# endif
ADMUX = ( ( 1 < < REFS0 ) | ( TEMP_3_PIN & 0x07 ) ) ;
ADCSRA | = 1 < < ADSC ; // Start conversion
# endif
lcd_buttons_update ( ) ;
temp_state = 9 ;
break ;
case 9 : // Measure TEMP_3
# if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
raw_temp_3_value + = ADC ;
# endif
temp_state = 10 ; //change so that Filament Width is also measured
break ;
case 10 : //Prepare FILWIDTH
# if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
# if FILWIDTH_PIN>7
ADCSRB = 1 < < MUX5 ;
@ -1628,9 +1779,9 @@ ISR(TIMER0_COMPB_vect)
ADCSRA | = 1 < < ADSC ; // Start conversion
# endif
lcd_buttons_update ( ) ;
temp_state = 9 ;
temp_state = 11 ;
break ;
case 9 : //Measure FILWIDTH
case 11 : //Measure FILWIDTH
# if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
//raw_filwidth_value += ADC; //remove to use an IIR filter approach
if ( ADC > 102 ) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
@ -1646,7 +1797,7 @@ ISR(TIMER0_COMPB_vect)
break ;
case 1 0 : //Startup, delay initial temp reading a tiny bit so the hardware can settle.
case 1 2 : //Startup, delay initial temp reading a tiny bit so the hardware can settle.
temp_state = 0 ;
break ;
// default:
@ -1668,6 +1819,9 @@ ISR(TIMER0_COMPB_vect)
# endif
# if EXTRUDERS > 2
current_temperature_raw [ 2 ] = raw_temp_2_value ;
# endif
# if EXTRUDERS > 3
current_temperature_raw [ 3 ] = raw_temp_3_value ;
# endif
current_temperature_bed_raw = raw_temp_bed_value ;
}
@ -1683,6 +1837,7 @@ ISR(TIMER0_COMPB_vect)
raw_temp_0_value = 0 ;
raw_temp_1_value = 0 ;
raw_temp_2_value = 0 ;
raw_temp_3_value = 0 ;
raw_temp_bed_value = 0 ;
# if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
@ -1699,6 +1854,8 @@ ISR(TIMER0_COMPB_vect)
# endif
min_temp_error ( 0 ) ;
}
# if EXTRUDERS > 1
# if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
if ( current_temperature_raw [ 1 ] < = maxttemp_raw [ 1 ] ) {
@ -1731,6 +1888,23 @@ ISR(TIMER0_COMPB_vect)
min_temp_error ( 2 ) ;
}
# endif
# if EXTRUDERS > 3
# if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
if ( current_temperature_raw [ 3 ] < = maxttemp_raw [ 3 ] ) {
# else
if ( current_temperature_raw [ 3 ] > = maxttemp_raw [ 3 ] ) {
# endif
max_temp_error ( 3 ) ;
}
# if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
if ( current_temperature_raw [ 3 ] > = minttemp_raw [ 3 ] ) {
# else
if ( current_temperature_raw [ 3 ] < = minttemp_raw [ 3 ] ) {
# endif
min_temp_error ( 3 ) ;
}
# endif
/* No bed MINTEMP error? */
# if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
@ -1790,5 +1964,3 @@ float unscalePID_d(float d)
}
# endif //PIDTEMP