Skip to content

Commit 631e35b

Browse files
authored
⚡️ Fix noisy ADC - 16x oversampling with 12-bit ADC (MarlinFirmware#23867)
1 parent bf7176f commit 631e35b

File tree

6 files changed

+118
-118
lines changed

6 files changed

+118
-118
lines changed

Marlin/src/core/types.h

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ typedef float feedRate_t;
128128
// celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
129129
// For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
130130
//
131+
typedef uint16_t raw_adc_t;
131132
typedef int16_t celsius_t;
132133
typedef float celsius_float_t;
133134

Marlin/src/feature/joystick.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ Joystick joystick;
6868
void Joystick::report() {
6969
SERIAL_ECHOPGM("Joystick");
7070
#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()));
7272
#endif
7373
#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()));
7575
#endif
7676
#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()));
7878
#endif
7979
#if HAS_JOY_ADC_EN
8080
SERIAL_ECHO_TERNARY(READ(JOY_EN_PIN), " EN=", "HIGH (dis", "LOW (en", "abled)");
@@ -91,29 +91,29 @@ Joystick joystick;
9191
if (READ(JOY_EN_PIN)) return;
9292
#endif
9393

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]) {
9595
if (WITHIN(raw, joy_limits[0], joy_limits[3])) {
9696
// within limits, check deadzone
9797
if (raw > joy_limits[2])
9898
axis_jog = (raw - joy_limits[2]) / float(joy_limits[3] - joy_limits[2]);
9999
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
101101
// Map normal to jog value via quadratic relationship
102102
axis_jog = SIGN(axis_jog) * sq(axis_jog);
103103
}
104104
};
105105

106106
#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);
109109
#endif
110110
#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);
113113
#endif
114114
#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);
117117
#endif
118118
}
119119

Marlin/src/lcd/marlinui.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ void MarlinUI::init() {
11761176
#if HAS_ADC_BUTTONS
11771177

11781178
typedef struct {
1179-
uint16_t ADCKeyValueMin, ADCKeyValueMax;
1179+
raw_adc_t ADCKeyValueMin, ADCKeyValueMax;
11801180
uint8_t ADCKeyNo;
11811181
} _stADCKeypadTable_;
11821182

@@ -1203,10 +1203,10 @@ void MarlinUI::init() {
12031203
#endif
12041204

12051205
// 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))
12071207

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;
12101210
static const _stADCKeypadTable_ stADCKeyTable[] PROGMEM = {
12111211
// VALUE_MIN, VALUE_MAX, KEY
12121212
{ adc_other_button, HAL_ADC_RANGE, 1 + BLEN_KEYPAD_F1 }, // F1
@@ -1226,13 +1226,13 @@ void MarlinUI::init() {
12261226

12271227
uint8_t get_ADC_keyValue() {
12281228
if (thermalManager.ADCKey_count >= 16) {
1229-
const uint16_t currentkpADCValue = thermalManager.current_ADCKey_raw;
1229+
const raw_adc_t currentkpADCValue = thermalManager.current_ADCKey_raw;
12301230
thermalManager.current_ADCKey_raw = HAL_ADC_RANGE;
12311231
thermalManager.ADCKey_count = 0;
12321232
if (currentkpADCValue < adc_other_button)
12331233
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);
12361236
if (WITHIN(currentkpADCValue, lo, hi)) return pgm_read_byte(&stADCKeyTable[i].ADCKeyNo);
12371237
}
12381238
}

0 commit comments

Comments
 (0)