Library for the DS1721, 2-Wire Digital Thermometer and Thermostat from Dallas Semiconductor (Maxim Integrated)

DS1721.cpp

Committer:
chaegle
Date:
2013-05-02
Revision:
1:4fd830a97574
Child:
2:22dbeccb82be

File content as of revision 1:4fd830a97574:

/***********************************************************************************
 * @file        ds1721.cpp
 * @brief       Source file, enabling communication to/from the Dallas Semiconductor 
 *              (Maxim Integrated) DS1721 2-Wire Digital Theremometer and Thermostat.
 * @version     1.00
 * @date        04/29/2013
 * 
 * @author      Cameron Haegle
 * @company     Digi International
 * @copyright   
 * 
 * @limitations 
 *
 * 
 **********************************************************************************/


#include "ds1721.h"

/**
 *
 **/
DS1721::DS1721(PinName sda, PinName scl, int addr) : m_i2c(sda, scl) , m_addr(addr)
{    
     _resolution = RES_9_BIT;
     _polarity   = POLARITY_ACTIVE_HIGH;
     _mode   = CONV_FOREVER;
     _num_read = 1;
     
     setConfig(_resolution | _polarity | _one_shot);
}

/**
 *
 **/
DS1721::DS1721(PinName sda, PinName scl, int addr, int resolution, int polarity, int mode) 
        : m_i2c(sda, scl) , m_addr(addr) , _resolution(resolution) , _polarity(polarity) , _one_shot(one_shot)
{    
    _num_read = 1;     
    setConfig(_resolution | _polarity | _mode);
}

/**
 * deconstructor
 *
 **/
DS1721::~DS1721(void) 
{ 
}


/**
 *
 **/
int DS1721::setConfig(int config)
{
    char cmd[2];
    cmd[0] = CMD_ACCESS_CFG;
    cmd[1] = (config & 0xFF);
    
    if(!m_i2c.write(m_addr, cmd, 2) != 0)
    {        
        return 0;
    }    
    return 1;
}

/**
 *
 **/
int DS1721::getConfig(int* config)
{
    char cmd[2];
    
    cmd[0] = CMD_ACCESS_CFG;      // 0xAC
    if(m_i2c.write(m_addr, cmd, 1) != 0)
    {       
        return 0;
    }
    //Delay(RW_DELAY);    
    if((m_i2c.read(m_addr, cmd, 1)) != 0)
    {      
        return 0;
    }      
    *config = cmd[0];
    return 1;
}
 

int DS1721::getTemp(void)
{
    char cmd[3];
   
    cmd[0] = CMD_READ_TEMP;
    
    // if this is the first read, since power up, perform the read twice
    // this eliminate  an erroneous initial read.
    do
    {        
        if((m_i2c.write(m_addr, cmd, 1)) != 0)
        {       
            return 0;
        }    
        //Delay(RW_DELAY);
        // read 1 byte, ignoring fractional portion of temperature
        if((m_i2c.read(m_addr, cmd, 2)) != 0)
        {
            return 0;
        }
        _num_read--;
    }while(_num_read);
    
    _num_read = 1;
    
    return cmd[0];
}


/**
 *
 *
 **/
int DS1721::setPolarity(int polarity)
{
    _polarity = polarity;
    return setConfig(_resolution | _polarity | _one_shot);
}

/**
 *
 *
 **/
int DS1721::getPolarity(void)
{
    int config;    
    getConfig(&config);
    return ((config & (1<<1)) ? 1: 0);
}


/**
 *
 *
 **/
int DS1721::startConversion(void)
{
   char cmd[1];
    
    cmd[0] = CMD_START_CONVT; // 0x51
    
    if((m_i2c.write(m_addr, cmd, 1)) != 1)
    {      
        return 0;
    }  
    return 1;
}

/**
 *
 *
 **/
int DS1721::stopConversion(void)
{
    char cmd[1];
    
    cmd[0] = CMD_STOP_CONVT; // 0x51
    
    if((m_i2c.write(m_addr, cmd, 1)) != 0)
    {       
        return 0;
    }  
    return 1;
}

/**
 *
 **/
int DS1721::getLowSetpoint(void)
{
    char cmd[4];
        
    cmd[0] = CMD_ACCESS_TL;
    if((m_i2c.write(m_addr, cmd, 1)) != 0)
    {     
        //return FALSE;
    }
    //Delay(RW_DELAY);
    
    // read back TL msb & lsb bytes
    if((m_i2c.read(m_addr, cmd, 2)) != 0)
    {       
        return 0;
    }    
    _tl = (cmd[0] & 0xFF);  
    return _tl;
}

/**
 *
 **/
int DS1721::setLowSetpoint(int newSp)
{
    char cmd[3];
    
    _tl = newSp;
    
    cmd[0] = CMD_ACCESS_TL;
    cmd[1] = _tl;         // temp MSB
    cmd[2] = 0x80;        // temp LSB (+/-0.5)

    if((m_i2c.write(m_addr, cmd, 3)) != 0)
    {       
        return 0;
    }
    return 1;
}

/**
 *
 **/
int DS1721::getHighSetpoint(void)
{
    char cmd[3];
        
    cmd[0] = CMD_ACCESS_TH;
    if((m_i2c.write(m_addr, cmd, 1)) != 0)
    {
        //return 0;
    }
    //Delay(RW_DELAY);
    // read back TL msb & lsb bytes
    if((m_i2c.read(m_addr, cmd, 2)) != 0)
    {
        //return 0;
    }    
    _th = (cmd[0] & 0xFF);
    return _th; 
}

/**
 *
 **/
int DS1721::setHighSetpoint(int newSp)
{
    char cmd[3];
    
    _th = newSp;

    cmd[0] = CMD_ACCESS_TH;
    cmd[1] = _th;           // temp MSB
    cmd[2] = 0x00;          // temp LSB

    if((m_i2c.write(m_addr, cmd, 3)) != 0)
    {      
        return 0;
    }
    return 1;
}