Added the functions to read and set the emissivity of the sensor.
mlx90614.cpp@5:3c4431224932, 2020-05-11 (annotated)
- Committer:
- lamell
- Date:
- Mon May 11 17:28:37 2020 +0000
- Revision:
- 5:3c4431224932
- Parent:
- 2:01d333d06727
- Child:
- 6:689f0dab386c
Added the functions to Read the Emissivity and Set Emissivity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aquahika | 1:4a60d3f1e91e | 1 | |
aquahika | 1:4a60d3f1e91e | 2 | #include "mlx90614.h" |
aquahika | 1:4a60d3f1e91e | 3 | |
aquahika | 2:01d333d06727 | 4 | |
aquahika | 2:01d333d06727 | 5 | |
aquahika | 2:01d333d06727 | 6 | |
aquahika | 1:4a60d3f1e91e | 7 | MLX90614::MLX90614(I2C* i2c,int addr){ |
aquahika | 1:4a60d3f1e91e | 8 | |
aquahika | 1:4a60d3f1e91e | 9 | this->i2caddress = addr; |
aquahika | 1:4a60d3f1e91e | 10 | this->i2c = i2c; |
aquahika | 1:4a60d3f1e91e | 11 | |
aquahika | 1:4a60d3f1e91e | 12 | } |
aquahika | 1:4a60d3f1e91e | 13 | |
aquahika | 1:4a60d3f1e91e | 14 | |
aquahika | 1:4a60d3f1e91e | 15 | bool MLX90614::getTemp(float* temp_val){ |
aquahika | 1:4a60d3f1e91e | 16 | |
aquahika | 1:4a60d3f1e91e | 17 | char p1,p2,p3; |
aquahika | 1:4a60d3f1e91e | 18 | float temp_thermo; |
aquahika | 1:4a60d3f1e91e | 19 | bool ch; |
aquahika | 1:4a60d3f1e91e | 20 | |
aquahika | 1:4a60d3f1e91e | 21 | i2c->stop(); //stop i2c if not ack |
aquahika | 1:4a60d3f1e91e | 22 | wait(0.01); |
aquahika | 1:4a60d3f1e91e | 23 | i2c->start(); //start I2C |
aquahika | 1:4a60d3f1e91e | 24 | ch=i2c->write(i2caddress); //device address with write condition |
aquahika | 1:4a60d3f1e91e | 25 | |
aquahika | 1:4a60d3f1e91e | 26 | if(!ch)return false; //No Ack, return False |
aquahika | 1:4a60d3f1e91e | 27 | |
aquahika | 1:4a60d3f1e91e | 28 | ch=i2c->write(0x07); //device ram address where Tobj value is present |
aquahika | 1:4a60d3f1e91e | 29 | |
aquahika | 1:4a60d3f1e91e | 30 | if(!ch)return false; //No Ack, return False |
aquahika | 1:4a60d3f1e91e | 31 | |
aquahika | 1:4a60d3f1e91e | 32 | |
aquahika | 1:4a60d3f1e91e | 33 | i2c->start(); //repeat start |
aquahika | 1:4a60d3f1e91e | 34 | ch=i2c->write(i2caddress|0x01); //device address with read condition |
aquahika | 1:4a60d3f1e91e | 35 | if(!ch)return false; //No Ack, return False |
aquahika | 1:4a60d3f1e91e | 36 | |
aquahika | 1:4a60d3f1e91e | 37 | p1=i2c->read(1); //Tobj low byte |
aquahika | 1:4a60d3f1e91e | 38 | p2=i2c->read(1); //Tobj heigh byte |
aquahika | 1:4a60d3f1e91e | 39 | p3=i2c->read(0); //PEC |
aquahika | 1:4a60d3f1e91e | 40 | |
aquahika | 1:4a60d3f1e91e | 41 | i2c->stop(); //stop condition |
aquahika | 1:4a60d3f1e91e | 42 | |
aquahika | 1:4a60d3f1e91e | 43 | |
aquahika | 1:4a60d3f1e91e | 44 | temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01; //degree centigrate conversion |
aquahika | 1:4a60d3f1e91e | 45 | *temp_val=temp_thermo-273; //Convert kelvin to degree Celsius |
aquahika | 1:4a60d3f1e91e | 46 | |
aquahika | 1:4a60d3f1e91e | 47 | return true; //load data successfully, return true |
lamell | 5:3c4431224932 | 48 | } |
lamell | 5:3c4431224932 | 49 | |
lamell | 5:3c4431224932 | 50 | float MLX90614::GetEmissivity(void) { |
lamell | 5:3c4431224932 | 51 | char p1,p2,p3; |
lamell | 5:3c4431224932 | 52 | float temp_thermo; |
lamell | 5:3c4431224932 | 53 | bool ch; |
lamell | 5:3c4431224932 | 54 | |
lamell | 5:3c4431224932 | 55 | i2c_thermo.stop(); //stop i2c if not ack |
lamell | 5:3c4431224932 | 56 | wait(0.01); |
lamell | 5:3c4431224932 | 57 | i2c_thermo.start(); //start I2C |
lamell | 5:3c4431224932 | 58 | ch=i2c_thermo.write(MLX96014_ADDRESS); //device address with write condition |
lamell | 5:3c4431224932 | 59 | |
lamell | 5:3c4431224932 | 60 | if(!ch)return -90.0f; //No Ack, return False |
lamell | 5:3c4431224932 | 61 | |
lamell | 5:3c4431224932 | 62 | ch=i2c_thermo.write(0x24); //device ram address where Tobj value is present |
lamell | 5:3c4431224932 | 63 | |
lamell | 5:3c4431224932 | 64 | if(!ch)return -91.0f; //No Ack, return False |
lamell | 5:3c4431224932 | 65 | |
lamell | 5:3c4431224932 | 66 | |
lamell | 5:3c4431224932 | 67 | i2c_thermo.start(); //repeat start |
lamell | 5:3c4431224932 | 68 | ch=i2c_thermo.write(MLX96014_ADDRESS|0x01); //device address with read condition |
lamell | 5:3c4431224932 | 69 | if(!ch)return -92.0f; //No Ack, return False |
lamell | 5:3c4431224932 | 70 | |
lamell | 5:3c4431224932 | 71 | p1=i2c_thermo.read(1); //Tobj low byte |
lamell | 5:3c4431224932 | 72 | p2=i2c_thermo.read(1); //Tobj heigh byte |
lamell | 5:3c4431224932 | 73 | p3=i2c_thermo.read(0); //PEC |
lamell | 5:3c4431224932 | 74 | |
lamell | 5:3c4431224932 | 75 | i2c_thermo.stop(); //stop condition |
lamell | 5:3c4431224932 | 76 | semaDHT.acquire(); |
lamell | 5:3c4431224932 | 77 | debug("p2: %02X, p1: %02X\r\n",p2,p1); |
lamell | 5:3c4431224932 | 78 | semaDHT.release(); |
lamell | 5:3c4431224932 | 79 | |
lamell | 5:3c4431224932 | 80 | //temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01; //degree centigrate conversion |
lamell | 5:3c4431224932 | 81 | //*temp_val=temp_thermo-273; //Convert kelvin to degree Celsius |
lamell | 5:3c4431224932 | 82 | |
lamell | 5:3c4431224932 | 83 | return (((p2<<8) + p1)/65535.0f); //load data successfully, return true |
lamell | 5:3c4431224932 | 84 | } |
lamell | 5:3c4431224932 | 85 | |
lamell | 5:3c4431224932 | 86 | int MLX90614::BlankEEPROM(int em) { |
lamell | 5:3c4431224932 | 87 | uint8_t p1[5]; |
lamell | 5:3c4431224932 | 88 | float temp_thermo; |
lamell | 5:3c4431224932 | 89 | bool ch; |
lamell | 5:3c4431224932 | 90 | |
lamell | 5:3c4431224932 | 91 | //CRC for this config is 0x3B |
lamell | 5:3c4431224932 | 92 | p1[0]=MLX96014_ADDRESS; |
lamell | 5:3c4431224932 | 93 | p1[1]=0x24; |
lamell | 5:3c4431224932 | 94 | p1[2]=0x00; |
lamell | 5:3c4431224932 | 95 | p1[3]=0x00; |
lamell | 5:3c4431224932 | 96 | |
lamell | 5:3c4431224932 | 97 | // uint8_t crc = crc8(p1, 4); |
lamell | 5:3c4431224932 | 98 | // debug("CRC: 0x%02X\r\n",crc); |
lamell | 5:3c4431224932 | 99 | |
lamell | 5:3c4431224932 | 100 | i2c_thermo.stop(); //stop i2c if not ack |
lamell | 5:3c4431224932 | 101 | wait(0.01); |
lamell | 5:3c4431224932 | 102 | i2c_thermo.start(); //start I2C |
lamell | 5:3c4431224932 | 103 | ch=i2c_thermo.write(p1[0]); //device address with write condition |
lamell | 5:3c4431224932 | 104 | if(!ch)return -1; //No Ack, return False |
lamell | 5:3c4431224932 | 105 | |
lamell | 5:3c4431224932 | 106 | ch=i2c_thermo.write(p1[1]); //device ram address where Tobj value is present |
lamell | 5:3c4431224932 | 107 | if(!ch)return -2; //No Ack, return False |
lamell | 5:3c4431224932 | 108 | |
lamell | 5:3c4431224932 | 109 | ch = i2c_thermo.write(p1[3]); //Tobj low byte |
lamell | 5:3c4431224932 | 110 | if (!ch) return -3; |
lamell | 5:3c4431224932 | 111 | |
lamell | 5:3c4431224932 | 112 | ch = i2c_thermo.write(p1[2]); //Tobj heigh byte |
lamell | 5:3c4431224932 | 113 | if (!ch) return -4; |
lamell | 5:3c4431224932 | 114 | |
lamell | 5:3c4431224932 | 115 | |
lamell | 5:3c4431224932 | 116 | |
lamell | 5:3c4431224932 | 117 | ch = i2c_thermo.write(em); |
lamell | 5:3c4431224932 | 118 | if (!ch) return -5; |
lamell | 5:3c4431224932 | 119 | |
lamell | 5:3c4431224932 | 120 | i2c_thermo.stop(); //stop condition |
lamell | 5:3c4431224932 | 121 | |
lamell | 5:3c4431224932 | 122 | debug("Em: %1.3f\r\n",MLX90617_GetEmissivity()); |
lamell | 5:3c4431224932 | 123 | |
lamell | 5:3c4431224932 | 124 | return 0; |
lamell | 5:3c4431224932 | 125 | } |
lamell | 5:3c4431224932 | 126 | |
lamell | 5:3c4431224932 | 127 | int MLX90614::SetEmissivity(float em, uint8_t lclCrc) { |
lamell | 5:3c4431224932 | 128 | uint8_t p1[5]; |
lamell | 5:3c4431224932 | 129 | float temp_thermo; |
lamell | 5:3c4431224932 | 130 | bool ch; |
lamell | 5:3c4431224932 | 131 | uint16_t number; |
lamell | 5:3c4431224932 | 132 | //uint8_t *num[2]; |
lamell | 5:3c4431224932 | 133 | |
lamell | 5:3c4431224932 | 134 | temp_thermo = 65535.0f * em; |
lamell | 5:3c4431224932 | 135 | number=temp_thermo; |
lamell | 5:3c4431224932 | 136 | uint8_t num[2]; |
lamell | 5:3c4431224932 | 137 | |
lamell | 5:3c4431224932 | 138 | num[0] = number & 0xFF; |
lamell | 5:3c4431224932 | 139 | num[1] = number >> 8; |
lamell | 5:3c4431224932 | 140 | |
lamell | 5:3c4431224932 | 141 | //debug("F: %1.3f, INt: %d, H:%02X, L: %02X\r\n",temp_thermo,number,num[1],num[0]); |
lamell | 5:3c4431224932 | 142 | // //CRC for this config is 0x3B |
lamell | 5:3c4431224932 | 143 | // p1[0]=thermometer.i2caddress; |
lamell | 5:3c4431224932 | 144 | // p1[1]=0x24; |
lamell | 5:3c4431224932 | 145 | // p1[2]=0x19; |
lamell | 5:3c4431224932 | 146 | // p1[3]=0x99; |
lamell | 5:3c4431224932 | 147 | |
lamell | 5:3c4431224932 | 148 | //CRC for this config is 0x05 |
lamell | 5:3c4431224932 | 149 | p1[0]=MLX96014_ADDRESS; |
lamell | 5:3c4431224932 | 150 | p1[1]=0x24; |
lamell | 5:3c4431224932 | 151 | p1[2]=num[0]; |
lamell | 5:3c4431224932 | 152 | p1[3]=num[1]; |
lamell | 5:3c4431224932 | 153 | |
lamell | 5:3c4431224932 | 154 | // uint8_t crc = crc8(p1, 4); |
lamell | 5:3c4431224932 | 155 | // debug("CRC: 0x%02X\r\n",crc); |
lamell | 5:3c4431224932 | 156 | |
lamell | 5:3c4431224932 | 157 | i2c_thermo.stop(); //stop i2c if not ack |
lamell | 5:3c4431224932 | 158 | wait(0.01); |
lamell | 5:3c4431224932 | 159 | i2c_thermo.start(); //start I2C |
lamell | 5:3c4431224932 | 160 | ch=i2c_thermo.write(p1[0]); //device address with write condition |
lamell | 5:3c4431224932 | 161 | if(!ch)return -1; //No Ack, return False |
lamell | 5:3c4431224932 | 162 | |
lamell | 5:3c4431224932 | 163 | ch=i2c_thermo.write(p1[1]); //device ram address where Tobj value is present |
lamell | 5:3c4431224932 | 164 | if(!ch)return -2; //No Ack, return False |
lamell | 5:3c4431224932 | 165 | |
lamell | 5:3c4431224932 | 166 | ch = i2c_thermo.write(p1[2]); //Tobj low byte |
lamell | 5:3c4431224932 | 167 | if (!ch) return -3; |
lamell | 5:3c4431224932 | 168 | |
lamell | 5:3c4431224932 | 169 | ch = i2c_thermo.write(p1[3]); //Tobj heigh byte |
lamell | 5:3c4431224932 | 170 | if (!ch) return -4; |
lamell | 5:3c4431224932 | 171 | |
lamell | 5:3c4431224932 | 172 | ch = i2c_thermo.write(lclCrc); |
lamell | 5:3c4431224932 | 173 | if (!ch) return -5; |
lamell | 5:3c4431224932 | 174 | |
lamell | 5:3c4431224932 | 175 | i2c_thermo.stop(); //stop condition |
lamell | 5:3c4431224932 | 176 | |
lamell | 5:3c4431224932 | 177 | debug("Em: %1.3f\r\n",MLX90617_GetEmissivity()); |
lamell | 5:3c4431224932 | 178 | |
lamell | 5:3c4431224932 | 179 | return 0; |
lamell | 5:3c4431224932 | 180 | } |