MLX90614 library. Working on both Mbed2 and Mbed5

Dependents:   MLX90614_Demo

Fork of MLX90614 by Hikaru Sugiura

Revision:
5:933b7db866c5
Parent:
2:01d333d06727
--- a/mlx90614.cpp	Thu Jun 09 13:50:26 2011 +0000
+++ b/mlx90614.cpp	Fri May 04 07:29:44 2018 +0000
@@ -14,34 +14,25 @@
 
 bool MLX90614::getTemp(float* temp_val){
 
-    char p1,p2,p3;
+    char data[3];
     float temp_thermo;
-    bool ch;
 
-    i2c->stop();                            //stop i2c if not ack
+    i2c->stop();
+    wait(0.02);
+    i2c->start();
     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
-
-    if(!ch)return false;                    //No Ack, return False
+    data[0] = 0x00;
+    i2c->write((i2caddress<<1), data, 1, true);
+    wait(0.01);
+    data[0] = 0x07;
+    i2c->write((i2caddress<<1), data, 1, true);
+    wait(0.01);
+    i2c->read(((i2caddress<<1)|0x01), data, 3, true);
+    wait(0.01);
+    i2c->stop();                            //stop condition
 
 
-    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_thermo=((((data[1]&0x007f)<<8)+data[0])*0.02)-0.01;      //degree centigrate conversion
     *temp_val=temp_thermo-273;                          //Convert kelvin to degree Celsius
     
     return true;                            //load data successfully, return true