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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. mcp4728.cpp - Arduino library for MicroChip MCP4728 I2C D/A converter
  3. For implementation details, please take a look at the datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
  4. For discussion and feedback, please go to http://arduino.cc/forum/index.php/topic,51842.0.html
  5. */
  6. /* _____PROJECT INCLUDES_____________________________________________________ */
  7. #include "dac_mcp4728.h"
  8. #if ENABLED(DAC_STEPPER_CURRENT)
  9. // Used Global variables
  10. uint16_t mcp4728_values[4];
  11. /*
  12. Begin I2C, get current values (input register and eeprom) of mcp4728
  13. */
  14. void mcp4728_init() {
  15. Wire.begin();
  16. Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
  17. while(Wire.available()) {
  18. int deviceID = Wire.receive();
  19. int hiByte = Wire.receive();
  20. int loByte = Wire.receive();
  21. int isEEPROM = (deviceID & 0B00001000) >> 3;
  22. int channel = (deviceID & 0B00110000) >> 4;
  23. if (isEEPROM != 1) {
  24. mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
  25. }
  26. }
  27. }
  28. /*
  29. Write input resister value to specified channel using fastwrite method.
  30. Channel : 0-3, Values : 0-4095
  31. */
  32. uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
  33. mcp4728_values[channel] = value;
  34. return mcp4728_fastWrite();
  35. }
  36. /*
  37. Write all input resistor values to EEPROM using SequencialWrite method.
  38. This will update both input register and EEPROM value
  39. This will also write current Vref, PowerDown, Gain settings to EEPROM
  40. */
  41. uint8_t mcp4728_eepromWrite() {
  42. Wire.beginTransmission(DAC_DEV_ADDRESS);
  43. Wire.send(SEQWRITE);
  44. for (uint8_t channel=0; channel <= 3; channel++) {
  45. Wire.send(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
  46. Wire.send(lowByte(mcp4728_values[channel]));
  47. }
  48. return Wire.endTransmission();
  49. }
  50. /*
  51. Write Voltage reference setting to all input regiters
  52. */
  53. uint8_t mcp4728_setVref_all(uint8_t value) {
  54. Wire.beginTransmission(DAC_DEV_ADDRESS);
  55. Wire.send(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
  56. return Wire.endTransmission();
  57. }
  58. /*
  59. Write Gain setting to all input regiters
  60. */
  61. uint8_t mcp4728_setGain_all(uint8_t value) {
  62. Wire.beginTransmission(DAC_DEV_ADDRESS);
  63. Wire.send(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
  64. return Wire.endTransmission();
  65. }
  66. /*
  67. Return Input Regiter value
  68. */
  69. uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
  70. /*
  71. // Steph: Might be useful in the future
  72. // Return Vout
  73. uint16_t mcp4728_getVout(uint8_t channel) {
  74. uint32_t vref = 2048;
  75. uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
  76. if (vOut > defaultVDD) vOut = defaultVDD;
  77. return vOut;
  78. }
  79. */
  80. /*
  81. FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
  82. DAC Input and PowerDown bits update.
  83. No EEPROM update
  84. */
  85. uint8_t mcp4728_fastWrite() {
  86. Wire.beginTransmission(DAC_DEV_ADDRESS);
  87. for (uint8_t channel=0; channel <= 3; channel++) {
  88. Wire.send(highByte(mcp4728_values[channel]));
  89. Wire.send(lowByte(mcp4728_values[channel]));
  90. }
  91. return Wire.endTransmission();
  92. }
  93. /*
  94. Common function for simple general commands
  95. */
  96. uint8_t mcp4728_simpleCommand(byte simpleCommand) {
  97. Wire.beginTransmission(GENERALCALL);
  98. Wire.send(simpleCommand);
  99. return Wire.endTransmission();
  100. }
  101. #endif // DAC_STEPPER_CURRENT