1
1
//
2
2
// FILE: MCP4725.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // PURPOSE: Simple MCP4725 DAC library for Arduino
5
- // VERSION: 0.1.05
4
+ // PURPOSE: Simple MCP4725 DAC (12 bit I2C) library for Arduino
5
+ // VERSION: 0.1.6
6
6
// HISTORY: See MCP4725.cpp
7
7
// URL:
8
8
//
13
13
// 0.1.03 - 2013-12-01 added powerDownMode code
14
14
// 0.1.04 - 2013-12-04 improved the generalCall code (still experimental)
15
15
// 0.1.05 - 2015-03-06 refactoring, stricter interfaces
16
+ // 0.1.6 - 2017-04-19 refactor + remove timeout - https://github.com/RobTillaart/Arduino/issues/63
16
17
//
17
18
// Released to the public domain
18
19
//
19
20
20
21
#include " MCP4725.h"
21
22
23
+ #if defined(ARDUINO) && ARDUINO >= 100
24
+ #define WIRE_WRITE Wire.write
25
+ #define WIRE_READ Wire.read
26
+ #else
27
+ #define WIRE_WRITE Wire.send
28
+ #define WIRE_READ Wire.receive
29
+ #endif
30
+
22
31
MCP4725::MCP4725 (const uint8_t deviceAddress)
23
32
{
24
33
_deviceAddress = deviceAddress;
@@ -159,13 +168,8 @@ int MCP4725::writeFastMode(const uint16_t value)
159
168
uint8_t h = ((value / 256 ) & 0x0F ); // set C0 = C1 = 0, no PDmode
160
169
h = h | (_powerDownMode << 4 );
161
170
uint8_t l = value & 0xFF ;
162
- #if defined(ARDUINO) && ARDUINO >= 100
163
- Wire.write (h);
164
- Wire.write (l);
165
- #else
166
- Wire.send (h);
167
- Wire.send (l);
168
- #endif
171
+ WIRE_WRITE (h);
172
+ WIRE_WRITE (l);
169
173
return Wire.endTransmission ();
170
174
}
171
175
@@ -188,15 +192,9 @@ int MCP4725::writeRegisterMode(const uint16_t value, const uint8_t reg)
188
192
uint8_t l = (value & 0x0F ) << 4 ;
189
193
Wire.beginTransmission (_deviceAddress);
190
194
reg = reg | (_powerDownMode << 1 );
191
- #if defined(ARDUINO) && ARDUINO >= 100
192
- Wire.write (reg);
193
- Wire.write (h);
194
- Wire.write (l);
195
- #else
196
- Wire.send (reg);
197
- Wire.send (h);
198
- Wire.send (l);
199
- #endif
195
+ WIRE_WRITE (reg);
196
+ WIRE_WRITE (h);
197
+ WIRE_WRITE (l);
200
198
return Wire.endTransmission ();
201
199
}
202
200
@@ -210,14 +208,10 @@ uint8_t MCP4725::readRegister(uint8_t* buffer, const uint8_t length)
210
208
211
209
Wire.requestFrom (_deviceAddress, length);
212
210
uint8_t cnt = 0 ;
213
- uint32_t before = millis ();
214
- while ((cnt < length ) && (( millis () - before) < MCP4725_TIMEOUT ))
211
+ uint8_t n = Wire. available ();
212
+ while ( (cnt < n ) && (cnt < length ))
215
213
{
216
- #if defined(ARDUINO) && ARDUINO >= 100
217
- if (Wire.available ()) buffer[cnt++] = Wire.read ();
218
- #else
219
- if (Wire.available ()) buffer[cnt++] = Wire.receive ();
220
- #endif
214
+ buffer[cnt++] = WIRE_READ ();
221
215
}
222
216
return cnt;
223
217
}
@@ -227,11 +221,7 @@ uint8_t MCP4725::readRegister(uint8_t* buffer, const uint8_t length)
227
221
int MCP4725::generalCall (const uint8_t gc)
228
222
{
229
223
Wire.beginTransmission (0 );
230
- #if defined(ARDUINO) && ARDUINO >= 100
231
- Wire.write (gc);
232
- #else
233
- Wire.send (gc);
234
- #endif
224
+ WIRE_WRITE (gc);
235
225
return Wire.endTransmission ();
236
226
}
237
227
#endif
0 commit comments