AM2321 library

Dependents:   mbed_AM2321_copy mbed_AM2321

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

Files at this revision

API Documentation at this revision

Comitter:
yasuyuki
Date:
Wed Oct 08 02:48:36 2014 +0000
Child:
1:39f20504d5c5
Commit message:
first release

Changed in this revision

AM2321.cpp Show annotated file Show diff for this revision Revisions of this file
AM2321.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AM2321.cpp	Wed Oct 08 02:48:36 2014 +0000
@@ -0,0 +1,61 @@
+//**********************
+// AM2321.cpp for mbed
+//
+// AM2321 am2321(P0_5,P0_4);
+// or
+// I2C i2c(P0_5,P0_4);
+// AM2321 am2321(i2c);
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+
+#include "mbed.h"
+#include "AM2321.h"
+
+AM2321::AM2321 (PinName sda, PinName scl) : _i2c(sda, scl) {
+}
+AM2321::AM2321 (I2C& p_i2c) : _i2c(p_i2c) {
+}
+
+
+void AM2321::get()
+{
+    // step 1:wakeup
+    _i2c.write(AM2321_ADDR, NULL, 0);
+    wait_us(800);   // 800us - 3ms
+
+    // step 2:command
+    buf[0] = 0x03;  // Get Humidity and Temperature
+    buf[1] = 0x00;  // Start address
+    buf[2] = 0x04;  // Length
+    _i2c.write(AM2321_ADDR, buf, 3);
+    wait_us(1500);  // 1.5ms
+
+    // step 3:data
+    _i2c.read( AM2321_ADDR, buf, 8);
+
+}
+
+int AM2321::humidity()
+{
+
+    // get hum
+    get();
+    hum.byte.HB=buf[2];
+    hum.byte.LB=buf[3];
+    return hum.Val;
+    
+}
+
+int AM2321::temperature()
+{
+
+    // get temp
+    get();
+    temp.byte.HB=buf[4];
+    temp.byte.LB=buf[5];
+    return temp.Val;
+    
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AM2321.h	Wed Oct 08 02:48:36 2014 +0000
@@ -0,0 +1,40 @@
+//**********************
+// AM2321.h for mbed
+//
+// (C)Copyright 2014 All rights reserved by Y.Onodera
+// http://einstlab.web.fc2.com
+//**********************
+#ifndef AM2321_H_
+#define AM2321_H_
+
+#define AM2321_ADDR    0xB8
+#define AM2321_HUM_H  0x00
+#define AM2321_HUM_L  0x01
+#define AM2321_TEMP_H  0x02
+#define AM2321_TEMP_L  0x03
+
+#include "mbed.h"
+#include "typedef.h"
+
+class AM2321{
+public:
+    AM2321 (PinName sda, PinName scl);
+    AM2321 (I2C& p_i2c);
+
+    void get();
+    int humidity();
+    int temperature();
+
+protected:
+    
+    I2C _i2c;
+
+    WORD_VAL hum;
+    WORD_VAL temp;
+    char buf[8];
+
+};
+
+#endif /* AM2321_H_ */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/typedef.h	Wed Oct 08 02:48:36 2014 +0000
@@ -0,0 +1,54 @@
+//**********************
+// 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];
+    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 */