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 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "MarlinConfig.h"
  23. #if ENABLED(DIGIPOT_I2C) && DISABLED(DIGIPOT_MCP4018)
  24. #include "Stream.h"
  25. #include "utility/twi.h"
  26. #include "Wire.h"
  27. // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
  28. #if MB(5DPRINT)
  29. #define DIGIPOT_I2C_FACTOR 117.96
  30. #define DIGIPOT_I2C_MAX_CURRENT 1.736
  31. #else
  32. #define DIGIPOT_I2C_FACTOR 106.7
  33. #define DIGIPOT_I2C_MAX_CURRENT 2.5
  34. #endif
  35. static byte current_to_wiper(const float current) {
  36. return byte(CEIL(float((DIGIPOT_I2C_FACTOR * current))));
  37. }
  38. static void i2c_send(const byte addr, const byte a, const byte b) {
  39. Wire.beginTransmission(addr);
  40. Wire.write(a);
  41. Wire.write(b);
  42. Wire.endTransmission();
  43. }
  44. // This is for the MCP4451 I2C based digipot
  45. void digipot_i2c_set_current(uint8_t channel, float current) {
  46. current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT);
  47. // these addresses are specific to Azteeg X3 Pro, can be set to others,
  48. // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
  49. byte addr = 0x2C; // channel 0-3
  50. if (channel >= 4) {
  51. addr = 0x2E; // channel 4-7
  52. channel -= 4;
  53. }
  54. // Initial setup
  55. i2c_send(addr, 0x40, 0xFF);
  56. i2c_send(addr, 0xA0, 0xFF);
  57. // Set actual wiper value
  58. byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
  59. i2c_send(addr, addresses[channel], current_to_wiper(current));
  60. }
  61. void digipot_i2c_init() {
  62. static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
  63. Wire.begin();
  64. // setup initial currents as defined in Configuration_adv.h
  65. for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
  66. digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
  67. }
  68. #endif // DIGIPOT_I2C