dac_mcp4728.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  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. * http://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
  27. *
  28. * For discussion and feedback, please go to:
  29. * http://arduino.cc/forum/index.php/topic,51842.0.html
  30. */
  31. #include "MarlinConfig.h"
  32. #if ENABLED(DAC_STEPPER_CURRENT)
  33. #include "dac_mcp4728.h"
  34. #include "enum.h"
  35. uint16_t mcp4728_values[XYZE];
  36. /**
  37. * Begin I2C, get current values (input register and eeprom) of mcp4728
  38. */
  39. void mcp4728_init() {
  40. Wire.begin();
  41. Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
  42. while (Wire.available()) {
  43. char deviceID = Wire.read(),
  44. hiByte = Wire.read(),
  45. loByte = Wire.read();
  46. if (!(deviceID & 0x08))
  47. mcp4728_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(uint8_t channel, uint16_t value) {
  55. mcp4728_values[channel] = value;
  56. return mcp4728_fastWrite();
  57. }
  58. /**
  59. * Write all input resistor values to EEPROM using SequencialWrite 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(DAC_DEV_ADDRESS);
  65. Wire.write(SEQWRITE);
  66. LOOP_XYZE(i) {
  67. Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[i]));
  68. Wire.write(lowByte(mcp4728_values[i]));
  69. }
  70. return Wire.endTransmission();
  71. }
  72. /**
  73. * Write Voltage reference setting to all input regiters
  74. */
  75. uint8_t mcp4728_setVref_all(uint8_t value) {
  76. Wire.beginTransmission(DAC_DEV_ADDRESS);
  77. Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
  78. return Wire.endTransmission();
  79. }
  80. /**
  81. * Write Gain setting to all input regiters
  82. */
  83. uint8_t mcp4728_setGain_all(uint8_t value) {
  84. Wire.beginTransmission(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(uint8_t channel) { return mcp4728_values[channel]; }
  92. /**
  93. * Steph: Might be useful in the future
  94. * Return Vout
  95. *
  96. uint16_t mcp4728_getVout(uint8_t channel) {
  97. uint32_t vref = 2048,
  98. vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
  99. if (vOut > defaultVDD) vOut = defaultVDD;
  100. return vOut;
  101. }
  102. */
  103. /**
  104. * Returns DAC values as a 0-100 percentage of drive strength
  105. */
  106. uint8_t mcp4728_getDrvPct(uint8_t channel) { return uint8_t(100.0 * mcp4728_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(uint8_t pct[XYZE]) {
  112. LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
  113. mcp4728_fastWrite();
  114. }
  115. /**
  116. * FastWrite input register values - All DAC ouput 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(DAC_DEV_ADDRESS);
  122. LOOP_XYZE(i) {
  123. Wire.write(highByte(mcp4728_values[i]));
  124. Wire.write(lowByte(mcp4728_values[i]));
  125. }
  126. return Wire.endTransmission();
  127. }
  128. /**
  129. * Common function for simple general commands
  130. */
  131. uint8_t mcp4728_simpleCommand(byte simpleCommand) {
  132. Wire.beginTransmission(GENERALCALL);
  133. Wire.write(simpleCommand);
  134. return Wire.endTransmission();
  135. }
  136. #endif // DAC_STEPPER_CURRENT