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.1KB

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