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