12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
-
-
- #include "MarlinConfig.h"
-
- #if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
-
- #include "Stream.h"
- #include "utility/twi.h"
- #include <SlowSoftI2CMaster.h> //https://github.com/stawel/SlowSoftI2CMaster
-
-
-
- #define DIGIPOT_I2C_ADDRESS 0x2f
-
- #define DIGIPOT_A4988_Rsx 0.250
- #define DIGIPOT_A4988_Vrefmax 5.0
- #define DIGIPOT_A4988_MAX_VALUE 127
-
- #define DIGIPOT_A4988_Itripmax(Vref) ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
-
- #define DIGIPOT_A4988_FACTOR (DIGIPOT_A4988_MAX_VALUE/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
-
- #define DIGIPOT_A4988_MAX_CURRENT (DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax) - 0.5)
-
- static byte current_to_wiper(float current) {
- return byte(ceil(float((DIGIPOT_A4988_FACTOR * current))));
- }
-
- static uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
- DIGIPOTS_I2C_SDA_X,
- DIGIPOTS_I2C_SDA_Y,
- DIGIPOTS_I2C_SDA_Z,
- DIGIPOTS_I2C_SDA_E0,
- DIGIPOTS_I2C_SDA_E1,
- };
-
- static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
- SlowSoftI2CMaster { sda_pins[0], DIGIPOTS_I2C_SCL },
- SlowSoftI2CMaster { sda_pins[1], DIGIPOTS_I2C_SCL },
- SlowSoftI2CMaster { sda_pins[2], DIGIPOTS_I2C_SCL },
- SlowSoftI2CMaster { sda_pins[3], DIGIPOTS_I2C_SCL },
- SlowSoftI2CMaster { sda_pins[4], DIGIPOTS_I2C_SCL }
- };
-
- static void i2c_send(int channel, byte v) {
- if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
- pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
- pots[channel].i2c_write(v);
- pots[channel].i2c_stop();
- }
- }
-
-
- void digipot_i2c_set_current(int channel, float current) {
- current = min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT));
-
- i2c_send(channel, current_to_wiper(current));
- }
-
- void digipot_i2c_init() {
- const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
-
- for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
- pots[i].i2c_init();
-
-
- for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
- digipot_i2c_set_current(i, digipot_motor_current[i]);
- }
-
- #endif
|