Skip to content

Commit 3a72f00

Browse files
sphawesthinkyhead
authored andcommitted
✨ Add M3426 A<addr> parameter (MarlinFirmware#24130)
Co-authored-by: Scott Lahteine <github@thinkyhead.com>
1 parent 69a19ee commit 3a72f00

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

Marlin/src/feature/adc/adc_mcp3426.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "adc_mcp3426.h"
3535

3636
// Read the ADC value from MCP342X on a specific channel
37-
int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
37+
int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain, uint8_t address) {
3838
Error = false;
3939

4040
#if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
@@ -44,7 +44,7 @@ int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
4444

4545
Wire.begin(); // No address joins the BUS as the master
4646

47-
Wire.beginTransmission(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS));
47+
Wire.beginTransmission(I2C_ADDRESS(address));
4848

4949
// Continuous Conversion Mode, 16 bit, Channel 1, Gain x4
5050
// 26 = 0b00011000
@@ -75,7 +75,7 @@ int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
7575
uint8_t buffer[len] = {};
7676

7777
do {
78-
Wire.requestFrom(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS), len);
78+
Wire.requestFrom(I2C_ADDRESS(address), len);
7979
if (Wire.available() != len) {
8080
Error = true;
8181
return 0;

Marlin/src/feature/adc/adc_mcp3426.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929
#include <stdint.h>
3030
#include <Wire.h>
3131

32-
// Address of MCP342X chip
33-
#define MCP342X_ADC_I2C_ADDRESS 104
34-
3532
class MCP3426 {
3633
public:
37-
int16_t ReadValue(uint8_t channel, uint8_t gain);
34+
int16_t ReadValue(uint8_t channel, uint8_t gain, uint8_t address);
3835
bool Error;
3936
};
4037

Marlin/src/gcode/feature/adc/M3426.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#include "../../../feature/adc/adc_mcp3426.h"
3030

31+
#define MCP3426_BASE_ADDR (0b1101 << 3)
32+
3133
/**
3234
* M3426: Read 16 bit (signed) value from I2C MCP3426 ADC device
3335
*
@@ -36,12 +38,15 @@
3638
* M3426 I<byte-2 value in base 10> 0 or 1, invert reply
3739
*/
3840
void GcodeSuite::M3426() {
39-
uint8_t channel = parser.byteval('C', 1), // Select the channel 1 or 2
40-
gain = parser.byteval('G', 1);
41-
const bool inverted = parser.byteval('I') == 1;
41+
uint8_t channel = parser.byteval('C', 1), // Channel 1 or 2
42+
gain = parser.byteval('G', 1), // Gain 1, 2, 4, or 8
43+
address = parser.byteval('A', 3); // Address 0-7 (or 104-111)
44+
const bool inverted = parser.boolval('I');
45+
46+
if (address <= 7) address += MCP3426_BASE_ADDR;
4247

43-
if (channel <= 2 && (gain == 1 || gain == 2 || gain == 4 || gain == 8)) {
44-
int16_t result = mcp3426.ReadValue(channel, gain);
48+
if (WITHIN(channel, 1, 2) && (gain == 1 || gain == 2 || gain == 4 || gain == 8) && WITHIN(address, MCP3426_BASE_ADDR, MCP3426_BASE_ADDR + 7)) {
49+
int16_t result = mcp3426.ReadValue(channel, gain, address);
4550

4651
if (mcp3426.Error == false) {
4752
if (inverted) {

0 commit comments

Comments
 (0)