Bladeren bron

Add digipot i2c control for MCP4018

Paweł Stawicki 7 jaren geleden
bovenliggende
commit
191fec009f
2 gewijzigde bestanden met toevoegingen van 92 en 1 verwijderingen
  1. 91
    0
      Marlin/digipot_mcp4018.cpp
  2. 1
    1
      Marlin/digipot_mcp4451.cpp

+ 91
- 0
Marlin/digipot_mcp4018.cpp Bestand weergeven

@@ -0,0 +1,91 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "MarlinConfig.h"
24
+
25
+#if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
26
+
27
+#include "Stream.h"
28
+#include "utility/twi.h"
29
+#include <SlowSoftI2CMaster.h>  //https://github.com/felias-fogg/SlowSoftI2CMaster
30
+
31
+// Settings for the I2C based DIGIPOT (MCP4018) based on WT150
32
+
33
+#define DIGIPOT_I2C_ADDRESS             0x2f
34
+
35
+#define DIGIPOT_A4988_Rsx               0.250
36
+#define DIGIPOT_A4988_Vrefmax           5.0
37
+#define DIGIPOT_A4988_MAX_VALUE         127
38
+
39
+#define DIGIPOT_A4988_Itripmax(Vref)    ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
40
+
41
+#define DIGIPOT_A4988_FACTOR            (DIGIPOT_A4988_MAX_VALUE/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
42
+//TODO: MAX_CURRENT -0.5A ?? (currently set to 2A, max possible current 2.5A)
43
+#define DIGIPOT_A4988_MAX_CURRENT       (DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax) - 0.5)
44
+
45
+static byte current_to_wiper(float current) {
46
+  return byte(ceil(float((DIGIPOT_A4988_FACTOR * current))));
47
+}
48
+
49
+static uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
50
+  DIGIPOTS_I2C_SDA_X,
51
+  DIGIPOTS_I2C_SDA_Y,
52
+  DIGIPOTS_I2C_SDA_Z,
53
+  DIGIPOTS_I2C_SDA_E0,
54
+  DIGIPOTS_I2C_SDA_E1,
55
+};
56
+
57
+static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
58
+  SlowSoftI2CMaster { sda_pins[0], DIGIPOTS_I2C_SCL },
59
+  SlowSoftI2CMaster { sda_pins[1], DIGIPOTS_I2C_SCL },
60
+  SlowSoftI2CMaster { sda_pins[2], DIGIPOTS_I2C_SCL },
61
+  SlowSoftI2CMaster { sda_pins[3], DIGIPOTS_I2C_SCL },
62
+  SlowSoftI2CMaster { sda_pins[4], DIGIPOTS_I2C_SCL }
63
+};
64
+
65
+static void i2c_send(int channel, byte v) {
66
+  if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
67
+    pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
68
+    pots[channel].i2c_write(v);
69
+    pots[channel].i2c_stop();
70
+  }
71
+}
72
+
73
+// This is for the MCP4018 I2C based digipot
74
+void digipot_i2c_set_current(int channel, float current) {
75
+  current = min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT));
76
+
77
+  i2c_send(channel, current_to_wiper(current));
78
+}
79
+
80
+void digipot_i2c_init() {
81
+  const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
82
+
83
+  for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
84
+    pots[i].i2c_init();
85
+
86
+  // setup initial currents as defined in Configuration_adv.h
87
+  for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
88
+    digipot_i2c_set_current(i, digipot_motor_current[i]);
89
+}
90
+
91
+#endif // DIGIPOT_I2C && DIGIPOT_MCP4018

+ 1
- 1
Marlin/digipot_mcp4451.cpp Bestand weergeven

@@ -22,7 +22,7 @@
22 22
 
23 23
 #include "MarlinConfig.h"
24 24
 
25
-#if ENABLED(DIGIPOT_I2C)
25
+#if ENABLED(DIGIPOT_I2C) && DISABLED(DIGIPOT_MCP4018)
26 26
 
27 27
 #include "Stream.h"
28 28
 #include "utility/twi.h"

Laden…
Annuleren
Opslaan