MCP3425 library
See http://developer.mbed.org/users/yasuyuki/notebook/MCP3425/
Revision 0:b7bc51be525f, committed 2014-10-15
- Comitter:
- yasuyuki
- Date:
- Wed Oct 15 14:37:59 2014 +0000
- Commit message:
- first release
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3425.cpp Wed Oct 15 14:37:59 2014 +0000
@@ -0,0 +1,50 @@
+//**********************
+// MCP3425.cpp for mbed
+//
+// MCP3425 mcp3425(P0_5,P0_4);
+// or
+// I2C i2c(P0_5,P0_4);
+// MCP3425 mcp3425(i2c);
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+
+#include "mbed.h"
+#include "MCP3425.h"
+
+MCP3425::MCP3425 (PinName sda, PinName scl) : _i2c(sda, scl) {
+ init();
+}
+MCP3425::MCP3425 (I2C& p_i2c) : _i2c(p_i2c) {
+ init();
+}
+
+
+short MCP3425::get()
+{
+
+ _i2c.read( MCP3425_ADDR, buf, 3);
+
+ ad.byte.HB=buf[0];
+ ad.byte.LB=buf[1];
+ config.UC=buf[2];
+ return ad.S;
+
+}
+
+
+void MCP3425::init()
+{
+
+ config.bit.RDY=1;
+ config.bit.C=0;
+ config.bit.OC=1;
+ config.bit.S=2;
+ config.bit.G=0;
+ // Initiate Continuous 16bits, 15SPS
+ buf[0]=config.UC;
+ _i2c.write(MCP3425_ADDR, buf, 1);
+
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3425.h Wed Oct 15 14:37:59 2014 +0000
@@ -0,0 +1,52 @@
+//**********************
+// MCP3425.h for mbed
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+
+#ifndef MCP3425_H_
+#define MCP3425_H_
+
+#define MCP3425_ADDR 0xD0
+
+#include "mbed.h"
+#include "typedef.h"
+
+
+typedef union
+{
+ unsigned char UC;
+ struct
+ {
+ unsigned char G:2; // 00=1, 01=2, 10=4, 11=8 Gain
+ unsigned char S:2; // 00=12bits, 01=14bits, 10=16bits
+ unsigned char OC:1; // 0=One-shot, 1=Continuous
+ unsigned char C:2; // NA
+ unsigned char RDY:1; // wrinting 1=Initiate, reading 0=Ready
+ } bit;
+} CONFIG;
+
+
+
+class MCP3425{
+public:
+ MCP3425 (PinName sda, PinName scl);
+ MCP3425 (I2C& p_i2c);
+ void init();
+ short get();
+
+protected:
+
+ I2C _i2c;
+
+ CONFIG config;
+ WORD_VAL ad;
+ char buf[3];
+
+};
+
+#endif /* MCP3425_H_ */
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/typedef.h Wed Oct 15 14:37:59 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 */