My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digipot_mcp4451.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(DIGIPOT_I2C) && DISABLED(DIGIPOT_MCP4018)
  24. #include "Stream.h"
  25. #include <Wire.h>
  26. #if MB(MKS_SBASE)
  27. #include "digipot_mcp4451_I2C_routines.h"
  28. #endif
  29. // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
  30. #if MB(5DPRINT)
  31. #define DIGIPOT_I2C_FACTOR 117.96
  32. #define DIGIPOT_I2C_MAX_CURRENT 1.736
  33. #elif MB(AZTEEG_X5_MINI, AZTEEG_X5_MINI_WIFI)
  34. #define DIGIPOT_I2C_FACTOR 113.5
  35. #define DIGIPOT_I2C_MAX_CURRENT 2.0
  36. #else
  37. #define DIGIPOT_I2C_FACTOR 106.7
  38. #define DIGIPOT_I2C_MAX_CURRENT 2.5
  39. #endif
  40. static byte current_to_wiper(const float current) {
  41. return byte(CEIL(float((DIGIPOT_I2C_FACTOR * current))));
  42. }
  43. static void digipot_i2c_send(const byte addr, const byte a, const byte b) {
  44. #if MB(MKS_SBASE)
  45. digipot_mcp4451_start(addr);
  46. digipot_mcp4451_send_byte(a);
  47. digipot_mcp4451_send_byte(b);
  48. #else
  49. Wire.beginTransmission(I2C_ADDRESS(addr));
  50. Wire.write(a);
  51. Wire.write(b);
  52. Wire.endTransmission();
  53. #endif
  54. }
  55. // This is for the MCP4451 I2C based digipot
  56. void digipot_i2c_set_current(const uint8_t channel, const float current) {
  57. // these addresses are specific to Azteeg X3 Pro, can be set to others,
  58. // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
  59. const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7
  60. // Initial setup
  61. digipot_i2c_send(addr, 0x40, 0xFF);
  62. digipot_i2c_send(addr, 0xA0, 0xFF);
  63. // Set actual wiper value
  64. byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
  65. digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
  66. }
  67. void digipot_i2c_init() {
  68. #if MB(MKS_SBASE)
  69. configure_i2c(16); // Setting clock_option to 16 ensure the I2C bus is initialized at 400kHz
  70. #else
  71. Wire.begin();
  72. #endif
  73. // setup initial currents as defined in Configuration_adv.h
  74. static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
  75. LOOP_L_N(i, COUNT(digipot_motor_current))
  76. digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
  77. }
  78. #endif // DIGIPOT_I2C