Library for the DS1721, 2-Wire Digital Thermometer and Thermostat from Dallas Semiconductor (Maxim Integrated)
Diff: DS1721.h
- Revision:
- 1:4fd830a97574
- Child:
- 2:22dbeccb82be
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1721.h Thu May 02 17:12:06 2013 +0000 @@ -0,0 +1,195 @@ +/*********************************************************************************** + * @file DS1721.h + * + * @version 1.00 + * @date 04/29/2013 + * + * @author Cameron Haegle + * @company Digi International + * @copyright + * + **********************************************************************************/ + +#ifndef DS1721_H +#define DS1721_H + +#include "mbed.h" + +// default DS1721 I2C address +#define DS1721_ADDR 0x48 + +// DS1721 commands +#define CMD_START_CONVT 0x51 +#define CMD_STOP_CONVT 0x22 +#define CMD_READ_TEMP 0xAA +#define CMD_ACCESS_CFG 0xAC +#define CMD_ACCESS_TH 0xA1 +#define CMD_ACCESS_TL 0xA2 + +// thermometer config register values +#define CONV_FOREVER 0x00 +#define CONV_ONE_SHOT 0x01 +#define POLARITY_ACTIVE_LOW 0x00 +#define POLARITY_ACTIVE_HIGH 0x02 +#define RES_9_BIT 0x00 // 0000 +#define RES_10_BIT 0x04 // 0100 +#define RES_11_BIT 0x08 // 1000 +#define RES_12_BIT 0x0C // 1100 + +// DS1721 operating modes (HEAT = active low, Cool = active high) +#define MODE_HEAT POLARITY_ACTIVE_LOW +#define MODE_COOL POLARITY_ACTIVE_HIGH + + +/** + * !Library for the DS1721 temperature sensor. + * The DS1721 is an I2C digital temperature sensor, with a range of -55C to +125C and a 0.125C resolution. + * + * @code + * #include "mbed.h" + * #include "DS1721.h" + * + * #define DS1721_I2C_ADDRESS (0x48<<1) + * + * int main(void) { + * + * DS1721 thermo(PTC9, PTC8, DS1721_I2C_ADDRESS); + * + * // initialize the temperature sensor + * thermo.startConversion(); + * thermo.setLowSetpoint(25); + * thermo.setHighSetpoint(27); + * thermo.setPolarity(POLARITY_ACTIVE_HIGH); + * + * while (true) { + * printf("Temp: %d\r\n", thermo.getTemp()); + * } + * } + * @endcode + **/ +class DS1721 +{ +public: + /** + * DS1721 constructor + * + * @param sda SDA pin + * @param sdl SCL pin + * @param addr addr of the I2C peripheral + **/ + DS1721(PinName sda, PinName scl, int addr); + + /** + * DS1721 constructor + * + * @param sda SDA pin + * @param sdl SCL pin + * @param addr addr of the I2C peripheral + * @param resolution readout resolution of the theremometer (9, 10, 11, 12 bit) + * @param polarity the state on which the thermostat output is enabled + * @param mode the temperature conversion mode (single shot or continuous) + **/ + DS1721(PinName sda, PinName scl, int addr,int resolution, int polarity, int mode); + + /*! + * DS1721 destructor + */ + ~DS1721(); + + /** + * Reads the temperature from the DS1721 and converts it to a useable value. + * + * @returns the current temperature, as in integer + **/ + int getTemp(void); + + /** + * Sets the temperature polarity (POL) bit of the sensor. This bit determines + * upon which set point the theremostat output is active. + * + * @param int the new polarity value + * @returns the result of the function (0 - Failure, 1 - Success) + **/ + int setPolarity(int polarity); + + /** + * Gets the temperature polarity (POL) bit of the sensor. This bit determines + * upon which set point the theremostat output is active. + * + * @returns the value of the POL bit (0 - active low, 1 - active high) + **/ + int getPolarity(void); + + /** + * Initiates a temperature conversion. If the DS1721 is configured for single shot mode + * this function must be called prior to reading the temperature (re: getTemp()) + * + * @returns the result of the function (0 - Failure, 1 - Success) + **/ + int startConversion(void); + + /** + * Stops a temperature conversion. + * + * @returns the result of the function (0 - Failure, 1 - Success) + **/ + int stopConversion(void); + + /** + * Sets the configuration register of the DS1721. + * + * @param int the new configuration value (resolution | polarity | mode) + * @returns the result of the function (0 - Failure, 1 - Success) + **/ + int setConfig(int config); + + /** + * Retrieves the configuration register of the DS1721. + * + * @returns + **/ + int getConfig(int* config); + + /** + * Retrieves the configured low temperature set point value. + * + * @returns the current low temperature set point value (degrees C) + **/ + int getLowSetpoint(void); + + /** + * Sets the low temperature set point value. + * + * @param int the new low set point temperature (degrees C) + * @returns the result of the function (0 - Failure, 1 - Success) + **/ + int setLowSetpoint(int newSp); + + /** + * Retrieves the configured high temperature set point value. + * + * @returns the current high temperature set point value (degrees C) + **/ + int getHighSetpoint(void); + + /** + * Sets the high temperature set point value. + * + * @param int the new high temperature set point value (degrees C) + * @returns the result of the function (0 - Failure, 1 - Success) + **/ + int setHighSetpoint(int newSp); + +private: + I2C m_i2c; + int m_addr; + int _resolution; + int _polarity; + int _mode; + int _tl; + int _num_read; + int _th; +}; + +#endif +