diff --git a/CHANGELOG.md b/CHANGELOG.md index 5670ba4..de3f2e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/). -## [0.3.8] - 2023-09-25 -- add Wire1 support for ESP32 +## [0.4.0] - 2023-12-08 +- 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 - fix #30 Voltage functions (wrapper). diff --git a/MCP4725.cpp b/MCP4725.cpp index 4e0f515..907323e 100644 --- a/MCP4725.cpp +++ b/MCP4725.cpp @@ -2,7 +2,7 @@ // FILE: MCP4725.cpp // AUTHOR: Rob Tillaart // PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725 -// VERSION: 0.3.9 +// VERSION: 0.4.0 // 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() { - _wire->begin(); + if ((_deviceAddress < 0x60) || (_deviceAddress > 0x67)) return false; if (! isConnected()) return false; _lastValue = readDAC(); @@ -89,9 +47,15 @@ bool MCP4725::isConnected() } +uint8_t MCP4725::getAddress() +{ + return _deviceAddress; +} + + 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; int rv = _writeFastMode(value); if (rv == 0) _lastValue = value; diff --git a/MCP4725.h b/MCP4725.h index 91b14c4..878d4c4 100644 --- a/MCP4725.h +++ b/MCP4725.h @@ -3,16 +3,15 @@ // FILE: MCP4725.h // AUTHOR: Rob Tillaart // PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725 -// VERSION: 0.3.9 +// VERSION: 0.4.0 // URL: https://github.com/RobTillaart/MCP4725 -// #include "Wire.h" #include "Arduino.h" -#define MCP4725_VERSION (F("0.3.9")) +#define MCP4725_VERSION (F("0.4.0")) // CONSTANTS @@ -38,23 +37,12 @@ class MCP4725 { public: + // address = 0x60..0x67 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 isConnected(); - + uint8_t getAddress(); // uses writeFastMode int setValue(const uint16_t value = 0); diff --git a/README.md b/README.md index 96d9671..e911a4f 100644 --- a/README.md +++ b/README.md @@ -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. +#### 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 ```cpp @@ -33,11 +49,11 @@ of 2.7V .. 5.5V. Check datasheet for the details. ### Constructor - **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()** for UNO and other boards with hard wired I2C pins. +- **bool begin()** initializes internals. +Returns false if address out of range. 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. - +- **uint8_t getAddress())** returns address set in constructor. ### 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) Assumes linear behaviour over 12 bit from 0..4095 == 0 .. maxVoltage. 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. - **void setMaxVoltage(float v = 5.0)** configures maximum voltage of Vout. - **float getMaxVoltage()** return set maximum. - **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 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 -It has 4 channels per chip (no experience /library yet) +It has 4 channels per chip (no experience / library yet) #### 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. 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. - 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 A0 (address bit) as SELECT pin. +- optimize + - voltage interface uses float divisions => store reciprocate? + - takes 2 extra floats. #### Could - extend unit tests +#### Wont + +- MCP4725_VERSION ==> MCP4725_LIB_VERSION + ## Support diff --git a/examples/MCP4725_minimal/MCP4725_minimal.ino b/examples/MCP4725_minimal/MCP4725_minimal.ino index 07e1f24..6b10c94 100644 --- a/examples/MCP4725_minimal/MCP4725_minimal.ino +++ b/examples/MCP4725_minimal/MCP4725_minimal.ino @@ -2,23 +2,24 @@ // FILE: mcp4725_minimal.ino // AUTHOR: Rob Tillaart // PURPOSE: Minimal sketch MCP4725 (#29) -// DATE: 2023-09-13 // URL: https://github.com/RobTillaart/MCP4725 #include "Wire.h" #include "MCP4725.h" -MCP4725 MCP(0x62); // 0x62 or 0x63 +MCP4725 MCP(0x62); void setup() { Serial.begin(115200); - - Serial.print("MCP4725 test program: "); + Serial.println(__FILE__); + Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + MCP.begin(); MCP.setValue(1000); diff --git a/examples/MCP4725_wave_generator/MCP4725_wave_generator.ino b/examples/MCP4725_wave_generator/MCP4725_wave_generator.ino index 74a540d..f052a11 100644 --- a/examples/MCP4725_wave_generator/MCP4725_wave_generator.ino +++ b/examples/MCP4725_wave_generator/MCP4725_wave_generator.ino @@ -2,7 +2,6 @@ // FILE: MCP4725_wave_generator.ino // AUTHOR: Rob Tillaart // PURPOSE: demo function generators -// DATE: 2021-01-07 // URL: https://github.com/RobTillaart/FunctionGenerator // // depending on the platform, the range of "smooth" sinus is limited. @@ -42,17 +41,19 @@ uint16_t sine[361]; void setup() { Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("MCP4725_VERSION: "); + Serial.println(MCP4725_VERSION); + Wire.begin(); + // Wire.setClock(3400000); + // fill table for (int i = 0; i < 361; i++) { sine[i] = 2047 + round(2047 * sin(i * PI / 180)); } - Wire.begin(); - // ESP32 - // MCP.begin(27, 26); - // Wire.setClock(3400000); MCP.begin(); Wire.setClock(800000); diff --git a/examples/MCP4725_wave_generator_RP2040/MCP4725_wave_generator_RP2040.ino b/examples/MCP4725_wave_generator_RP2040/MCP4725_wave_generator_RP2040.ino index 05261f9..bd704ab 100644 --- a/examples/MCP4725_wave_generator_RP2040/MCP4725_wave_generator_RP2040.ino +++ b/examples/MCP4725_wave_generator_RP2040/MCP4725_wave_generator_RP2040.ino @@ -2,7 +2,6 @@ // FILE: MCP4725_wave_generator_RP2040.ino // AUTHOR: Rob Tillaart / Intubun // PURPOSE: demo function generators -// DATE: 2021-01-07 // URL: https://github.com/RobTillaart/FunctionGenerator // // depending on the platform, the range of "smooth" sinus is limited. @@ -42,6 +41,13 @@ uint16_t sine[361]; void setup() { 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 for (int i = 0; i < 361; i++) @@ -49,7 +55,7 @@ void setup() sine[i] = 2047 + round(2047 * sin(i * PI / 180)); } - MCP.begin(26, 27); + MCP.begin(); Wire1.setClock(800000); MCP.setValue(0); @@ -155,9 +161,9 @@ void setup() break; default: case 's': - // reference - // float f = ((PI * 2) * t)/period; - // MCP.setValue(2047 + 2047 * sin(f)); + // reference + // float f = ((PI * 2) * t)/period; + // MCP.setValue(2047 + 2047 * sin(f)); // int idx = (360 * t) / period; MCP.setValue(sine[idx]); // fetch from lookup table @@ -172,4 +178,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/mcp4725_isConnected/mcp4725_isConnected.ino b/examples/mcp4725_isConnected/mcp4725_isConnected.ino index b817046..c0aa1b3 100644 --- a/examples/mcp4725_isConnected/mcp4725_isConnected.ino +++ b/examples/mcp4725_isConnected/mcp4725_isConnected.ino @@ -2,7 +2,6 @@ // FILE: mcp4725_isConnected.ino // AUTHOR: Rob Tillaart // PURPOSE: test mcp4725 lib -// DATE: 2020-12-26 // URL: https://github.com/RobTillaart/MCP4725 // // test to see behaviour when sensor is not connected and reconnected again. E.g. loose wires.. @@ -11,7 +10,7 @@ #include "Wire.h" #include "MCP4725.h" -MCP4725 MCP(0x62); // 0x62 or 0x63 +MCP4725 MCP(0x62); bool connected = false; @@ -19,10 +18,12 @@ bool connected = false; void setup() { Serial.begin(115200); - Serial.println(__FILE__); + Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + if (MCP.begin() == false) { Serial.println("Could not find sensor"); diff --git a/examples/mcp4725_keypad/mcp4725_keypad.ino b/examples/mcp4725_keypad/mcp4725_keypad.ino index 3f17d22..1016361 100644 --- a/examples/mcp4725_keypad/mcp4725_keypad.ino +++ b/examples/mcp4725_keypad/mcp4725_keypad.ino @@ -2,7 +2,6 @@ // FILE: MCP4725_keypad.ino // AUTHOR: Rob Tillaart // PURPOSE: test MCP4725 lib -// DATE: 2019-10-16 // URL: https://github.com/RobTillaart/MCP4725 // // Note: possible to replace I2CKeypad with Serial code @@ -11,10 +10,10 @@ #include "Wire.h" #include "MCP4725.h" -MCP4725 MCP(0x62); // 0x62 or 0x63 +MCP4725 MCP(0x62); -#include "I2CKeyPad.h" // at least version 0.2.1 -#define KEYPAD_ADDR 0x38 +#include "I2CKeyPad.h" // at least version 0.2.1 +#define KEYPAD_ADDR 0x38 I2CKeyPad keyPad(KEYPAD_ADDR); @@ -26,9 +25,11 @@ void setup() { Serial.begin(115200); Serial.println(__FILE__); - + Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + MCP.begin(); MCP.setValue(0); keyPad.begin(); @@ -52,7 +53,7 @@ void loop() 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; uint8_t idx = keyPad.getKey(); @@ -82,5 +83,5 @@ char updateKeyPadValue(uint32_t &value, uint32_t maxValue) } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/mcp4725_multiple/mcp4725_multiple.ino b/examples/mcp4725_multiple/mcp4725_multiple.ino index cfc8a58..28390b0 100644 --- a/examples/mcp4725_multiple/mcp4725_multiple.ino +++ b/examples/mcp4725_multiple/mcp4725_multiple.ino @@ -37,6 +37,8 @@ void setup() Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + for (int i = 0; i < 4; i++) { pinMode(selectPin[i], OUTPUT); @@ -54,7 +56,6 @@ void setup() } - void select(uint8_t nr) { for (int i = 0; i < 4; i++) @@ -65,7 +66,6 @@ void select(uint8_t nr) } - void loop() { int x = analogRead(A0); // to create an output value. diff --git a/examples/mcp4725_multiple_minimal/mcp4725_multiple_minimal.ino b/examples/mcp4725_multiple_minimal/mcp4725_multiple_minimal.ino index f17f48b..9a1f41d 100644 --- a/examples/mcp4725_multiple_minimal/mcp4725_multiple_minimal.ino +++ b/examples/mcp4725_multiple_minimal/mcp4725_multiple_minimal.ino @@ -20,7 +20,7 @@ // assume the 4 devices have the same A1 and A2 // hard-coded address bits. (par 7.2 datasheet) // 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 @@ -35,6 +35,8 @@ void setup() Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + for (int i = 0; i < 4; i++) { pinMode(selectPin[i], OUTPUT); @@ -46,7 +48,6 @@ void setup() } - void select(uint8_t nr) { for (int i = 0; i < 4; i++) @@ -57,7 +58,6 @@ void select(uint8_t nr) } - void loop() { int x = analogRead(A0); // to create an output value. diff --git a/examples/mcp4725_test/mcp4725_test.ino b/examples/mcp4725_test/mcp4725_test.ino index 073e4e2..cc38307 100644 --- a/examples/mcp4725_test/mcp4725_test.ino +++ b/examples/mcp4725_test/mcp4725_test.ino @@ -2,14 +2,13 @@ // FILE: mcp4725_test.ino // AUTHOR: Rob Tillaart // PURPOSE: test mcp4725 lib -// DATE: 2013-11-24 // URL: https://github.com/RobTillaart/MCP4725 #include "Wire.h" #include "MCP4725.h" -MCP4725 MCP(0x62); // 0x62 or 0x63 +MCP4725 MCP(0x62); volatile int x; uint32_t start, stop; @@ -18,10 +17,13 @@ uint32_t start, stop; void setup() { Serial.begin(115200); - - Serial.print("MCP4725 test program: "); + Serial.println(__FILE__); + Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + Wire.setClock(100000); + MCP.begin(); test1(); test2(); @@ -34,7 +36,9 @@ void setup() void test1() { - Serial.print("\nValue:\t"); + Serial.println(); + Serial.println(__FUNCTION__); + Serial.print("Value:\t"); Serial.println(MCP.getValue()); Serial.println(); @@ -53,7 +57,9 @@ void test1() 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) { @@ -93,7 +99,9 @@ void test2() 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++) { @@ -140,7 +148,9 @@ void test3() void test4() { - Serial.println("\n\nEXPERIMENTAL"); + Serial.println(); + Serial.println(__FUNCTION__); + Serial.println("EXPERIMENTAL"); Serial.println("MCP.writePowerDownMode(2)"); MCP.writePowerDownMode(2); MCP.writeDAC(405); @@ -175,7 +185,9 @@ void test4() void test5() { - Serial.println("\n\nPERFORMANCE"); + Serial.println(); + Serial.println(__FUNCTION__); + Serial.println("PERFORMANCE"); Serial.println(); start = micros(); @@ -253,7 +265,9 @@ void test5() void test6() { - Serial.println("\n\nEXPERIMENTAL II"); + Serial.println(); + Serial.println(__FUNCTION__); + Serial.println("EXPERIMENTAL II"); start = micros(); for (int i = 0; i < 10; i++) @@ -301,5 +315,4 @@ void loop() } -// -- END OF FILE -- - +// -- END OF FILE -- diff --git a/examples/mcp4725_voltage/mcp4725_voltage.ino b/examples/mcp4725_voltage/mcp4725_voltage.ino index ba6abd5..dfe50a8 100644 --- a/examples/mcp4725_voltage/mcp4725_voltage.ino +++ b/examples/mcp4725_voltage/mcp4725_voltage.ino @@ -8,7 +8,7 @@ #include "Wire.h" #include "MCP4725.h" -MCP4725 MCP(0x62); // 0x62 or 0x63 +MCP4725 MCP(0x62); volatile int x; uint32_t start, stop; @@ -17,19 +17,16 @@ uint32_t start, stop; void setup() { Serial.begin(115200); - - Serial.print("MCP4725 test program: "); + Serial.println(__FILE__); + Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + MCP.begin(); // calibrate max voltage MCP.setMaxVoltage(5.1); - -} - -void test1() -{ Serial.print("\nVoltage:\t"); Serial.println(MCP.getVoltage()); 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() { - for (uint16_t i = 0; i < 4096; i++) + // triangle wave + for (float v = 0.0; v <= 5.1; v += 0.01) { - MCP.setValue(i); - delay(10); + MCP.setVoltage(v); + 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); - delay(10); + MCP.setVoltage(v); + delay(1); } } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/smooth2Value/smooth2Value.ino b/examples/smooth2Value/smooth2Value.ino index e502584..0dbc53e 100644 --- a/examples/smooth2Value/smooth2Value.ino +++ b/examples/smooth2Value/smooth2Value.ino @@ -2,23 +2,24 @@ // FILE: smooth2Value.ino // AUTHOR: Rob Tillaart // PURPOSE: test mcp4725 lib -// DATE: 2013-12-01 // URL: https://github.com/RobTillaart/MCP4725 #include "Wire.h" #include "MCP4725.h" -MCP4725 MCP(0x62); // 0x62 or 0x63 +MCP4725 MCP(0x62); void setup() { Serial.begin(115200); - - Serial.print("MCP4725 test program: "); + Serial.println(__FILE__); + Serial.print("MCP4725_VERSION: "); Serial.println(MCP4725_VERSION); + Wire.begin(); + MCP.begin(); Serial.print("\nValue:\t"); diff --git a/keywords.txt b/keywords.txt index 58dba4d..64cc931 100644 --- a/keywords.txt +++ b/keywords.txt @@ -8,6 +8,7 @@ MCP4725 KEYWORD1 # Methods and Functions (KEYWORD2) begin KEYWORD2 isConnected KEYWORD2 +getAddress KEYWORD2 setValue KEYWORD2 getValue KEYWORD2 diff --git a/library.json b/library.json index 36989fe..35ea2be 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/MCP4725.git" }, - "version": "0.3.9", + "version": "0.4.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index 4f803e7..6a1c6c0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MCP4725 -version=0.3.9 +version=0.4.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for 12 bit I2C DAC - MCP4725 diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index c09c1a1..ffaf6e9 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -39,11 +39,15 @@ unittest_teardown() } +// MCP.begin() has blocking calls +// cannot be tested without stub unittest(test_constructor) { MCP4725 MCP(0x62); Wire.begin(); + assertEqual(0x62, MCP.getAddress()); + assertEqual(0, MCP.getValue()); 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) { fprintf(stderr, "test default values\n"); @@ -102,4 +113,6 @@ unittest(test_writeDAC) unittest_main() -// -------- + +// -- END OF FILE -- +