123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
-
-
-
-
- #include "dac_mcp4728.h"
-
- #if ENABLED(DAC_STEPPER_CURRENT)
-
- uint16_t mcp4728_values[4];
-
-
- void mcp4728_init() {
- Wire.begin();
- Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
- while(Wire.available()) {
- int deviceID = Wire.receive();
- int hiByte = Wire.receive();
- int loByte = Wire.receive();
-
- int isEEPROM = (deviceID & 0B00001000) >> 3;
- int channel = (deviceID & 0B00110000) >> 4;
- if (isEEPROM != 1) {
- mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
- }
- }
- }
-
-
- uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
- mcp4728_values[channel] = value;
- return mcp4728_fastWrite();
- }
-
- uint8_t mcp4728_eepromWrite() {
- Wire.beginTransmission(DAC_DEV_ADDRESS);
- Wire.send(SEQWRITE);
- for (uint8_t channel=0; channel <= 3; channel++) {
- Wire.send(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
- Wire.send(lowByte(mcp4728_values[channel]));
- }
- return Wire.endTransmission();
- }
-
-
- uint8_t mcp4728_setVref_all(uint8_t value) {
- Wire.beginTransmission(DAC_DEV_ADDRESS);
- Wire.send(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
- return Wire.endTransmission();
- }
-
- uint8_t mcp4728_setGain_all(uint8_t value) {
- Wire.beginTransmission(DAC_DEV_ADDRESS);
- Wire.send(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
- return Wire.endTransmission();
- }
-
-
- uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
-
-
-
-
- uint8_t mcp4728_fastWrite() {
- Wire.beginTransmission(DAC_DEV_ADDRESS);
- for (uint8_t channel=0; channel <= 3; channel++) {
- Wire.send(highByte(mcp4728_values[channel]));
- Wire.send(lowByte(mcp4728_values[channel]));
- }
- return Wire.endTransmission();
- }
-
-
- uint8_t mcp4728_simpleCommand(byte simpleCommand) {
- Wire.beginTransmission(GENERALCALL);
- Wire.send(simpleCommand);
- return Wire.endTransmission();
- }
-
- #endif
|