TI DAC library for DAC5578 (8bit) / DAC6578 (10bit) / DAC7578 (12bit)

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Mon May 14 04:23:31 2018 +0000
Commit message:
1st build

Changed in this revision

DACx578.cpp Show annotated file Show diff for this revision Revisions of this file
DACx578.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DACx578.cpp	Mon May 14 04:23:31 2018 +0000
@@ -0,0 +1,68 @@
+/*
+ * DAC5578 (8bit)
+ * DAC6578 (10bit)
+ * DAC7578 (12bit)
+ */
+
+#include "DACx578.h"
+
+DACx578::DACx578 (PinName sda, PinName scl, int addr, enum DEVICE device) : _i2c(sda, scl) {
+    _addr = addr;
+    _device = device;
+    init();
+}
+
+DACx578::DACx578 (I2C &i2c, int addr, enum DEVICE device) : _i2c(i2c) {
+    _addr = addr;
+    _device = device;
+    init();
+}
+
+int DACx578::init () {
+    char buf[3];
+
+    buf[0] = 0x70; // reset
+    buf[1] = 0x00;
+    buf[2] = 0x00;
+    if (_i2c.write(_addr, buf, 3)) return -1;
+    wait_ms(10);
+
+    buf[0] = 0x40; // power on
+    buf[1] = 0x1f;
+    buf[2] = 0xe0;
+    if (_i2c.write(_addr, buf, 3)) return -1;
+/*
+    buf[0] = 0x60; // ignore LDAC
+    buf[1] = 0xff;
+    buf[2] = 0x00;
+    if (_i2c.write(_addr, buf, 3)) return -1;
+*/
+    return 0;
+}
+
+int DACx578::write (int channel, int value, int ldac) {
+    char buf[3];
+
+    buf[0] = (ldac ? 0x30 : 0x00) | (channel & 0x0f); // Individual Software LDAC / Input Register
+    buf[1] = (value >> _device) & 0xff;
+    buf[2] = ((8 - value) << _device) & 0xff;
+    return _i2c.write(_addr, buf, 3);
+}
+
+int DACx578::ldacWrite (int channel, int value) {
+    char buf[3];
+
+    buf[0] = 0x20 | (channel & 0x0f); // Global Software LDAC
+    buf[1] = (value >> _device) & 0xff;
+    buf[2] = ((8 - value) << _device) & 0xff;
+    return _i2c.write(_addr, buf, 3);
+}
+
+int DACx578::update (int channel) {
+    char buf[3];
+
+    buf[0] = 0x10 | (channel & 0x0f); // Register Update
+    buf[1] = 0;
+    buf[2] = 0;
+    return _i2c.write(_addr, buf, 3);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DACx578.h	Mon May 14 04:23:31 2018 +0000
@@ -0,0 +1,36 @@
+/*
+ * DAC5578 (8bit)
+ * DAC6578 (10bit)
+ * DAC7578 (12bit)
+ */
+
+#ifndef __DACx578_h__
+#define __DACx578_h__
+
+#include "mbed.h"
+
+#define DACx578_I2C_ADDR 0x90
+
+class DACx578 {
+public:
+    enum DEVICE {
+        DAC5578 = 0,
+        DAC6578 = 2,
+        DAC7578 = 4,
+    };
+
+    DACx578 (PinName sda, PinName scl, int addr, enum DEVICE device = DAC5578);
+    DACx578 (I2C& i2c, int addr, enum DEVICE device = DAC5578);
+
+    int init ();
+    int write (int channel, int value, int ldac = 0);
+    int ldacWrite (int channel, int value);
+    int update (int channel);
+
+protected:
+    I2C _i2c;
+    int _addr;
+    int _device;
+};
+
+#endif