2016-03-25 06:19:46 +00:00
|
|
|
/**
|
2016-03-24 18:01:20 +00:00
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
2016-03-24 18:01:20 +00:00
|
|
|
* mcp4728.cpp - Arduino library for MicroChip MCP4728 I2C D/A converter
|
|
|
|
*
|
|
|
|
* For implementation details, please take a look at the datasheet:
|
|
|
|
* http://ww1.microchip.com/downloads/en/DeviceDoc/22187a.pdf
|
|
|
|
*
|
|
|
|
* For discussion and feedback, please go to:
|
|
|
|
* http://arduino.cc/forum/index.php/topic,51842.0.html
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
|
|
|
|
#include "dac_mcp4728.h"
|
|
|
|
|
|
|
|
#if ENABLED(DAC_STEPPER_CURRENT)
|
|
|
|
|
|
|
|
uint16_t mcp4728_values[4];
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Begin I2C, get current values (input register and eeprom) of mcp4728
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
void mcp4728_init() {
|
|
|
|
Wire.begin();
|
|
|
|
Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
|
|
|
|
while(Wire.available()) {
|
2016-06-27 19:51:50 +00:00
|
|
|
int deviceID = Wire.read();
|
|
|
|
int hiByte = Wire.read();
|
|
|
|
int loByte = Wire.read();
|
2016-03-10 22:33:53 +00:00
|
|
|
|
|
|
|
int isEEPROM = (deviceID & 0B00001000) >> 3;
|
|
|
|
int channel = (deviceID & 0B00110000) >> 4;
|
|
|
|
if (isEEPROM != 1) {
|
|
|
|
mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Write input resister value to specified channel using fastwrite method.
|
|
|
|
* Channel : 0-3, Values : 0-4095
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
|
|
|
|
mcp4728_values[channel] = value;
|
|
|
|
return mcp4728_fastWrite();
|
|
|
|
}
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Write all input resistor values to EEPROM using SequencialWrite method.
|
|
|
|
* This will update both input register and EEPROM value
|
|
|
|
* This will also write current Vref, PowerDown, Gain settings to EEPROM
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint8_t mcp4728_eepromWrite() {
|
|
|
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
2016-06-27 19:51:50 +00:00
|
|
|
Wire.write(SEQWRITE);
|
2016-03-10 22:33:53 +00:00
|
|
|
for (uint8_t channel=0; channel <= 3; channel++) {
|
2016-06-27 19:51:50 +00:00
|
|
|
Wire.write(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
|
|
|
|
Wire.write(lowByte(mcp4728_values[channel]));
|
2016-03-10 22:33:53 +00:00
|
|
|
}
|
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Write Voltage reference setting to all input regiters
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint8_t mcp4728_setVref_all(uint8_t value) {
|
|
|
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
2016-06-27 19:51:50 +00:00
|
|
|
Wire.write(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
|
2016-03-10 22:33:53 +00:00
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Write Gain setting to all input regiters
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint8_t mcp4728_setGain_all(uint8_t value) {
|
|
|
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
2016-06-27 19:51:50 +00:00
|
|
|
Wire.write(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
|
2016-03-10 22:33:53 +00:00
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Return Input Regiter value
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Steph: Might be useful in the future
|
|
|
|
* Return Vout
|
|
|
|
*
|
2016-03-10 22:33:53 +00:00
|
|
|
uint16_t mcp4728_getVout(uint8_t channel) {
|
|
|
|
uint32_t vref = 2048;
|
|
|
|
uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
|
|
|
|
if (vOut > defaultVDD) vOut = defaultVDD;
|
|
|
|
return vOut;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* FastWrite input register values - All DAC ouput update. refer to DATASHEET 5.6.1
|
|
|
|
* DAC Input and PowerDown bits update.
|
|
|
|
* No EEPROM update
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint8_t mcp4728_fastWrite() {
|
|
|
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
|
|
|
for (uint8_t channel=0; channel <= 3; channel++) {
|
2016-06-27 19:51:50 +00:00
|
|
|
Wire.write(highByte(mcp4728_values[channel]));
|
|
|
|
Wire.write(lowByte(mcp4728_values[channel]));
|
2016-03-10 22:33:53 +00:00
|
|
|
}
|
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2016-03-25 06:19:46 +00:00
|
|
|
/**
|
|
|
|
* Common function for simple general commands
|
|
|
|
*/
|
2016-03-10 22:33:53 +00:00
|
|
|
uint8_t mcp4728_simpleCommand(byte simpleCommand) {
|
|
|
|
Wire.beginTransmission(GENERALCALL);
|
2016-06-27 19:51:50 +00:00
|
|
|
Wire.write(simpleCommand);
|
2016-03-10 22:33:53 +00:00
|
|
|
return Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DAC_STEPPER_CURRENT
|