Updated i2c interface to work correctly with mBed and Hexiwear

Dependents:   Hexi_Click_IRThermo_Example

Fork of MLX90614 by Hikaru Sugiura

Files at this revision

API Documentation at this revision

Comitter:
daveyclk
Date:
Thu Oct 27 20:06:15 2016 +0000
Parent:
4:dcd4fe76bd13
Commit message:
Completely re-written i2c interface as original did not work.

Changed in this revision

mlx90614.cpp Show annotated file Show diff for this revision Revisions of this file
mlx90614.h Show annotated file Show diff for this revision Revisions of this file
diff -r dcd4fe76bd13 -r 8f8aedd25609 mlx90614.cpp
--- a/mlx90614.cpp	Thu Jun 09 13:50:26 2011 +0000
+++ b/mlx90614.cpp	Thu Oct 27 20:06:15 2016 +0000
@@ -1,10 +1,16 @@
- 
+//Melexis Infrared Thermometer MLX90614 Library
+
+//*****************************************************************
+//  Build : 27/10/16 Dave Clarke
+//          Only read thermo data.
+//
+//  This program does not check CRC.
+//  If you want to check CRC, please do it your self :)
+//****************************************************************// 
+
 #include "mlx90614.h"
 
-
-
-
-MLX90614::MLX90614(I2C* i2c,int addr){
+MLX90614::MLX90614(I2C* i2c, uint8_t addr){
 
     this->i2caddress = addr;
     this->i2c = i2c; 
@@ -14,35 +20,24 @@
 
 bool MLX90614::getTemp(float* temp_val){
 
-    char p1,p2,p3;
-    float temp_thermo;
-    bool ch;
+    bool ch;                    // Check flag for ack
+    char data[3];               // Raw data storage
+    char ram[1] = {0x07};       // RAM address what temperature is stored
+    char readaddr = 0xB5;       // Read Data address
+    uint16_t  temp_thermo;
+       
+    ch = i2c->write(i2caddress, ram, 1, true); // ping i2c with address and RAM location
+    if (ch) return false;                      // check for ack
+    
+    ch = i2c->read(readaddr, data , 3); // read raw temperature value
+    if (ch) return false;               // check for ack
 
-    i2c->stop();                            //stop i2c if not ack
-    wait(0.01);
-    i2c->start();                           //start I2C                   
-    ch=i2c->write(i2caddress);              //device address with write condition
-    
-    if(!ch)return false;                    //No Ack, return False
-    
-    ch=i2c->write(0x07);                    //device ram address where Tobj value is present
+    temp_thermo |=  data[1] << 8;   // make MSB
+    temp_thermo |=  data[0];        // make LSB
 
-    if(!ch)return false;                    //No Ack, return False
+    *temp_val = ((float)temp_thermo * 0.02) - 273;  //Convert kelvin to degree Celsius
+   
+    return true;                            //load data successfully, return true 
+}
 
 
-    i2c->start();                           //repeat start
-    ch=i2c->write(i2caddress|0x01);         //device address with read condition 
-    if(!ch)return false;                    //No Ack, return False
-
-    p1=i2c->read(1);     //Tobj low byte
-    p2=i2c->read(1);     //Tobj heigh byte
-    p3=i2c->read(0);     //PEC
-    
-    i2c->stop();                            //stop condition
-     
-    
-    temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01;      //degree centigrate conversion
-    *temp_val=temp_thermo-273;                          //Convert kelvin to degree Celsius
-    
-    return true;                            //load data successfully, return true 
-}
\ No newline at end of file
diff -r dcd4fe76bd13 -r 8f8aedd25609 mlx90614.h
--- a/mlx90614.h	Thu Jun 09 13:50:26 2011 +0000
+++ b/mlx90614.h	Thu Oct 27 20:06:15 2016 +0000
@@ -3,11 +3,8 @@
 //Melexis Infrared Thermometer MLX90614 Library
 
 //*****************************************************************
-//  Build : 2011-06-08 Hikaru Sugiura
+//  Build : 27/10/16 Dave Clarke
 //          Only read thermo data.
-//  
-//  This program is based on Mr.Mitesh Patel's "mlx90614".
-//  http://mbed.org/users/mitesh2patel/programs/mlx90614/lqnetj
 //
 //  This program does not check CRC.
 //  If you want to check CRC, please do it your self :)
@@ -43,7 +40,7 @@
         * @param i2c I2C device pointer
         * @param addr Device address(default=0xB4)  
         */    
-        MLX90614(I2C* i2c,int addr=0xB4);
+        MLX90614(I2C* i2c,uint8_t addr=0xB4); 
         
         /** Get Temperature data from MLX90614. 
         *
@@ -56,4 +53,6 @@
        I2C* i2c;
        int i2caddress;
 
+       
+
 };
\ No newline at end of file