Преглед на файлове

⚡️ Fix noisy ADC - 16x oversampling with 12-bit ADC (#23867)

tombrazier преди 3 години
родител
ревизия
631e35bfd6
No account linked to committer's email address

+ 1
- 0
Marlin/src/core/types.h Целия файл

128
 // celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
128
 // celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
129
 // For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
129
 // For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
130
 //
130
 //
131
+typedef uint16_t raw_adc_t;
131
 typedef int16_t celsius_t;
132
 typedef int16_t celsius_t;
132
 typedef float celsius_float_t;
133
 typedef float celsius_float_t;
133
 
134
 

+ 11
- 11
Marlin/src/feature/joystick.cpp Целия файл

68
   void Joystick::report() {
68
   void Joystick::report() {
69
     SERIAL_ECHOPGM("Joystick");
69
     SERIAL_ECHOPGM("Joystick");
70
     #if HAS_JOY_ADC_X
70
     #if HAS_JOY_ADC_X
71
-      SERIAL_ECHOPGM_P(SP_X_STR, JOY_X(x.raw));
71
+      SERIAL_ECHOPGM_P(SP_X_STR, JOY_X(x.getraw()));
72
     #endif
72
     #endif
73
     #if HAS_JOY_ADC_Y
73
     #if HAS_JOY_ADC_Y
74
-      SERIAL_ECHOPGM_P(SP_Y_STR, JOY_Y(y.raw));
74
+      SERIAL_ECHOPGM_P(SP_Y_STR, JOY_Y(y.getraw()));
75
     #endif
75
     #endif
76
     #if HAS_JOY_ADC_Z
76
     #if HAS_JOY_ADC_Z
77
-      SERIAL_ECHOPGM_P(SP_Z_STR, JOY_Z(z.raw));
77
+      SERIAL_ECHOPGM_P(SP_Z_STR, JOY_Z(z.getraw()));
78
     #endif
78
     #endif
79
     #if HAS_JOY_ADC_EN
79
     #if HAS_JOY_ADC_EN
80
       SERIAL_ECHO_TERNARY(READ(JOY_EN_PIN), " EN=", "HIGH (dis", "LOW (en", "abled)");
80
       SERIAL_ECHO_TERNARY(READ(JOY_EN_PIN), " EN=", "HIGH (dis", "LOW (en", "abled)");
91
       if (READ(JOY_EN_PIN)) return;
91
       if (READ(JOY_EN_PIN)) return;
92
     #endif
92
     #endif
93
 
93
 
94
-    auto _normalize_joy = [](float &axis_jog, const int16_t raw, const int16_t (&joy_limits)[4]) {
94
+    auto _normalize_joy = [](float &axis_jog, const raw_adc_t raw, const raw_adc_t (&joy_limits)[4]) {
95
       if (WITHIN(raw, joy_limits[0], joy_limits[3])) {
95
       if (WITHIN(raw, joy_limits[0], joy_limits[3])) {
96
         // within limits, check deadzone
96
         // within limits, check deadzone
97
         if (raw > joy_limits[2])
97
         if (raw > joy_limits[2])
98
           axis_jog = (raw - joy_limits[2]) / float(joy_limits[3] - joy_limits[2]);
98
           axis_jog = (raw - joy_limits[2]) / float(joy_limits[3] - joy_limits[2]);
99
         else if (raw < joy_limits[1])
99
         else if (raw < joy_limits[1])
100
-          axis_jog = (raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]);  // negative value
100
+          axis_jog = int16_t(raw - joy_limits[1]) / float(joy_limits[1] - joy_limits[0]);  // negative value
101
         // Map normal to jog value via quadratic relationship
101
         // Map normal to jog value via quadratic relationship
102
         axis_jog = SIGN(axis_jog) * sq(axis_jog);
102
         axis_jog = SIGN(axis_jog) * sq(axis_jog);
103
       }
103
       }
104
     };
104
     };
105
 
105
 
106
     #if HAS_JOY_ADC_X
106
     #if HAS_JOY_ADC_X
107
-      static constexpr int16_t joy_x_limits[4] = JOY_X_LIMITS;
108
-      _normalize_joy(norm_jog.x, JOY_X(x.raw), joy_x_limits);
107
+      static constexpr raw_adc_t joy_x_limits[4] = JOY_X_LIMITS;
108
+      _normalize_joy(norm_jog.x, JOY_X(x.getraw()), joy_x_limits);
109
     #endif
109
     #endif
110
     #if HAS_JOY_ADC_Y
110
     #if HAS_JOY_ADC_Y
111
-      static constexpr int16_t joy_y_limits[4] = JOY_Y_LIMITS;
112
-      _normalize_joy(norm_jog.y, JOY_Y(y.raw), joy_y_limits);
111
+      static constexpr raw_adc_t joy_y_limits[4] = JOY_Y_LIMITS;
112
+      _normalize_joy(norm_jog.y, JOY_Y(y.getraw()), joy_y_limits);
113
     #endif
113
     #endif
114
     #if HAS_JOY_ADC_Z
114
     #if HAS_JOY_ADC_Z
115
-      static constexpr int16_t joy_z_limits[4] = JOY_Z_LIMITS;
116
-      _normalize_joy(norm_jog.z, JOY_Z(z.raw), joy_z_limits);
115
+      static constexpr raw_adc_t joy_z_limits[4] = JOY_Z_LIMITS;
116
+      _normalize_joy(norm_jog.z, JOY_Z(z.getraw()), joy_z_limits);
117
     #endif
117
     #endif
118
   }
118
   }
119
 
119
 

+ 7
- 7
Marlin/src/lcd/marlinui.cpp Целия файл

1176
   #if HAS_ADC_BUTTONS
1176
   #if HAS_ADC_BUTTONS
1177
 
1177
 
1178
     typedef struct {
1178
     typedef struct {
1179
-      uint16_t ADCKeyValueMin, ADCKeyValueMax;
1179
+      raw_adc_t ADCKeyValueMin, ADCKeyValueMax;
1180
       uint8_t  ADCKeyNo;
1180
       uint8_t  ADCKeyNo;
1181
     } _stADCKeypadTable_;
1181
     } _stADCKeypadTable_;
1182
 
1182
 
1203
     #endif
1203
     #endif
1204
 
1204
 
1205
     // Calculate the ADC value for the voltage divider with specified pull-down resistor value
1205
     // Calculate the ADC value for the voltage divider with specified pull-down resistor value
1206
-    #define ADC_BUTTON_VALUE(r)  int(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
1206
+    #define ADC_BUTTON_VALUE(r)  raw_adc_t(HAL_ADC_RANGE * (ADC_BUTTONS_VALUE_SCALE) * r / (r + ADC_BUTTONS_R_PULLUP))
1207
 
1207
 
1208
-    static constexpr uint16_t adc_button_tolerance = HAL_ADC_RANGE *   25 / 1024,
1209
-                                  adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
1208
+    static constexpr raw_adc_t adc_button_tolerance = HAL_ADC_RANGE *   25 / 1024,
1209
+                                   adc_other_button = HAL_ADC_RANGE * 1000 / 1024;
1210
     static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
1210
     static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
1211
       // VALUE_MIN, VALUE_MAX, KEY
1211
       // VALUE_MIN, VALUE_MAX, KEY
1212
       { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1     }, // F1
1212
       { adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1     }, // F1
1226
 
1226
 
1227
     uint8_t get_ADC_keyValue() {
1227
     uint8_t get_ADC_keyValue() {
1228
       if (thermalManager.ADCKey_count >= 16) {
1228
       if (thermalManager.ADCKey_count >= 16) {
1229
-        const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
1229
+        const raw_adc_t currentkpADCValue = thermalManager.current_ADCKey_raw;
1230
         thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
1230
         thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
1231
         thermalManager.ADCKey_count = 0;
1231
         thermalManager.ADCKey_count = 0;
1232
         if (currentkpADCValue < adc_other_button)
1232
         if (currentkpADCValue < adc_other_button)
1233
           LOOP_L_N(i, ADC_KEY_NUM) {
1233
           LOOP_L_N(i, ADC_KEY_NUM) {
1234
-            const uint16_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
1235
-                           hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
1234
+            const raw_adc_t lo = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMin),
1235
+                            hi = pgm_read_word(&stADCKeyTable[i].ADCKeyValueMax);
1236
             if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
1236
             if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
1237
           }
1237
           }
1238
       }
1238
       }

+ 63
- 63
Marlin/src/module/temperature.cpp Целия файл

437
 #if HAS_HEATED_BED
437
 #if HAS_HEATED_BED
438
   bed_info_t Temperature::temp_bed; // = { 0 }
438
   bed_info_t Temperature::temp_bed; // = { 0 }
439
   // Init min and max temp with extreme values to prevent false errors during startup
439
   // Init min and max temp with extreme values to prevent false errors during startup
440
-  int16_t Temperature::mintemp_raw_BED = TEMP_SENSOR_BED_RAW_LO_TEMP,
441
-          Temperature::maxtemp_raw_BED = TEMP_SENSOR_BED_RAW_HI_TEMP;
440
+  raw_adc_t Temperature::mintemp_raw_BED = TEMP_SENSOR_BED_RAW_LO_TEMP,
441
+            Temperature::maxtemp_raw_BED = TEMP_SENSOR_BED_RAW_HI_TEMP;
442
   TERN_(WATCH_BED, bed_watch_t Temperature::watch_bed); // = { 0 }
442
   TERN_(WATCH_BED, bed_watch_t Temperature::watch_bed); // = { 0 }
443
   IF_DISABLED(PIDTEMPBED, millis_t Temperature::next_bed_check_ms);
443
   IF_DISABLED(PIDTEMPBED, millis_t Temperature::next_bed_check_ms);
444
 #endif
444
 #endif
448
   #if HAS_HEATED_CHAMBER
448
   #if HAS_HEATED_CHAMBER
449
     millis_t next_cool_check_ms_2 = 0;
449
     millis_t next_cool_check_ms_2 = 0;
450
     celsius_float_t old_temp = 9999;
450
     celsius_float_t old_temp = 9999;
451
-    int16_t Temperature::mintemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_LO_TEMP,
452
-            Temperature::maxtemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_HI_TEMP;
451
+    raw_adc_t Temperature::mintemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_LO_TEMP,
452
+              Temperature::maxtemp_raw_CHAMBER = TEMP_SENSOR_CHAMBER_RAW_HI_TEMP;
453
     TERN_(WATCH_CHAMBER, chamber_watch_t Temperature::watch_chamber{0});
453
     TERN_(WATCH_CHAMBER, chamber_watch_t Temperature::watch_chamber{0});
454
     IF_DISABLED(PIDTEMPCHAMBER, millis_t Temperature::next_chamber_check_ms);
454
     IF_DISABLED(PIDTEMPCHAMBER, millis_t Temperature::next_chamber_check_ms);
455
   #endif
455
   #endif
461
     bool flag_cooler_state;
461
     bool flag_cooler_state;
462
     //bool flag_cooler_excess = false;
462
     //bool flag_cooler_excess = false;
463
     celsius_float_t previous_temp = 9999;
463
     celsius_float_t previous_temp = 9999;
464
-    int16_t Temperature::mintemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_LO_TEMP,
465
-            Temperature::maxtemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_HI_TEMP;
464
+    raw_adc_t Temperature::mintemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_LO_TEMP,
465
+              Temperature::maxtemp_raw_COOLER = TEMP_SENSOR_COOLER_RAW_HI_TEMP;
466
     #if WATCH_COOLER
466
     #if WATCH_COOLER
467
       cooler_watch_t Temperature::watch_cooler{0};
467
       cooler_watch_t Temperature::watch_cooler{0};
468
     #endif
468
     #endif
477
 #if HAS_TEMP_BOARD
477
 #if HAS_TEMP_BOARD
478
   board_info_t Temperature::temp_board; // = { 0 }
478
   board_info_t Temperature::temp_board; // = { 0 }
479
   #if ENABLED(THERMAL_PROTECTION_BOARD)
479
   #if ENABLED(THERMAL_PROTECTION_BOARD)
480
-    int16_t Temperature::mintemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_LO_TEMP,
481
-            Temperature::maxtemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_HI_TEMP;
480
+    raw_adc_t Temperature::mintemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_LO_TEMP,
481
+              Temperature::maxtemp_raw_BOARD = TEMP_SENSOR_BOARD_RAW_HI_TEMP;
482
   #endif
482
   #endif
483
 #endif
483
 #endif
484
 
484
 
508
 #endif
508
 #endif
509
 
509
 
510
 #define TEMPDIR(N) ((TEMP_SENSOR_##N##_RAW_LO_TEMP) < (TEMP_SENSOR_##N##_RAW_HI_TEMP) ? 1 : -1)
510
 #define TEMPDIR(N) ((TEMP_SENSOR_##N##_RAW_LO_TEMP) < (TEMP_SENSOR_##N##_RAW_HI_TEMP) ? 1 : -1)
511
+#define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
511
 
512
 
512
 #if HAS_HOTEND
513
 #if HAS_HOTEND
513
   // Init mintemp and maxtemp with extreme values to prevent false errors during startup
514
   // Init mintemp and maxtemp with extreme values to prevent false errors during startup
1689
     m = (l + r) >> 1;                                                     \
1690
     m = (l + r) >> 1;                                                     \
1690
     if (!m) return celsius_t(pgm_read_word(&TBL[0].celsius));             \
1691
     if (!m) return celsius_t(pgm_read_word(&TBL[0].celsius));             \
1691
     if (m == l || m == r) return celsius_t(pgm_read_word(&TBL[LEN-1].celsius)); \
1692
     if (m == l || m == r) return celsius_t(pgm_read_word(&TBL[LEN-1].celsius)); \
1692
-    int16_t v00 = pgm_read_word(&TBL[m-1].value),                         \
1693
-            v10 = pgm_read_word(&TBL[m-0].value);                         \
1693
+    raw_adc_t v00 = pgm_read_word(&TBL[m-1].value),                       \
1694
+              v10 = pgm_read_word(&TBL[m-0].value);                       \
1694
          if (raw < v00) r = m;                                            \
1695
          if (raw < v00) r = m;                                            \
1695
     else if (raw > v10) l = m;                                            \
1696
     else if (raw > v10) l = m;                                            \
1696
     else {                                                                \
1697
     else {                                                                \
1784
     SERIAL_EOL();
1785
     SERIAL_EOL();
1785
   }
1786
   }
1786
 
1787
 
1787
-  celsius_float_t Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const int16_t raw) {
1788
+  celsius_float_t Temperature::user_thermistor_to_deg_c(const uint8_t t_index, const raw_adc_t raw) {
1788
 
1789
 
1789
     if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
1790
     if (!WITHIN(t_index, 0, COUNT(user_thermistor) - 1)) return 25;
1790
 
1791
 
1799
     }
1800
     }
1800
 
1801
 
1801
     // maximum adc value .. take into account the over sampling
1802
     // maximum adc value .. take into account the over sampling
1802
-    const int adc_max = MAX_RAW_THERMISTOR_VALUE,
1803
-              adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
1803
+    constexpr raw_adc_t adc_max = MAX_RAW_THERMISTOR_VALUE;
1804
+    const raw_adc_t adc_raw = constrain(raw, 1, adc_max - 1); // constrain to prevent divide-by-zero
1804
 
1805
 
1805
     const float adc_inverse = (adc_max - adc_raw) - 0.5f,
1806
     const float adc_inverse = (adc_max - adc_raw) - 0.5f,
1806
                 resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
1807
                 resistance = t.series_res * (adc_raw + 0.5f) / adc_inverse,
1820
 #if HAS_HOTEND
1821
 #if HAS_HOTEND
1821
   // Derived from RepRap FiveD extruder::getTemperature()
1822
   // Derived from RepRap FiveD extruder::getTemperature()
1822
   // For hot end temperature measurement.
1823
   // For hot end temperature measurement.
1823
-  celsius_float_t Temperature::analog_to_celsius_hotend(const int16_t raw, const uint8_t e) {
1824
+  celsius_float_t Temperature::analog_to_celsius_hotend(const raw_adc_t raw, const uint8_t e) {
1824
     if (e >= HOTENDS) {
1825
     if (e >= HOTENDS) {
1825
       SERIAL_ERROR_START();
1826
       SERIAL_ERROR_START();
1826
       SERIAL_ECHO(e);
1827
       SERIAL_ECHO(e);
1836
         #elif TEMP_SENSOR_0_IS_MAX_TC
1837
         #elif TEMP_SENSOR_0_IS_MAX_TC
1837
           #if TEMP_SENSOR_0_IS_MAX31865
1838
           #if TEMP_SENSOR_0_IS_MAX31865
1838
             return TERN(LIB_INTERNAL_MAX31865,
1839
             return TERN(LIB_INTERNAL_MAX31865,
1839
-              max31865_0.temperature((uint16_t)raw),
1840
+              max31865_0.temperature(raw),
1840
               max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0)
1841
               max31865_0.temperature(MAX31865_SENSOR_OHMS_0, MAX31865_CALIBRATION_OHMS_0)
1841
             );
1842
             );
1842
           #else
1843
           #else
1843
-            return raw * 0.25;
1844
+            return (int16_t)raw * 0.25;
1844
           #endif
1845
           #endif
1845
         #elif TEMP_SENSOR_0_IS_AD595
1846
         #elif TEMP_SENSOR_0_IS_AD595
1846
           return TEMP_AD595(raw);
1847
           return TEMP_AD595(raw);
1855
         #elif TEMP_SENSOR_1_IS_MAX_TC
1856
         #elif TEMP_SENSOR_1_IS_MAX_TC
1856
           #if TEMP_SENSOR_0_IS_MAX31865
1857
           #if TEMP_SENSOR_0_IS_MAX31865
1857
             return TERN(LIB_INTERNAL_MAX31865,
1858
             return TERN(LIB_INTERNAL_MAX31865,
1858
-              max31865_1.temperature((uint16_t)raw),
1859
+              max31865_1.temperature(raw),
1859
               max31865_1.temperature(MAX31865_SENSOR_OHMS_1, MAX31865_CALIBRATION_OHMS_1)
1860
               max31865_1.temperature(MAX31865_SENSOR_OHMS_1, MAX31865_CALIBRATION_OHMS_1)
1860
             );
1861
             );
1861
           #else
1862
           #else
1862
-            return raw * 0.25;
1863
+            return (int16_t)raw * 0.25;
1863
           #endif
1864
           #endif
1864
         #elif TEMP_SENSOR_1_IS_AD595
1865
         #elif TEMP_SENSOR_1_IS_AD595
1865
           return TEMP_AD595(raw);
1866
           return TEMP_AD595(raw);
1943
 
1944
 
1944
 #if HAS_HEATED_BED
1945
 #if HAS_HEATED_BED
1945
   // For bed temperature measurement.
1946
   // For bed temperature measurement.
1946
-  celsius_float_t Temperature::analog_to_celsius_bed(const int16_t raw) {
1947
+  celsius_float_t Temperature::analog_to_celsius_bed(const raw_adc_t raw) {
1947
     #if TEMP_SENSOR_BED_IS_CUSTOM
1948
     #if TEMP_SENSOR_BED_IS_CUSTOM
1948
       return user_thermistor_to_deg_c(CTI_BED, raw);
1949
       return user_thermistor_to_deg_c(CTI_BED, raw);
1949
     #elif TEMP_SENSOR_BED_IS_THERMISTOR
1950
     #elif TEMP_SENSOR_BED_IS_THERMISTOR
1961
 
1962
 
1962
 #if HAS_TEMP_CHAMBER
1963
 #if HAS_TEMP_CHAMBER
1963
   // For chamber temperature measurement.
1964
   // For chamber temperature measurement.
1964
-  celsius_float_t Temperature::analog_to_celsius_chamber(const int16_t raw) {
1965
+  celsius_float_t Temperature::analog_to_celsius_chamber(const raw_adc_t raw) {
1965
     #if TEMP_SENSOR_CHAMBER_IS_CUSTOM
1966
     #if TEMP_SENSOR_CHAMBER_IS_CUSTOM
1966
       return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
1967
       return user_thermistor_to_deg_c(CTI_CHAMBER, raw);
1967
     #elif TEMP_SENSOR_CHAMBER_IS_THERMISTOR
1968
     #elif TEMP_SENSOR_CHAMBER_IS_THERMISTOR
1979
 
1980
 
1980
 #if HAS_TEMP_COOLER
1981
 #if HAS_TEMP_COOLER
1981
   // For cooler temperature measurement.
1982
   // For cooler temperature measurement.
1982
-  celsius_float_t Temperature::analog_to_celsius_cooler(const int16_t raw) {
1983
+  celsius_float_t Temperature::analog_to_celsius_cooler(const raw_adc_t raw) {
1983
     #if TEMP_SENSOR_COOLER_IS_CUSTOM
1984
     #if TEMP_SENSOR_COOLER_IS_CUSTOM
1984
       return user_thermistor_to_deg_c(CTI_COOLER, raw);
1985
       return user_thermistor_to_deg_c(CTI_COOLER, raw);
1985
     #elif TEMP_SENSOR_COOLER_IS_THERMISTOR
1986
     #elif TEMP_SENSOR_COOLER_IS_THERMISTOR
1997
 
1998
 
1998
 #if HAS_TEMP_PROBE
1999
 #if HAS_TEMP_PROBE
1999
   // For probe temperature measurement.
2000
   // For probe temperature measurement.
2000
-  celsius_float_t Temperature::analog_to_celsius_probe(const int16_t raw) {
2001
+  celsius_float_t Temperature::analog_to_celsius_probe(const raw_adc_t raw) {
2001
     #if TEMP_SENSOR_PROBE_IS_CUSTOM
2002
     #if TEMP_SENSOR_PROBE_IS_CUSTOM
2002
       return user_thermistor_to_deg_c(CTI_PROBE, raw);
2003
       return user_thermistor_to_deg_c(CTI_PROBE, raw);
2003
     #elif TEMP_SENSOR_PROBE_IS_THERMISTOR
2004
     #elif TEMP_SENSOR_PROBE_IS_THERMISTOR
2015
 
2016
 
2016
 #if HAS_TEMP_BOARD
2017
 #if HAS_TEMP_BOARD
2017
   // For motherboard temperature measurement.
2018
   // For motherboard temperature measurement.
2018
-  celsius_float_t Temperature::analog_to_celsius_board(const int16_t raw) {
2019
+  celsius_float_t Temperature::analog_to_celsius_board(const raw_adc_t raw) {
2019
     #if TEMP_SENSOR_BOARD_IS_CUSTOM
2020
     #if TEMP_SENSOR_BOARD_IS_CUSTOM
2020
       return user_thermistor_to_deg_c(CTI_BOARD, raw);
2021
       return user_thermistor_to_deg_c(CTI_BOARD, raw);
2021
     #elif TEMP_SENSOR_BOARD_IS_THERMISTOR
2022
     #elif TEMP_SENSOR_BOARD_IS_THERMISTOR
2033
 
2034
 
2034
 #if HAS_TEMP_REDUNDANT
2035
 #if HAS_TEMP_REDUNDANT
2035
   // For redundant temperature measurement.
2036
   // For redundant temperature measurement.
2036
-  celsius_float_t Temperature::analog_to_celsius_redundant(const int16_t raw) {
2037
+  celsius_float_t Temperature::analog_to_celsius_redundant(const raw_adc_t raw) {
2037
     #if TEMP_SENSOR_REDUNDANT_IS_CUSTOM
2038
     #if TEMP_SENSOR_REDUNDANT_IS_CUSTOM
2038
       return user_thermistor_to_deg_c(CTI_REDUNDANT, raw);
2039
       return user_thermistor_to_deg_c(CTI_REDUNDANT, raw);
2039
     #elif TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E0)
2040
     #elif TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E0)
2040
-      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_0.temperature((uint16_t)raw), raw * 0.25);
2041
+      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_0.temperature(raw), (int16_t)raw * 0.25);
2041
     #elif TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E1)
2042
     #elif TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E1)
2042
-      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_1.temperature((uint16_t)raw), raw * 0.25);
2043
+      return TERN(TEMP_SENSOR_REDUNDANT_IS_MAX31865, max31865_1.temperature(raw), (int16_t)raw * 0.25);
2043
     #elif TEMP_SENSOR_REDUNDANT_IS_THERMISTOR
2044
     #elif TEMP_SENSOR_REDUNDANT_IS_THERMISTOR
2044
       SCAN_THERMISTOR_TABLE(TEMPTABLE_REDUNDANT, TEMPTABLE_REDUNDANT_LEN);
2045
       SCAN_THERMISTOR_TABLE(TEMPTABLE_REDUNDANT, TEMPTABLE_REDUNDANT_LEN);
2045
     #elif TEMP_SENSOR_REDUNDANT_IS_AD595
2046
     #elif TEMP_SENSOR_REDUNDANT_IS_AD595
2069
 
2070
 
2070
   watchdog_refresh(); // Reset because raw_temps_ready was set by the interrupt
2071
   watchdog_refresh(); // Reset because raw_temps_ready was set by the interrupt
2071
 
2072
 
2072
-  TERN_(TEMP_SENSOR_0_IS_MAX_TC, temp_hotend[0].raw = READ_MAX_TC(0));
2073
-  TERN_(TEMP_SENSOR_1_IS_MAX_TC, temp_hotend[1].raw = READ_MAX_TC(1));
2074
-  TERN_(TEMP_SENSOR_REDUNDANT_IS_MAX_TC, temp_redundant.raw = READ_MAX_TC(HEATER_ID(TEMP_SENSOR_REDUNDANT_SOURCE)));
2073
+  TERN_(TEMP_SENSOR_0_IS_MAX_TC, temp_hotend[0].setraw(READ_MAX_TC(0)));
2074
+  TERN_(TEMP_SENSOR_1_IS_MAX_TC, temp_hotend[1].setraw(READ_MAX_TC(1)));
2075
+  TERN_(TEMP_SENSOR_REDUNDANT_IS_MAX_TC, temp_redundant.setraw(READ_MAX_TC(HEATER_ID(TEMP_SENSOR_REDUNDANT_SOURCE))));
2075
 
2076
 
2076
   #if HAS_HOTEND
2077
   #if HAS_HOTEND
2077
-    HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].raw, e);
2078
+    HOTEND_LOOP() temp_hotend[e].celsius = analog_to_celsius_hotend(temp_hotend[e].getraw(), e);
2078
   #endif
2079
   #endif
2079
 
2080
 
2080
-  TERN_(HAS_HEATED_BED,     temp_bed.celsius       = analog_to_celsius_bed(temp_bed.raw));
2081
-  TERN_(HAS_TEMP_CHAMBER,   temp_chamber.celsius   = analog_to_celsius_chamber(temp_chamber.raw));
2082
-  TERN_(HAS_TEMP_COOLER,    temp_cooler.celsius    = analog_to_celsius_cooler(temp_cooler.raw));
2083
-  TERN_(HAS_TEMP_PROBE,     temp_probe.celsius     = analog_to_celsius_probe(temp_probe.raw));
2084
-  TERN_(HAS_TEMP_BOARD,     temp_board.celsius     = analog_to_celsius_board(temp_board.raw));
2085
-  TERN_(HAS_TEMP_REDUNDANT, temp_redundant.celsius = analog_to_celsius_redundant(temp_redundant.raw));
2081
+  TERN_(HAS_HEATED_BED,     temp_bed.celsius       = analog_to_celsius_bed(temp_bed.getraw()));
2082
+  TERN_(HAS_TEMP_CHAMBER,   temp_chamber.celsius   = analog_to_celsius_chamber(temp_chamber.getraw()));
2083
+  TERN_(HAS_TEMP_COOLER,    temp_cooler.celsius    = analog_to_celsius_cooler(temp_cooler.getraw()));
2084
+  TERN_(HAS_TEMP_PROBE,     temp_probe.celsius     = analog_to_celsius_probe(temp_probe.getraw()));
2085
+  TERN_(HAS_TEMP_BOARD,     temp_board.celsius     = analog_to_celsius_board(temp_board.getraw()));
2086
+  TERN_(HAS_TEMP_REDUNDANT, temp_redundant.celsius = analog_to_celsius_redundant(temp_redundant.getraw()));
2086
 
2087
 
2087
   TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm());
2088
   TERN_(FILAMENT_WIDTH_SENSOR, filwidth.update_measured_mm());
2088
   TERN_(HAS_POWER_MONITOR,     power_monitor.capture_values());
2089
   TERN_(HAS_POWER_MONITOR,     power_monitor.capture_values());
2108
     };
2109
     };
2109
 
2110
 
2110
     LOOP_L_N(e, COUNT(temp_dir)) {
2111
     LOOP_L_N(e, COUNT(temp_dir)) {
2111
-      const int8_t tdir = temp_dir[e];
2112
-      if (tdir) {
2113
-        const int16_t rawtemp = temp_hotend[e].raw * tdir; // normal direction, +rawtemp, else -rawtemp
2114
-        if (rawtemp > temp_range[e].raw_max * tdir) max_temp_error((heater_id_t)e);
2115
-
2116
-        const bool heater_on = temp_hotend[e].target > 0;
2117
-        if (heater_on && rawtemp < temp_range[e].raw_min * tdir && !is_preheating(e)) {
2118
-          #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2119
-            if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
2120
-          #endif
2121
-              min_temp_error((heater_id_t)e);
2122
-        }
2112
+      const raw_adc_t r = temp_hotend[e].getraw();
2113
+      const bool neg = temp_dir[e] < 0, pos = temp_dir[e] > 0;
2114
+      if ((neg && r < temp_range[e].raw_max) || (pos && r > temp_range[e].raw_max))
2115
+        max_temp_error((heater_id_t)e);
2116
+
2117
+      const bool heater_on = temp_hotend[e].target > 0;
2118
+      if (heater_on && ((neg && r > temp_range[e].raw_min) || (pos && r < temp_range[e].raw_min))) {
2123
         #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2119
         #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2124
-          else
2125
-            consecutive_low_temperature_error[e] = 0;
2120
+          if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
2126
         #endif
2121
         #endif
2122
+            min_temp_error((heater_id_t)e);
2127
       }
2123
       }
2124
+      #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
2125
+        else
2126
+          consecutive_low_temperature_error[e] = 0;
2127
+      #endif
2128
     }
2128
     }
2129
 
2129
 
2130
   #endif // HAS_HOTEND
2130
   #endif // HAS_HOTEND
2131
 
2131
 
2132
   #define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
2132
   #define TP_CMP(S,A,B) (TEMPDIR(S) < 0 ? ((A)<(B)) : ((A)>(B)))
2133
   #if ENABLED(THERMAL_PROTECTION_BED)
2133
   #if ENABLED(THERMAL_PROTECTION_BED)
2134
-    if (TP_CMP(BED, temp_bed.raw, maxtemp_raw_BED)) max_temp_error(H_BED);
2135
-    if (temp_bed.target > 0 && TP_CMP(BED, mintemp_raw_BED, temp_bed.raw)) min_temp_error(H_BED);
2134
+    if (TP_CMP(BED, temp_bed.getraw(), maxtemp_raw_BED)) max_temp_error(H_BED);
2135
+    if (temp_bed.target > 0 && TP_CMP(BED, mintemp_raw_BED, temp_bed.getraw())) min_temp_error(H_BED);
2136
   #endif
2136
   #endif
2137
 
2137
 
2138
   #if BOTH(HAS_HEATED_CHAMBER, THERMAL_PROTECTION_CHAMBER)
2138
   #if BOTH(HAS_HEATED_CHAMBER, THERMAL_PROTECTION_CHAMBER)
2139
-    if (TP_CMP(CHAMBER, temp_chamber.raw, maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
2140
-    if (temp_chamber.target > 0 && TP_CMP(CHAMBER, mintemp_raw_CHAMBER, temp_chamber.raw)) min_temp_error(H_CHAMBER);
2139
+    if (TP_CMP(CHAMBER, temp_chamber.getraw(), maxtemp_raw_CHAMBER)) max_temp_error(H_CHAMBER);
2140
+    if (temp_chamber.target > 0 && TP_CMP(CHAMBER, mintemp_raw_CHAMBER, temp_chamber.getraw())) min_temp_error(H_CHAMBER);
2141
   #endif
2141
   #endif
2142
 
2142
 
2143
   #if BOTH(HAS_COOLER, THERMAL_PROTECTION_COOLER)
2143
   #if BOTH(HAS_COOLER, THERMAL_PROTECTION_COOLER)
2144
-    if (cutter.unitPower > 0 && TP_CMP(COOLER, temp_cooler.raw, maxtemp_raw_COOLER)) max_temp_error(H_COOLER);
2145
-    if (TP_CMP(COOLER, mintemp_raw_COOLER, temp_cooler.raw)) min_temp_error(H_COOLER);
2144
+    if (cutter.unitPower > 0 && TP_CMP(COOLER, temp_cooler.getraw(), maxtemp_raw_COOLER)) max_temp_error(H_COOLER);
2145
+    if (TP_CMP(COOLER, mintemp_raw_COOLER, temp_cooler.getraw())) min_temp_error(H_COOLER);
2146
   #endif
2146
   #endif
2147
 
2147
 
2148
   #if BOTH(HAS_TEMP_BOARD, THERMAL_PROTECTION_BOARD)
2148
   #if BOTH(HAS_TEMP_BOARD, THERMAL_PROTECTION_BOARD)
2149
-    if (TP_CMP(BOARD, temp_board.raw, maxtemp_raw_BOARD)) max_temp_error(H_BOARD);
2150
-    if (TP_CMP(BOARD, mintemp_raw_BOARD, temp_board.raw)) min_temp_error(H_BOARD);
2149
+    if (TP_CMP(BOARD, temp_board.getraw(), maxtemp_raw_BOARD)) max_temp_error(H_BOARD);
2150
+    if (TP_CMP(BOARD, mintemp_raw_BOARD, temp_board.getraw())) min_temp_error(H_BOARD);
2151
   #endif
2151
   #endif
2152
   #undef TP_CMP
2152
   #undef TP_CMP
2153
 
2153
 
2731
    * @param  hindex  the hotend we're referencing (if MULTI_MAX_TC)
2731
    * @param  hindex  the hotend we're referencing (if MULTI_MAX_TC)
2732
    * @return         integer representing the board's buffer, to be converted later if needed
2732
    * @return         integer representing the board's buffer, to be converted later if needed
2733
    */
2733
    */
2734
-  int16_t Temperature::read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex/*=0*/)) {
2734
+  raw_adc_t Temperature::read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex/*=0*/)) {
2735
     #define MAXTC_HEAT_INTERVAL 250UL
2735
     #define MAXTC_HEAT_INTERVAL 250UL
2736
 
2736
 
2737
     #if HAS_MAX31855
2737
     #if HAS_MAX31855
2750
 
2750
 
2751
     #if HAS_MULTI_MAX_TC
2751
     #if HAS_MULTI_MAX_TC
2752
       // Needed to return the correct temp when this is called between readings
2752
       // Needed to return the correct temp when this is called between readings
2753
-      static int16_t max_tc_temp_previous[MAX_TC_COUNT] = { 0 };
2753
+      static raw_adc_t max_tc_temp_previous[MAX_TC_COUNT] = { 0 };
2754
       #define THERMO_TEMP(I) max_tc_temp_previous[I]
2754
       #define THERMO_TEMP(I) max_tc_temp_previous[I]
2755
       #define THERMO_SEL(A,B) (hindex ? (B) : (A))
2755
       #define THERMO_SEL(A,B) (hindex ? (B) : (A))
2756
       #define MAXTC_CS_WRITE(V) do{ switch (hindex) { case 1: WRITE(TEMP_1_CS_PIN, V); break; default: WRITE(TEMP_0_CS_PIN, V); } }while(0)
2756
       #define MAXTC_CS_WRITE(V) do{ switch (hindex) { case 1: WRITE(TEMP_1_CS_PIN, V); break; default: WRITE(TEMP_0_CS_PIN, V); } }while(0)
2779
     // Return last-read value between readings
2779
     // Return last-read value between readings
2780
     millis_t ms = millis();
2780
     millis_t ms = millis();
2781
     if (PENDING(ms, next_max_tc_ms[hindex]))
2781
     if (PENDING(ms, next_max_tc_ms[hindex]))
2782
-      return (int16_t)THERMO_TEMP(hindex);
2782
+      return THERMO_TEMP(hindex);
2783
 
2783
 
2784
     next_max_tc_ms[hindex] = ms + MAXTC_HEAT_INTERVAL;
2784
     next_max_tc_ms[hindex] = ms + MAXTC_HEAT_INTERVAL;
2785
 
2785
 
2876
 
2876
 
2877
     THERMO_TEMP(hindex) = max_tc_temp;
2877
     THERMO_TEMP(hindex) = max_tc_temp;
2878
 
2878
 
2879
-    return (int16_t)max_tc_temp;
2879
+    return max_tc_temp;
2880
   }
2880
   }
2881
 
2881
 
2882
 #endif // HAS_MAX_TC
2882
 #endif // HAS_MAX_TC
3017
   uint8_t pwm_count_tmp = pwm_count;
3017
   uint8_t pwm_count_tmp = pwm_count;
3018
 
3018
 
3019
   #if HAS_ADC_BUTTONS
3019
   #if HAS_ADC_BUTTONS
3020
-    static unsigned int raw_ADCKey_value = 0;
3020
+    static raw_adc_t raw_ADCKey_value = 0;
3021
     static bool ADCKey_pressed = false;
3021
     static bool ADCKey_pressed = false;
3022
   #endif
3022
   #endif
3023
 
3023
 

+ 29
- 28
Marlin/src/module/temperature.h Целия файл

192
 
192
 
193
 // A temperature sensor
193
 // A temperature sensor
194
 typedef struct TempInfo {
194
 typedef struct TempInfo {
195
-  uint16_t acc;
196
-  int16_t raw;
195
+private:
196
+  raw_adc_t acc;
197
+  raw_adc_t raw;
198
+public:
197
   celsius_float_t celsius;
199
   celsius_float_t celsius;
198
   inline void reset() { acc = 0; }
200
   inline void reset() { acc = 0; }
199
-  inline void sample(const uint16_t s) { acc += s; }
201
+  inline void sample(const raw_adc_t s) { acc += s; }
200
   inline void update() { raw = acc; }
202
   inline void update() { raw = acc; }
203
+  void setraw(const raw_adc_t r) { raw = r; }
204
+  raw_adc_t getraw() { return raw; }
201
 } temp_info_t;
205
 } temp_info_t;
202
 
206
 
203
 #if HAS_TEMP_REDUNDANT
207
 #if HAS_TEMP_REDUNDANT
287
 #endif
291
 #endif
288
 
292
 
289
 // Temperature sensor read value ranges
293
 // Temperature sensor read value ranges
290
-typedef struct { int16_t raw_min, raw_max; } raw_range_t;
291
-typedef struct { celsius_t mintemp, maxtemp; } celsius_range_t;
292
-typedef struct { int16_t raw_min, raw_max; celsius_t mintemp, maxtemp; } temp_range_t;
294
+typedef struct { raw_adc_t raw_min, raw_max; celsius_t mintemp, maxtemp; } temp_range_t;
293
 
295
 
294
 #define THERMISTOR_ABS_ZERO_C           -273.15f  // bbbbrrrrr cold !
296
 #define THERMISTOR_ABS_ZERO_C           -273.15f  // bbbbrrrrr cold !
295
 #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f     // mmmmm comfortable
297
 #define THERMISTOR_RESISTANCE_NOMINAL_C 25.0f     // mmmmm comfortable
492
         static bed_watch_t watch_bed;
494
         static bed_watch_t watch_bed;
493
       #endif
495
       #endif
494
       IF_DISABLED(PIDTEMPBED, static millis_t next_bed_check_ms);
496
       IF_DISABLED(PIDTEMPBED, static millis_t next_bed_check_ms);
495
-      static int16_t mintemp_raw_BED, maxtemp_raw_BED;
497
+      static raw_adc_t mintemp_raw_BED, maxtemp_raw_BED;
496
     #endif
498
     #endif
497
 
499
 
498
     #if HAS_HEATED_CHAMBER
500
     #if HAS_HEATED_CHAMBER
500
         static chamber_watch_t watch_chamber;
502
         static chamber_watch_t watch_chamber;
501
       #endif
503
       #endif
502
       TERN(PIDTEMPCHAMBER,,static millis_t next_chamber_check_ms);
504
       TERN(PIDTEMPCHAMBER,,static millis_t next_chamber_check_ms);
503
-      static int16_t mintemp_raw_CHAMBER, maxtemp_raw_CHAMBER;
505
+      static raw_adc_t mintemp_raw_CHAMBER, maxtemp_raw_CHAMBER;
504
     #endif
506
     #endif
505
 
507
 
506
     #if HAS_COOLER
508
     #if HAS_COOLER
508
         static cooler_watch_t watch_cooler;
510
         static cooler_watch_t watch_cooler;
509
       #endif
511
       #endif
510
       static millis_t next_cooler_check_ms, cooler_fan_flush_ms;
512
       static millis_t next_cooler_check_ms, cooler_fan_flush_ms;
511
-      static int16_t mintemp_raw_COOLER, maxtemp_raw_COOLER;
513
+      static raw_adc_t mintemp_raw_COOLER, maxtemp_raw_COOLER;
512
     #endif
514
     #endif
513
 
515
 
514
     #if HAS_TEMP_BOARD && ENABLED(THERMAL_PROTECTION_BOARD)
516
     #if HAS_TEMP_BOARD && ENABLED(THERMAL_PROTECTION_BOARD)
515
-      static int16_t mintemp_raw_BOARD, maxtemp_raw_BOARD;
517
+      static raw_adc_t mintemp_raw_BOARD, maxtemp_raw_BOARD;
516
     #endif
518
     #endif
517
 
519
 
518
     #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
520
     #if MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED > 1
566
       static user_thermistor_t user_thermistor[USER_THERMISTORS];
568
       static user_thermistor_t user_thermistor[USER_THERMISTORS];
567
       static void M305_report(const uint8_t t_index, const bool forReplay=true);
569
       static void M305_report(const uint8_t t_index, const bool forReplay=true);
568
       static void reset_user_thermistors();
570
       static void reset_user_thermistors();
569
-      static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const int16_t raw);
571
+      static celsius_float_t user_thermistor_to_deg_c(const uint8_t t_index, const raw_adc_t raw);
570
       static bool set_pull_up_res(int8_t t_index, float value) {
572
       static bool set_pull_up_res(int8_t t_index, float value) {
571
         //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
573
         //if (!WITHIN(t_index, 0, USER_THERMISTORS - 1)) return false;
572
         if (!WITHIN(value, 1, 1000000)) return false;
574
         if (!WITHIN(value, 1, 1000000)) return false;
594
     #endif
596
     #endif
595
 
597
 
596
     #if HAS_HOTEND
598
     #if HAS_HOTEND
597
-      static celsius_float_t analog_to_celsius_hotend(const int16_t raw, const uint8_t e);
599
+      static celsius_float_t analog_to_celsius_hotend(const raw_adc_t raw, const uint8_t e);
598
     #endif
600
     #endif
599
     #if HAS_HEATED_BED
601
     #if HAS_HEATED_BED
600
-      static celsius_float_t analog_to_celsius_bed(const int16_t raw);
602
+      static celsius_float_t analog_to_celsius_bed(const raw_adc_t raw);
601
     #endif
603
     #endif
602
     #if HAS_TEMP_CHAMBER
604
     #if HAS_TEMP_CHAMBER
603
-      static celsius_float_t analog_to_celsius_chamber(const int16_t raw);
605
+      static celsius_float_t analog_to_celsius_chamber(const raw_adc_t raw);
604
     #endif
606
     #endif
605
     #if HAS_TEMP_PROBE
607
     #if HAS_TEMP_PROBE
606
-      static celsius_float_t analog_to_celsius_probe(const int16_t raw);
608
+      static celsius_float_t analog_to_celsius_probe(const raw_adc_t raw);
607
     #endif
609
     #endif
608
     #if HAS_TEMP_COOLER
610
     #if HAS_TEMP_COOLER
609
-      static celsius_float_t analog_to_celsius_cooler(const int16_t raw);
611
+      static celsius_float_t analog_to_celsius_cooler(const raw_adc_t raw);
610
     #endif
612
     #endif
611
     #if HAS_TEMP_BOARD
613
     #if HAS_TEMP_BOARD
612
-      static celsius_float_t analog_to_celsius_board(const int16_t raw);
614
+      static celsius_float_t analog_to_celsius_board(const raw_adc_t raw);
613
     #endif
615
     #endif
614
     #if HAS_TEMP_REDUNDANT
616
     #if HAS_TEMP_REDUNDANT
615
-      static celsius_float_t analog_to_celsius_redundant(const int16_t raw);
617
+      static celsius_float_t analog_to_celsius_redundant(const raw_adc_t raw);
616
     #endif
618
     #endif
617
 
619
 
618
     #if HAS_FAN
620
     #if HAS_FAN
707
     }
709
     }
708
 
710
 
709
     #if ENABLED(SHOW_TEMP_ADC_VALUES)
711
     #if ENABLED(SHOW_TEMP_ADC_VALUES)
710
-      static int16_t rawHotendTemp(const uint8_t E_NAME) {
711
-        return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].raw);
712
+      static raw_adc_t rawHotendTemp(const uint8_t E_NAME) {
713
+        return TERN0(HAS_HOTEND, temp_hotend[HOTEND_INDEX].getraw());
712
       }
714
       }
713
     #endif
715
     #endif
714
 
716
 
770
     #if HAS_HEATED_BED
772
     #if HAS_HEATED_BED
771
 
773
 
772
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
774
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
773
-        static int16_t rawBedTemp()    { return temp_bed.raw; }
775
+        static raw_adc_t rawBedTemp()  { return temp_bed.getraw(); }
774
       #endif
776
       #endif
775
       static celsius_float_t degBed()  { return temp_bed.celsius; }
777
       static celsius_float_t degBed()  { return temp_bed.celsius; }
776
       static celsius_t wholeDegBed()   { return static_cast<celsius_t>(degBed() + 0.5f); }
778
       static celsius_t wholeDegBed()   { return static_cast<celsius_t>(degBed() + 0.5f); }
801
 
803
 
802
     #if HAS_TEMP_PROBE
804
     #if HAS_TEMP_PROBE
803
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
805
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
804
-        static int16_t rawProbeTemp()    { return temp_probe.raw; }
806
+        static raw_adc_t rawProbeTemp()  { return temp_probe.getraw(); }
805
       #endif
807
       #endif
806
       static celsius_float_t degProbe()  { return temp_probe.celsius; }
808
       static celsius_float_t degProbe()  { return temp_probe.celsius; }
807
       static celsius_t wholeDegProbe()   { return static_cast<celsius_t>(degProbe() + 0.5f); }
809
       static celsius_t wholeDegProbe()   { return static_cast<celsius_t>(degProbe() + 0.5f); }
812
 
814
 
813
     #if HAS_TEMP_CHAMBER
815
     #if HAS_TEMP_CHAMBER
814
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
816
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
815
-        static int16_t rawChamberTemp()      { return temp_chamber.raw; }
817
+        static raw_adc_t rawChamberTemp()    { return temp_chamber.getraw(); }
816
       #endif
818
       #endif
817
       static celsius_float_t degChamber()    { return temp_chamber.celsius; }
819
       static celsius_float_t degChamber()    { return temp_chamber.celsius; }
818
       static celsius_t wholeDegChamber()     { return static_cast<celsius_t>(degChamber() + 0.5f); }
820
       static celsius_t wholeDegChamber()     { return static_cast<celsius_t>(degChamber() + 0.5f); }
835
 
837
 
836
     #if HAS_TEMP_COOLER
838
     #if HAS_TEMP_COOLER
837
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
839
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
838
-        static int16_t rawCoolerTemp()     { return temp_cooler.raw; }
840
+        static raw_adc_t rawCoolerTemp()   { return temp_cooler.getraw(); }
839
       #endif
841
       #endif
840
       static celsius_float_t degCooler()   { return temp_cooler.celsius; }
842
       static celsius_float_t degCooler()   { return temp_cooler.celsius; }
841
       static celsius_t wholeDegCooler()    { return static_cast<celsius_t>(temp_cooler.celsius + 0.5f); }
843
       static celsius_t wholeDegCooler()    { return static_cast<celsius_t>(temp_cooler.celsius + 0.5f); }
849
 
851
 
850
     #if HAS_TEMP_BOARD
852
     #if HAS_TEMP_BOARD
851
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
853
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
852
-        static int16_t rawBoardTemp()    { return temp_board.raw; }
854
+        static raw_adc_t rawBoardTemp()  { return temp_board.getraw(); }
853
       #endif
855
       #endif
854
       static celsius_float_t degBoard()  { return temp_board.celsius; }
856
       static celsius_float_t degBoard()  { return temp_board.celsius; }
855
       static celsius_t wholeDegBoard()   { return static_cast<celsius_t>(temp_board.celsius + 0.5f); }
857
       static celsius_t wholeDegBoard()   { return static_cast<celsius_t>(temp_board.celsius + 0.5f); }
857
 
859
 
858
     #if HAS_TEMP_REDUNDANT
860
     #if HAS_TEMP_REDUNDANT
859
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
861
       #if ENABLED(SHOW_TEMP_ADC_VALUES)
860
-        static int16_t rawRedundantTemp()         { return temp_redundant.raw; }
861
-        static int16_t rawRedundanTargetTemp()    { return (*temp_redundant.target).raw; }
862
+        static raw_adc_t rawRedundantTemp()       { return temp_redundant.getraw(); }
862
       #endif
863
       #endif
863
       static celsius_float_t degRedundant()       { return temp_redundant.celsius; }
864
       static celsius_float_t degRedundant()       { return temp_redundant.celsius; }
864
       static celsius_float_t degRedundantTarget() { return (*temp_redundant.target).celsius; }
865
       static celsius_float_t degRedundantTarget() { return (*temp_redundant.target).celsius; }
991
       #else
992
       #else
992
         #define READ_MAX_TC(N) read_max_tc()
993
         #define READ_MAX_TC(N) read_max_tc()
993
       #endif
994
       #endif
994
-      static int16_t read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex=0));
995
+      static raw_adc_t read_max_tc(TERN_(HAS_MULTI_MAX_TC, const uint8_t hindex=0));
995
     #endif
996
     #endif
996
 
997
 
997
     #if HAS_AUTO_FAN
998
     #if HAS_AUTO_FAN

+ 7
- 9
Marlin/src/module/thermistor/thermistors.h Целия файл

27
 #define THERMISTOR_TABLE_SCALE (HAL_ADC_RANGE / _BV(THERMISTOR_TABLE_ADC_RESOLUTION))
27
 #define THERMISTOR_TABLE_SCALE (HAL_ADC_RANGE / _BV(THERMISTOR_TABLE_ADC_RESOLUTION))
28
 #if ENABLED(HAL_ADC_FILTERED)
28
 #if ENABLED(HAL_ADC_FILTERED)
29
   #define OVERSAMPLENR 1
29
   #define OVERSAMPLENR 1
30
-#elif HAL_ADC_RESOLUTION > 10
31
-  #define OVERSAMPLENR (20 - HAL_ADC_RESOLUTION)
32
 #else
30
 #else
33
   #define OVERSAMPLENR 16
31
   #define OVERSAMPLENR 16
34
 #endif
32
 #endif
35
-#define MAX_RAW_THERMISTOR_VALUE (HAL_ADC_RANGE * (OVERSAMPLENR) - 1)
36
 
33
 
37
-// Currently Marlin stores all oversampled ADC values as int16_t, make sure the HAL settings do not overflow 15bit
38
-#if MAX_RAW_THERMISTOR_VALUE > ((1 << 15) - 1)
39
-  #error "MAX_RAW_THERMISTOR_VALUE is too large for int16_t. Reduce OVERSAMPLENR or HAL_ADC_RESOLUTION."
34
+// Currently Marlin stores all oversampled ADC values as uint16_t, make sure the HAL settings do not overflow 16 bit
35
+#if (HAL_ADC_RANGE) * (OVERSAMPLENR) > 1 << 16
36
+  #error "MAX_RAW_THERMISTOR_VALUE is too large for uint16_t. Reduce OVERSAMPLENR or HAL_ADC_RESOLUTION."
40
 #endif
37
 #endif
38
+#define MAX_RAW_THERMISTOR_VALUE (uint16_t(HAL_ADC_RANGE) * (OVERSAMPLENR) - 1)
41
 
39
 
42
-#define OV_SCALE(N) (N)
43
-#define OV(N) int16_t(OV_SCALE(N) * (OVERSAMPLENR) * (THERMISTOR_TABLE_SCALE))
40
+#define OV_SCALE(N) float(N)
41
+#define OV(N) raw_adc_t(OV_SCALE(N) * (OVERSAMPLENR) * (THERMISTOR_TABLE_SCALE))
44
 
42
 
45
-typedef struct { int16_t value; celsius_t celsius; } temp_entry_t;
43
+typedef struct { raw_adc_t value; celsius_t celsius; } temp_entry_t;
46
 
44
 
47
 // Pt1000 and Pt100 handling
45
 // Pt1000 and Pt100 handling
48
 //
46
 //

Loading…
Отказ
Запис