My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dac_mcp4728.cpp 4.3KB

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