Library for ADT7410 I2C temperature sensor. Use this instead of TMP102 when you need to measure temperatures lower than -40 degrees C. The device is guaranteed to work at -55 C but will actually read lower temps. See http://mbed.org/users/tkreyche/notebook/adt7140-i2c-temperature-sensor/ for more info.

Dependents:   BLE_ADT7410_TMP102_Sample BLE_HTM_HRM1017 BLENano_SimpleTemplate_temp_170802 BLENano_SimpleTemplate_temp_170813 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADT7410.cpp Source File

ADT7410.cpp

00001 #include "ADT7410.h"
00002 
00003 
00004  // constructor takes paramters and initializes I2C bus
00005 ADT7410::ADT7410(PinName sda, PinName scl, char addr, int hz)
00006         : i2c(sda, scl)
00007         , busAddr(addr)
00008 {
00009     i2c.frequency(hz);
00010 }
00011 
00012 // destructor
00013 ADT7410::~ADT7410() {
00014 }
00015 
00016 // read 13 bit temperature
00017 float ADT7410::getTemp() {
00018 
00019     char wReg[1] = {TEMP_REG_ADDR};
00020     char rReg[2] = {0,0};
00021     float tFin = 0;
00022     int tRaw = 0;
00023 
00024     // set address pointer to temperature register
00025     i2c.write(busAddr, wReg, 1);
00026 
00027     // read temperature register, two bytes
00028     i2c.read(busAddr, rReg, 2);
00029 
00030     // temperature returned is only 13 bits
00031     // discard alarm flags in lower bits
00032     tRaw = (rReg[0] << 8) | (rReg[1]);
00033     tRaw >>= 3;
00034 
00035     // handle positive and negative temperatures
00036     // results in two's complement
00037     if ( tRaw & 0x1000) {
00038         tFin = (float) (tRaw - 8192) / 16;
00039     } else {
00040         tFin = (float) tRaw  / 16;
00041     }
00042      
00043     return tFin;
00044 }
00045 
00046 void ADT7410::setConfig(char regVal) {
00047 
00048     char wReg[2] = {CONFIG_REG_ADDR, regVal};
00049     i2c.write(busAddr, wReg, 2);
00050     return;
00051 }
00052 
00053 char ADT7410::getConfig() {
00054 
00055     char rReg[1] = {0};
00056     char wReg[1] = {CONFIG_REG_ADDR};
00057     // need to add repeat to supress stop
00058    i2c.write(busAddr, wReg, 1, 1); 
00059    i2c.read(busAddr, rReg, 1);
00060     return rReg[0];
00061 }
00062 
00063 
00064 // reset the sensor
00065 void ADT7410::reset() {
00066 
00067     char wReg[1] = {RESET};
00068     i2c.write(busAddr, wReg, 1);
00069     // wait for sensor to reload and convert
00070     wait_ms(250);
00071     return;
00072 }
00073