My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

dac_mcp4728.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "dac_mcp4728.h"
  32. #if ENABLED(DAC_STEPPER_CURRENT)
  33. uint16_t mcp4728_values[XYZE];
  34. /**
  35. * Begin I2C, get current values (input register and eeprom) of mcp4728
  36. */
  37. void mcp4728_init() {
  38. Wire.begin();
  39. Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
  40. while (Wire.available()) {
  41. char deviceID = Wire.read(),
  42. hiByte = Wire.read(),
  43. loByte = Wire.read();
  44. if (!(deviceID & 0x08))
  45. mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
  46. }
  47. }
  48. /**
  49. * Write input resister value to specified channel using fastwrite method.
  50. * Channel : 0-3, Values : 0-4095
  51. */
  52. uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
  53. mcp4728_values[channel] = value;
  54. return mcp4728_fastWrite();
  55. }
  56. /**
  57. * Write all input resistor values to EEPROM using SequencialWrite method.
  58. * This will update both input register and EEPROM value
  59. * This will also write current Vref, PowerDown, Gain settings to EEPROM
  60. */
  61. uint8_t mcp4728_eepromWrite() {
  62. Wire.beginTransmission(DAC_DEV_ADDRESS);
  63. Wire.write(SEQWRITE);
  64. for (uint8_t channel = 0; channel < COUNT(mcp4728_values); channel++) {
  65. Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
  66. Wire.write(lowByte(mcp4728_values[channel]));
  67. }
  68. return Wire.endTransmission();
  69. }
  70. /**
  71. * Write Voltage reference setting to all input regiters
  72. */
  73. uint8_t mcp4728_setVref_all(uint8_t value) {
  74. Wire.beginTransmission(DAC_DEV_ADDRESS);
  75. Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
  76. return Wire.endTransmission();
  77. }
  78. /**
  79. * Write Gain setting to all input regiters
  80. */
  81. uint8_t mcp4728_setGain_all(uint8_t value) {
  82. Wire.beginTransmission(DAC_DEV_ADDRESS);
  83. Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
  84. return Wire.endTransmission();
  85. }
  86. /**
  87. * Return Input Register value
  88. */
  89. uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
  90. /**
  91. * Steph: Might be useful in the future
  92. * Return Vout
  93. *
  94. uint16_t mcp4728_getVout(uint8_t channel) {
  95. uint32_t vref = 2048,
  96. vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
  97. if (vOut > defaultVDD) vOut = defaultVDD;
  98. return vOut;
  99. }
  100. */
  101. /**
  102. * Returns DAC values as a 0-100 percentage of drive strength
  103. */
  104. uint16_t mcp4728_getDrvPct(uint8_t channel) { return uint16_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
  105. /**
  106. * Receives all Drive strengths as 0-100 percent values, updates
  107. * DAC Values array and calls fastwrite to update the DAC.
  108. */
  109. void mcp4728_setDrvPct(int16_t pct[XYZE]) {
  110. LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
  111. mcp4728_fastWrite();
  112. }
  113. /**
  114. * FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
  115. * DAC Input and PowerDown bits update.
  116. * No EEPROM update
  117. */
  118. uint8_t mcp4728_fastWrite() {
  119. Wire.beginTransmission(DAC_DEV_ADDRESS);
  120. for (uint8_t channel = 0; channel < COUNT(mcp4728_values); channel++) {
  121. Wire.write(highByte(mcp4728_values[channel]));
  122. Wire.write(lowByte(mcp4728_values[channel]));
  123. }
  124. return Wire.endTransmission();
  125. }
  126. /**
  127. * Common function for simple general commands
  128. */
  129. uint8_t mcp4728_simpleCommand(byte simpleCommand) {
  130. Wire.beginTransmission(GENERALCALL);
  131. Wire.write(simpleCommand);
  132. return Wire.endTransmission();
  133. }
  134. #endif // DAC_STEPPER_CURRENT