From e17f9b8b99c3a460eee99186aef1ea87203dadea Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 10 Oct 2016 14:08:04 -0500 Subject: [PATCH] DAC patches As suggested by #4985 --- Marlin/dac_mcp4728.cpp | 14 ++++++++++---- Marlin/dac_mcp4728.h | 7 +++---- Marlin/stepper_dac.cpp | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Marlin/dac_mcp4728.cpp b/Marlin/dac_mcp4728.cpp index 5ba78e051..bfe406671 100644 --- a/Marlin/dac_mcp4728.cpp +++ b/Marlin/dac_mcp4728.cpp @@ -60,6 +60,7 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) { mcp4728_values[channel] = value; return mcp4728_fastWrite(); } + /** * Write all input resistor values to EEPROM using SequencialWrite method. * This will update both input register and EEPROM value @@ -68,7 +69,7 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) { uint8_t mcp4728_eepromWrite() { Wire.beginTransmission(DAC_DEV_ADDRESS); Wire.write(SEQWRITE); - for (uint8_t channel = 0; channel < COUNT(channel); channel++) { + for (uint8_t channel = 0; channel < COUNT(mcp4728_values); channel++) { Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel])); Wire.write(lowByte(mcp4728_values[channel])); } @@ -109,10 +110,15 @@ uint16_t mcp4728_getVout(uint8_t channel) { } */ -/* Returns DAC values as a 0-100 percentage of drive strength */ +/** + * Returns DAC values as a 0-100 percentage of drive strength + */ uint16_t mcp4728_getDrvPct(uint8_t channel) { return uint16_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); } -/* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */ +/** + * Receives all Drive strengths as 0-100 percent values, updates + * DAC Values array and calls fastwrite to update the DAC. + */ void mcp4728_setDrvPct(int16_t pct[XYZE]) { LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX); mcp4728_fastWrite(); @@ -125,7 +131,7 @@ void mcp4728_setDrvPct(int16_t pct[XYZE]) { */ uint8_t mcp4728_fastWrite() { Wire.beginTransmission(DAC_DEV_ADDRESS); - for (uint8_t channel = 0; channel < COUNT(channel); channel++) { + for (uint8_t channel = 0; channel < COUNT(mcp4728_values); channel++) { Wire.write(highByte(mcp4728_values[channel])); Wire.write(lowByte(mcp4728_values[channel])); } diff --git a/Marlin/dac_mcp4728.h b/Marlin/dac_mcp4728.h index 1949d405b..b2c9ec7b7 100644 --- a/Marlin/dac_mcp4728.h +++ b/Marlin/dac_mcp4728.h @@ -24,8 +24,8 @@ * Arduino library for MicroChip MCP4728 I2C D/A converter. */ -#ifndef mcp4728_h -#define mcp4728_h +#ifndef DAC_MCP4728_H +#define DAC_MCP4728_H #include "MarlinConfig.h" @@ -63,5 +63,4 @@ uint16_t mcp4728_getDrvPct(uint8_t channel); void mcp4728_setDrvPct(int16_t pct[XYZE]); #endif -#endif - +#endif // DAC_MCP4728_H diff --git a/Marlin/stepper_dac.cpp b/Marlin/stepper_dac.cpp index 5443abcd8..efdea60a3 100644 --- a/Marlin/stepper_dac.cpp +++ b/Marlin/stepper_dac.cpp @@ -87,7 +87,7 @@ } static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * (1.0 / (DAC_STEPPER_MAX)); } - static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * (0.125 * (DAC_STEPPER_SENSE)); } + static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * (1.0 / (DAC_STEPPER_SENSE)); } int16_t dac_current_get_percent(AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); } void dac_current_set_percents(int16_t pct[XYZE]) {