AM2321 Temperature and Humidity Sensor mbed library

Dependents:   AM2321_Example mbed_vfd_thermometer

/media/uploads/tomozh/am2321_1.png/media/uploads/tomozh/p5061476.jpg

Import programAM2321_Example

AM2321 Temperature and Humidity Sensor mbed library Example

Import library

Public Member Functions

AM2321 (PinName sda, PinName scl)
Constructor.
bool poll ()
Read current temperature and humidity from AM2321 .
float getTemperature (void) const
Get last read temperature value.
float getHumidity (void) const
Get last read humidity value.

Files at this revision

API Documentation at this revision

Comitter:
tomozh
Date:
Tue May 06 10:21:11 2014 +0000
Commit message:
1st 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AM2321.cpp	Tue May 06 10:21:11 2014 +0000
@@ -0,0 +1,116 @@
+/*
+ *  AM2321 (Aosong Guangzhou Electronics)
+ *  Temperature and Humidity Sensor mbed library
+ * 
+ *  Copyright (c) 2014 tomozh <tomozh@gmail.com>
+ *  
+ *  This software is released under the MIT License.
+ *  http://opensource.org/licenses/mit-license.php
+ *
+ *  Last update : 2014/05/06
+ */
+
+#include "mbed.h"
+#include "AM2321.h"
+
+const int AM2321_I2C_ADDR = 0xB8;
+
+AM2321::AM2321(PinName sda, PinName scl)
+: _i2c(sda, scl)
+{
+    _result.temperature = 0.0f;
+    _result.humidity    = 0.0f;        
+}
+
+float AM2321::getLogicalValue(uint16_t regVal) const
+{
+    if(regVal & 0x8000)
+    {
+        regVal &= ~0x8000;
+        return (float)regVal / -10.0;
+    }
+    else
+    {
+        return (float)regVal / 10.0;    
+    }
+}
+
+uint16_t AM2321::calcCRC16(const uint8_t* src, int len) const
+{
+    uint16_t crc = 0xFFFF;
+
+    while(len--)
+    {
+        crc ^= *(src++);
+        
+        for(uint8_t i = 0; i < 8; i++)
+        {
+            if(crc & 0x01)
+            {
+                crc >>= 1;
+                crc ^= 0xA001;
+            }
+            else
+            {
+                crc >>= 1;
+            }
+        }
+    }
+    
+    return crc; 
+}
+
+bool AM2321::poll(void)
+{
+    bool    success = false;
+    uint8_t data[8];
+    
+    const static uint8_t READ_REGISTER_CMD[] = {
+           0x03     // Read register command
+         , 0x00     // start addrress
+         , 0x04     // read length
+          };
+
+    // wakeup
+    _i2c.write(AM2321_I2C_ADDR, NULL, 0);
+
+    // read data
+    _i2c.write(AM2321_I2C_ADDR, (char*)READ_REGISTER_CMD, 3);
+    wait_us(1600);
+
+    if(_i2c.read(AM2321_I2C_ADDR, (char*)data, 8) == 0)
+    {
+        uint8_t  cmd     = data[0];
+        uint8_t  dataLen = data[1];
+        uint16_t humiVal = (data[2] * 256) + data[3];
+        uint16_t tempVal = (data[4] * 256) + data[5];
+        uint16_t recvCRC = data[6] + (data[7] * 256);
+        uint16_t chkCRC  = calcCRC16(&data[0], 6);
+
+        if(dataLen == 4)
+        {
+            if(recvCRC == chkCRC)
+            {
+                if(cmd == 0x03)
+                {
+                    _result.temperature = getLogicalValue(tempVal);
+                    _result.humidity    = getLogicalValue(humiVal);
+
+                    success = true;
+                }
+            }
+        }
+    }
+    
+    return success;
+}
+
+float AM2321::getTemperature(void) const
+{
+    return _result.temperature;
+}
+
+float AM2321::getHumidity(void) const
+{
+    return _result.humidity;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AM2321.h	Tue May 06 10:21:11 2014 +0000
@@ -0,0 +1,79 @@
+/**
+ *  AM2321 (Aosong Guangzhou Electronics)
+ *  Temperature and Humidity Sensor mbed library
+ *  Last update : 2014/05/06
+ */
+
+#ifndef __AM2321_H__
+#define __AM2321_H__
+
+/** AM2321 (Aosong Guangzhou Electronics)
+ * Temperature and Humidity Sensor mbed library
+ * 
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "AM2321.h"
+ *
+ * Serial pc(USBTX, USBRX);    // Tx, Rx
+ * AM2321 am2321(p28, p27);    // SDA, SCL
+ *
+ * int main()
+ * {
+ *     while(1)
+ *     {
+ *        if(am2321.poll())
+ *         {
+ *             pc.printf(":%05u,%.1f,%.1f\n"
+ *                 , count++
+ *                 , am2321.getTemperature()
+ *                 , am2321.getHumidity()
+ *             );
+ *         }
+ * 
+ *         wait(0.5);
+ *     }
+ * }
+ * @endcode
+ */
+class AM2321
+{
+private:
+    typedef struct tagRESULT
+    {
+        float temperature;
+        float humidity;
+    }RESULT;
+
+public:
+    /** Constructor
+     * @param   sda  [in]    I2C Pin name (SDA)
+     * @param   scl  [in]    I2C Pin name (SCL)
+     */
+    AM2321(PinName sda, PinName scl);
+    
+    /** Read current temperature and humidity from AM2321
+     * @return  result (true=success)
+     */
+    bool poll();
+
+    /** Get last read temperature value
+     * @return  temperature value (degress)
+     */
+    float getTemperature(void) const;
+    
+    /** Get last read humidity value
+     * @return  humidity value (%RH)
+     */ 
+    float getHumidity(void) const;
+
+private:
+    float getLogicalValue(uint16_t regVal) const;
+    uint16_t calcCRC16(const uint8_t* src, int len) const;
+
+    I2C    _i2c;
+    RESULT _result;
+};
+
+
+#endif // __AM2321_H__