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
ADT7410.cpp@0:380ac1ac0101, 2011-02-01 (annotated)
- Committer:
- tkreyche
- Date:
- Tue Feb 01 18:57:41 2011 +0000
- Revision:
- 0:380ac1ac0101
- Child:
- 8:e1aee50340ec
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tkreyche | 0:380ac1ac0101 | 1 | #include "ADT7410.h" |
tkreyche | 0:380ac1ac0101 | 2 | |
tkreyche | 0:380ac1ac0101 | 3 | |
tkreyche | 0:380ac1ac0101 | 4 | // constructor taks paramters and initializes I2C bus |
tkreyche | 0:380ac1ac0101 | 5 | ADT7410::ADT7410(PinName sda, PinName scl, char addr, int hz) |
tkreyche | 0:380ac1ac0101 | 6 | : i2c(sda, scl) |
tkreyche | 0:380ac1ac0101 | 7 | , busAddr(addr) |
tkreyche | 0:380ac1ac0101 | 8 | { |
tkreyche | 0:380ac1ac0101 | 9 | i2c.frequency(hz); |
tkreyche | 0:380ac1ac0101 | 10 | } |
tkreyche | 0:380ac1ac0101 | 11 | |
tkreyche | 0:380ac1ac0101 | 12 | // destructor |
tkreyche | 0:380ac1ac0101 | 13 | ADT7410::~ADT7410() { |
tkreyche | 0:380ac1ac0101 | 14 | } |
tkreyche | 0:380ac1ac0101 | 15 | |
tkreyche | 0:380ac1ac0101 | 16 | // read 13 bit temperature |
tkreyche | 0:380ac1ac0101 | 17 | float ADT7410::getTemp() { |
tkreyche | 0:380ac1ac0101 | 18 | |
tkreyche | 0:380ac1ac0101 | 19 | char wReg[1] = {TEMP_REG_ADDR}; |
tkreyche | 0:380ac1ac0101 | 20 | char rReg[2] = {0,0}; |
tkreyche | 0:380ac1ac0101 | 21 | float tFin = 0; |
tkreyche | 0:380ac1ac0101 | 22 | int tRaw = 0; |
tkreyche | 0:380ac1ac0101 | 23 | |
tkreyche | 0:380ac1ac0101 | 24 | // set address pointer to temperature register |
tkreyche | 0:380ac1ac0101 | 25 | i2c.write(busAddr, wReg, 1); |
tkreyche | 0:380ac1ac0101 | 26 | |
tkreyche | 0:380ac1ac0101 | 27 | // read temperature register, two bytes |
tkreyche | 0:380ac1ac0101 | 28 | i2c.read(busAddr, rReg, 2); |
tkreyche | 0:380ac1ac0101 | 29 | |
tkreyche | 0:380ac1ac0101 | 30 | // temperature returned is only 13 bits |
tkreyche | 0:380ac1ac0101 | 31 | // discard alarm flags in lower bits |
tkreyche | 0:380ac1ac0101 | 32 | tRaw = (rReg[0] << 8) | (rReg[1]); |
tkreyche | 0:380ac1ac0101 | 33 | tRaw >>= 3; |
tkreyche | 0:380ac1ac0101 | 34 | |
tkreyche | 0:380ac1ac0101 | 35 | // handle positive and negative temperatures |
tkreyche | 0:380ac1ac0101 | 36 | // results in two's complement |
tkreyche | 0:380ac1ac0101 | 37 | if ( tRaw & 0x1000) { |
tkreyche | 0:380ac1ac0101 | 38 | tFin = (float) (tRaw - 8192) / 16; |
tkreyche | 0:380ac1ac0101 | 39 | } else { |
tkreyche | 0:380ac1ac0101 | 40 | tFin = (float) tRaw / 16; |
tkreyche | 0:380ac1ac0101 | 41 | } |
tkreyche | 0:380ac1ac0101 | 42 | |
tkreyche | 0:380ac1ac0101 | 43 | return tFin; |
tkreyche | 0:380ac1ac0101 | 44 | } |
tkreyche | 0:380ac1ac0101 | 45 | |
tkreyche | 0:380ac1ac0101 | 46 | void ADT7410::setConfig(char regVal) { |
tkreyche | 0:380ac1ac0101 | 47 | |
tkreyche | 0:380ac1ac0101 | 48 | char wReg[2] = {CONFIG_REG_ADDR, regVal}; |
tkreyche | 0:380ac1ac0101 | 49 | i2c.write(busAddr, wReg, 2); |
tkreyche | 0:380ac1ac0101 | 50 | return; |
tkreyche | 0:380ac1ac0101 | 51 | } |
tkreyche | 0:380ac1ac0101 | 52 | |
tkreyche | 0:380ac1ac0101 | 53 | char ADT7410::getConfig() { |
tkreyche | 0:380ac1ac0101 | 54 | |
tkreyche | 0:380ac1ac0101 | 55 | char rReg[1] = {0}; |
tkreyche | 0:380ac1ac0101 | 56 | char wReg[1] = {CONFIG_REG_ADDR}; |
tkreyche | 0:380ac1ac0101 | 57 | // need to add repeat to supress stop |
tkreyche | 0:380ac1ac0101 | 58 | i2c.write(busAddr, wReg, 1, 1); |
tkreyche | 0:380ac1ac0101 | 59 | i2c.read(busAddr, rReg, 1); |
tkreyche | 0:380ac1ac0101 | 60 | return rReg[0]; |
tkreyche | 0:380ac1ac0101 | 61 | } |
tkreyche | 0:380ac1ac0101 | 62 | |
tkreyche | 0:380ac1ac0101 | 63 | |
tkreyche | 0:380ac1ac0101 | 64 | // reset the sensor |
tkreyche | 0:380ac1ac0101 | 65 | void ADT7410::reset() { |
tkreyche | 0:380ac1ac0101 | 66 | |
tkreyche | 0:380ac1ac0101 | 67 | char wReg[1] = {RESET}; |
tkreyche | 0:380ac1ac0101 | 68 | i2c.write(busAddr, wReg, 1); |
tkreyche | 0:380ac1ac0101 | 69 | // wait for sensor to reload and convert |
tkreyche | 0:380ac1ac0101 | 70 | wait_ms(250); |
tkreyche | 0:380ac1ac0101 | 71 | return; |
tkreyche | 0:380ac1ac0101 | 72 | } |
tkreyche | 0:380ac1ac0101 | 73 |