Skip to content

Commit 0611abf

Browse files
committed
+ 0.1.04
+ fixed powerDownMode (still experimental,use with care) + added corrections to powerOnReset() + added corrections to powerOnWakeUp() + changed interface + behavior of writePowerDownMode() + added conditional code where needed + added testcode for powerDownMode()
1 parent e2cd8c1 commit 0611abf

File tree

3 files changed

+67
-35
lines changed

3 files changed

+67
-35
lines changed

libraries/MCP4725/MCP4725.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: MCP4725.cpp
33
// AUTHOR: Rob Tillaart
44
// PURPOSE: Simple MCP4725 DAC library for Arduino
5-
// VERSION: 1.0.03
5+
// VERSION: 1.0.04
66
// HISTORY: See MCP4725.cpp
77
// URL:
88
//
@@ -11,6 +11,7 @@
1111
// 0.1.01 - 2013-11-30 added readDAC() & writeDAC (registerwrite)
1212
// 0.1.02 - 2013-12-01 added readEEPROM() & RDY()
1313
// 0.1.03 - 2013-12-01 added powerDownMode code
14+
// 0.1.04 - 2013-12-04 improved the generalCall code (still experimental)
1415
//
1516
// Released to the public domain
1617
//
@@ -32,8 +33,17 @@ void MCP4725::begin()
3233
// 12=400KHz 24=250 32=200 72=100 152=50
3334
// F_CPU/16+(2*TWBR) // TWBR is a uint8_t
3435

36+
#ifdef MCP4725_EXTENDED
3537
_lastValue = readDAC();
36-
_powerDownMode = readPowerDownModeEEPROM();
38+
#else
39+
_lastValue = 0;
40+
#endif
41+
42+
#ifdef MCP4725_POWERDOWNMODE
43+
_powerDownMode = readPowerDownModeDAC();
44+
#else
45+
_powerDownMode = 0;
46+
#endif
3747
}
3848

3949
int MCP4725::setValue(uint16_t value)
@@ -88,26 +98,21 @@ uint16_t MCP4725::readEEPROM()
8898

8999
#ifdef MCP4725_POWERDOWNMODE
90100
//
91-
// PDmode can be written to DAC or to DAC&EEPROM,
92-
// for now the lib only support DAC&EEPROM
93-
// this will change after enough experience is gathered how to use
94-
95-
// write to DAC & EEPROM
96-
int MCP4725::writePowerDownMode(uint8_t PDM)
101+
// depending on bool EEPROM the value of PDM is written to
102+
// (false) DAC or
103+
// (true) DAC & EEPROM,
104+
int MCP4725::writePowerDownMode(uint8_t PDM, bool EEPROM)
97105
{
98-
_powerDownMode = (PDM & 0x03); // force only pdm bits
99-
return writeDAC(_lastValue, true);
106+
_powerDownMode = (PDM & 0x03); // mask pdm bits only
107+
return writeDAC(_lastValue, EEPROM);
100108
}
101109

102110
uint8_t MCP4725::readPowerDownModeEEPROM()
103111
{
104112
while(!RDY());
105113
uint8_t buffer[4];
106114
readRegister(buffer, 4);
107-
// EEPROM
108115
uint8_t value = (buffer[3] >> 5) & 0x03;
109-
// DAC
110-
// uint8_t value = (buffer[0] >> 1) & 0x03;
111116
return value;
112117
}
113118

@@ -120,21 +125,23 @@ uint8_t MCP4725::readPowerDownModeDAC()
120125
return value;
121126
}
122127

123-
// PAGE 22
124-
// experimental
128+
// PAGE 22 - experimental
129+
// DAC value is reset to EEPROM value
130+
// need to reflect this in cached value
125131
int MCP4725::powerOnReset()
126132
{
127-
int rv = command(MCP4725_GENERAL_RESET);
128-
// what happens to _lastValue and _powerDownMode...
133+
int rv = generalCall(MCP4725_GC_RESET);
134+
_lastValue = readDAC(); // update cache to actual value;
129135
return rv;
130136
}
131137

132-
// PAGE 22
133-
// experimental
138+
// PAGE 22 - experimental
139+
// _powerDownMode DAC resets to 0 -- pdm EEPROM stays same !!!
140+
// need to reflect this in cached value
134141
int MCP4725::powerOnWakeUp()
135142
{
136-
int rv = command(MCP4725_GENERAL_WAKEUP);
137-
// what happens to _lastValue and _powerDownMode...
143+
int rv = generalCall(MCP4725_GC_WAKEUP);
144+
_powerDownMode = readPowerDownModeDAC(); // update to actual value;
138145
return rv;
139146
}
140147
#endif
@@ -216,13 +223,13 @@ uint8_t MCP4725::readRegister(uint8_t* buffer, uint8_t length)
216223
#endif
217224

218225
#ifdef MCP4725_POWERDOWNMODE
219-
int MCP4725::command(uint8_t cmd)
226+
int MCP4725::generalCall(uint8_t gc)
220227
{
221-
Wire.beginTransmission(_deviceAddress);
228+
Wire.beginTransmission(0);
222229
#if defined(ARDUINO) && ARDUINO >= 100
223-
Wire.write(cmd);
230+
Wire.write(gc);
224231
#else
225-
Wire.send(cmd);
232+
Wire.send(gc);
226233
#endif
227234
return Wire.endTransmission();
228235
}

libraries/MCP4725/MCP4725.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// FILE: MCP4725.h
55
// AUTHOR: Rob Tillaart
66
// PURPOSE: Simple MCP4725 DAC library for Arduino
7-
// VERSION: 1.0.03
7+
// VERSION: 1.0.04
88
// HISTORY: See MCP4725.cpp
99
// URL:
1010
//
@@ -21,11 +21,10 @@
2121
#include "Wiring.h"
2222
#endif
2323

24-
#define MCP4725_VERSION "1.0.03"
24+
#define MCP4725_VERSION "1.0.04"
2525

2626
// regisiterMode
2727
#define MCP4725_DAC 0x40
28-
#define MCP4725_EEPROM 0x20
2928
#define MCP4725_DACEEPROM 0x60
3029

3130
// constants
@@ -37,8 +36,8 @@
3736
#define MCP4725_REG_ERROR -998
3837

3938
// page 22
40-
#define MCP4725_GENERAL_RESET 0x06
41-
#define MCP4725_GENERAL_WAKEUP 0x09
39+
#define MCP4725_GC_RESET 0x06
40+
#define MCP4725_GC_WAKEUP 0x09
4241

4342
// powerDown Mode - TODO ENUM?
4443
#define MCP4725_PDMODE_NORMAL 0x00
@@ -47,8 +46,8 @@
4746
#define MCP4725_PDMODE_500K 0x03
4847

4948
// conditional to minimize footprint.
50-
#define MCP4725_EXTENDED
51-
#define MCP4725_POWERDOWNMODE
49+
//#define MCP4725_EXTENDED
50+
//#define MCP4725_POWERDOWNMODE
5251

5352
class MCP4725
5453
{
@@ -70,7 +69,7 @@ class MCP4725
7069

7170
#ifdef MCP4725_POWERDOWNMODE
7271
// experimental
73-
int writePowerDownMode(uint8_t PDM);
72+
int writePowerDownMode(uint8_t PDM, bool EEPROM = false);
7473
uint8_t readPowerDownModeEEPROM();
7574
uint8_t readPowerDownModeDAC();
7675
int powerOnReset();
@@ -89,7 +88,7 @@ class MCP4725
8988
#endif
9089

9190
#ifdef MCP4725_POWERDOWNMODE
92-
int command(uint8_t cmd);
91+
int generalCall(uint8_t gc);
9392
#endif
9493

9594
};

libraries/MCP4725/examples/mcp4725_test/mcp4725_test.ino

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: mcp4725_test.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.02
4+
// VERSION: 0.1.04
55
// PURPOSE: test mcp4725 lib
66
// DATE: 2013-11-24
77
// URL:
@@ -94,36 +94,62 @@ void setup()
9494
Serial.println("EXPERIMENTAL");
9595
Serial.println("DAC.writePowerDownMode(3)");
9696
DAC.writePowerDownMode(3);
97+
DAC.writeDAC(305);
98+
Serial.print("Value:\t");
99+
Serial.println(DAC.getValue());
97100
Serial.println("DAC.powerOnReset()");
98101
Serial.println("Before");
99102
Serial.print("DAC PDM Value:\t");
100103
Serial.println(DAC.readPowerDownModeDAC());
101104
Serial.print("EPR PDM Value:\t");
102105
Serial.println(DAC.readPowerDownModeEEPROM());
106+
Serial.print("DACValue:\t");
107+
Serial.println(DAC.readDAC());
108+
Serial.print("EEValue:\t");
109+
Serial.println(DAC.readEEPROM());
103110
DAC.powerOnReset();
104111
Serial.println("After");
105112
Serial.print("DAC PDM Value:\t");
106113
Serial.println(DAC.readPowerDownModeDAC());
107114
Serial.print("EPR PDM Value:\t");
108115
Serial.println(DAC.readPowerDownModeEEPROM());
116+
Serial.print("DACValue:\t");
117+
Serial.println(DAC.readDAC());
118+
Serial.print("EEValue:\t");
119+
Serial.println(DAC.readEEPROM());
120+
Serial.print("Value:\t");
121+
Serial.println(DAC.getValue());
109122
Serial.println();
110123

111124

112125
Serial.println("EXPERIMENTAL");
113126
Serial.println("DAC.writePowerDownMode(2)");
114127
DAC.writePowerDownMode(2);
128+
DAC.writeDAC(405);
129+
Serial.print("Value:\t");
130+
Serial.println(DAC.getValue());
115131
Serial.println("DAC.powerOnWakeUp()");
116132
Serial.println("Before");
117133
Serial.print("DAC PDM Value:\t");
118134
Serial.println(DAC.readPowerDownModeDAC());
119135
Serial.print("EPR PDM Value:\t");
120136
Serial.println(DAC.readPowerDownModeEEPROM());
137+
Serial.print("DACValue:\t");
138+
Serial.println(DAC.readDAC());
139+
Serial.print("EEValue:\t");
140+
Serial.println(DAC.readEEPROM());
121141
DAC.powerOnWakeUp();
122142
Serial.println("after");
123143
Serial.print("DAC PDM Value:\t");
124144
Serial.println(DAC.readPowerDownModeDAC());
125145
Serial.print("EPR PDM Value:\t");
126146
Serial.println(DAC.readPowerDownModeEEPROM());
147+
Serial.print("DACValue:\t");
148+
Serial.println(DAC.readDAC());
149+
Serial.print("EEValue:\t");
150+
Serial.println(DAC.readEEPROM());
151+
Serial.print("Value:\t");
152+
Serial.println(DAC.getValue());
127153
Serial.println();
128154

129155
#endif

0 commit comments

Comments
 (0)