My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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[4];
  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. int deviceID = Wire.read();
  42. int hiByte = Wire.read();
  43. int loByte = Wire.read();
  44. int isEEPROM = (deviceID & 0B00001000) >> 3;
  45. int channel = (deviceID & 0B00110000) >> 4;
  46. if (isEEPROM != 1) {
  47. mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
  48. }
  49. }
  50. }
  51. /**
  52. * Write input resister value to specified channel using fastwrite method.
  53. * Channel : 0-3, Values : 0-4095
  54. */
  55. uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
  56. mcp4728_values[channel] = value;
  57. return mcp4728_fastWrite();
  58. }
  59. /**
  60. * Write all input resistor values to EEPROM using SequencialWrite method.
  61. * This will update both input register and EEPROM value
  62. * This will also write current Vref, PowerDown, Gain settings to EEPROM
  63. */
  64. uint8_t mcp4728_eepromWrite() {
  65. Wire.beginTransmission(DAC_DEV_ADDRESS);
  66. Wire.write(SEQWRITE);
  67. for (uint8_t channel=0; channel <= 3; channel++) {
  68. Wire.write(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
  69. Wire.write(lowByte(mcp4728_values[channel]));
  70. }
  71. return Wire.endTransmission();
  72. }
  73. /**
  74. * Write Voltage reference setting to all input regiters
  75. */
  76. uint8_t mcp4728_setVref_all(uint8_t value) {
  77. Wire.beginTransmission(DAC_DEV_ADDRESS);
  78. Wire.write(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
  79. return Wire.endTransmission();
  80. }
  81. /**
  82. * Write Gain setting to all input regiters
  83. */
  84. uint8_t mcp4728_setGain_all(uint8_t value) {
  85. Wire.beginTransmission(DAC_DEV_ADDRESS);
  86. Wire.write(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
  87. return Wire.endTransmission();
  88. }
  89. /**
  90. * Return Input Regiter value
  91. */
  92. uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
  93. /**
  94. * Steph: Might be useful in the future
  95. * Return Vout
  96. *
  97. uint16_t mcp4728_getVout(uint8_t channel) {
  98. uint32_t vref = 2048;
  99. uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
  100. if (vOut > defaultVDD) vOut = defaultVDD;
  101. return vOut;
  102. }
  103. */
  104. /**
  105. * FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
  106. * DAC Input and PowerDown bits update.
  107. * No EEPROM update
  108. */
  109. uint8_t mcp4728_fastWrite() {
  110. Wire.beginTransmission(DAC_DEV_ADDRESS);
  111. for (uint8_t channel=0; channel <= 3; channel++) {
  112. Wire.write(highByte(mcp4728_values[channel]));
  113. Wire.write(lowByte(mcp4728_values[channel]));
  114. }
  115. return Wire.endTransmission();
  116. }
  117. /**
  118. * Common function for simple general commands
  119. */
  120. uint8_t mcp4728_simpleCommand(byte simpleCommand) {
  121. Wire.beginTransmission(GENERALCALL);
  122. Wire.write(simpleCommand);
  123. return Wire.endTransmission();
  124. }
  125. #endif // DAC_STEPPER_CURRENT