Added the functions to read and set the emissivity of the sensor.

mlx90614.cpp

Committer:
lamell
Date:
2020-05-11
Revision:
5:3c4431224932
Parent:
2:01d333d06727
Child:
6:689f0dab386c

File content as of revision 5:3c4431224932:

 
#include "mlx90614.h"




MLX90614::MLX90614(I2C* i2c,int addr){

    this->i2caddress = addr;
    this->i2c = i2c; 
    
}


bool MLX90614::getTemp(float* temp_val){

    char p1,p2,p3;
    float temp_thermo;
    bool ch;

    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

    if(!ch)return false;                    //No Ack, return False


    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 
}

float MLX90614::GetEmissivity(void) {
    char p1,p2,p3;
    float temp_thermo;
    bool ch;

    i2c_thermo.stop();                            //stop i2c if not ack
    wait(0.01);
    i2c_thermo.start();                           //start I2C                   
    ch=i2c_thermo.write(MLX96014_ADDRESS);              //device address with write condition
    
    if(!ch)return -90.0f;                    //No Ack, return False
    
    ch=i2c_thermo.write(0x24);                    //device ram address where Tobj value is present

    if(!ch)return -91.0f;                    //No Ack, return False


    i2c_thermo.start();                           //repeat start
    ch=i2c_thermo.write(MLX96014_ADDRESS|0x01);         //device address with read condition 
    if(!ch)return -92.0f;                    //No Ack, return False

    p1=i2c_thermo.read(1);     //Tobj low byte
    p2=i2c_thermo.read(1);     //Tobj heigh byte
    p3=i2c_thermo.read(0);     //PEC
    
    i2c_thermo.stop();                            //stop condition
    semaDHT.acquire();
    debug("p2: %02X, p1: %02X\r\n",p2,p1);
    semaDHT.release();

    //temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01;      //degree centigrate conversion
    //*temp_val=temp_thermo-273;                          //Convert kelvin to degree Celsius
    
    return (((p2<<8) + p1)/65535.0f);                            //load data successfully, return true 
}

int MLX90614::BlankEEPROM(int em) {
    uint8_t p1[5];
    float temp_thermo;
    bool ch;

//CRC for this config is 0x3B
    p1[0]=MLX96014_ADDRESS;
    p1[1]=0x24;
    p1[2]=0x00;
    p1[3]=0x00;

    // uint8_t crc = crc8(p1, 4);
    // debug("CRC: 0x%02X\r\n",crc);

    i2c_thermo.stop();                            //stop i2c if not ack
    wait(0.01);
    i2c_thermo.start();                           //start I2C                   
    ch=i2c_thermo.write(p1[0]);              //device address with write condition    
    if(!ch)return -1;                    //No Ack, return False    

    ch=i2c_thermo.write(p1[1]);                    //device ram address where Tobj value is present
    if(!ch)return -2;                    //No Ack, return False
    
    ch = i2c_thermo.write(p1[3]);     //Tobj low byte
    if (!ch) return -3;

    ch = i2c_thermo.write(p1[2]);     //Tobj heigh byte
    if (!ch) return -4;
    


    ch = i2c_thermo.write(em);
    if (!ch) return -5;
    
    i2c_thermo.stop();                            //stop condition
     
    debug("Em: %1.3f\r\n",MLX90617_GetEmissivity());

    return 0;
}

int MLX90614::SetEmissivity(float em, uint8_t lclCrc) {
    uint8_t p1[5];
    float temp_thermo;
    bool ch;
    uint16_t number;
    //uint8_t *num[2];

    temp_thermo = 65535.0f * em;
    number=temp_thermo;
    uint8_t num[2];

    num[0] = number & 0xFF;
    num[1] = number >> 8; 

    //debug("F: %1.3f, INt: %d, H:%02X, L: %02X\r\n",temp_thermo,number,num[1],num[0]);
// //CRC for this config is 0x3B
//     p1[0]=thermometer.i2caddress;
//     p1[1]=0x24;
//     p1[2]=0x19;
//     p1[3]=0x99;

//CRC for this config is 0x05
    p1[0]=MLX96014_ADDRESS;
    p1[1]=0x24;
    p1[2]=num[0];
    p1[3]=num[1];

    // uint8_t crc = crc8(p1, 4);
    // debug("CRC: 0x%02X\r\n",crc);

    i2c_thermo.stop();                            //stop i2c if not ack
    wait(0.01);
    i2c_thermo.start();                           //start I2C                   
    ch=i2c_thermo.write(p1[0]);              //device address with write condition    
    if(!ch)return -1;                    //No Ack, return False    

    ch=i2c_thermo.write(p1[1]);                    //device ram address where Tobj value is present
    if(!ch)return -2;                    //No Ack, return False
    
    ch = i2c_thermo.write(p1[2]);     //Tobj low byte
    if (!ch) return -3;

    ch = i2c_thermo.write(p1[3]);     //Tobj heigh byte
    if (!ch) return -4;
    
    ch = i2c_thermo.write(lclCrc);
    if (!ch) return -5;
    
    i2c_thermo.stop();                            //stop condition
     
    debug("Em: %1.3f\r\n",MLX90617_GetEmissivity());

    return 0;
}