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.

dac_mcp4728.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /**
  23. * mcp4728.cpp - Arduino library for MicroChip MCP4728 I2C D/A converter
  24. *
  25. * For implementation details, please take a look at the datasheet:
  26. * https://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
  27. *
  28. * For discussion and feedback, please go to:
  29. * https://forum.arduino.cc/index.php/topic,51842.0.html
  30. */
  31. #include "../../inc/MarlinConfig.h"
  32. #if HAS_MOTOR_CURRENT_DAC
  33. #include "dac_mcp4728.h"
  34. MCP4728 mcp4728;
  35. xyze_uint_t dac_values;
  36. /**
  37. * Begin I2C, get current values (input register and eeprom) of mcp4728
  38. */
  39. void MCP4728::init() {
  40. Wire.begin();
  41. Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24));
  42. while (Wire.available()) {
  43. char deviceID = Wire.read(),
  44. hiByte = Wire.read(),
  45. loByte = Wire.read();
  46. if (!(deviceID & 0x08))
  47. dac_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
  48. }
  49. }
  50. /**
  51. * Write input resister value to specified channel using fastwrite method.
  52. * Channel : 0-3, Values : 0-4095
  53. */
  54. uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) {
  55. dac_values[channel] = value;
  56. return fastWrite();
  57. }
  58. /**
  59. * Write all input resistor values to EEPROM using SequentialWrite method.
  60. * This will update both input register and EEPROM value
  61. * This will also write current Vref, PowerDown, Gain settings to EEPROM
  62. */
  63. uint8_t MCP4728::eepromWrite() {
  64. Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
  65. Wire.write(SEQWRITE);
  66. LOOP_LOGICAL_AXES(i) {
  67. Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i]));
  68. Wire.write(lowByte(dac_values[i]));
  69. }
  70. return Wire.endTransmission();
  71. }
  72. /**
  73. * Write Voltage reference setting to all input registers
  74. */
  75. uint8_t MCP4728::setVref_all(const uint8_t value) {
  76. Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
  77. Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
  78. return Wire.endTransmission();
  79. }
  80. /**
  81. * Write Gain setting to all input registers
  82. */
  83. uint8_t MCP4728::setGain_all(const uint8_t value) {
  84. Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
  85. Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
  86. return Wire.endTransmission();
  87. }
  88. /**
  89. * Return Input Register value
  90. */
  91. uint16_t MCP4728::getValue(const uint8_t channel) { return dac_values[channel]; }
  92. #if 0
  93. /**
  94. * Steph: Might be useful in the future
  95. * Return Vout
  96. */
  97. uint16_t MCP4728::getVout(const uint8_t channel) {
  98. const uint32_t vref = 2048,
  99. vOut = (vref * dac_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
  100. return _MIN(vOut, defaultVDD);
  101. }
  102. #endif
  103. /**
  104. * Returns DAC values as a 0-100 percentage of drive strength
  105. */
  106. uint8_t MCP4728::getDrvPct(const uint8_t channel) { return uint8_t(100.0 * dac_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
  107. /**
  108. * Receives all Drive strengths as 0-100 percent values, updates
  109. * DAC Values array and calls fastwrite to update the DAC.
  110. */
  111. void MCP4728::setDrvPct(xyze_uint_t &pct) {
  112. dac_values = pct * (DAC_STEPPER_MAX) * 0.01f;
  113. fastWrite();
  114. }
  115. /**
  116. * FastWrite input register values - All DAC output update. refer to DATASHEET 5.6.1
  117. * DAC Input and PowerDown bits update.
  118. * No EEPROM update
  119. */
  120. uint8_t MCP4728::fastWrite() {
  121. Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
  122. LOOP_LOGICAL_AXES(i) {
  123. Wire.write(highByte(dac_values[i]));
  124. Wire.write(lowByte(dac_values[i]));
  125. }
  126. return Wire.endTransmission();
  127. }
  128. /**
  129. * Common function for simple general commands
  130. */
  131. uint8_t MCP4728::simpleCommand(const byte simpleCommand) {
  132. Wire.beginTransmission(I2C_ADDRESS(GENERALCALL));
  133. Wire.write(simpleCommand);
  134. return Wire.endTransmission();
  135. }
  136. #endif // HAS_MOTOR_CURRENT_DAC