Library for the DS1721, 2-Wire Digital Thermometer and Thermostat from Dallas Semiconductor (Maxim Integrated)
DS1721.h@1:4fd830a97574, 2013-05-02 (annotated)
- Committer:
- chaegle
- Date:
- Thu May 02 17:12:06 2013 +0000
- Revision:
- 1:4fd830a97574
- Child:
- 2:22dbeccb82be
added library documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chaegle | 1:4fd830a97574 | 1 | /*********************************************************************************** |
chaegle | 1:4fd830a97574 | 2 | * @file DS1721.h |
chaegle | 1:4fd830a97574 | 3 | * |
chaegle | 1:4fd830a97574 | 4 | * @version 1.00 |
chaegle | 1:4fd830a97574 | 5 | * @date 04/29/2013 |
chaegle | 1:4fd830a97574 | 6 | * |
chaegle | 1:4fd830a97574 | 7 | * @author Cameron Haegle |
chaegle | 1:4fd830a97574 | 8 | * @company Digi International |
chaegle | 1:4fd830a97574 | 9 | * @copyright |
chaegle | 1:4fd830a97574 | 10 | * |
chaegle | 1:4fd830a97574 | 11 | **********************************************************************************/ |
chaegle | 1:4fd830a97574 | 12 | |
chaegle | 1:4fd830a97574 | 13 | #ifndef DS1721_H |
chaegle | 1:4fd830a97574 | 14 | #define DS1721_H |
chaegle | 1:4fd830a97574 | 15 | |
chaegle | 1:4fd830a97574 | 16 | #include "mbed.h" |
chaegle | 1:4fd830a97574 | 17 | |
chaegle | 1:4fd830a97574 | 18 | // default DS1721 I2C address |
chaegle | 1:4fd830a97574 | 19 | #define DS1721_ADDR 0x48 |
chaegle | 1:4fd830a97574 | 20 | |
chaegle | 1:4fd830a97574 | 21 | // DS1721 commands |
chaegle | 1:4fd830a97574 | 22 | #define CMD_START_CONVT 0x51 |
chaegle | 1:4fd830a97574 | 23 | #define CMD_STOP_CONVT 0x22 |
chaegle | 1:4fd830a97574 | 24 | #define CMD_READ_TEMP 0xAA |
chaegle | 1:4fd830a97574 | 25 | #define CMD_ACCESS_CFG 0xAC |
chaegle | 1:4fd830a97574 | 26 | #define CMD_ACCESS_TH 0xA1 |
chaegle | 1:4fd830a97574 | 27 | #define CMD_ACCESS_TL 0xA2 |
chaegle | 1:4fd830a97574 | 28 | |
chaegle | 1:4fd830a97574 | 29 | // thermometer config register values |
chaegle | 1:4fd830a97574 | 30 | #define CONV_FOREVER 0x00 |
chaegle | 1:4fd830a97574 | 31 | #define CONV_ONE_SHOT 0x01 |
chaegle | 1:4fd830a97574 | 32 | #define POLARITY_ACTIVE_LOW 0x00 |
chaegle | 1:4fd830a97574 | 33 | #define POLARITY_ACTIVE_HIGH 0x02 |
chaegle | 1:4fd830a97574 | 34 | #define RES_9_BIT 0x00 // 0000 |
chaegle | 1:4fd830a97574 | 35 | #define RES_10_BIT 0x04 // 0100 |
chaegle | 1:4fd830a97574 | 36 | #define RES_11_BIT 0x08 // 1000 |
chaegle | 1:4fd830a97574 | 37 | #define RES_12_BIT 0x0C // 1100 |
chaegle | 1:4fd830a97574 | 38 | |
chaegle | 1:4fd830a97574 | 39 | // DS1721 operating modes (HEAT = active low, Cool = active high) |
chaegle | 1:4fd830a97574 | 40 | #define MODE_HEAT POLARITY_ACTIVE_LOW |
chaegle | 1:4fd830a97574 | 41 | #define MODE_COOL POLARITY_ACTIVE_HIGH |
chaegle | 1:4fd830a97574 | 42 | |
chaegle | 1:4fd830a97574 | 43 | |
chaegle | 1:4fd830a97574 | 44 | /** |
chaegle | 1:4fd830a97574 | 45 | * !Library for the DS1721 temperature sensor. |
chaegle | 1:4fd830a97574 | 46 | * The DS1721 is an I2C digital temperature sensor, with a range of -55C to +125C and a 0.125C resolution. |
chaegle | 1:4fd830a97574 | 47 | * |
chaegle | 1:4fd830a97574 | 48 | * @code |
chaegle | 1:4fd830a97574 | 49 | * #include "mbed.h" |
chaegle | 1:4fd830a97574 | 50 | * #include "DS1721.h" |
chaegle | 1:4fd830a97574 | 51 | * |
chaegle | 1:4fd830a97574 | 52 | * #define DS1721_I2C_ADDRESS (0x48<<1) |
chaegle | 1:4fd830a97574 | 53 | * |
chaegle | 1:4fd830a97574 | 54 | * int main(void) { |
chaegle | 1:4fd830a97574 | 55 | * |
chaegle | 1:4fd830a97574 | 56 | * DS1721 thermo(PTC9, PTC8, DS1721_I2C_ADDRESS); |
chaegle | 1:4fd830a97574 | 57 | * |
chaegle | 1:4fd830a97574 | 58 | * // initialize the temperature sensor |
chaegle | 1:4fd830a97574 | 59 | * thermo.startConversion(); |
chaegle | 1:4fd830a97574 | 60 | * thermo.setLowSetpoint(25); |
chaegle | 1:4fd830a97574 | 61 | * thermo.setHighSetpoint(27); |
chaegle | 1:4fd830a97574 | 62 | * thermo.setPolarity(POLARITY_ACTIVE_HIGH); |
chaegle | 1:4fd830a97574 | 63 | * |
chaegle | 1:4fd830a97574 | 64 | * while (true) { |
chaegle | 1:4fd830a97574 | 65 | * printf("Temp: %d\r\n", thermo.getTemp()); |
chaegle | 1:4fd830a97574 | 66 | * } |
chaegle | 1:4fd830a97574 | 67 | * } |
chaegle | 1:4fd830a97574 | 68 | * @endcode |
chaegle | 1:4fd830a97574 | 69 | **/ |
chaegle | 1:4fd830a97574 | 70 | class DS1721 |
chaegle | 1:4fd830a97574 | 71 | { |
chaegle | 1:4fd830a97574 | 72 | public: |
chaegle | 1:4fd830a97574 | 73 | /** |
chaegle | 1:4fd830a97574 | 74 | * DS1721 constructor |
chaegle | 1:4fd830a97574 | 75 | * |
chaegle | 1:4fd830a97574 | 76 | * @param sda SDA pin |
chaegle | 1:4fd830a97574 | 77 | * @param sdl SCL pin |
chaegle | 1:4fd830a97574 | 78 | * @param addr addr of the I2C peripheral |
chaegle | 1:4fd830a97574 | 79 | **/ |
chaegle | 1:4fd830a97574 | 80 | DS1721(PinName sda, PinName scl, int addr); |
chaegle | 1:4fd830a97574 | 81 | |
chaegle | 1:4fd830a97574 | 82 | /** |
chaegle | 1:4fd830a97574 | 83 | * DS1721 constructor |
chaegle | 1:4fd830a97574 | 84 | * |
chaegle | 1:4fd830a97574 | 85 | * @param sda SDA pin |
chaegle | 1:4fd830a97574 | 86 | * @param sdl SCL pin |
chaegle | 1:4fd830a97574 | 87 | * @param addr addr of the I2C peripheral |
chaegle | 1:4fd830a97574 | 88 | * @param resolution readout resolution of the theremometer (9, 10, 11, 12 bit) |
chaegle | 1:4fd830a97574 | 89 | * @param polarity the state on which the thermostat output is enabled |
chaegle | 1:4fd830a97574 | 90 | * @param mode the temperature conversion mode (single shot or continuous) |
chaegle | 1:4fd830a97574 | 91 | **/ |
chaegle | 1:4fd830a97574 | 92 | DS1721(PinName sda, PinName scl, int addr,int resolution, int polarity, int mode); |
chaegle | 1:4fd830a97574 | 93 | |
chaegle | 1:4fd830a97574 | 94 | /*! |
chaegle | 1:4fd830a97574 | 95 | * DS1721 destructor |
chaegle | 1:4fd830a97574 | 96 | */ |
chaegle | 1:4fd830a97574 | 97 | ~DS1721(); |
chaegle | 1:4fd830a97574 | 98 | |
chaegle | 1:4fd830a97574 | 99 | /** |
chaegle | 1:4fd830a97574 | 100 | * Reads the temperature from the DS1721 and converts it to a useable value. |
chaegle | 1:4fd830a97574 | 101 | * |
chaegle | 1:4fd830a97574 | 102 | * @returns the current temperature, as in integer |
chaegle | 1:4fd830a97574 | 103 | **/ |
chaegle | 1:4fd830a97574 | 104 | int getTemp(void); |
chaegle | 1:4fd830a97574 | 105 | |
chaegle | 1:4fd830a97574 | 106 | /** |
chaegle | 1:4fd830a97574 | 107 | * Sets the temperature polarity (POL) bit of the sensor. This bit determines |
chaegle | 1:4fd830a97574 | 108 | * upon which set point the theremostat output is active. |
chaegle | 1:4fd830a97574 | 109 | * |
chaegle | 1:4fd830a97574 | 110 | * @param int the new polarity value |
chaegle | 1:4fd830a97574 | 111 | * @returns the result of the function (0 - Failure, 1 - Success) |
chaegle | 1:4fd830a97574 | 112 | **/ |
chaegle | 1:4fd830a97574 | 113 | int setPolarity(int polarity); |
chaegle | 1:4fd830a97574 | 114 | |
chaegle | 1:4fd830a97574 | 115 | /** |
chaegle | 1:4fd830a97574 | 116 | * Gets the temperature polarity (POL) bit of the sensor. This bit determines |
chaegle | 1:4fd830a97574 | 117 | * upon which set point the theremostat output is active. |
chaegle | 1:4fd830a97574 | 118 | * |
chaegle | 1:4fd830a97574 | 119 | * @returns the value of the POL bit (0 - active low, 1 - active high) |
chaegle | 1:4fd830a97574 | 120 | **/ |
chaegle | 1:4fd830a97574 | 121 | int getPolarity(void); |
chaegle | 1:4fd830a97574 | 122 | |
chaegle | 1:4fd830a97574 | 123 | /** |
chaegle | 1:4fd830a97574 | 124 | * Initiates a temperature conversion. If the DS1721 is configured for single shot mode |
chaegle | 1:4fd830a97574 | 125 | * this function must be called prior to reading the temperature (re: getTemp()) |
chaegle | 1:4fd830a97574 | 126 | * |
chaegle | 1:4fd830a97574 | 127 | * @returns the result of the function (0 - Failure, 1 - Success) |
chaegle | 1:4fd830a97574 | 128 | **/ |
chaegle | 1:4fd830a97574 | 129 | int startConversion(void); |
chaegle | 1:4fd830a97574 | 130 | |
chaegle | 1:4fd830a97574 | 131 | /** |
chaegle | 1:4fd830a97574 | 132 | * Stops a temperature conversion. |
chaegle | 1:4fd830a97574 | 133 | * |
chaegle | 1:4fd830a97574 | 134 | * @returns the result of the function (0 - Failure, 1 - Success) |
chaegle | 1:4fd830a97574 | 135 | **/ |
chaegle | 1:4fd830a97574 | 136 | int stopConversion(void); |
chaegle | 1:4fd830a97574 | 137 | |
chaegle | 1:4fd830a97574 | 138 | /** |
chaegle | 1:4fd830a97574 | 139 | * Sets the configuration register of the DS1721. |
chaegle | 1:4fd830a97574 | 140 | * |
chaegle | 1:4fd830a97574 | 141 | * @param int the new configuration value (resolution | polarity | mode) |
chaegle | 1:4fd830a97574 | 142 | * @returns the result of the function (0 - Failure, 1 - Success) |
chaegle | 1:4fd830a97574 | 143 | **/ |
chaegle | 1:4fd830a97574 | 144 | int setConfig(int config); |
chaegle | 1:4fd830a97574 | 145 | |
chaegle | 1:4fd830a97574 | 146 | /** |
chaegle | 1:4fd830a97574 | 147 | * Retrieves the configuration register of the DS1721. |
chaegle | 1:4fd830a97574 | 148 | * |
chaegle | 1:4fd830a97574 | 149 | * @returns |
chaegle | 1:4fd830a97574 | 150 | **/ |
chaegle | 1:4fd830a97574 | 151 | int getConfig(int* config); |
chaegle | 1:4fd830a97574 | 152 | |
chaegle | 1:4fd830a97574 | 153 | /** |
chaegle | 1:4fd830a97574 | 154 | * Retrieves the configured low temperature set point value. |
chaegle | 1:4fd830a97574 | 155 | * |
chaegle | 1:4fd830a97574 | 156 | * @returns the current low temperature set point value (degrees C) |
chaegle | 1:4fd830a97574 | 157 | **/ |
chaegle | 1:4fd830a97574 | 158 | int getLowSetpoint(void); |
chaegle | 1:4fd830a97574 | 159 | |
chaegle | 1:4fd830a97574 | 160 | /** |
chaegle | 1:4fd830a97574 | 161 | * Sets the low temperature set point value. |
chaegle | 1:4fd830a97574 | 162 | * |
chaegle | 1:4fd830a97574 | 163 | * @param int the new low set point temperature (degrees C) |
chaegle | 1:4fd830a97574 | 164 | * @returns the result of the function (0 - Failure, 1 - Success) |
chaegle | 1:4fd830a97574 | 165 | **/ |
chaegle | 1:4fd830a97574 | 166 | int setLowSetpoint(int newSp); |
chaegle | 1:4fd830a97574 | 167 | |
chaegle | 1:4fd830a97574 | 168 | /** |
chaegle | 1:4fd830a97574 | 169 | * Retrieves the configured high temperature set point value. |
chaegle | 1:4fd830a97574 | 170 | * |
chaegle | 1:4fd830a97574 | 171 | * @returns the current high temperature set point value (degrees C) |
chaegle | 1:4fd830a97574 | 172 | **/ |
chaegle | 1:4fd830a97574 | 173 | int getHighSetpoint(void); |
chaegle | 1:4fd830a97574 | 174 | |
chaegle | 1:4fd830a97574 | 175 | /** |
chaegle | 1:4fd830a97574 | 176 | * Sets the high temperature set point value. |
chaegle | 1:4fd830a97574 | 177 | * |
chaegle | 1:4fd830a97574 | 178 | * @param int the new high temperature set point value (degrees C) |
chaegle | 1:4fd830a97574 | 179 | * @returns the result of the function (0 - Failure, 1 - Success) |
chaegle | 1:4fd830a97574 | 180 | **/ |
chaegle | 1:4fd830a97574 | 181 | int setHighSetpoint(int newSp); |
chaegle | 1:4fd830a97574 | 182 | |
chaegle | 1:4fd830a97574 | 183 | private: |
chaegle | 1:4fd830a97574 | 184 | I2C m_i2c; |
chaegle | 1:4fd830a97574 | 185 | int m_addr; |
chaegle | 1:4fd830a97574 | 186 | int _resolution; |
chaegle | 1:4fd830a97574 | 187 | int _polarity; |
chaegle | 1:4fd830a97574 | 188 | int _mode; |
chaegle | 1:4fd830a97574 | 189 | int _tl; |
chaegle | 1:4fd830a97574 | 190 | int _num_read; |
chaegle | 1:4fd830a97574 | 191 | int _th; |
chaegle | 1:4fd830a97574 | 192 | }; |
chaegle | 1:4fd830a97574 | 193 | |
chaegle | 1:4fd830a97574 | 194 | #endif |
chaegle | 1:4fd830a97574 | 195 |