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.

stepper_dac.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. stepper_dac.cpp - To set stepper current via DAC
  3. Part of Marlin
  4. Copyright (c) 2016 MarlinFirmware
  5. Marlin is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Marlin is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Marlin. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "Marlin.h"
  17. #if ENABLED(DAC_STEPPER_CURRENT)
  18. #include "stepper_dac.h"
  19. bool dac_present = false;
  20. const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER;
  21. int dac_init() {
  22. mcp4728_init();
  23. if (mcp4728_simpleCommand(RESET)) return -1;
  24. dac_present = true;
  25. mcp4728_setVref_all(DAC_STEPPER_VREF);
  26. mcp4728_setGain_all(DAC_STEPPER_GAIN);
  27. return 0;
  28. }
  29. void dac_current_percent(uint8_t channel, float val) {
  30. if (!dac_present) return;
  31. NOMORE(val, 100);
  32. mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100);
  33. mcp4728_simpleCommand(UPDATE);
  34. }
  35. void dac_current_raw(uint8_t channel, uint16_t val) {
  36. if (!dac_present) return;
  37. NOMORE(val, DAC_STEPPER_MAX);
  38. mcp4728_analogWrite(dac_order[channel], val);
  39. mcp4728_simpleCommand(UPDATE);
  40. }
  41. static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; }
  42. static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); }
  43. void dac_print_values() {
  44. if (!dac_present) return;
  45. SERIAL_ECHO_START;
  46. SERIAL_ECHOLNPGM("Stepper current values in % (Amps):");
  47. SERIAL_ECHO_START;
  48. SERIAL_ECHOPAIR(" X:", dac_perc(0));
  49. SERIAL_ECHOPAIR(" (", dac_amps(0));
  50. SERIAL_ECHOPAIR(") Y:", dac_perc(1));
  51. SERIAL_ECHOPAIR(" (", dac_amps(1));
  52. SERIAL_ECHOPAIR(") Z:", dac_perc(2));
  53. SERIAL_ECHOPAIR(" (", dac_amps(2));
  54. SERIAL_ECHOPAIR(") E:", dac_perc(3));
  55. SERIAL_ECHOPAIR(" (", dac_amps(3));
  56. SERIAL_ECHOLN(")");
  57. }
  58. void dac_commit_eeprom() {
  59. if (!dac_present) return;
  60. mcp4728_eepromWrite();
  61. }
  62. #endif // DAC_STEPPER_CURRENT