Updated i2c interface to work correctly with mBed and Hexiwear

Dependents:   Hexi_Click_IRThermo_Example

Fork of MLX90614 by Hikaru Sugiura

Committer:
daveyclk
Date:
Thu Oct 27 20:06:15 2016 +0000
Revision:
5:8f8aedd25609
Parent:
2:01d333d06727
Completely re-written i2c interface as original did not work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daveyclk 5:8f8aedd25609 1 //Melexis Infrared Thermometer MLX90614 Library
daveyclk 5:8f8aedd25609 2
daveyclk 5:8f8aedd25609 3 //*****************************************************************
daveyclk 5:8f8aedd25609 4 // Build : 27/10/16 Dave Clarke
daveyclk 5:8f8aedd25609 5 // Only read thermo data.
daveyclk 5:8f8aedd25609 6 //
daveyclk 5:8f8aedd25609 7 // This program does not check CRC.
daveyclk 5:8f8aedd25609 8 // If you want to check CRC, please do it your self :)
daveyclk 5:8f8aedd25609 9 //****************************************************************//
daveyclk 5:8f8aedd25609 10
aquahika 1:4a60d3f1e91e 11 #include "mlx90614.h"
aquahika 1:4a60d3f1e91e 12
daveyclk 5:8f8aedd25609 13 MLX90614::MLX90614(I2C* i2c, uint8_t addr){
aquahika 1:4a60d3f1e91e 14
aquahika 1:4a60d3f1e91e 15 this->i2caddress = addr;
aquahika 1:4a60d3f1e91e 16 this->i2c = i2c;
aquahika 1:4a60d3f1e91e 17
aquahika 1:4a60d3f1e91e 18 }
aquahika 1:4a60d3f1e91e 19
aquahika 1:4a60d3f1e91e 20
aquahika 1:4a60d3f1e91e 21 bool MLX90614::getTemp(float* temp_val){
aquahika 1:4a60d3f1e91e 22
daveyclk 5:8f8aedd25609 23 bool ch; // Check flag for ack
daveyclk 5:8f8aedd25609 24 char data[3]; // Raw data storage
daveyclk 5:8f8aedd25609 25 char ram[1] = {0x07}; // RAM address what temperature is stored
daveyclk 5:8f8aedd25609 26 char readaddr = 0xB5; // Read Data address
daveyclk 5:8f8aedd25609 27 uint16_t temp_thermo;
daveyclk 5:8f8aedd25609 28
daveyclk 5:8f8aedd25609 29 ch = i2c->write(i2caddress, ram, 1, true); // ping i2c with address and RAM location
daveyclk 5:8f8aedd25609 30 if (ch) return false; // check for ack
daveyclk 5:8f8aedd25609 31
daveyclk 5:8f8aedd25609 32 ch = i2c->read(readaddr, data , 3); // read raw temperature value
daveyclk 5:8f8aedd25609 33 if (ch) return false; // check for ack
aquahika 1:4a60d3f1e91e 34
daveyclk 5:8f8aedd25609 35 temp_thermo |= data[1] << 8; // make MSB
daveyclk 5:8f8aedd25609 36 temp_thermo |= data[0]; // make LSB
aquahika 1:4a60d3f1e91e 37
daveyclk 5:8f8aedd25609 38 *temp_val = ((float)temp_thermo * 0.02) - 273; //Convert kelvin to degree Celsius
daveyclk 5:8f8aedd25609 39
daveyclk 5:8f8aedd25609 40 return true; //load data successfully, return true
daveyclk 5:8f8aedd25609 41 }
aquahika 1:4a60d3f1e91e 42
aquahika 1:4a60d3f1e91e 43