diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index eb29b810..ddffcfac 100644 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -45,6 +45,12 @@ #include "Configuration_prusa.h" +extern "C" { +extern void timer02_init(void); +extern void timer02_set_pwm0(uint8_t pwm0); +} + + //=========================================================================== //=============================public variables============================ //=========================================================================== @@ -257,6 +263,7 @@ static void temp_runaway_stop(bool isPreheat, bool isBed); if (extruder<0) { soft_pwm_bed = (MAX_BED_POWER)/2; + timer02_set_pwm0(soft_pwm_bed << 1); bias = d = (MAX_BED_POWER)/2; } else @@ -293,7 +300,10 @@ static void temp_runaway_stop(bool isPreheat, bool isBed); if(millis() - t2 > 5000) { heating=false; if (extruder<0) + { soft_pwm_bed = (bias - d) >> 1; + timer02_set_pwm0(soft_pwm_bed << 1); + } else soft_pwm[extruder] = (bias - d) >> 1; t1=millis(); @@ -347,7 +357,10 @@ static void temp_runaway_stop(bool isPreheat, bool isBed); } } if (extruder<0) + { soft_pwm_bed = (bias + d) >> 1; + timer02_set_pwm0(soft_pwm_bed << 1); + } else soft_pwm[extruder] = (bias + d) >> 1; pid_cycle++; @@ -781,9 +794,11 @@ void manage_heater() if(current_temperature_bed < BED_MAXTEMP) { soft_pwm_bed = (int)pid_output >> 1; + timer02_set_pwm0(soft_pwm_bed << 1); } else { soft_pwm_bed = 0; + timer02_set_pwm0(soft_pwm_bed << 1); } #elif !defined(BED_LIMIT_SWITCHING) @@ -793,15 +808,18 @@ void manage_heater() if(current_temperature_bed >= target_temperature_bed) { soft_pwm_bed = 0; + timer02_set_pwm0(soft_pwm_bed << 1); } else { soft_pwm_bed = MAX_BED_POWER>>1; + timer02_set_pwm0(soft_pwm_bed << 1); } } else { soft_pwm_bed = 0; + timer02_set_pwm0(soft_pwm_bed << 1); WRITE(HEATER_BED_PIN,LOW); } #else //#ifdef BED_LIMIT_SWITCHING @@ -811,15 +829,18 @@ void manage_heater() if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS) { soft_pwm_bed = 0; + timer02_set_pwm0(soft_pwm_bed << 1); } else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS) { soft_pwm_bed = MAX_BED_POWER>>1; + timer02_set_pwm0(soft_pwm_bed << 1); } } else { soft_pwm_bed = 0; + timer02_set_pwm0(soft_pwm_bed << 1); WRITE(HEATER_BED_PIN,LOW); } #endif @@ -996,7 +1017,6 @@ static void updateTemperaturesFromRawValues() CRITICAL_SECTION_END; } - void tp_init() { #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1)) @@ -1063,10 +1083,12 @@ void tp_init() adc_init(); + timer02_init(); + // Use timer0 for temperature measurement // Interleave temperature interrupt with millies interrupt - OCR0B = 128; - TIMSK0 |= (1< -1 target_temperature_bed=0; soft_pwm_bed=0; + timer02_set_pwm0(soft_pwm_bed << 1); #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1 WRITE(HEATER_BED_PIN,LOW); #endif @@ -1538,8 +1561,8 @@ void adc_ready(void) //callback from adc when sampling finished } // extern "C" -// Timer 0 is shared with millies -ISR(TIMER0_COMPB_vect) // @ 1kHz ~ 1ms +// Timer2 (originaly timer0) is shared with millies +ISR(TIMER2_COMPB_vect) { static bool _lock = false; if (_lock) return; @@ -1606,7 +1629,7 @@ ISR(TIMER0_COMPB_vect) // @ 1kHz ~ 1ms #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); + //if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0); #endif #ifdef FAN_SOFT_PWM soft_pwm_fan = fanSpeedSoftPwm / 2; @@ -1740,7 +1763,7 @@ ISR(TIMER0_COMPB_vect) // @ 1kHz ~ 1ms state_timer_heater_b = MIN_STATE_TIME; } state_heater_b = 1; - WRITE(HEATER_BED_PIN, 1); + //WRITE(HEATER_BED_PIN, 1); } } else { // turn OFF heather only if the minimum time is up diff --git a/Firmware/temperature.h b/Firmware/temperature.h index 9697f440..e94629d1 100644 --- a/Firmware/temperature.h +++ b/Firmware/temperature.h @@ -27,8 +27,8 @@ #include "stepper.h" #endif -#define ENABLE_TEMPERATURE_INTERRUPT() TIMSK0 |= (1< +#include +#include + + +uint8_t timer02_pwm0 = 0; + +void timer02_set_pwm0(uint8_t pwm0) +{ + if (timer02_pwm0 == pwm0) return; + if (pwm0) + { + TCCR0A |= (2 << COM0B0); + OCR0B = pwm0 - 1; + } + else + { + TCCR0A &= ~(2 << COM0B0); + OCR0B = 0; + } +} + +void timer02_init(void) +{ + //save sreg + uint8_t _sreg = SREG; + //disable interrupts for sure + cli(); + //mask timer0 interrupts - disable all + TIMSK0 &= ~(1<> 3) +#define FRACT_MAX (1000 >> 3) + +extern volatile unsigned long timer0_overflow_count; +extern volatile unsigned long timer0_millis; +unsigned char timer0_fract = 0; + +ISR(TIMER2_OVF_vect) +{ + // copy these to local variables so they can be stored in registers + // (volatile variables must be read from memory on every access) + unsigned long m = timer0_millis; + unsigned char f = timer0_fract; + + m += MILLIS_INC; + f += FRACT_INC; + if (f >= FRACT_MAX) + { + f -= FRACT_MAX; + m += 1; + } + + timer0_fract = f; + timer0_millis = m; + timer0_overflow_count++; +} +