Browse Source

✨ Add M3426 A<addr> parameter (#24130)

Co-authored-by: Scott Lahteine <github@thinkyhead.com>
Stephen Hawes 3 years ago
parent
commit
81a6834876
No account linked to committer's email address

+ 3
- 3
Marlin/src/feature/adc/adc_mcp3426.cpp View File

34
 #include "adc_mcp3426.h"
34
 #include "adc_mcp3426.h"
35
 
35
 
36
 // Read the ADC value from MCP342X on a specific channel
36
 // 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) {
38
   Error = false;
38
   Error = false;
39
 
39
 
40
   #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
40
   #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
44
 
44
 
45
   Wire.begin(); // No address joins the BUS as the master
45
   Wire.begin(); // No address joins the BUS as the master
46
 
46
 
47
-  Wire.beginTransmission(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS));
47
+  Wire.beginTransmission(I2C_ADDRESS(address));
48
 
48
 
49
   // Continuous Conversion Mode, 16 bit, Channel 1, Gain x4
49
   // Continuous Conversion Mode, 16 bit, Channel 1, Gain x4
50
   // 26 = 0b00011000
50
   // 26 = 0b00011000
75
   uint8_t buffer[len] = {};
75
   uint8_t buffer[len] = {};
76
 
76
 
77
   do {
77
   do {
78
-    Wire.requestFrom(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS), len);
78
+    Wire.requestFrom(I2C_ADDRESS(address), len);
79
     if (Wire.available() != len) {
79
     if (Wire.available() != len) {
80
       Error = true;
80
       Error = true;
81
       return 0;
81
       return 0;

+ 1
- 4
Marlin/src/feature/adc/adc_mcp3426.h View File

29
 #include <stdint.h>
29
 #include <stdint.h>
30
 #include <Wire.h>
30
 #include <Wire.h>
31
 
31
 
32
-// Address of MCP342X chip
33
-#define MCP342X_ADC_I2C_ADDRESS 104
34
-
35
 class MCP3426 {
32
 class MCP3426 {
36
   public:
33
   public:
37
-    int16_t ReadValue(uint8_t channel, uint8_t gain);
34
+    int16_t ReadValue(uint8_t channel, uint8_t gain, uint8_t address);
38
     bool Error;
35
     bool Error;
39
 };
36
 };
40
 
37
 

+ 10
- 5
Marlin/src/gcode/feature/adc/M3426.cpp View File

28
 
28
 
29
 #include "../../../feature/adc/adc_mcp3426.h"
29
 #include "../../../feature/adc/adc_mcp3426.h"
30
 
30
 
31
+#define MCP3426_BASE_ADDR (0b1101 << 3)
32
+
31
 /**
33
 /**
32
  * M3426: Read 16 bit (signed) value from I2C MCP3426 ADC device
34
  * M3426: Read 16 bit (signed) value from I2C MCP3426 ADC device
33
  *
35
  *
36
  *  M3426 I<byte-2 value in base 10> 0 or 1, invert reply
38
  *  M3426 I<byte-2 value in base 10> 0 or 1, invert reply
37
  */
39
  */
38
 void GcodeSuite::M3426() {
40
 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;
42
 
47
 
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);
45
 
50
 
46
     if (mcp3426.Error == false) {
51
     if (mcp3426.Error == false) {
47
       if (inverted) {
52
       if (inverted) {

Loading…
Cancel
Save