0.4.0 MCP4725

This commit is contained in:
Rob Tillaart 2023-12-08 16:32:16 +01:00
parent 60c9575c7a
commit b0736c59be
18 changed files with 154 additions and 365 deletions

View File

@ -5,9 +5,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.8] - 2023-09-25 ## [0.4.0] - 2023-12-08
- add Wire1 support for ESP32 - refactor API, begin()
- update readme.md
- add **uint8_t getAddress()**
- update examples
- fix **MCP4725_voltage.ino** example
- minor edits
----
## [0.3.9] - 2023-09-25
- add Wire1 support for ESP32
## [0.3.8] - 2023-09-18 ## [0.3.8] - 2023-09-18
- fix #30 Voltage functions (wrapper). - fix #30 Voltage functions (wrapper).

View File

@ -2,7 +2,7 @@
// FILE: MCP4725.cpp // FILE: MCP4725.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725 // PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.9 // VERSION: 0.4.0
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
@ -29,51 +29,9 @@ MCP4725::MCP4725(const uint8_t deviceAddress, TwoWire *wire)
} }
#if defined(ESP8266) || defined(ESP32)
bool MCP4725::begin(const uint8_t dataPin, const uint8_t clockPin)
{
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
if (isConnected())
{
_lastValue = readDAC();
_powerDownMode = readPowerDownModeDAC();
return true;
}
return false;
}
#endif
#if defined (ARDUINO_ARCH_RP2040)
bool MCP4725::begin(int sda, int scl)
{
_wire->setSDA(sda);
_wire->setSCL(scl);
_wire->begin();
if (isConnected())
{
_lastValue = readDAC();
_powerDownMode = readPowerDownModeDAC();
return true;
}
return false;
}
#endif
bool MCP4725::begin() bool MCP4725::begin()
{ {
_wire->begin(); if ((_deviceAddress < 0x60) || (_deviceAddress > 0x67)) return false;
if (! isConnected()) return false; if (! isConnected()) return false;
_lastValue = readDAC(); _lastValue = readDAC();
@ -89,9 +47,15 @@ bool MCP4725::isConnected()
} }
uint8_t MCP4725::getAddress()
{
return _deviceAddress;
}
int MCP4725::setValue(const uint16_t value) int MCP4725::setValue(const uint16_t value)
{ {
if (value == _lastValue) return 0; if (value == _lastValue) return MCP4725_OK;
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR; if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
int rv = _writeFastMode(value); int rv = _writeFastMode(value);
if (rv == 0) _lastValue = value; if (rv == 0) _lastValue = value;

View File

@ -3,16 +3,15 @@
// FILE: MCP4725.h // FILE: MCP4725.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725 // PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.9 // VERSION: 0.4.0
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
//
#include "Wire.h" #include "Wire.h"
#include "Arduino.h" #include "Arduino.h"
#define MCP4725_VERSION (F("0.3.9")) #define MCP4725_VERSION (F("0.4.0"))
// CONSTANTS // CONSTANTS
@ -38,23 +37,12 @@
class MCP4725 class MCP4725
{ {
public: public:
// address = 0x60..0x67
explicit MCP4725(const uint8_t deviceAddress, TwoWire *wire = &Wire); explicit MCP4725(const uint8_t deviceAddress, TwoWire *wire = &Wire);
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
#if defined (ARDUINO_ARCH_RP2040)
bool begin(int sda, int scl);
#endif
bool begin(); bool begin();
bool isConnected(); bool isConnected();
uint8_t getAddress();
// uses writeFastMode // uses writeFastMode
int setValue(const uint16_t value = 0); int setValue(const uint16_t value = 0);

View File

@ -24,6 +24,22 @@ The output of the MCP4725 depends on the voltage supplied, which is in the range
of 2.7V .. 5.5V. Check datasheet for the details. of 2.7V .. 5.5V. Check datasheet for the details.
#### 0.4.0 Breaking change
Version 0.4.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.
#### Related
- https://github.com/RobTillaart/AD56x8 (multi channel)
- https://github.com/RobTillaart/AD568X (single channel lower resolution)
- https://github.com/RobTillaart/MCP_DAC (SPI interface)
## Interface ## Interface
```cpp ```cpp
@ -33,11 +49,11 @@ of 2.7V .. 5.5V. Check datasheet for the details.
### Constructor ### Constructor
- **MCP4725(uint8_t deviceAddress, TwoWire \*wire = &Wire)** Constructor, needs I2C address, optional set Wire bus - **MCP4725(uint8_t deviceAddress, TwoWire \*wire = &Wire)** Constructor, needs I2C address, optional set Wire bus
- **bool begin(uint8_t dataPin, uint8_t clockPin)** for ESP32. Returns true if connected. - **bool begin()** initializes internals.
- **bool begin()** for UNO and other boards with hard wired I2C pins. Returns false if address out of range.
Returns true if deviceAddress can be found on the I2C bus. Returns true if deviceAddress can be found on the I2C bus.
- **bool isConnected()** returns true if device (address) can be seen on the I2C bus. - **bool isConnected()** returns true if device (address) can be seen on the I2C bus.
- **uint8_t getAddress())** returns address set in constructor.
### Base ### Base
@ -71,12 +87,13 @@ If one know the specific timing of a sensor one can tune this or even make it ad
(Since 0.3.8) (Since 0.3.8)
Assumes linear behaviour over 12 bit from 0..4095 == 0 .. maxVoltage. Assumes linear behaviour over 12 bit from 0..4095 == 0 .. maxVoltage.
The default value is 5.0 volt. The default value is 5.0 volt.
Allows sort of calibration e.g. setting maxVoltage to 4.9 Volt. Allows sort of calibration e.g. setting maxVoltage to 4.952 Volt.
Furthermore it can be a preferred interface over percentage and raw values. Furthermore it can be a preferred interface over percentage and raw values.
- **void setMaxVoltage(float v = 5.0)** configures maximum voltage of Vout. - **void setMaxVoltage(float v = 5.0)** configures maximum voltage of Vout.
- **float getMaxVoltage()** return set maximum. - **float getMaxVoltage()** return set maximum.
- **void setVoltage(float v)** set the DAC to voltage v. - **void setVoltage(float v)** set the DAC to voltage v.
This maps the voltage to 0..4095 and calls **setValue()**
- **float getVoltage()** get the current setting as a voltage - **float getVoltage()** get the current setting as a voltage
If this behaviour is not precise enough or should be more "complex" the user can If this behaviour is not precise enough or should be more "complex" the user can
@ -121,7 +138,7 @@ MCP4725A3T-E/CH: 0110 011U 0x66 - 0x67
``` ```
If one need more DAC's one might have a look at the MCP4728 If one need more DAC's one might have a look at the MCP4728
It has 4 channels per chip (no experience /library yet) It has 4 channels per chip (no experience / library yet)
#### RP2040 specific #### RP2040 specific
@ -168,7 +185,7 @@ Note that other multiplexers do exist.
Need to do more tests to see how this solution behaves in practice. Need to do more tests to see how this solution behaves in practice.
Verified to work - see https://forum.arduino.cc/t/using-digital-pins-to-control-two-mcp4725-modules/1161482/7. Verified to work - see https://forum.arduino.cc/t/using-digital-pins-to-control-two-mcp4725-modules/1161482/7.
The assumption here is that the devices are all from the same address range. The assumption here is that the devices are all from the same address range (factory bits).
You can control multiple MCP4725 over the hardware I2C bus with an extra IO pin per device. You can control multiple MCP4725 over the hardware I2C bus with an extra IO pin per device.
- Connect the address pin of every MCP4725 to an IO pin which will work as a **SELECT** pin. - Connect the address pin of every MCP4725 to an IO pin which will work as a **SELECT** pin.
@ -188,11 +205,18 @@ You can control multiple MCP4725 over the hardware I2C bus with an extra IO pin
- test the powerDown modes / functions. - test the powerDown modes / functions.
- test A0 (address bit) as SELECT pin. - test A0 (address bit) as SELECT pin.
- optimize
- voltage interface uses float divisions => store reciprocate?
- takes 2 extra floats.
#### Could #### Could
- extend unit tests - extend unit tests
#### Wont
- MCP4725_VERSION ==> MCP4725_LIB_VERSION
## Support ## Support

View File

@ -2,23 +2,24 @@
// FILE: mcp4725_minimal.ino // FILE: mcp4725_minimal.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: Minimal sketch MCP4725 (#29) // PURPOSE: Minimal sketch MCP4725 (#29)
// DATE: 2023-09-13
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
#include "Wire.h" #include "Wire.h"
#include "MCP4725.h" #include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63 MCP4725 MCP(0x62);
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MCP4725 test program: "); Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
MCP.begin(); MCP.begin();
MCP.setValue(1000); MCP.setValue(1000);

View File

@ -2,7 +2,6 @@
// FILE: MCP4725_wave_generator.ino // FILE: MCP4725_wave_generator.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: demo function generators // PURPOSE: demo function generators
// DATE: 2021-01-07
// URL: https://github.com/RobTillaart/FunctionGenerator // URL: https://github.com/RobTillaart/FunctionGenerator
// //
// depending on the platform, the range of "smooth" sinus is limited. // depending on the platform, the range of "smooth" sinus is limited.
@ -42,17 +41,19 @@ uint16_t sine[361];
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION);
Wire.begin();
// Wire.setClock(3400000);
// fill table // fill table
for (int i = 0; i < 361; i++) for (int i = 0; i < 361; i++)
{ {
sine[i] = 2047 + round(2047 * sin(i * PI / 180)); sine[i] = 2047 + round(2047 * sin(i * PI / 180));
} }
Wire.begin();
// ESP32
// MCP.begin(27, 26);
// Wire.setClock(3400000);
MCP.begin(); MCP.begin();
Wire.setClock(800000); Wire.setClock(800000);

View File

@ -2,7 +2,6 @@
// FILE: MCP4725_wave_generator_RP2040.ino // FILE: MCP4725_wave_generator_RP2040.ino
// AUTHOR: Rob Tillaart / Intubun // AUTHOR: Rob Tillaart / Intubun
// PURPOSE: demo function generators // PURPOSE: demo function generators
// DATE: 2021-01-07
// URL: https://github.com/RobTillaart/FunctionGenerator // URL: https://github.com/RobTillaart/FunctionGenerator
// //
// depending on the platform, the range of "smooth" sinus is limited. // depending on the platform, the range of "smooth" sinus is limited.
@ -42,6 +41,13 @@ uint16_t sine[361];
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION);
// Wire.setSDA(16); // adjust if needed
// Wire.setSCL(17);
Wire.begin();
// fill table // fill table
for (int i = 0; i < 361; i++) for (int i = 0; i < 361; i++)
@ -49,7 +55,7 @@ void setup()
sine[i] = 2047 + round(2047 * sin(i * PI / 180)); sine[i] = 2047 + round(2047 * sin(i * PI / 180));
} }
MCP.begin(26, 27); MCP.begin();
Wire1.setClock(800000); Wire1.setClock(800000);
MCP.setValue(0); MCP.setValue(0);
@ -155,9 +161,9 @@ void setup()
break; break;
default: default:
case 's': case 's':
// reference // reference
// float f = ((PI * 2) * t)/period; // float f = ((PI * 2) * t)/period;
// MCP.setValue(2047 + 2047 * sin(f)); // MCP.setValue(2047 + 2047 * sin(f));
// //
int idx = (360 * t) / period; int idx = (360 * t) / period;
MCP.setValue(sine[idx]); // fetch from lookup table MCP.setValue(sine[idx]); // fetch from lookup table
@ -172,4 +178,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -2,7 +2,6 @@
// FILE: mcp4725_isConnected.ino // FILE: mcp4725_isConnected.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: test mcp4725 lib // PURPOSE: test mcp4725 lib
// DATE: 2020-12-26
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
// //
// test to see behaviour when sensor is not connected and reconnected again. E.g. loose wires.. // test to see behaviour when sensor is not connected and reconnected again. E.g. loose wires..
@ -11,7 +10,7 @@
#include "Wire.h" #include "Wire.h"
#include "MCP4725.h" #include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63 MCP4725 MCP(0x62);
bool connected = false; bool connected = false;
@ -19,10 +18,12 @@ bool connected = false;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__); Serial.println(__FILE__);
Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
if (MCP.begin() == false) if (MCP.begin() == false)
{ {
Serial.println("Could not find sensor"); Serial.println("Could not find sensor");

View File

@ -2,7 +2,6 @@
// FILE: MCP4725_keypad.ino // FILE: MCP4725_keypad.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: test MCP4725 lib // PURPOSE: test MCP4725 lib
// DATE: 2019-10-16
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
// //
// Note: possible to replace I2CKeypad with Serial code // Note: possible to replace I2CKeypad with Serial code
@ -11,10 +10,10 @@
#include "Wire.h" #include "Wire.h"
#include "MCP4725.h" #include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63 MCP4725 MCP(0x62);
#include "I2CKeyPad.h" // at least version 0.2.1 #include "I2CKeyPad.h" // at least version 0.2.1
#define KEYPAD_ADDR 0x38 #define KEYPAD_ADDR 0x38
I2CKeyPad keyPad(KEYPAD_ADDR); I2CKeyPad keyPad(KEYPAD_ADDR);
@ -26,9 +25,11 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__); Serial.println(__FILE__);
Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
MCP.begin(); MCP.begin();
MCP.setValue(0); MCP.setValue(0);
keyPad.begin(); keyPad.begin();
@ -52,7 +53,7 @@ void loop()
char updateKeyPadValue(uint32_t &value, uint32_t maxValue) char updateKeyPadValue(uint32_t &value, uint32_t maxValue)
{ {
char v[19] = "123A456B789C*0#DNF"; // last 2 are Fail and Nokey char v[19] = "123A456B789C*0#DNF"; // last 2 are Fail and Nokey
static uint8_t lastKey = 0; static uint8_t lastKey = 0;
uint8_t idx = keyPad.getKey(); uint8_t idx = keyPad.getKey();
@ -82,5 +83,5 @@ char updateKeyPadValue(uint32_t &value, uint32_t maxValue)
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -37,6 +37,8 @@ void setup()
Serial.print("MCP4725_VERSION: "); Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
pinMode(selectPin[i], OUTPUT); pinMode(selectPin[i], OUTPUT);
@ -54,7 +56,6 @@ void setup()
} }
void select(uint8_t nr) void select(uint8_t nr)
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
@ -65,7 +66,6 @@ void select(uint8_t nr)
} }
void loop() void loop()
{ {
int x = analogRead(A0); // to create an output value. int x = analogRead(A0); // to create an output value.

View File

@ -20,7 +20,7 @@
// assume the 4 devices have the same A1 and A2 // assume the 4 devices have the same A1 and A2
// hard-coded address bits. (par 7.2 datasheet) // hard-coded address bits. (par 7.2 datasheet)
// we will access all devices with as 0x63. // we will access all devices with as 0x63.
MCP4725 MCP(0x63); // 0x62 or 0x63 MCP4725 MCP(0x63);
// connect the select pins to the A0 pins // connect the select pins to the A0 pins
@ -35,6 +35,8 @@ void setup()
Serial.print("MCP4725_VERSION: "); Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
pinMode(selectPin[i], OUTPUT); pinMode(selectPin[i], OUTPUT);
@ -46,7 +48,6 @@ void setup()
} }
void select(uint8_t nr) void select(uint8_t nr)
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
@ -57,7 +58,6 @@ void select(uint8_t nr)
} }
void loop() void loop()
{ {
int x = analogRead(A0); // to create an output value. int x = analogRead(A0); // to create an output value.

View File

@ -2,14 +2,13 @@
// FILE: mcp4725_test.ino // FILE: mcp4725_test.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: test mcp4725 lib // PURPOSE: test mcp4725 lib
// DATE: 2013-11-24
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
#include "Wire.h" #include "Wire.h"
#include "MCP4725.h" #include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63 MCP4725 MCP(0x62);
volatile int x; volatile int x;
uint32_t start, stop; uint32_t start, stop;
@ -18,10 +17,13 @@ uint32_t start, stop;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MCP4725 test program: "); Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
Wire.setClock(100000);
MCP.begin(); MCP.begin();
test1(); test1();
test2(); test2();
@ -34,7 +36,9 @@ void setup()
void test1() void test1()
{ {
Serial.print("\nValue:\t"); Serial.println();
Serial.println(__FUNCTION__);
Serial.print("Value:\t");
Serial.println(MCP.getValue()); Serial.println(MCP.getValue());
Serial.println(); Serial.println();
@ -53,7 +57,9 @@ void test1()
void test2() void test2()
{ {
Serial.println("\n\nMCP4725_II\n\n"); Serial.println();
Serial.println(__FUNCTION__);
Serial.println("MCP4725_II\n\n");
for (int i = 100; i < 500; i += 100) for (int i = 100; i < 500; i += 100)
{ {
@ -93,7 +99,9 @@ void test2()
void test3() void test3()
{ {
Serial.println("\n\nMCP4725_POWERDOWNMODE\n\n"); Serial.println();
Serial.println(__FUNCTION__);
Serial.println("MCP4725_POWERDOWNMODE\n\n");
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
@ -140,7 +148,9 @@ void test3()
void test4() void test4()
{ {
Serial.println("\n\nEXPERIMENTAL"); Serial.println();
Serial.println(__FUNCTION__);
Serial.println("EXPERIMENTAL");
Serial.println("MCP.writePowerDownMode(2)"); Serial.println("MCP.writePowerDownMode(2)");
MCP.writePowerDownMode(2); MCP.writePowerDownMode(2);
MCP.writeDAC(405); MCP.writeDAC(405);
@ -175,7 +185,9 @@ void test4()
void test5() void test5()
{ {
Serial.println("\n\nPERFORMANCE"); Serial.println();
Serial.println(__FUNCTION__);
Serial.println("PERFORMANCE");
Serial.println(); Serial.println();
start = micros(); start = micros();
@ -253,7 +265,9 @@ void test5()
void test6() void test6()
{ {
Serial.println("\n\nEXPERIMENTAL II"); Serial.println();
Serial.println(__FUNCTION__);
Serial.println("EXPERIMENTAL II");
start = micros(); start = micros();
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
@ -301,5 +315,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -8,7 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "MCP4725.h" #include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63 MCP4725 MCP(0x62);
volatile int x; volatile int x;
uint32_t start, stop; uint32_t start, stop;
@ -17,19 +17,16 @@ uint32_t start, stop;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MCP4725 test program: "); Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
MCP.begin(); MCP.begin();
// calibrate max voltage // calibrate max voltage
MCP.setMaxVoltage(5.1); MCP.setMaxVoltage(5.1);
}
void test1()
{
Serial.print("\nVoltage:\t"); Serial.print("\nVoltage:\t");
Serial.println(MCP.getVoltage()); Serial.println(MCP.getVoltage());
Serial.println(); Serial.println();
@ -48,254 +45,23 @@ void test1()
} }
void test2()
{
Serial.println("\n\nMCP4725_II\n\n");
for (int i = 100; i < 500; i += 100)
{
Serial.print("writeDAC(");
Serial.print(i);
Serial.print(")\n");
MCP.writeDAC(i);
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
}
Serial.println();
for (int i = 100; i < 500; i += 100)
{
Serial.print("writeDAC(");
Serial.print(i);
Serial.print(", true)\n");
MCP.writeDAC(i, true);
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
}
Serial.println();
Serial.println("writeDAC(200)");
MCP.writeDAC(200);
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
Serial.println();
}
void test3()
{
Serial.println("\n\nMCP4725_POWERDOWNMODE\n\n");
for (int i = 0; i < 4; i++)
{
Serial.print("MCP.writePowerDownMode(");
Serial.print(i);
Serial.println(")");
MCP.writePowerDownMode(i);
Serial.print("EPR PDM Value:\t");
Serial.println(MCP.readPowerDownModeEEPROM());
Serial.println();
}
Serial.println("\n\nEXPERIMENTAL");
Serial.println("MCP.writePowerDownMode(3)");
MCP.writePowerDownMode(3);
MCP.writeDAC(305);
Serial.print("Value:\t");
Serial.println(MCP.getValue());
Serial.println("MCP.powerOnReset()");
Serial.println("Before");
Serial.print("MCP PDM Value:\t");
Serial.println(MCP.readPowerDownModeDAC());
Serial.print("EPR PDM Value:\t");
Serial.println(MCP.readPowerDownModeEEPROM());
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
MCP.powerOnReset();
Serial.println("After");
Serial.print("MCP PDM Value:\t");
Serial.println(MCP.readPowerDownModeDAC());
Serial.print("EPR PDM Value:\t");
Serial.println(MCP.readPowerDownModeEEPROM());
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
Serial.print("Value:\t");
Serial.println(MCP.getValue());
Serial.println();
}
void test4()
{
Serial.println("\n\nEXPERIMENTAL");
Serial.println("MCP.writePowerDownMode(2)");
MCP.writePowerDownMode(2);
MCP.writeDAC(405);
Serial.print("Value:\t");
Serial.println(MCP.getValue());
Serial.println("MCP.powerOnWakeUp()");
Serial.println("Before");
Serial.print("MCP PDM Value:\t");
Serial.println(MCP.readPowerDownModeDAC());
Serial.print("EPR PDM Value:\t");
Serial.println(MCP.readPowerDownModeEEPROM());
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
MCP.powerOnWakeUp();
Serial.println("after");
Serial.print("MCP PDM Value:\t");
Serial.println(MCP.readPowerDownModeDAC());
Serial.print("EPR PDM Value:\t");
Serial.println(MCP.readPowerDownModeEEPROM());
Serial.print("MCPValue:\t");
Serial.println(MCP.readDAC());
Serial.print("EEValue:\t");
Serial.println(MCP.readEEPROM());
Serial.print("Value:\t");
Serial.println(MCP.getValue());
Serial.println();
}
void test5()
{
Serial.println("\n\nPERFORMANCE");
Serial.println();
start = micros();
for (int i = 0; i < 1000; i++)
{
x = MCP.getValue();
}
stop = micros();
Serial.print("1000x MCP.getValue():\t\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 1000; i++)
{
MCP.setValue(i);
}
stop = micros();
Serial.print("1000x MCP.setValue(i):\t\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 1000; i++)
{
MCP.setValue(1000);
}
stop = micros();
Serial.print("1000x MCP.setValue(1000):\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 1000; i++)
{
x = MCP.readDAC();
}
stop = micros();
Serial.print("1000x MCP.readDAC():\t\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 1000; i++)
{
x = MCP.writeDAC(i);
}
stop = micros();
Serial.print("1000x MCP.writeDAC(i):\t\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 10; i++)
{
x = MCP.writeDAC(i, true);
}
stop = micros();
Serial.print("10x MCP.writeDAC(i, true):\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 1000; i++)
{
x = MCP.ready();
}
stop = micros();
Serial.print("1000x MCP.ready():\t\t");
Serial.println(stop - start);
while (!MCP.ready());
MCP.writeDAC(0, true);
start = micros();
while (!MCP.ready());
stop = micros();
Serial.print("EEPROM write latency:\t\t");
Serial.println(stop - start);
}
void test6()
{
Serial.println("\n\nEXPERIMENTAL II");
start = micros();
for (int i = 0; i < 10; i++)
{
x = MCP.readPowerDownModeDAC();
}
stop = micros();
Serial.print("10x MCP.readPowerDownModeDAC():\t\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 10; i++)
{
x = MCP.readPowerDownModeEEPROM();
}
stop = micros();
Serial.print("10x MCP.readPowerDownModeEEPROM():\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 10; i++)
{
x = MCP.writePowerDownMode(i & 0x03);
}
stop = micros();
Serial.print("10x MCP.writePowerDownMode(i):\t\t");
Serial.println(stop - start);
Serial.print("\nDone... (start triangle mode)");
}
void loop() void loop()
{ {
for (uint16_t i = 0; i < 4096; i++) // triangle wave
for (float v = 0.0; v <= 5.1; v += 0.01)
{ {
MCP.setValue(i); MCP.setVoltage(v);
delay(10); delay(1);
} }
for (uint16_t i = 0; i < 4096; i++) for (float v = 5.1; v >= 0.0; v -= 0.01)
{ {
MCP.setValue(4096 - i); MCP.setVoltage(v);
delay(10); delay(1);
} }
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -2,23 +2,24 @@
// FILE: smooth2Value.ino // FILE: smooth2Value.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: test mcp4725 lib // PURPOSE: test mcp4725 lib
// DATE: 2013-12-01
// URL: https://github.com/RobTillaart/MCP4725 // URL: https://github.com/RobTillaart/MCP4725
#include "Wire.h" #include "Wire.h"
#include "MCP4725.h" #include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63 MCP4725 MCP(0x62);
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MCP4725 test program: "); Serial.print("MCP4725_VERSION: ");
Serial.println(MCP4725_VERSION); Serial.println(MCP4725_VERSION);
Wire.begin();
MCP.begin(); MCP.begin();
Serial.print("\nValue:\t"); Serial.print("\nValue:\t");

View File

@ -8,6 +8,7 @@ MCP4725 KEYWORD1
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
begin KEYWORD2 begin KEYWORD2
isConnected KEYWORD2 isConnected KEYWORD2
getAddress KEYWORD2
setValue KEYWORD2 setValue KEYWORD2
getValue KEYWORD2 getValue KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/MCP4725.git" "url": "https://github.com/RobTillaart/MCP4725.git"
}, },
"version": "0.3.9", "version": "0.4.0",
"license": "MIT", "license": "MIT",
"frameworks": "*", "frameworks": "*",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=MCP4725 name=MCP4725
version=0.3.9 version=0.4.0
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for 12 bit I2C DAC - MCP4725 sentence=Arduino library for 12 bit I2C DAC - MCP4725

View File

@ -39,11 +39,15 @@ unittest_teardown()
} }
// MCP.begin() has blocking calls
// cannot be tested without stub
unittest(test_constructor) unittest(test_constructor)
{ {
MCP4725 MCP(0x62); MCP4725 MCP(0x62);
Wire.begin(); Wire.begin();
assertEqual(0x62, MCP.getAddress());
assertEqual(0, MCP.getValue()); assertEqual(0, MCP.getValue());
assertEqual(0, MCP.getLastWriteEEPROM()); assertEqual(0, MCP.getLastWriteEEPROM());
@ -53,6 +57,13 @@ unittest(test_constructor)
} }
unittest(test_invalid_address)
{
MCP4725 MCP_F(0x22);
assertFalse(MCP_F.begin());
}
unittest(test_constant) unittest(test_constant)
{ {
fprintf(stderr, "test default values\n"); fprintf(stderr, "test default values\n");
@ -102,4 +113,6 @@ unittest(test_writeDAC)
unittest_main() unittest_main()
// --------
// -- END OF FILE --