Skip to content

Commit e2cd8c1

Browse files
committed
+ 0.1.03
+ added powerDownCode (code/interface is experimental, test + refactor needed) + added RDY test in several places + removed smooth2Value() => sample sketch + extended test sketch + tested on 100, 400, 800 KHz (works)
1 parent 79a66c4 commit e2cd8c1

File tree

4 files changed

+230
-81
lines changed

4 files changed

+230
-81
lines changed

libraries/MCP4725/MCP4725.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// FILE: MCP4725.cpp
33
// AUTHOR: Rob Tillaart
44
// PURPOSE: Simple MCP4725 DAC library for Arduino
5-
// VERSION: 1.0.02
5+
// VERSION: 1.0.03
66
// HISTORY: See MCP4725.cpp
77
// URL:
88
//
99
// HISTORY:
1010
// 0.1.00 - 2013-11-24 initial version
1111
// 0.1.01 - 2013-11-30 added readDAC() & writeDAC (registerwrite)
1212
// 0.1.02 - 2013-12-01 added readEEPROM() & RDY()
13+
// 0.1.03 - 2013-12-01 added powerDownMode code
1314
//
1415
// Released to the public domain
1516
//
@@ -30,9 +31,9 @@ void MCP4725::begin()
3031
// 0=1000 1=888 2=800 8=500
3132
// 12=400KHz 24=250 32=200 72=100 152=50
3233
// F_CPU/16+(2*TWBR) // TWBR is a uint8_t
33-
34+
3435
_lastValue = readDAC();
35-
// _powerDownMode = readPowerDownMode();
36+
_powerDownMode = readPowerDownModeEEPROM();
3637
}
3738

3839
int MCP4725::setValue(uint16_t value)
@@ -51,31 +52,7 @@ uint16_t MCP4725::getValue()
5152

5253
#ifdef MCP4725_EXTENDED
5354

54-
int MCP4725::smooth2Value(uint16_t value, uint8_t steps)
55-
{
56-
// speed optimization
57-
if (value == _lastValue) return 0;
58-
59-
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
60-
if (steps == 0) steps++;
61-
62-
uint16_t delta = (value - _lastValue)/steps;
63-
if (delta > 0)
64-
{
65-
uint16_t v = _lastValue;
66-
for (int i=0; i < steps-1; i++)
67-
{
68-
v += delta;
69-
writeFastMode(v);
70-
}
71-
}
72-
// be sure to get the end value right
73-
int rv = writeFastMode(value);
74-
_lastValue = value;
75-
return rv;
76-
}
77-
78-
// unfortunately it is not possible to write a different value
55+
// unfortunately it is not possible to write a different value
7956
// to the DAC and EEPROM simultaneously or write EEPROM only.
8057
int MCP4725::writeDAC(uint16_t value, bool EEPROM)
8158
{
@@ -110,29 +87,55 @@ uint16_t MCP4725::readEEPROM()
11087
#endif
11188

11289
#ifdef MCP4725_POWERDOWNMODE
90+
//
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
11396
int MCP4725::writePowerDownMode(uint8_t PDM)
11497
{
115-
_powerDownMode = PDM;
98+
_powerDownMode = (PDM & 0x03); // force only pdm bits
11699
return writeDAC(_lastValue, true);
117100
}
118101

119-
// from EEPROM
120-
uint8_t MCP4725::readPowerDownMode()
102+
uint8_t MCP4725::readPowerDownModeEEPROM()
121103
{
104+
while(!RDY());
122105
uint8_t buffer[4];
123106
readRegister(buffer, 4);
124-
uint8_t value = (buffer[3] & 0x60) >> 5;
107+
// EEPROM
108+
uint8_t value = (buffer[3] >> 5) & 0x03;
109+
// DAC
110+
// uint8_t value = (buffer[0] >> 1) & 0x03;
111+
return value;
112+
}
113+
114+
uint8_t MCP4725::readPowerDownModeDAC()
115+
{
116+
while(!RDY());
117+
uint8_t buffer[1];
118+
readRegister(buffer, 1);
119+
uint8_t value = (buffer[0] >> 1) & 0x03;
125120
return value;
126121
}
127122

123+
// PAGE 22
124+
// experimental
128125
int MCP4725::powerOnReset()
129126
{
130-
return command(MCP4725_GENERAL_RESET);
127+
int rv = command(MCP4725_GENERAL_RESET);
128+
// what happens to _lastValue and _powerDownMode...
129+
return rv;
131130
}
132131

132+
// PAGE 22
133+
// experimental
133134
int MCP4725::powerOnWakeUp()
134135
{
135-
return command(MCP4725_GENERAL_WAKEUP);
136+
int rv = command(MCP4725_GENERAL_WAKEUP);
137+
// what happens to _lastValue and _powerDownMode...
138+
return rv;
136139
}
137140
#endif
138141

@@ -146,6 +149,7 @@ int MCP4725::writeFastMode(uint16_t value)
146149
{
147150
Wire.beginTransmission(_deviceAddress);
148151
uint8_t h = ((value / 256) & 0x0F); // set C0 = C1 = 0, no PDmode
152+
h = h | (_powerDownMode << 4);
149153
uint8_t l = value & 0xFF;
150154
#if defined(ARDUINO) && ARDUINO >= 100
151155
Wire.write(h);
@@ -169,13 +173,13 @@ bool MCP4725::RDY()
169173
}
170174

171175
// PAGE 19 DATASHEET
172-
// reg = MCP4725_DAC | MCP4725_EEPROM
176+
// reg = MCP4725_DAC | MCP4725_EEPROM
173177
int MCP4725::writeRegisterMode(uint16_t value, uint8_t reg)
174178
{
175179
uint8_t h = (value / 16);
176180
uint8_t l = (value & 0x0F) << 4;
177181
Wire.beginTransmission(_deviceAddress);
178-
reg = reg | _powerDownMode;
182+
reg = reg | (_powerDownMode << 1);
179183
#if defined(ARDUINO) && ARDUINO >= 100
180184
Wire.write(reg);
181185
Wire.write(h);
@@ -195,7 +199,7 @@ uint8_t MCP4725::readRegister(uint8_t* buffer, uint8_t length)
195199
Wire.beginTransmission(_deviceAddress);
196200
int rv = Wire.endTransmission();
197201
if (rv != 0) return 0; // error
198-
202+
199203
Wire.requestFrom(_deviceAddress, length);
200204
uint8_t cnt = 0;
201205
uint32_t before = millis();

libraries/MCP4725/MCP4725.h

Lines changed: 17 additions & 19 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.02
7+
// VERSION: 1.0.03
88
// HISTORY: See MCP4725.cpp
99
// URL:
1010
//
@@ -21,10 +21,11 @@
2121
#include "Wiring.h"
2222
#endif
2323

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

2626
// regisiterMode
2727
#define MCP4725_DAC 0x40
28+
#define MCP4725_EEPROM 0x20
2829
#define MCP4725_DACEEPROM 0x60
2930

3031
// constants
@@ -39,11 +40,11 @@
3940
#define MCP4725_GENERAL_RESET 0x06
4041
#define MCP4725_GENERAL_WAKEUP 0x09
4142

42-
// powerDown Mode
43+
// powerDown Mode - TODO ENUM?
4344
#define MCP4725_PDMODE_NORMAL 0x00
44-
#define MCP4725_PDMODE_1K 0x02
45-
#define MCP4725_PDMODE_100K 0x04
46-
#define MCP4725_PDMODE_500K 0x06
45+
#define MCP4725_PDMODE_1K 0x01
46+
#define MCP4725_PDMODE_100K 0x02
47+
#define MCP4725_PDMODE_500K 0x03
4748

4849
// conditional to minimize footprint.
4950
#define MCP4725_EXTENDED
@@ -58,42 +59,39 @@ class MCP4725
5859
// uses writeFastMode
5960
int setValue(uint16_t value);
6061
// returns last value set - cached - much faster than readDAC();
61-
uint16_t getValue();
62-
62+
uint16_t getValue();
63+
6364
#ifdef MCP4725_EXTENDED
64-
int smooth2Value(uint16_t value, uint8_t steps);
65-
6665
int writeDAC(uint16_t value, bool EEPROM = false);
6766
bool RDY();
6867
uint16_t readDAC();
6968
uint16_t readEEPROM();
7069
#endif
71-
70+
7271
#ifdef MCP4725_POWERDOWNMODE
72+
// experimental
7373
int writePowerDownMode(uint8_t PDM);
74-
uint8_t readPowerDownMode();
74+
uint8_t readPowerDownModeEEPROM();
75+
uint8_t readPowerDownModeDAC();
7576
int powerOnReset();
7677
int powerOnWakeUp();
7778
#endif
7879

79-
8080
private:
8181
uint8_t _deviceAddress;
8282
uint16_t _lastValue;
83-
uint8_t _powerDownMode; // DATASHEET P15?
84-
83+
uint8_t _powerDownMode; // DATASHEET P15?
8584
int writeFastMode(uint16_t value);
86-
87-
#ifdef MCP4725_EXTENDED
8885

86+
#ifdef MCP4725_EXTENDED
8987
int writeRegisterMode(uint16_t value, uint8_t reg);
9088
uint8_t readRegister(uint8_t* buffer, uint8_t length);
9189
#endif
92-
90+
9391
#ifdef MCP4725_POWERDOWNMODE
9492
int command(uint8_t cmd);
9593
#endif
96-
94+
9795
};
9896

9997
#endif

libraries/MCP4725/examples/mcp4725_test/mcp4725_test.ino

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ void setup()
4141
#ifdef MCP4725_EXTENDED
4242
Serial.println("\n\nMCP4725_EXTENDED\n\n");
4343

44-
Serial.println("smooth2Value(2000, 10)");
45-
DAC.smooth2Value(2000, 10);
46-
Serial.print("Value:\t");
47-
Serial.println(DAC.getValue());
48-
Serial.println();
49-
5044
for (int i=100; i<500; i+=100)
5145
{
5246
Serial.print("writeDAC(");
@@ -86,17 +80,56 @@ void setup()
8680
#ifdef MCP4725_POWERDOWNMODE
8781
Serial.println("\n\nMCP4725_POWERDOWNMODE\n\n");
8882

89-
/*
90-
int writePowerDownMode(uint8_t PDM);
91-
uint8_t readPowerDownMode();
92-
int powerOnReset();
93-
int powerOnWakeUp();
94-
*/
83+
for (int i=0; i<4; i++)
84+
{
85+
Serial.print("DAC.writePowerDownMode(");
86+
Serial.print(i);
87+
Serial.println(")");
88+
DAC.writePowerDownMode(i);
89+
Serial.print("EPR PDM Value:\t");
90+
Serial.println(DAC.readPowerDownModeEEPROM());
91+
Serial.println();
92+
}
93+
94+
Serial.println("EXPERIMENTAL");
95+
Serial.println("DAC.writePowerDownMode(3)");
96+
DAC.writePowerDownMode(3);
97+
Serial.println("DAC.powerOnReset()");
98+
Serial.println("Before");
99+
Serial.print("DAC PDM Value:\t");
100+
Serial.println(DAC.readPowerDownModeDAC());
101+
Serial.print("EPR PDM Value:\t");
102+
Serial.println(DAC.readPowerDownModeEEPROM());
103+
DAC.powerOnReset();
104+
Serial.println("After");
105+
Serial.print("DAC PDM Value:\t");
106+
Serial.println(DAC.readPowerDownModeDAC());
107+
Serial.print("EPR PDM Value:\t");
108+
Serial.println(DAC.readPowerDownModeEEPROM());
109+
Serial.println();
110+
111+
112+
Serial.println("EXPERIMENTAL");
113+
Serial.println("DAC.writePowerDownMode(2)");
114+
DAC.writePowerDownMode(2);
115+
Serial.println("DAC.powerOnWakeUp()");
116+
Serial.println("Before");
117+
Serial.print("DAC PDM Value:\t");
118+
Serial.println(DAC.readPowerDownModeDAC());
119+
Serial.print("EPR PDM Value:\t");
120+
Serial.println(DAC.readPowerDownModeEEPROM());
121+
DAC.powerOnWakeUp();
122+
Serial.println("after");
123+
Serial.print("DAC PDM Value:\t");
124+
Serial.println(DAC.readPowerDownModeDAC());
125+
Serial.print("EPR PDM Value:\t");
126+
Serial.println(DAC.readPowerDownModeEEPROM());
127+
Serial.println();
95128

96129
#endif
97130

98131
//////////////////////////////////////////////////
99-
Serial.println("\n\nPERFORMANCE\n");
132+
Serial.println("\n\nPERFORMANCE");
100133
Serial.print("TWBR:\t");
101134
Serial.println(TWBR);
102135
Serial.println();
@@ -131,15 +164,6 @@ void setup()
131164

132165
#ifdef MCP4725_EXTENDED
133166

134-
// start = micros();
135-
// for (int i=0; i< 100; i++)
136-
// {
137-
// DAC.smooth2Value(i*10, 10);
138-
// }
139-
// end = micros();
140-
// Serial.print("100x DAC.smooth2Value(i*10, 10):\t");
141-
// Serial.println(end - start);
142-
143167
start = micros();
144168
for (int i=0; i< 1000; i++)
145169
{
@@ -188,7 +212,34 @@ void setup()
188212

189213
#ifdef MCP4725_POWERDOWNMODE
190214

191-
// TODO TIMING TEST
215+
Serial.println("\nEXPERIMENTAL");
216+
217+
start = micros();
218+
for (int i=0; i< 10; i++)
219+
{
220+
volatile int x = DAC.readPowerDownModeDAC();
221+
}
222+
end = micros();
223+
Serial.print("10x DAC.readPowerDownModeDAC():\t\t");
224+
Serial.println(end - start);
225+
226+
start = micros();
227+
for (int i=0; i< 10; i++)
228+
{
229+
volatile int x = DAC.readPowerDownModeEEPROM();
230+
}
231+
end = micros();
232+
Serial.print("10x DAC.readPowerDownModeEEPROM():\t");
233+
Serial.println(end - start);
234+
235+
start = micros();
236+
for (int i=0; i< 10; i++)
237+
{
238+
volatile int x = DAC.writePowerDownMode(i & 0x03);
239+
}
240+
end = micros();
241+
Serial.print("10x DAC.writePowerDownMode(i):\t\t");
242+
Serial.println(end - start);
192243

193244
#endif
194245

@@ -213,5 +264,3 @@ void loop()
213264

214265

215266

216-
217-

0 commit comments

Comments
 (0)