|
@@ -34,7 +34,7 @@
|
34
|
34
|
|
35
|
35
|
#if ENABLED(DAC_STEPPER_CURRENT)
|
36
|
36
|
|
37
|
|
-uint16_t mcp4728_values[XYZE];
|
|
37
|
+uint16_t mcp4728_values[XYZE];
|
38
|
38
|
|
39
|
39
|
/**
|
40
|
40
|
* Begin I2C, get current values (input register and eeprom) of mcp4728
|
|
@@ -42,16 +42,13 @@ uint16_t mcp4728_values[XYZE];
|
42
|
42
|
void mcp4728_init() {
|
43
|
43
|
Wire.begin();
|
44
|
44
|
Wire.requestFrom(int(DAC_DEV_ADDRESS), 24);
|
45
|
|
- while(Wire.available()) {
|
46
|
|
- int deviceID = Wire.read();
|
47
|
|
- int hiByte = Wire.read();
|
48
|
|
- int loByte = Wire.read();
|
|
45
|
+ while (Wire.available()) {
|
|
46
|
+ char deviceID = Wire.read(),
|
|
47
|
+ hiByte = Wire.read(),
|
|
48
|
+ loByte = Wire.read();
|
49
|
49
|
|
50
|
|
- int isEEPROM = (deviceID & 0B00001000) >> 3;
|
51
|
|
- int channel = (deviceID & 0B00110000) >> 4;
|
52
|
|
- if (isEEPROM != 1) {
|
53
|
|
- mcp4728_values[channel] = word((hiByte & 0B00001111), loByte);
|
54
|
|
- }
|
|
50
|
+ if (!(deviceID & 0x08))
|
|
51
|
+ mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
|
55
|
52
|
}
|
56
|
53
|
}
|
57
|
54
|
|
|
@@ -71,8 +68,8 @@ uint8_t mcp4728_analogWrite(uint8_t channel, uint16_t value) {
|
71
|
68
|
uint8_t mcp4728_eepromWrite() {
|
72
|
69
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
73
|
70
|
Wire.write(SEQWRITE);
|
74
|
|
- for (uint8_t channel=0; channel <= 3; channel++) {
|
75
|
|
- Wire.write(DAC_STEPPER_VREF << 7 | 0 << 5 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
|
|
71
|
+ for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
|
|
72
|
+ Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[channel]));
|
76
|
73
|
Wire.write(lowByte(mcp4728_values[channel]));
|
77
|
74
|
}
|
78
|
75
|
return Wire.endTransmission();
|
|
@@ -83,7 +80,7 @@ uint8_t mcp4728_eepromWrite() {
|
83
|
80
|
*/
|
84
|
81
|
uint8_t mcp4728_setVref_all(uint8_t value) {
|
85
|
82
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
86
|
|
- Wire.write(VREFWRITE | value << 3 | value << 2 | value << 1 | value);
|
|
83
|
+ Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
|
87
|
84
|
return Wire.endTransmission();
|
88
|
85
|
}
|
89
|
86
|
/**
|
|
@@ -91,7 +88,7 @@ uint8_t mcp4728_setVref_all(uint8_t value) {
|
91
|
88
|
*/
|
92
|
89
|
uint8_t mcp4728_setGain_all(uint8_t value) {
|
93
|
90
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
94
|
|
- Wire.write(GAINWRITE | value << 3 | value << 2 | value << 1 | value);
|
|
91
|
+ Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
|
95
|
92
|
return Wire.endTransmission();
|
96
|
93
|
}
|
97
|
94
|
|
|
@@ -105,21 +102,19 @@ uint16_t mcp4728_getValue(uint8_t channel) { return mcp4728_values[channel]; }
|
105
|
102
|
* Return Vout
|
106
|
103
|
*
|
107
|
104
|
uint16_t mcp4728_getVout(uint8_t channel) {
|
108
|
|
- uint32_t vref = 2048;
|
109
|
|
- uint32_t vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
|
|
105
|
+ uint32_t vref = 2048,
|
|
106
|
+ vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
|
110
|
107
|
if (vOut > defaultVDD) vOut = defaultVDD;
|
111
|
108
|
return vOut;
|
112
|
109
|
}
|
113
|
110
|
*/
|
114
|
111
|
|
115
|
112
|
/* Returns DAC values as a 0-100 percentage of drive strength */
|
116
|
|
-uint16_t mcp4728_getDrvPct(uint8_t channel) {return (uint16_t)(.5+(((float)mcp4728_values[channel]*100)/DAC_STEPPER_MAX));}
|
|
113
|
+uint16_t mcp4728_getDrvPct(uint8_t channel) { return uint16_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
|
117
|
114
|
|
118
|
115
|
/* Recieves all Drive strengths as 0-100 percent values, updates DAC Values array and calls fastwrite to update the DAC */
|
119
|
116
|
void mcp4728_setDrvPct(int16_t pct[XYZE]) {
|
120
|
|
- for (uint8_t i=0; i <= 3; i++) {
|
121
|
|
- mcp4728_values[i] = ((float)pct[i] * DAC_STEPPER_MAX)/100;
|
122
|
|
- }
|
|
117
|
+ LOOP_XYZE(i) mcp4728_values[i] = 0.01 * pct[i] * (DAC_STEPPER_MAX);
|
123
|
118
|
mcp4728_fastWrite();
|
124
|
119
|
}
|
125
|
120
|
|
|
@@ -130,7 +125,7 @@ void mcp4728_setDrvPct(int16_t pct[XYZE]) {
|
130
|
125
|
*/
|
131
|
126
|
uint8_t mcp4728_fastWrite() {
|
132
|
127
|
Wire.beginTransmission(DAC_DEV_ADDRESS);
|
133
|
|
- for (uint8_t channel=0; channel <= 3; channel++) {
|
|
128
|
+ for (uint8_t channel = 0; channel < COUNT(channel); channel++) {
|
134
|
129
|
Wire.write(highByte(mcp4728_values[channel]));
|
135
|
130
|
Wire.write(lowByte(mcp4728_values[channel]));
|
136
|
131
|
}
|