0.3.8 MCP4725

This commit is contained in:
Rob Tillaart 2023-09-19 20:21:06 +02:00
parent 1a6c20ff2e
commit 8d81583609
9 changed files with 414 additions and 7 deletions

View File

@ -5,13 +5,22 @@ 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-18
- fix #30 Voltage functions (wrapper).
- **void setMaxVoltage(float v)**
- **void getMaxVoltage(float v)**
- **void setVoltage(float v)**
- **float getVoltage()**
- add examples
- update readme.md
## [0.3.7] - 2023-08-25
- fix #27 - support for multiple MCP4725
- add two examples.
- update readme.md
- minor edits
## [0.3.6] - 2023-07-19
- fix #25 - buffer overflow in examples
- update keywords.txt

View File

@ -2,7 +2,7 @@
// FILE: MCP4725.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.7
// VERSION: 0.3.8
// URL: https://github.com/RobTillaart/MCP4725
@ -25,6 +25,7 @@ MCP4725::MCP4725(const uint8_t deviceAddress, TwoWire *wire)
_lastValue = 0;
_powerDownMode = 0;
_lastWriteEEPROM = 0;
_maxVoltage = 5.0;
}
@ -115,7 +116,31 @@ int MCP4725::setPercentage(float percentage)
float MCP4725::getPercentage()
{
return getValue() * (100.0 / MCP4725_MAXVALUE);
};
}
void MCP4725::setMaxVoltage(float v)
{
_maxVoltage = v;
}
float MCP4725::getMaxVoltage()
{
return _maxVoltage;
}
int MCP4725::setVoltage(float v)
{
return setValue(round((v * MCP4725_MAXVALUE) / _maxVoltage));
}
float MCP4725::getVoltage()
{
return getValue() * (_maxVoltage / MCP4725_MAXVALUE);
}
// unfortunately it is not possible to write a different value

View File

@ -3,7 +3,7 @@
// FILE: MCP4725.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for 12 bit I2C DAC - MCP4725
// VERSION: 0.3.7
// VERSION: 0.3.8
// URL: https://github.com/RobTillaart/MCP4725
//
@ -12,7 +12,7 @@
#include "Arduino.h"
#define MCP4725_VERSION (F("0.3.7"))
#define MCP4725_VERSION (F("0.3.8"))
// CONSTANTS
@ -68,6 +68,14 @@ public:
// due to rounding the returned value can differ slightly.
float getPercentage();
// typical and default value = 5.0 V
void setMaxVoltage(float v = 5.0);
float getMaxVoltage();
// will set 0..5V to the closest integer value in range 0..4095
int setVoltage(float v);
// returns (approximation of) the set voltage. Assumes linear mapping.
float getVoltage();
// unfortunately it is not possible to write a different value
// to the DAC and EEPROM simultaneously or write EEPROM only.
@ -100,6 +108,8 @@ private:
int _generalCall(const uint8_t gc);
float _maxVoltage;
TwoWire* _wire;
};

View File

@ -66,6 +66,23 @@ When writing to EEPROM with **writeDAC()** one should check it is at least 50 ms
If one know the specific timing of a sensor one can tune this or even make it adaptive.
### Voltage interface
(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.
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.
- **float getVoltage()** get the current setting as a voltage
If this behaviour is not precise enough or should be more "complex" the user can
always define its own set of wrapper functions around **getValue()** and **setValue()**.
## Experimental
Check datasheet for these functions, (not tested enough yet).

View File

@ -0,0 +1,40 @@
//
// 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
void setup()
{
Serial.begin(115200);
Serial.print("MCP4725 test program: ");
Serial.println(MCP4725_VERSION);
MCP.begin();
MCP.setValue(1000);
// define button
pinMode(7, INPUT_PULLUP); // connect button to GND
}
void loop()
{
if (digitalRead(7) == LOW)
{
MCP.setValue(0);
}
}
// -- END OF FILE --

View File

@ -0,0 +1,301 @@
//
// FILE: mcp4725_voltage.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test mcp4725 lib
// URL: https://github.com/RobTillaart/MCP4725
#include "Wire.h"
#include "MCP4725.h"
MCP4725 MCP(0x62); // 0x62 or 0x63
volatile int x;
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.print("MCP4725 test program: ");
Serial.println(MCP4725_VERSION);
MCP.begin();
// calibrate max voltage
MCP.setMaxVoltage(5.1);
}
void test1()
{
Serial.print("\nVoltage:\t");
Serial.println(MCP.getVoltage());
Serial.println();
for (float v = 0.0; v <= 5.1; v += 0.1)
{
Serial.print("setVoltage(");
Serial.print(v);
Serial.print(")\n");
MCP.setVoltage(v);
Serial.print("getVoltage:\t");
Serial.println(MCP.getVoltage());
}
Serial.println();
}
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++)
{
MCP.setValue(i);
delay(10);
}
for (uint16_t i = 0; i < 4096; i++)
{
MCP.setValue(4096 - i);
delay(10);
}
}
// -- END OF FILE --

View File

@ -15,6 +15,11 @@ getValue KEYWORD2
setPercentage KEYWORD2
getPercentage KEYWORD2
setMaxVoltage KEYWORD2
getMaxVoltage KEYWORD2
setVoltage KEYWORD2
getVoltage KEYWORD2
writeDAC KEYWORD2
ready KEYWORD2
getLastWriteEEPROM KEYWORD2

View File

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

View File

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