/*############################################################# Program Name : ex2_F446RE_MLX90614 Author : Grant Phillips Date Modified : 07/11/2016 Compiler : ARMmbed Tested On : NUCLEO-F446RE Description : Example that demonstrate the use of a Melexis MLX90614 Digital IR Thermometer. Here it is demonstrated how temperatures can be read from 2 MLX90614 devices on the same I2C bus. In this case it is assumed that the two devices have been programmed with addresses 0x02 and 0x5A. Requirements : * NUCLEO-F446RE Board Circuit : * Both MLX90614 devices are connected as follows: VCC/VDD - 3.3V VSS/GND - GND SDA (with 2K2 pullup resistor to VCC) - PB9 (F446's SDA pin) SCL (with 2K2 pullup resistor to VCC) - PA1 (F446's SCL pin) (there is also a 5V version available, so just change the VCC/VDD from 3.3V to 5V Only one set of 2K2 pullup resistors must be used, not for both devices. ##############################################################*/ #include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX); //to use the PC as a console (display output) I2C i2c(PB_9,PB_8); //I2C object for communicating with the MLX90614 via I2C bus // sda ,scl bool MLX90614getTemp(int i2caddress, float* temp_val); //user defined function int main(){ float temp; //float variable for holding the temperature result bool ok; //boolean variable for the result of the temp read while(1) { //Read temperature from a MLX90614 at address 0x02 ok = MLX90614getTemp(0x02, &temp); if(ok == true){ pc.printf("Temperature 1: %f \n",temp); } else { pc.printf("MLX90614 not found\n"); } //Read temperature from a MLX90614 at address 0x5A ok = MLX90614getTemp(0x5A, &temp); if(ok == true){ pc.printf("Temperature 2: %f \n\n",temp); } else { pc.printf("MLX90614 not found\n\n"); } wait(1); } } /** User function for reading the temperature from the MLX90614. * @param * i2caddress I2C address of the MLX90614 to read * temp_val Pointer variable for storing the temperature value in celsius * @returns * true on success, false for error. */ bool MLX90614getTemp(int i2caddress, float* temp_val){ char p1,p2,p3; float temp_thermo; bool ch; i2caddress = i2caddress << 1; //address is 7 bits plus R/W bit i2c.start(); //start I2C ch=i2c.write(i2caddress); //device address with write condition if(!ch)return false; //No Ack, return False ch=i2c.write(0x07); //device ram address where Tobj value is present if(!ch)return false; //No Ack, return False i2c.start(); //repeat start ch=i2c.write(i2caddress|0x01); //device address with read condition if(!ch)return false; //No Ack, return False p1=i2c.read(1); //Tobj low byte p2=i2c.read(1); //Tobj heigh byte p3=i2c.read(0); //PEC i2c.stop(); //stop condition temp_thermo=((((p2&0x007f)<<8)+p1)*0.02)-0.01; //degree centigrate conversion *temp_val=temp_thermo-273; //Convert kelvin to degree Celsius return true; //load data successfully, return true }