MCP4726 library

Dependents:   mbed_MCP4726

See http://developer.mbed.org/users/yasuyuki/notebook/MCP4726/

Files at this revision

API Documentation at this revision

Comitter:
yasuyuki
Date:
Wed Oct 15 14:42:50 2014 +0000
Commit message:
first release

Changed in this revision

MCP4726.cpp Show annotated file Show diff for this revision Revisions of this file
MCP4726.h Show annotated file Show diff for this revision Revisions of this file
typedef.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f0980556dfc0 MCP4726.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4726.cpp	Wed Oct 15 14:42:50 2014 +0000
@@ -0,0 +1,62 @@
+//**********************
+// MCP4726.cpp for mbed
+//
+// MCP4726 mcp4726(P0_5,P0_4);
+// or
+// I2C i2c(P0_5,P0_4);
+// MCP4726 mcp4726(i2c);
+//
+// Max Frequency with I2C mode
+// Standard mode   3.4KHz=100KHz / 29bits
+// Fast mode      13.8KHz=400KHz / 29bits
+// Fast mode Plus 34.5KHz=1MHz / 29bits
+// HS mode        117KHz =3.4MHz / 29bits
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+
+#include "mbed.h"
+#include "MCP4726.h"
+
+MCP4726::MCP4726 (PinName sda, PinName scl) : _i2c(sda, scl) {
+    init();
+}
+MCP4726::MCP4726 (I2C& p_i2c) : _i2c(p_i2c) {
+    init();
+}
+
+
+void MCP4726::put(unsigned short a)
+{
+
+    da.Val = a;
+    buf[0]=0x0F & da.byte.HB;
+    buf[1]=da.byte.LB;
+    _i2c.write(MCP4726_ADDR, buf, 2);
+ 
+}
+
+
+void MCP4726::init()
+{
+
+    // Set fast mode
+    _i2c.frequency(400000);  // default 100Kbps
+
+    // wait POR 60us+
+    wait_us(100);
+
+    // reset
+//    buf[0]=0x06;
+//    _i2c.write(0x00, buf, 1);
+    // wakeup
+//    buf[0]=0x09;
+//    _i2c.write(0x00, buf, 1);
+        
+    // Set Configuration
+    buf[0]=0x80;
+    _i2c.write(MCP4726_ADDR, buf, 1);
+
+}
+
diff -r 000000000000 -r f0980556dfc0 MCP4726.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP4726.h	Wed Oct 15 14:42:50 2014 +0000
@@ -0,0 +1,36 @@
+//**********************
+// MCP4726.h for mbed
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+
+#ifndef MCP4726_H_
+#define MCP4726_H_
+
+#define MCP4726_ADDR                0xC0
+
+#include "mbed.h"
+#include "typedef.h"
+
+
+class MCP4726{
+public:
+    MCP4726 (PinName sda, PinName scl);
+    MCP4726 (I2C& p_i2c);
+    void init();
+    void put(unsigned short a);
+
+protected:
+    
+    I2C _i2c;
+
+    WORD_VAL da;
+    char buf[3];
+
+};
+
+#endif /* MCP4726_H_ */
+
+
+
diff -r 000000000000 -r f0980556dfc0 typedef.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/typedef.h	Wed Oct 15 14:42:50 2014 +0000
@@ -0,0 +1,55 @@
+//**********************
+// typedef for mbed
+//
+// Condition:
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+#ifndef TYPEDEF_H
+#define TYPEDEF_H
+
+typedef unsigned char       BYTE;                           /* 8-bit unsigned  */
+typedef unsigned short int  WORD;                           /* 16-bit unsigned */
+typedef unsigned int        DWORD;                          /* 32-bit unsigned */
+typedef unsigned long long  QWORD;                          /* 64-bit unsigned */
+
+
+typedef union
+{
+    WORD Val;
+    BYTE v[2];
+    short S;
+    struct
+    {
+        BYTE LB;
+        BYTE HB;
+    } byte;
+} WORD_VAL;
+
+typedef union
+{
+    DWORD Val;
+    WORD w[2];
+    BYTE v[4];
+    struct
+    {
+        WORD LW;
+        WORD HW;
+    } word;
+    struct
+    {
+        BYTE LB;
+        BYTE HB;
+        BYTE UB;
+        BYTE MB;
+    } byte;
+    struct
+    {
+        WORD_VAL low;
+        WORD_VAL high;
+    }wordUnion;
+
+} DWORD_VAL;
+
+#endif /* TYPEDEF_H */