work.

Dependencies:   Blynk mbed

Revision:
2:6cd3b0947188
Parent:
0:d8f4c441e032
Child:
3:4cd9171ba989
--- a/AM2321.cpp	Mon Jun 13 02:21:11 2016 +0000
+++ b/AM2321.cpp	Wed Jun 15 03:08:40 2016 +0000
@@ -1,4 +1,4 @@
-// 
+//
 // AM2321 Temperature & Humidity Sensor library for Arduino
 //
 // The MIT License (MIT)
@@ -25,8 +25,13 @@
 //
 
 #include "AM2321.h"
+#include "I2Cdev.h"
 //#include <Wire.h>
-#if 0
+#include "mbed.h"
+extern I2C g_i2c;
+//I2C gI2C(P0_11, P0_10);
+extern Serial pc;
+
 #define I2C_ADDR_AM2321                 (0xB8 >> 1)          //AM2321温湿度计I2C地址
 #define PARAM_AM2321_READ                0x03                //读寄存器命令
 #define REG_AM2321_HUMIDITY_MSB          0x00                //湿度寄存器高位
@@ -36,7 +41,8 @@
 #define REG_AM2321_DEVICE_ID_BIT_24_31   0x0B                //32位设备ID高8位
 
 template<int I2CADDR, int COMMAND, int REGADDR, int REGCOUNT>
-class DataReader {
+class DataReader
+{
 protected:
     enum { len = 32 };
     uint8_t buf[len];
@@ -44,31 +50,39 @@
 protected:
     DataReader() {
         memset(buf, 0, len);
-    } 
+    }
     bool readRaw() {
         //
         // Wakeup
         //
-        Wire.beginTransmission(I2CADDR);
-        Wire.endTransmission();
+        //Wire.beginTransmission(I2CADDR);
+        //Wire.endTransmission();
+        g_i2c.write(I2CADDR<<1, (char*)&buf[0], 1);
 
         //
         // Read Command
         //
+#if 0
         Wire.beginTransmission(I2CADDR);
         Wire.write(COMMAND);
         Wire.write(REGADDR);
         Wire.write(REGCOUNT);
         Wire.endTransmission();
+#else
+        char bu[3] = {COMMAND, REGADDR, REGCOUNT};
+        g_i2c.write(I2CADDR<<1, bu, 3);
+#endif
 
         //
         // Waiting
         //
-        delayMicroseconds(1600); //>1.5ms
+        //delayMicroseconds(1600); //>1.5ms
+        wait_ms(1.6);
 
         //
         // Read
         //
+#if 0
         Wire.requestFrom(I2CADDR, 2 + REGCOUNT + 2); // COMMAND + REGCOUNT + DATA + CRCLSB + CRCMSB
         int i = 0;
         for (; i < 2 + REGCOUNT; ++i)
@@ -81,25 +95,41 @@
         if (crc == crc16(buf, i))
             return true;
         return false;
+#else
+        uint8_t realAddr = (I2CADDR << 1) | 0x01;
+        //pc.printf("realAddr = 0x%x\r\n", realAddr);
+        g_i2c.read(realAddr, (char*)buf, 2 + REGCOUNT + 2);
+        unsigned short crc = 0;
+        crc  = buf[2+REGCOUNT];       //CRC LSB
+        crc |= buf[2+REGCOUNT+1] << 8;//CRC MSB
+#if 0
+        for (int i = 0; i < 2 + REGCOUNT + 2; i++) {
+            pc.printf("0x%X ", buf[i]);
+        }
+        pc.printf("\r\n");
+#endif
+        if (crc == crc16(buf, 2 + REGCOUNT))
+            return true;
+        return false;
+#endif
     }
 
 private:
     unsigned short crc16(unsigned char *ptr, unsigned char len) {
-        unsigned short crc = 0xFFFF; 
+        unsigned short crc = 0xFFFF;
         unsigned char  i   = 0;
         while(len--) {
-            crc ^= *ptr++; 
+            crc ^= *ptr++;
             for(i = 0 ; i < 8 ; i++) {
                 if(crc & 0x01) {
                     crc >>= 1;
-                    crc  ^= 0xA001; 
+                    crc  ^= 0xA001;
+                } else {
+                    crc >>= 1;
                 }
-                else {
-                    crc >>= 1;
-                } 
             }
         }
-        return crc; 
+        return crc;
     }
 };
 
@@ -109,7 +139,7 @@
     unsigned int uid;
 public:
     bool read() {
-        if(!readRaw()) 
+        if(!readRaw())
             return false;
         uid  = buf[2] << 24;
         uid += buf[3] << 16;
@@ -126,26 +156,28 @@
     int temperature;
 public:
     bool read() {
-        if(!readRaw()) 
+        if(!readRaw())
             return false;
         humidity     = buf[2] << 8;
         humidity    += buf[3];
         temperature  = (buf[4]&0x7F) << 8;
         temperature += buf[5];
-		if((buf[4]&0x80) == 0x80)
-			temperature = -temperature;
+        if((buf[4]&0x80) == 0x80)
+            temperature = -temperature;
         return true;
     }
 };
 
 
-AM2321::AM2321() {
-    Wire.begin();
+AM2321::AM2321()
+{
+    //Wire.begin();
     temperature = 0;
     humidity    = 0;
 }
 
-uint32_t AM2321::uid() {
+unsigned long AM2321::uid()
+{
     UidReader reader;
     if (reader.read())
         return reader.uid;
@@ -153,11 +185,13 @@
 }
 
 
-bool AM2321::available() {
+bool AM2321::available()
+{
     return !(temperature == 0 && humidity == 0);
 }
 
-bool AM2321::read() {
+bool AM2321::read()
+{
     AirConditionReader reader;
     if (reader.read()) {
         temperature = reader.temperature;
@@ -169,5 +203,3 @@
 //
 // END OF FILE
 //
-
-#endif
\ No newline at end of file