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.2KB

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