0.3.3 MCP4725

This commit is contained in:
rob tillaart 2021-12-21 16:28:24 +01:00
parent f0b8e6ecfe
commit d44d7e680e
15 changed files with 136 additions and 76 deletions

View File

@ -2,6 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2013-2021 Rob Tillaart
Copyright (c) 2013-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -2,7 +2,7 @@
// FILE: MCP4725.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.2
// VERSION: 0.3.3
// URL: https://github.com/RobTillaart/MCP4725
//
// HISTORY:
@ -21,10 +21,11 @@
// 0.2.1 2020-07-04 Add yield(); add getLastWriteEEPROM();
// update readme.md + keywords.txt
// 0.2.2 2020-07-05 add get/setPercentage();
// 0.2.3 2020-12-26 arduino-CI, bool isConnected(), bool begin()
// 0.2.3 2020-12-26 Arduino-CI, bool isConnected(), bool begin()
// 0.3.0 2021-01-15 Add WireN support (e.g. teensy)
// 0.3.1 2021-05-27 Fix arduino-CI / arduino-lint
// 0.3.1 2021-05-27 Fix Arduino-CI / Arduino-lint
// 0.3.2 2021-06-06 Verify input of setPercentage()
// 0.3.3 2021-12-21 update library.json, license, minor edits
#include "MCP4725.h"
@ -104,14 +105,13 @@ uint16_t MCP4725::getValue()
}
int MCP4725::setPercentage(float perc)
int MCP4725::setPercentage(float percentage)
{
if ((perc > 100) || (perc < 0)) return MCP4725_VALUE_ERROR;
return setValue(round(perc * (0.01 * MCP4725_MAXVALUE)));
if ((percentage > 100) || (percentage < 0)) return MCP4725_VALUE_ERROR;
return setValue(round(percentage * (0.01 * MCP4725_MAXVALUE)));
}
// unfortunately it is not possible to write a different value
// to the DAC and EEPROM simultaneously or write EEPROM only.
int MCP4725::writeDAC(const uint16_t value, const bool EEPROM)
@ -153,7 +153,7 @@ uint16_t MCP4725::readEEPROM()
// (true) DAC & EEPROM,
int MCP4725::writePowerDownMode(const uint8_t PDM, const bool EEPROM)
{
_powerDownMode = (PDM & 0x03); // mask pdm bits only (written later low level)
_powerDownMode = (PDM & 0x03); // mask PDM bits only (written later low level)
return writeDAC(_lastValue, EEPROM);
}
@ -190,7 +190,7 @@ int MCP4725::powerOnReset()
// PAGE 22 - experimental
// _powerDownMode DAC resets to 0 -- pdm EEPROM stays same !!!
// _powerDownMode DAC resets to 0 -- PDM EEPROM stays same !!!
// need to reflect this in cached value
int MCP4725::powerOnWakeUp()
{
@ -270,4 +270,6 @@ int MCP4725::_generalCall(const uint8_t gc)
return _wire->endTransmission();
}
// -- END OF FILE --

View File

@ -3,7 +3,7 @@
// FILE: MCP4725.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.2
// VERSION: 0.3.3
// URL: https://github.com/RobTillaart/MCP4725
//
@ -12,7 +12,7 @@
#include "Arduino.h"
#define MCP4725_VERSION (F("0.3.2"))
#define MCP4725_VERSION (F("0.3.3"))
// constants
@ -52,12 +52,12 @@ public:
// 0..100.0% - no input check.
int setPercentage(float perc = 0);
int setPercentage(float percentage = 0);
float getPercentage() { return getValue() * (100.0 / MCP4725_MAXVALUE); };
int writeDAC(const uint16_t value, const bool EEPROM = false);
// RDY isdepreciated in the future, use ready() instead.
// RDY is depreciated in the future, use ready() instead.
// inline bool RDY() { return ready(); };
bool ready();
uint32_t getLastWriteEEPROM() { return _lastWriteEEPROM; };
@ -88,4 +88,6 @@ private:
TwoWire* _wire;
};
// -- END OF FILE --

View File

@ -1,17 +1,21 @@
[![Arduino CI](https://github.com/RobTillaart/MCP4725/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/MCP4725/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP4725/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/MCP4725/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MCP4725/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MCP4725/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MCP4725.svg?maxAge=3600)](https://github.com/RobTillaart/MCP4725/releases)
# MCP4725
Arduino library for 12 bit I2C DAC - MCP4725
Arduino library for 12 bit I2C DAC - MCP4725.
## Description
The MCP4725 is an I2C 12 bit Digital to Analog Converter (DAC). It is possible to have
up to 8 MCP4725 on one bus. The MCP4725 supports 100KHz 400KHz and 3.4MHz bus speeds.
The MCP4725 is an I2C 12 bit Digital to Analog Converter (DAC).
It is possible to have up to 8 MCP4725 on one I2C bus.
The MCP4725 supports 100 KHz 400 KHz and 3.4 MHz bus speeds.
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.
@ -21,32 +25,36 @@ of 2.7V .. 5.5V. Check datasheet for the details.
### Constructor
- **MCP4725(deviceAddress, TwoWire \*wire = &Wire);)** Constructor, needs I2C address, optional set Wire bus
- **begin(dataPin, clockPin)** for ESP32. Returns true if connected.
- **begin()** for UNO and other boards with hard wired I2C pins. Returns true if connected.
Only suppport for Wire so not for Wire1 or Wire2 etc
- **isConnected()** returns true if device (address) can be seen on the I2C 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()** for UNO and other boards with hard wired I2C pins.
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.
### base
- **setValue(value)** value = 0 .. 4095
- **int setValue(uint16_t value = 0)** value = 0 .. 4095.
Uses writeFastMode and does not write to EEPROM.
Therfore it does not update the lastWriteEEPROM timestamp.
- **getValue()** returns last value set from cache, this is much faster than readDAC().
This latter gives the real value from the MCP4725. A difference can be caused by power
outage a reset etc.
- **setPercentage(perc)** perc = 0..100.0% Convenience wrapper around setValue().
- **getPercentage()** returns percentage. Wrapper around getValue().
- **writeDAC(value, const bool EEPROM = false)** Writes to DAC and conditionally to EEPROM.
This latter is for startup / reset behavior. Check datasheet for the detail behavior.
- **ready()** returns true if a new value can be written to the MCP4725.
Therefore it does not update the lastWriteEEPROM timestamp.
The default value is 0.
Returns 0 on success
- **uint16_t getValue()** returns last value set from cache, this is much faster than readDAC().
This latter gives the real value from the MCP4725.
Note: a difference can be caused by power outage a reset etc.
- **int setPercentage(float percentage)** percentage = 0..100.0%.
Convenience wrapper around setValue().
- **float getPercentage()** returns percentage. Wrapper around getValue().
- **int writeDAC(value, bool EEPROM = false)** Writes to DAC and conditionally to EEPROM.
This latter is for startup / reset behaviour. Check datasheet for the detail behaviour.
- **bool ready()** returns true if a new value can be written to the MCP4725.
Return false if recently was written to EEPROM.
- **readDAC()** reads the current value set in the MCP4725.
- **readEEPROM()** reads the current value in the EEPROM of the MCP4725.
- **getLastWriteEEPROM()** returns time in millis() when last value was written to EEPROM.
- **uint16_t readDAC()** reads the current value set in the MCP4725.
- **uint16_t readEEPROM()** reads the current value in the EEPROM of the MCP4725.
- **uint32_t getLastWriteEEPROM()** returns time in millis() when last value was written to EEPROM.
Page 3 of datasheet states it takes **25 - 50 ms** before values are written to EEPROM.
So the exact timing of a sensor can differ a lot.
So the exact timing of a sensor can differ quite a lot.
When writing to EEPROM with **writeDAC()** one should check it is at least 50 ms ago.
If one know the specific timing of a sensor one can tune this or even make it adaptive.
@ -55,11 +63,11 @@ If one know the specific timing of a sensor one can tune this or even make it ad
Check datasheet for these functions, (not tested enough yet).
- **writePowerDownMode(PDM, const bool EEPROM = false)**
- **readPowerDownModeEEPROM()**
- **readPowerDownModeDAC()**
- **powerOnReset()**
- **powerOnWakeUp()**
- **int writePowerDownMode(uint8_t PDM, bool EEPROM = false)**
- **uint8_t readPowerDownModeEEPROM()**
- **uint8_t readPowerDownModeDAC()**
- **int powerOnReset()**
- **int powerOnWakeUp()**
More investigations needed for:
- Writing to EEPROM, **ready()** and **getLastWriteEEPROM()**
@ -95,3 +103,14 @@ It has 4 channels per chip (no experience /library yet)
## Operation
See examples
## Future
- update documentation
- test the powerDown modes / functions.
- write a sketch to measure the **writeDac()** timing if possible.
- extend unit tests
-

View File

@ -1,7 +1,6 @@
//
// FILE: MCP4725_wave_generator.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo function generators
// DATE: 2021-01-07
// URL: https://github.com/RobTillaart/FunctionGenerator
@ -10,10 +9,11 @@
// other signals are less difficult so have a slightly larger range.
//
// PLATFORM SINUS SQUARE SAWTOOTH TRIANGLE
// UNO -100 Hz
// UNO -100 Hz
// ESP32 -200 Hz -1000 -250 -100
//
#include "MCP4725.h"
#include "Wire.h"
@ -171,4 +171,6 @@ void loop()
{
}
// END OF FILE
// -- END OF FILE --

View File

@ -1,13 +1,12 @@
//
// FILE: mcp4725_isConnected.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// 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..
// test to see behavior when sensor is not connected and reconnected again. E.g. loose wires..
#include "Wire.h"
#include "MCP4725.h"
@ -16,6 +15,7 @@ MCP4725 MCP(0x62); // 0x62 or 0x63
bool connected = false;
void setup()
{
Serial.begin(115200);
@ -31,6 +31,7 @@ void setup()
Serial.println("end of setup()");
}
void loop()
{
if (!connected && MCP.isConnected())
@ -47,5 +48,5 @@ void loop()
}
// -- END OF FILE --

View File

@ -1,12 +1,10 @@
//
// FILE: MCP4725_keypad.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: test MCP4725 lib
// DATE: 2019-10-16
// URL: https://github.com/RobTillaart/MCP4725
//
// Note: possible to replace I2CKeypad with Serial code
@ -23,6 +21,7 @@ I2CKeyPad keyPad(KEYPAD_ADDR);
uint32_t value = 0;
uint32_t oldValue = 0;
void setup()
{
Serial.begin(115200);
@ -35,6 +34,7 @@ void setup()
keyPad.begin();
}
void loop()
{
char c = updateKeyPadValue(value, 4095);
@ -81,4 +81,6 @@ char updateKeyPadValue(uint32_t &value, uint32_t maxValue)
return c;
}
// -- END OF FILE --

View File

@ -1,11 +1,10 @@
//
// FILE: mcp4725_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// PURPOSE: test mcp4725 lib
// DATE: 2013-11-24
// URL: https://github.com/RobTillaart/MCP4725
//
#include "Wire.h"
#include "MCP4725.h"
@ -15,6 +14,7 @@ MCP4725 MCP(0x62); // 0x62 or 0x63
volatile int x;
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
@ -31,6 +31,7 @@ void setup()
test6();
}
void test1()
{
Serial.print("\nValue:\t");
@ -49,6 +50,7 @@ void test1()
Serial.println();
}
void test2()
{
Serial.println("\n\nMCP4725_II\n\n");
@ -88,6 +90,7 @@ void test2()
Serial.println();
}
void test3()
{
Serial.println("\n\nMCP4725_POWERDOWNMODE\n\n");
@ -134,6 +137,7 @@ void test3()
Serial.println();
}
void test4()
{
Serial.println("\n\nEXPERIMENTAL");
@ -168,6 +172,7 @@ void test4()
Serial.println();
}
void test5()
{
Serial.println("\n\nPERFORMANCE");
@ -245,6 +250,7 @@ void test5()
Serial.println(stop - start);
}
void test6()
{
Serial.println("\n\nEXPERIMENTAL II");
@ -279,6 +285,7 @@ void test6()
Serial.print("\nDone... (start triangle mode)");
}
void loop()
{
for (uint16_t i = 0; i < 4096; i++)
@ -293,4 +300,6 @@ void loop()
}
}
// -- END OF FILE --

View File

@ -1,17 +1,17 @@
//
// FILE: smooth2Value.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// 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
void setup()
{
Serial.begin(115200);
@ -54,6 +54,7 @@ void setup()
Serial.print("\nDone... (start triangle mode)");
}
void loop()
{
// different gradients
@ -84,4 +85,6 @@ int smooth2Value(uint16_t value, uint16_t steps)
return MCP.setValue(value);
}
// -- END OF FILE --

View File

@ -1,34 +1,43 @@
# Syntax Coloring Map For MCP4725
# Syntax Colouring Map For MCP4725
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
MCP4725 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
read KEYWORD2
setValue KEYWORD2
getValue KEYWORD2
writeDAC KEYWORD2
ready KEYWORD2
readDAC KEYWORD2
readEEPROM KEYWORD2
writePowerDownMode KEYWORD2
readPowerDownModeEEPROM KEYWORD2
readPowerDownModeDAC KEYWORD2
powerOnReset KEYWORD2
powerOnWakeUp KEYWORD2
getLastWriteEEPROM KEYWORD2
setPercentage KEYWORD2
getPercentage KEYWORD2
# Instances (KEYWORD2)
# Constants (LITERAL1)
MCP4725_VERSION LITERAL1
MCP4725_MAXVALUE LITERAL1
MCP4725_VALUE_ERROR LITERAL1
MCP4725_REG_ERROR LITERAL1
MCP4725_PDMODE_NORMAL LITERAL1
MCP4725_PDMODE_1K LITERAL1
MCP4725_PDMODE_100K LITERAL1

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/MCP4725.git"
},
"version": "0.3.2",
"version": "0.3.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
"platforms": "*",
"headers": "MCP4725.h"
}

View File

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

View File

@ -28,11 +28,12 @@
#include "MCP4725.h"
unittest_setup()
{
fprintf(stderr, "MCP4725_VERSION: %s\n", (char *) MCP4725_VERSION);
}
unittest_teardown()
{
}
@ -40,10 +41,20 @@ unittest_teardown()
unittest(test_constructor)
{
fprintf(stderr, "VERSION: %s\n", MCP4725_VERSION);
MCP4725 MCP(0x62);
Wire.begin();
assertEqual(0, MCP.getValue());
assertEqual(0, MCP.getLastWriteEEPROM());
fprintf(stderr, "test start\n");
assureTrue(MCP.isConnected());
// assertTrue(MCP.begin());
}
unittest(test_constant)
{
fprintf(stderr, "test default values\n");
assertEqual(MCP4725_MAXVALUE, 4095);
assertEqual(MCP4725_OK, 0);
@ -56,16 +67,6 @@ unittest(test_constructor)
assertEqual(MCP4725_PDMODE_1K, 1);
assertEqual(MCP4725_PDMODE_100K, 2);
assertEqual(MCP4725_PDMODE_500K, 3);
assertEqual(0, MCP.getValue());
assertEqual(0, MCP.getLastWriteEEPROM());
fprintf(stderr, "test start\n");
assureTrue(MCP.isConnected());
// assertTrue(MCP.begin());
// assertTrue(MCP.isConnected());
}
@ -95,6 +96,7 @@ unittest(test_writeDAC)
assertEqual(MCP4725_VALUE_ERROR, MCP.writeDAC(4096, true));
}
unittest_main()
// --------