Analog Devices / ADT7410 16-Bit Digital I2C Temperature Sensor

Dependents:   Frequency_Counter_w_GPS_1PPS MQTToverCC3000 Frequency_Cntr_1PPS_F746ZG

ADT7410.cpp

Committer:
kenjiArai
Date:
2014-11-28
Revision:
0:6ec4df1fa459
Child:
1:4a1eb0f32025

File content as of revision 0:6ec4df1fa459:

/*
 * mbed library program
 *  Analog Devices / ADT7410 16-Bit Digital I2C Temperature Sensor
 *  http://www.analog.com/en/mems-sensors/digital-temperature-sensors/adt7410/products/product.html
 *
 * Copyright (c) 2014 Kenji Arai / JH1PJL
 *  http://www.page.sannet.ne.jp/kenjia/index.html
 *  http://mbed.org/users/kenjiArai/
 *      Created: November   26th, 2014
 *      Revised: November   28th, 2014
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include    "mbed.h"
#include    "ADT7410.h"

//#define DEBUG

#ifdef DEBUG
Serial pcm(USBTX, USBRX);
#endif

#ifdef DEBUG
#define PRINTF(...)     pcm.printf(__VA_ARGS__)
#else
#define PRINTF(...)     {;}
#endif

////////////// REGISTER DEFINITION //////////////
#define ADT7410_A_T_MSB         0x00
#define ADT7410_A_T_LSB         0x01
#define ADT7410_A_STATUS        0x02
#define ADT7410_A_CONFIG        0x03
#define ADT7410_A_T_H_MSB       0x04
#define ADT7410_A_T_H_LSB       0x05
#define ADT7410_A_T_L_MSB       0x06
#define ADT7410_A_T_L_LSB       0x07
#define ADT7410_A_T_C_MSB       0x08
#define ADT7410_A_T_C_LSB       0x09
#define ADT7410_A_T_HYS         0x0a
#define ADT7410_A_ID            0x0b
#define ADT7410_A_SW_RESET      0x2f

ADT7410::ADT7410 (PinName p_sda, PinName p_scl, uint8_t addr) : _i2c(p_sda, p_scl)
{
    ADT7410_addr = (char)addr;
    init();
}

ADT7410::ADT7410 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c)
{
    ADT7410_addr = (char)addr;
    init();
}

/////////////// Read All data /////////////////////////////
void ADT7410::read_all()
{
    dt[0] = ADT7410_A_T_MSB;
    _i2c.write((int)ADT7410_addr, (char *)dt, 1, false);
    _i2c.read((int)ADT7410_addr, (char *)dt, 12, true);
}

/////////////// Read Temperature //////////////////////////
float ADT7410::read_temp()
{
    dt[0] = ADT7410_A_T_MSB;
    _i2c.write((int)ADT7410_addr, (char *)dt, 1, false);
    _i2c.read((int)ADT7410_addr, (char *)dt, 2, true);
    uint32_t d = (dt[0] << 8) + dt[1];
    PRINTF("d= 0x%04x , dt0= 0x%02x, dt1= 0x%02x\r\n", d, dt[0], dt[1]);
    return (float)d;
}


/////////////// Check Status //////////////////////////////
uint8_t ADT7410::read_status()
{
    read_all();
    PRINTF("status= 0x%02x\r\n", dt[2]);
    return dt[2];
}

/////////////// Check Configration //////////////////////////
uint8_t ADT7410::read_config()
{
    read_all();
    PRINTF("config= 0x%02x\r\n", dt[3]);
    return dt[3];
}

/////////////// Set Configration //////////////////////////
uint8_t ADT7410::set_config(uint8_t cfg)
{
    uint8_t dx[2];

    dx[0] = ADT7410_A_CONFIG;
    dx[1] = cfg;
    _i2c.write((int)ADT7410_addr, (char *)dx, 2);
    return read_config();
}

/////////////// Read ID ///////////////////////////////////
uint8_t ADT7410::read_id()
{
    read_all();
    PRINTF("ID reg= 0x%02x\r\n", dt[11]);
    return (dt[11 ]>> 3);
}

/////////////// Read Revision /////////////////////////////
uint8_t ADT7410::read_revision()
{
    read_all();
    PRINTF("ID reg= 0x%02x\r\n", dt[11]);
    return (dt[11] & 0x7);
}

/////////////// Initialize ////////////////////////////////
void ADT7410::init()
{
    uint8_t dx[2];

    dx[0] = ADT7410_A_CONFIG;
    dx[1] = RESOLUTION_16BIT + OPERATION_MODE_1SPS;
    _i2c.write((int)ADT7410_addr, (char *)dx, 2);
}