Added the functions to read and set the emissivity of the sensor.
Diff: mlx90614.cpp
- Revision:
- 6:689f0dab386c
- Parent:
- 5:3c4431224932
- Child:
- 7:be827071bcb0
--- a/mlx90614.cpp Mon May 11 17:28:37 2020 +0000 +++ b/mlx90614.cpp Mon May 11 18:38:27 2020 -0400 @@ -1,9 +1,6 @@ #include "mlx90614.h" - - - MLX90614::MLX90614(I2C* i2c,int addr){ this->i2caddress = addr; @@ -19,7 +16,7 @@ bool ch; i2c->stop(); //stop i2c if not ack - wait(0.01); + thread_sleep_for(10); i2c->start(); //start I2C ch=i2c->write(i2caddress); //device address with write condition @@ -52,33 +49,30 @@ 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 + + i2c->stop(); //stop i2c if not ack + thread_sleep_for(10); + i2c->start(); //start I2C + ch = i2c->write(i2caddress); //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 + ch = i2c->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 + i2c->start(); //repeat start + ch = i2c->write(i2caddress | 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 + p1 = i2c->read(1); //Tobj low byte + p2 = i2c->read(1); //Tobj heigh byte + p3 = i2c->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 + i2c->stop(); //stop condition + + debug_if(debug_mlx90614==true,"p2: %02X, p1: %02X\r\n",p2,p1); return (((p2<<8) + p1)/65535.0f); //load data successfully, return true } @@ -88,93 +82,99 @@ float temp_thermo; bool ch; -//CRC for this config is 0x3B - p1[0]=MLX96014_ADDRESS; + p1[0]=i2caddress; 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 + i2c->stop(); //stop i2c if not ack + thread_sleep_for(10); + i2c->start(); //start I2C + ch = i2c->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 + ch = i2c->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 + ch = i2c->write(p1[3]); //Tobj low byte if (!ch) return -3; - ch = i2c_thermo.write(p1[2]); //Tobj heigh byte + ch = i2c->write(p1[2]); //Tobj heigh byte if (!ch) return -4; - ch = i2c_thermo.write(em); + ch = i2c->write(em); if (!ch) return -5; - i2c_thermo.stop(); //stop condition + i2c->stop(); //stop condition - debug("Em: %1.3f\r\n",MLX90617_GetEmissivity()); + debug_if(debug_mlx90614==true,"Em: %1.3f\r\n",GetEmissivity()); return 0; } -int MLX90614::SetEmissivity(float em, uint8_t lclCrc) { +int MLX90614::InternalSet(uint8_t *p, int idx) { + uint8_t ch; + + i2c->stop(); //stop i2c if not ack + thread_sleep_for(10); + i2c->start(); //start I2C + ch = i2c->write(p[0]); //device address with write condition + if(!ch)return -1; //No Ack, return False + + ch = i2c->write(p[1]); //device ram address where Tobj value is present + if(!ch)return -2; //No Ack, return False + + ch = i2c->write(p[2]); //Tobj low byte + if (!ch) return -3; + + ch = i2c->write(p[3]); //Tobj heigh byte + if (!ch) return -4; + + ch = i2c->write(idx); + if (!ch) return -5; + + i2c->stop(); //stop condition + + return 0; +} + +int MLX90614::SetEmissivity(float *em) { uint8_t p1[5]; - float temp_thermo; + //float temp_thermo; bool ch; - uint16_t number; - //uint8_t *num[2]; - - temp_thermo = 65535.0f * em; - number=temp_thermo; + int result; uint8_t num[2]; - num[0] = number & 0xFF; - num[1] = number >> 8; + uint16_t res; + + *em = 65535.0f * (*em); - //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; + num[0] = (int)(*em) & 0xff; + num[1] = (int)(*em) >> 8; -//CRC for this config is 0x05 - p1[0]=MLX96014_ADDRESS; + p1[0]=i2caddress; p1[1]=0x24; p1[2]=num[0]; p1[3]=num[1]; - // uint8_t crc = crc8(p1, 4); - // debug("CRC: 0x%02X\r\n",crc); + for (res=0;res<0xff;res++) { + result = BlankEEPROM(res); + if (result != -5) break; + } + debug_if(debug_mlx90614==true,"Blank EEPROM: %d, ",result); - 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 + for (res=0;res<0xff;res++) { + + result = InternalSet(p1, res); - ch=i2c_thermo.write(p1[1]); //device ram address where Tobj value is present - if(!ch)return -2; //No Ack, return False + if ((res > 0xff) || (result == 0) ){ + break; + } + } - 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()); + debug_if(debug_mlx90614==true,"Emissivity Set to: %1.3f\r\n",GetEmissivity()); return 0; }