Single and Dual Zone Infra Red Thermometer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MLX90614.h Source File

MLX90614.h

00001 /**
00002  * @brief       MLX90614.h
00003  * @details     Single and Dual Zone Infra Red Thermometer.
00004  *              Header file.
00005  *
00006  *
00007  * @return      NA
00008  *
00009  * @author      Manuel Caballero
00010  * @date        26/December/2017
00011  * @version     26/December/2017    The ORIGIN
00012  * @pre         NaN.
00013  * @warning     NaN
00014  * @pre         This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ).
00015  */
00016 #ifndef MLX90614_H
00017 #define MLX90614_H
00018 
00019 #include "mbed.h"
00020 
00021 
00022 /**
00023     Example:
00024 
00025 #include "mbed.h"
00026 #include "MLX90614.h"
00027 
00028 MLX90614 myMLX90614   ( I2C_SDA, I2C_SCL, MLX90614::MLX90614_ADDRESS, 100000 );
00029 Serial pc             ( USBTX, USBRX );
00030 
00031 Ticker     newReading;
00032 DigitalOut myled(LED1);
00033 
00034 MLX90614::MLX90614_status_t         aux;
00035 MLX90614::MLX90614_vector_data_t    myMLX90614Data;
00036 uint8_t                             myState = 0;
00037 
00038 
00039 void changeDATA ( void )
00040 {
00041     myState = 1;
00042 }
00043 
00044 
00045 int main()
00046 {
00047     pc.baud ( 115200 );
00048 
00049     // Get the IDs
00050     aux  =   myMLX90614.MLX90614_GetID_Numbers ( &myMLX90614Data );
00051     pc.printf( "ID_0: %d\nID_1: %d\nID_2: %d\nID_3: %d\r\n", myMLX90614Data.ID[0], myMLX90614Data.ID[1], myMLX90614Data.ID[2], myMLX90614Data.ID[3] );
00052 
00053     newReading.attach( &changeDATA, 1 );                                        // the address of the function to be attached ( changeDATA ) and the interval ( 1s )
00054 
00055     // Let the callbacks take care of everything
00056     while(1) {
00057         sleep();
00058 
00059         myled = 1;
00060 
00061         if ( myState == 1 ) {
00062             // Get the data
00063             aux  =   myMLX90614.MLX90614_ReadTA     ( &myMLX90614Data );
00064             aux  =   myMLX90614.MLX90614_ReadTObj1  ( &myMLX90614Data );
00065 
00066             pc.printf( "Ta: %0.2fC\nTObj1: %0.2fC\r\n", myMLX90614Data.TA, myMLX90614Data.TObj1 );
00067 
00068             myState  =   0;                                                     // Reset the variable
00069         }
00070 
00071         myled = 0;
00072     }
00073 }
00074 */
00075 
00076 
00077 /*!
00078  Library for the MLX90614 Single and Dual Zone Infra Red Thermometer.
00079 */
00080 class MLX90614
00081 {
00082 public:
00083     /**
00084     * @brief   DEFAULT ADDRESSES
00085     */
00086     typedef enum {
00087         MLX90614_ADDRESS      =   ( 0x5A << 1 )                                    /*!<   MLX90614 I2C Address                                   */
00088     } MLX90614_address_t;
00089 
00090 
00091 // COMMANDS
00092     /**
00093       * @brief   COMMANDS
00094       */
00095     typedef enum {
00096         MLX90614_RAM_ACCESS      =   0b00011111,         /*!<  Seconds. RANGE 00-59                                  */
00097         MLX90614_EEPROM_ACCESS   =   0b00100000,         /*!<  LSB of Temp                                           */
00098         MLX90614_FLAGS           =   0b11110000,         /*!<  LSB of Temp                                           */
00099         MLX90614_SLEEP           =   0b11111111          /*!<  LSB of Temp                                           */
00100     } MLX90614_command_t;
00101 
00102 
00103 // REGISTERS
00104     /**
00105       * @brief   EEPROM REGISTERS
00106       */
00107     typedef enum {
00108         MLX90614_TO_MAX                              =   ( MLX90614_EEPROM_ACCESS  | 0x00  ),   /*!<  To max                              */
00109         MLX90614_TO_MIN                              =   ( MLX90614_EEPROM_ACCESS  | 0x01  ),   /*!<  To min                              */
00110         MLX90614_PWMCTRL                             =   ( MLX90614_EEPROM_ACCESS  | 0x02  ),   /*!<  PWMCTRL                             */
00111         MLX90614_TA_RANGE                            =   ( MLX90614_EEPROM_ACCESS  | 0x03  ),   /*!<  Ta range                            */
00112         MLX90614_EMISSIVITY_CORRECTION_COEFFICIENT   =   ( MLX90614_EEPROM_ACCESS  | 0x04  ),   /*!<  Emissivity correction coefficient   */
00113         MLX90614_CONFIG_REGISTER_1                   =   ( MLX90614_EEPROM_ACCESS  | 0x05  ),   /*!<  Config Register1                    */
00114         MLX90614_SMBUS_ADDRESS                       =   ( MLX90614_EEPROM_ACCESS  | 0x0E  ),   /*!<  SMBus address (LSByte only)         */
00115         MLX90614_ID_NUMBER_0                         =   ( MLX90614_EEPROM_ACCESS  | 0x0C  ),   /*!<  ID number                           */
00116         MLX90614_ID_NUMBER_1                         =   ( MLX90614_EEPROM_ACCESS  | 0x0D  ),   /*!<  ID number                           */
00117         MLX90614_ID_NUMBER_2                         =   ( MLX90614_EEPROM_ACCESS  | 0x0E  ),   /*!<  ID number                           */
00118         MLX90614_ID_NUMBER_3                         =   ( MLX90614_EEPROM_ACCESS  | 0x0F  )    /*!<  ID number                           */
00119     } MLX90614_eeprom_registers_t;
00120 
00121 
00122     /**
00123       * @brief   RAM REGISTERS
00124       */
00125     typedef enum {
00126         MLX90614_RAW_DATA_IR_CHANNEL_1               =   ( MLX90614_RAM_ACCESS  & 0x04  ),    /*!<  Raw data IR channel 1                 */
00127         MLX90614_RAW_DATA_IR_CHANNEL_2               =   ( MLX90614_RAM_ACCESS  & 0x05  ),    /*!<  Raw data IR channel 2                 */
00128         MLX90614_TA                                  =   ( MLX90614_RAM_ACCESS  & 0x06  ),    /*!<  TA                                    */
00129         MLX90614_TOBJ_1                              =   ( MLX90614_RAM_ACCESS  & 0x07  ),    /*!<  TOBJ 1                                */
00130         MLX90614_TOBJ_2                              =   ( MLX90614_RAM_ACCESS  & 0x08  )     /*!<  TOBJ 2                                */
00131     } MLX90614_ram_registers_t;
00132 
00133 
00134 
00135 // COMMANDS
00136     /**
00137       * @brief   FLAGS
00138       */
00139     typedef enum {
00140         FLAG_EEBUSY_HIGH     =   ( 1 << 7 ),           /*!<  The previous write/erase EEPROM access is still in progress. High active      */
00141         FLAG_EEBUSY_LOW      =   ( 0 << 7 ),           /*!<  The previous write/erase EEPROM access is still in progress. LOW              */
00142         FLAG_EE_DEAD_HIGH    =   ( 1 << 5 ),           /*!<  EEPROM double error has occurred. High active                                 */
00143         FLAG_EE_DEAD_LOW     =   ( 0 << 5 ),           /*!<  EEPROM double error has occurred. LOW                                         */
00144         FLAG_INIT_LOW        =   ( 0 << 4 ),           /*!<  POR initialization routine is still ongoing. Low active                       */
00145         FLAG_INIT_HIGH       =   ( 1 << 4 )            /*!<  POR initialization routine is still ongoing. HIGH                             */
00146     } MLX90614_flags_t;
00147 
00148 
00149     /**
00150       * @brief   CONFIG REGISTER 1
00151       */
00152 // IIR
00153     typedef enum {
00154         CONFIGREG1_IIR_MASK  =   ( 7 << 0 ),         /*!<  IIR Mask                                                                      */
00155         CONFIGREG1_IIR_100   =   ( 4 << 0 ),         /*!<  IIR (100%) a1=1, b1=0                                                         */
00156         CONFIGREG1_IIR_80    =   ( 5 << 0 ),         /*!<  IIR (80%) a1=0.8, b1=0.2                                                      */
00157         CONFIGREG1_IIR_67    =   ( 6 << 0 ),         /*!<  IIR (67%) a1=0.666, b1=0.333                                                  */
00158         CONFIGREG1_IIR_57    =   ( 7 << 0 ),         /*!<  IIR (57%) a1=0.571, b1=0.428                                                  */
00159         CONFIGREG1_IIR_50    =   ( 0 << 0 ),         /*!<  IIR (50%) a1=0.5, b1=0.5                                                      */
00160         CONFIGREG1_IIR_25    =   ( 1 << 0 ),         /*!<  IIR (25%) a1=0.25, b1=0.75                                                    */
00161         CONFIGREG1_IIR_17    =   ( 2 << 0 ),         /*!<  IIR (17%) a1=0.166(6), b1=0.83(3)                                             */
00162         CONFIGREG1_IIR_13    =   ( 3 << 0 )          /*!<  IIR (13%) a1=0.125, b1=0.875                                                  */
00163     } MLX90614_configregister1_iir_t;
00164 
00165 // TEMPERATURE SOURCES
00166     typedef enum {
00167         CONFIGREG1_TEMP_MASK         =   ( 3 << 4 ),     /*!<  Temp Mask                                                                 */
00168         CONFIGREG1_TEMP_TA_TOBJ1     =   ( 0 << 4 ),     /*!<  Ta, Tobj1                                                                 */
00169         CONFIGREG1_TEMP_TA_TOBJ2     =   ( 1 << 4 ),     /*!<  Ta, Tobj2                                                                 */
00170         CONFIGREG1_TEMP_TOBJ2        =   ( 2 << 4 ),     /*!<  Tobj2                                                                     */
00171         CONFIGREG1_TEMP_TOBJ1_TOBJ2  =   ( 3 << 4 )      /*!<  Tobj1, Tobj2                                                              */
00172     } MLX90614_configregister1_temp_t ;
00173 
00174 
00175 
00176 
00177 
00178 #ifndef MLX90614_VECTOR_STRUCT_H
00179 #define MLX90614_VECTOR_STRUCT_H
00180     typedef struct {
00181         uint16_t                        RawTA;
00182         uint16_t                        RawTObj1;
00183         uint16_t                        RawTObj2;
00184         uint8_t                         PEC;
00185 
00186         float                           TA;
00187         float                           TObj1;
00188         float                           TObj2;
00189 
00190         uint16_t                        ID[4];
00191         float                           Emissivity;
00192         MLX90614_configregister1_iir_t  IIR;
00193         MLX90614_flags_t                Flags;
00194         MLX90614_configregister1_temp_t  TempSource;
00195     } MLX90614_vector_data_t;
00196 #endif
00197 
00198 
00199 
00200 
00201     /**
00202       * @brief   INTERNAL CONSTANTS
00203       */
00204 #define MLX90614_KELVIN_TO_CELSIUS  273.15
00205 #define MLX90614_KELVIN_CONVERSION  0.02
00206 
00207 // MACRO: round function
00208 #define _MYROUND( x ) ({        \
00209         uint32_t aux_pre;       \
00210         float    aux_x;         \
00211                                 \
00212         aux_x    =   (x);       \
00213         aux_pre  =   (x);       \
00214         aux_x   -=   aux_pre;   \
00215         aux_x   *=   10;        \
00216                                 \
00217         if ( aux_x >= 5 )       \
00218             aux_pre++;          \
00219                                 \
00220         aux_pre;                \
00221         })
00222 
00223 
00224     typedef enum {
00225         MLX90614_SUCCESS            =   0,
00226         MLX90614_FAILURE            =   1,
00227 
00228         MLX90614_TIMEOUT            =   2*65535,
00229         MLX90614_FLAG_ERROR         =   0x8000,
00230 
00231         I2C_SUCCESS         =       0,                                           /*!<   I2C communication was fine        */
00232         I2C_FAILURE        =       1
00233     } MLX90614_status_t ;
00234 
00235 
00236 
00237 
00238 
00239     /** Create an MLX90614 object connected to the specified I2C pins.
00240       *
00241       * @param sda     I2C data pin
00242       * @param scl     I2C clock pin
00243       * @param addr    I2C slave address
00244       * @param freq    I2C frequency in Hz.
00245       */
00246     MLX90614 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq );
00247 
00248     /** Delete MLX90614 object.
00249      */
00250     ~MLX90614();
00251 
00252     /** It gets the ID numbers.
00253       */
00254     MLX90614_status_t   MLX90614_GetID_Numbers           ( MLX90614_vector_data_t*  myID );
00255 
00256     /** It reads the raw ambient temperature.
00257           */
00258     MLX90614_status_t   MLX90614_ReadRawTA               ( MLX90614_vector_data_t*  myRawTA );
00259 
00260     /** It reads the ambient temperature in Celsius degrees.
00261           */
00262     MLX90614_status_t   MLX90614_ReadTA                  ( MLX90614_vector_data_t*  myTA );
00263 
00264     /** It reads the raw object 1 temperature.
00265           */
00266     MLX90614_status_t   MLX90614_ReadRawTObj1            ( MLX90614_vector_data_t*  myRawTObj1 );
00267 
00268     /** It reads the object 1 temperature.
00269           */
00270     MLX90614_status_t   MLX90614_ReadTObj1               ( MLX90614_vector_data_t*  myObj1 );
00271 
00272     /** It reads the raw object 2 temperature.
00273           */
00274     MLX90614_status_t   MLX90614_ReadRawTObj2            ( MLX90614_vector_data_t*  myRawTObj2 );
00275 
00276     /** It reads the object 2 temperature.
00277           */
00278     MLX90614_status_t   MLX90614_ReadTObj2               ( MLX90614_vector_data_t*  myObj2 );
00279 
00280     /** It gets the Emissivity correction coefficient.
00281           */
00282     MLX90614_status_t   MLX90614_GetEmissivity           ( MLX90614_vector_data_t* myEmissivity );
00283 
00284     /** It sets the Emissivity correction coefficient.
00285           */
00286     MLX90614_status_t   MLX90614_SetEmissivity           ( MLX90614_vector_data_t myEmissivity );
00287 
00288     /** It gets the IIR.
00289           */
00290     MLX90614_status_t   MLX90614_GetIIR                  ( MLX90614_vector_data_t* myIIR );
00291 
00292     /** It sets the IIR.
00293           */
00294     MLX90614_status_t   MLX90614_SetIIR                  ( MLX90614_configregister1_iir_t myIIR );
00295 
00296     /** It gets the Temperature Source.
00297       */
00298     MLX90614_status_t   MLX90614_GetTemperatureSource    ( MLX90614_vector_data_t* myTempSource );
00299 
00300     /** It sets the Temperature Source.
00301           */
00302     MLX90614_status_t   MLX90614_SetTemperatureSource    ( MLX90614_configregister1_temp_t  myTempSource );
00303 
00304     /** It gets the FLAGS.
00305           */
00306     MLX90614_status_t   MLX90614_GetFLAGS                ( MLX90614_vector_data_t* myFlags );
00307 
00308 
00309 
00310 
00311 private:
00312     I2C         _i2c;
00313     uint32_t    _MLX90614_Addr;
00314 };
00315 
00316 #endif