Application Notes
3. Software
This sample program reads the temperature from the MLX90614ESF’s RAM using SMBus. The program works with our
Orangutan robot controllers [http://www.pololu.com/catalog/category/8] (except for the Orangutan X2) and uses the Pololu AVR
C/C++ Library [http://www.pololu.com/docs/0J20] to print the measured temperature to the LCD. For specific information on how
to program your Orangutan, read its User’s Guide, which can be found on the resources tab of the product’s website.
#include <avr/io.h>
#include <pololu/orangutan.h>
void i2c_start() {
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); // send start condition
while (!(TWCR & (1 << TWINT)));
}
void i2c_write_byte(char byte) {
TWDR = byte;
TWCR = (1 << TWINT) | (1 << TWEN); // start address transmission
while (!(TWCR & (1 << TWINT)));
}
char i2c_read_byte() {
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN); // start data reception, transmit ACK
while (!(TWCR & (1 << TWINT)));
return TWDR;
}
void i2c_receive_pec() {
TWCR = (1 << TWINT) | (1 << TWEN); // start PEC reception, transmit NACK
while (!(TWCR & (1 << TWINT)));
}
void i2c_stop() {
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); // send stop condition
}
//Returns 100 times the temperature read by the sensor giving a 0.01 degree resolution.
long i2c_read_temperature_f() {
long low_byte, high_byte;
DDRC = 0; // all inputs
PORTC = (1 << PORTC4) | (1 << PORTC5); // enable pull-ups on SDA and SCL, respectively
TWSR = 0; // clear bit-rate prescale bits
TWBR = 192; // produces an SCL frequency of 50 kHz with a 20 MHz CPU clock speed.
i2c_start();
// The expected value of TWSR & 0xF8 is now 0x08 (Start condition transmitted).
i2c_write_byte(0);// 0 is the universal write address for slaves.
// The expected value of TWSR & 0xF8 is now 0x18 (SLA+W transmitted ACK received).
i2c_write_byte(0x07); // read TObj1 (0x07) from RAM
// The expected value of TWSR & 0xF8 is now 0x28 (Data transmitted ACK received).
i2c_start();
// The expected value of TWSR & 0xF8 is now 0x10 (Repeated start has been transmitted).
i2c_write_byte(1); // 1 is the universal read address for slaves.
// The expected value of TWSR & 0xF8 is now 0x40 (SLA+R transmitted ACK received).
low_byte = i2c_read_byte();
// The expected value of TWSR & 0xF8 is now 0x50 (Data received ACK received).
high_byte = i2c_read_byte();
// The expected value of TWSR & 0xF8 is now 0x50 (Data received ACK received).
i2c_receive_pec(); // read packet error code (PEC)
// The expected value of TWSR & 0xF8 is now 0x58 (Data received NOT ACK received).
i2c_stop();
// Tk is temperature in Kelvin, Tf is temperature in degrees Fahrenheit, To is the raw
// value of the object temperature as returned by the sensor
Application Note: MLX90614ESF SMBus Communication with Orangutan Robot
Controllers
© 2001–2010 Pololu
Corporation
3. Software Page 4 of 5