Dependencies:   BLE_API mbed nRF51822

Revision:
1:bca7ac3aedac
Parent:
0:d843c5528596
Child:
2:fd94a7e87ac5
--- a/main.cpp	Fri Dec 09 15:06:45 2016 +0000
+++ b/main.cpp	Fri Dec 09 17:04:02 2016 +0100
@@ -17,99 +17,126 @@
 
 */
 
+#include <string.h>
 #include "mbed.h"
 #include "wire.h"
 
-#define BLE_Nano
-//#define nRF_51822
-
-
-#ifdef nRF_51822
-#define SCL         28
-#define SDA         29
-#endif
-
-#ifdef BLE_Nano
 #define SCL         7  
 #define SDA         6
-#endif
 
-#define DEV_ADDR    0xA0
+#define DEV_ADDR    0x80
 
-Serial pc(USBTX, USBRX);
 TwoWire Wire = TwoWire(NRF_TWI0);
 
-void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
-{
-    Wire.beginTransmission(DEV_ADDR);
-    Wire.write( (uint8_t)addr>>8 );
-    Wire.write( (uint8_t)addr );
-    Wire.write(pbuf, length);
-    Wire.endTransmission();
-}
+Serial pc(USBTX, USBRX);
 
-void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
-{
-    Wire.beginTransmission(DEV_ADDR);
-    Wire.write( (uint8_t)addr>>8 );
-    Wire.write( (uint8_t)addr );    
-    Wire.endTransmission();
-       
-    Wire.requestFrom(DEV_ADDR+1, length);
-    while( Wire.available() > 0 )
-    {
-        *pbuf = Wire.read();
-        pbuf++;
-    }
-}
-
-
-static uint8_t wt_data[10] = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};
-static uint8_t rd_data[10];
-
-static uint16_t index;
+uint16_t ReadHumidity(void);
+int16_t ReadTemperature(void);
 
 int main(void)
 {
+    uint16_t rh;
+    int16_t t;
+    char buf[200];
+    
     pc.baud(9600);
     wait(5);
-    //Wire.begin();
+    
     Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
-    pc.printf("IIC Demo Start \r\n");
-    
-    AT24C512_WriteBytes(0, wt_data, 10);
-    wait(0.1);
-    
+
     while(1)
     {
-        AT24C512_ReadBytes(0, rd_data, 10);     
-        pc.printf("Read data from AT24C512 \r\n");
-        for(index=0; index<10; index++)
-        {
-            pc.putc(rd_data[index]);
-            rd_data[index] = 0x00;
-        }
-        pc.printf("\r\n");
-        wait(1);
+        rh = ReadHumidity();
+        t = ReadTemperature();
+        
+        sprintf(buf,"RH=%d, T=%d\r\n\0",rh,t);
+        
+        pc.printf(buf);
+        wait(5);
     }
 }
 
 
 
 
-
-
+uint16_t ReadHumidity(void)
+{
+    uint16_t msbyte=0;
+    uint16_t lsbyte=0;
+    uint16_t checksum=0;
+    uint16_t rhcode=0;
+        
+    Wire.beginTransmission(DEV_ADDR);
+    // Here should be standing 0xE5 or 0xF5
+    Wire.write(0xE3);
+//    wait(1);
+    Wire.beginTransmission(DEV_ADDR);
+    Wire.write( 0x00 );
+    Wire.write( 0x00 );
+    Wire.write( 0x00 );
 
-
+    Wire.requestFrom(DEV_ADDR+1, 3);
+    
+    if(Wire.available())
+    {
+        msbyte = Wire.read();
+    }
+    if(Wire.available())
+    {
+        lsbyte = Wire.read();
+    }
+    if(Wire.available())
+    {
+        checksum = Wire.read();
+    }
+    
+    Wire.endTransmission();
+    
+    
+    rhcode = (((msbyte*256.0)+lsbyte)*125.0/65536.0-6.0)*100.0;
+    
+    if(rhcode<=0)
+    {
+        rhcode=0;
+    }
+    if(rhcode>=100*100)
+    {
+        rhcode=100*100;
+    }
+    return rhcode;
+}
+int16_t ReadTemperature(void)
+{
+    int16_t msbyte=0;
+    int16_t lsbyte=0;
+    int16_t checksum=0;
+    
+    Wire.beginTransmission(DEV_ADDR);
+    // Here should be standing 0xE3 or 0xF3
+    Wire.write(0xE5);
+//    wait(1);
+    Wire.beginTransmission(DEV_ADDR);
+    Wire.write( 0x00 );
+    Wire.write( 0x00 );
+    Wire.write( 0x00 );
 
-
+    Wire.requestFrom(DEV_ADDR+1, 3);
+    
+    if(Wire.available())
+    {
+        msbyte = Wire.read();
+    }
+    if(Wire.available())
+    {
+        lsbyte = Wire.read();
+    }
+    if(Wire.available())
+    {
+        checksum = Wire.read();
+    }
+    
+    Wire.endTransmission();
+    return (((msbyte*256.0)+lsbyte)*175.72/65536.0-46.85)*100.0;
+}
 
 
-
-
-
-
-
-
-
-