|
| 1 | +/****************************************************************************** |
| 2 | +SparkFunMLX90614.h |
| 3 | +Header file for the SparkFun IR Thermometer Library |
| 4 | +
|
| 5 | +Jim Lindblom @ SparkFun Electronics |
| 6 | +October 23, 2015 |
| 7 | +https://github.com/sparkfun/SparkFun_MLX90614_Arduino_Library |
| 8 | +
|
| 9 | +This file defines the SMBus hardware interface(s) for the MLX90614 IR thermometer |
| 10 | +and abstracts temperature measurments and other features of the MLX90614 |
| 11 | +
|
| 12 | +Development environment specifics: |
| 13 | +Arduino 1.6.5 |
| 14 | +SparkFun IR Thermometer Evaluation Board - MLX90614 |
| 15 | +******************************************************************************/ |
| 16 | + |
| 17 | +#ifndef SPARKFUNMLX90164_H |
| 18 | +#define SPARKFUNMLX90164_H |
| 19 | + |
| 20 | +#include <Arduino.h> |
| 21 | +#include <Wire.h> |
| 22 | + |
| 23 | +////////////////////////////////// |
| 24 | +// MLX90614 Default I2C Address // |
| 25 | +////////////////////////////////// |
| 26 | +#define MLX90614_DEFAULT_ADDRESS 0x5A |
| 27 | + |
| 28 | +/////////////////////////////////////// |
| 29 | +// MLX90614 RAM and EEPROM Addresses // |
| 30 | +/////////////////////////////////////// |
| 31 | +#define MLX90614_REGISTER_TA 0x06 |
| 32 | +#define MLX90614_REGISTER_TOBJ1 0x07 |
| 33 | +#define MLX90614_REGISTER_TOBJ2 0x08 |
| 34 | +#define MLX90614_REGISTER_TOMAX 0x20 |
| 35 | +#define MLX90614_REGISTER_TOMIN 0x21 |
| 36 | +#define MLX90614_REGISTER_PWMCTRL 0x22 |
| 37 | +#define MLX90614_REGISTER_TARANGE 0x23 |
| 38 | +#define MLX90614_REGISTER_KE 0x24 |
| 39 | +#define MLX90614_REGISTER_CONFIG 0x25 |
| 40 | +#define MLX90614_REGISTER_ADDRESS 0x2E |
| 41 | +#define MLX90614_REGISTER_ID0 0x1C |
| 42 | +#define MLX90614_REGISTER_ID1 0x1D |
| 43 | +#define MLX90614_REGISTER_ID2 0x1E |
| 44 | +#define MLX90614_REGISTER_ID3 0x1F |
| 45 | +#define MLX90614_REGISTER_SLEEP 0xFF // Not really a register, but close enough |
| 46 | + |
| 47 | +#define I2C_READ_TIMEOUT 1000 |
| 48 | + |
| 49 | +typedef enum temperature_units { |
| 50 | + TEMP_RAW, |
| 51 | + TEMP_K, |
| 52 | + TEMP_C, |
| 53 | + TEMP_F |
| 54 | +}; |
| 55 | + |
| 56 | +class IRTherm |
| 57 | +{ |
| 58 | +public: |
| 59 | + // Default constructor, does very little besides setting class variable |
| 60 | + // initial values. |
| 61 | + IRTherm(); |
| 62 | + |
| 63 | + // begin(<address>) initializes the Wire library, and prepares |
| 64 | + // communication with an MLX90614 device at the specified 7-bit I2C |
| 65 | + // address. |
| 66 | + // If no parameter is supplied, the default MLX90614 address is used. |
| 67 | + uint8_t begin(uint8_t address = MLX90614_DEFAULT_ADDRESS); |
| 68 | + |
| 69 | + // setUnit(<unit>) configures the units returned by the ambient(), |
| 70 | + // object(), minimum() and maximum() functions, and it determines what |
| 71 | + // units the setMin() and setMax() functions should expect. |
| 72 | + // <unit> can be either: |
| 73 | + // - TEMP_RAW: No conversion, just the raw 12-bit ADC reading |
| 74 | + // - TEMP_K: Kelvin |
| 75 | + // - TEMP_C: Celsius |
| 76 | + // - TEMP_F: Farenheit |
| 77 | + void setUnit(temperature_units unit); |
| 78 | + |
| 79 | + // read() pulls the latest ambient and object temperatures from the |
| 80 | + // MLX90614. It will return either 1 on success or 0 on failure. (Failure |
| 81 | + // can result from either a timed out I2C transmission, or an incorrect |
| 82 | + // checksum value). |
| 83 | + uint8_t read(void); |
| 84 | + |
| 85 | + // object() returns the MLX90614's most recently read object temperature |
| 86 | + // after the read() function has returned successfully. The float value |
| 87 | + // returned will be in the units specified by setUnit(). |
| 88 | + float object(void); |
| 89 | + |
| 90 | + // ambient() returns the MLX90614's most recently read ambient temperature |
| 91 | + // after the read() function has returned successfully. The float value |
| 92 | + // returned will be in the units specified by setUnit(). |
| 93 | + float ambient(void); |
| 94 | + |
| 95 | + // readEmissivity() reads the MLX90614's emissivity setting. It will |
| 96 | + // return a value between 0.1 and 1.0. |
| 97 | + float readEmissivity(void); |
| 98 | + |
| 99 | + // setEmissivity(<emis>) can set the MLX90614's configured emissivity |
| 100 | + // EEPROM value. |
| 101 | + // The <emis> parameter should be a value between 0.1 and 1.0. |
| 102 | + // The function will return either 1 on success or 0 on failure. |
| 103 | + uint8_t setEmissivity(float emis); |
| 104 | + |
| 105 | + // readAddress() returns the MLX90614's configured 7-bit I2C bus address. |
| 106 | + // A value between 0x01 and 0x7F should be returned. |
| 107 | + uint8_t readAddress(void); |
| 108 | + |
| 109 | + // setAddress(<newAdd>) can set the MLX90614's 7-bit I2C bus address. |
| 110 | + // The <newAdd> parameter should be a value between 0x01 and 0x7F. |
| 111 | + // The function returns 1 on success and 0 on failure. |
| 112 | + // The new address won't take effect until the device is reset. |
| 113 | + uint8_t setAddress(uint8_t newAdd); |
| 114 | + |
| 115 | + // readID() reads the 64-bit ID of the MLX90614. |
| 116 | + // Return value is either 1 on success or 0 on failure. |
| 117 | + uint8_t readID(void); |
| 118 | + |
| 119 | + // After calling readID() getIDH() and getIDL() can be called to read |
| 120 | + // the upper 4 bytes and lower 4-bytes, respectively, of the MLX90614's |
| 121 | + // identification registers. |
| 122 | + uint32_t getIDH(void); |
| 123 | + uint32_t getIDL(void); |
| 124 | + |
| 125 | + // readRange() pulls the object maximum and minimum values stored in the |
| 126 | + // MLX90614's EEPROM. |
| 127 | + // It will return either 1 on success or 0 on failure. |
| 128 | + uint8_t readRange(void); |
| 129 | + |
| 130 | + // minimum() and maximum() return the MLX90614's minimum and maximum object |
| 131 | + // sensor readings. |
| 132 | + // The float values returned will be in the units specified by setUnit(). |
| 133 | + float minimum(void); |
| 134 | + float maximum(void); |
| 135 | + |
| 136 | + // setMax(<maxTemp>) and setMin(<minTemp>) configure the MLX90614's |
| 137 | + // maximum and minimum object sensor temperatures. |
| 138 | + uint8_t setMax(float maxTemp); |
| 139 | + uint8_t setMin(float minTemp); |
| 140 | + |
| 141 | + // sleep() sets the MLX90614 into a low-power sleep mode. |
| 142 | + uint8_t sleep(void); |
| 143 | + |
| 144 | + // wake() should revive the MLX90614 from low-power sleep mode. |
| 145 | + uint8_t wake(void); |
| 146 | + |
| 147 | +private: |
| 148 | + uint8_t _deviceAddress; // MLX90614's 7-bit I2C address |
| 149 | + temperature_units _defaultUnit; // Keeps track of configured temperature unit |
| 150 | + |
| 151 | + // These keep track of the raw temperature values read from the sensor: |
| 152 | + int16_t _rawAmbient, _rawObject, _rawObject2, _rawMax, _rawMin; |
| 153 | + |
| 154 | + uint16_t id[4]; // Keeps track of the 64-bit ID value |
| 155 | + |
| 156 | + // These functions individually read the object, object2, and ambient |
| 157 | + // temperature values from the MLX90614's RAM: |
| 158 | + uint8_t readObject(void); |
| 159 | + uint8_t readObject2(void); |
| 160 | + uint8_t readAmbient(void); |
| 161 | + |
| 162 | + // These functions individually read the min and mx temperatures in |
| 163 | + // the MLX90614's EEPROM: |
| 164 | + uint8_t readMax(void); |
| 165 | + uint8_t readMin(void); |
| 166 | + |
| 167 | + // calcTemperature converts a raw ADC temperature reading to the |
| 168 | + // set unit. |
| 169 | + float calcTemperature(int16_t rawTemp); |
| 170 | + |
| 171 | + // calcRawTemperature converts a set unit temperature to a |
| 172 | + // raw ADC value: |
| 173 | + int16_t calcRawTemp(float calcTemp); |
| 174 | + |
| 175 | + // Abstract function to write 16-bits to an address in the MLX90614's |
| 176 | + // EEPROM |
| 177 | + uint8_t writeEEPROM(byte reg, int16_t data); |
| 178 | + |
| 179 | + // Abstract functions to read and write 16-bit values from a RAM |
| 180 | + // or EEPROM address in the MLX90614 |
| 181 | + uint8_t I2CReadWord(byte reg, int16_t * dest); |
| 182 | + uint8_t I2CWriteWord(byte reg, int16_t data); |
| 183 | + |
| 184 | + // crc8 returns a calculated crc value given an initial value and |
| 185 | + // input data. |
| 186 | + // It's configured to calculate the CRC using a x^8+x^2+x^1+1 poly |
| 187 | + uint8_t crc8 (uint8_t inCrc, uint8_t inData); |
| 188 | +}; |
| 189 | + |
| 190 | +#endif |
0 commit comments