C code and C++ library, driver software for Maxim Integrated DS1775, DS75 thermometer and thermostat temperature sensor. Code supports continuous or shut-down/standby, hysteresis, alarm limits, comparator or interrupt mode, fault filtering, and active low/high. Compact 5-pin SOT23 packaging
Dependents: DS1775_Digital_Thermostat_Temperature
Revision 9:315236fb3c6a, committed 2019-04-07
- Comitter:
- phonemacro
- Date:
- Sun Apr 07 08:59:48 2019 +0000
- Parent:
- 8:b2d4c71268aa
- Child:
- 10:03645de9c017
- Commit message:
- updated
Changed in this revision
--- a/ds1775.h Sun Apr 07 07:43:11 2019 +0000 +++ b/ds1775.h Sun Apr 07 08:59:48 2019 +0000 @@ -45,36 +45,35 @@ #define DS1775_REG_TOS_HIGH_TRIP 0X03 #define DS1775_REG_MAX 0X03 -#define DS1775_CFG_ONE_SHOT_START (1) /* Start one-shot measurement */ -#define DS1775_CFG_CONV_RATE_0_25 (0x00 << 1) /* 0.25 conversions/sec */ -#define DS1775_CFG_CONV_RATE_1 (0x01 << 1) /* 1.0 conversions/sec */ -#define DS1775_CFG_CONV_RATE_4 (0x02 << 1) /* 4.0 conversions/sec */ -#define DS1775_CFG_CONV_RATE_8 (0x03 << 1) /* 8.0 conversions/sec */ #define DS1775_WAIT_CONV_TIME_9BIT (0.1875) #define DS1775_WAIT_CONV_TIME_10BIT (0.375) #define DS1775_WAIT_CONV_TIME_11BIT (0.750) -#define DS1775_WAIT_CONV_TIME_12BIR (1.5) +#define DS1775_WAIT_CONV_TIME_12BIT (1.5) + +#define DS1775_CFG_CONTINUOUS (0X00 << 0) +#define DS1775_CFG_SHUTDOWN (0X01 << 0) + +#define DS1775_CFG_COMPARATOR_MODE (0X00 << 1) +#define DS1775_CFG_INTERRUPT_MODE (0X01 << 1) + +#define DS1775_CFG_OS_POLARITY_ACT_LOW (0x00 << 2) +#define DS1775_CFG_OS_POLARITY_ACT_HIGH (0x01 << 2) + +#define DS1775_CFG_FAULT_FILTER_1 (0x00 << 3) +#define DS1775_CFG_FAULT_FILTER_2 (0x01 << 3) +#define DS1775_CFG_FAULT_FILTER_4 (0x02 << 3) +#define DS1775_CFG_FAULT_FILTER_6 (0x03 << 3) #define DS1775_CFG_RESOLUTION_9BIT (0x00 << 5) #define DS1775_CFG_RESOLUTION_10BIT (0x01 << 5) #define DS1775_CFG_RESOLUTION_11BIT (0x02 << 5) #define DS1775_CFG_RESOLUTION_12BIT (0x03 << 5) -#define DS1775_CFG_NORMAL_FORMAT (0X00 << 7) -#define DS1775_CFG_EXTENDED_FORMAT (0X01 << 7) - -#define DS1775_CFG_CONTINUOUS (0X00 << 8) -#define DS1775_CFG_SHUTDOWN (0X01 << 8) - -#define DS1775_CFG_COMPARATOR_MODE (0X00 << 9) -#define DS1775_CFG_INTERRUPT_MODE (0X01 << 9) -#define DS1775_CFG_FAULT_FILTER_1 (0x00 << 11) -#define DS1775_CFG_FAULT_FILTER_2 (0x01 << 11) -#define DS1775_CFG_FAULT_FILTER_4 (0x02 << 11) -#define DS1775_CFG_FAULT_FILTER_6 (0x03 << 11) + + #define DS1775_CFG_OVER_TEMP_MASK (0x80)
--- a/ds1775_c.cpp Sun Apr 07 07:43:11 2019 +0000 +++ b/ds1775_c.cpp Sun Apr 07 08:59:48 2019 +0000 @@ -57,6 +57,31 @@ return DS1775_NO_ERROR; } +/******************************************************************************/ +int ds1775_read_cfg_reg(uint8_t *value, I2C &i2c_bus) +{ + int32_t ret; + char data[1] = {0}; + char reg = DS1775_REG_CONFIGURATION; + + /* write to the Register Select, true is for repeated start */ + ret = i2c_bus.write(ds1775_write_address, ®, 1, true); + if (ret == 0) { + ret = i2c_bus.read(ds1775_read_address, data, 1, false); + if (ret == 0) { + *value = data[0]; + printf("%s: cfg %x\r\n", __func__, *value); + return DS1775_NO_ERROR; + } else { + printf("%s: failed to read data: ret: %d\r\n", __func__, ret); + } + } else { + printf("%s: failed to write to Register Select: ret: %d\r\n", + __func__, ret); + } + return DS1775_ERROR; +} + /******************************************************************************/ int ds1775_read_reg(uint16_t *value, char reg, I2C &i2c_bus) @@ -66,7 +91,7 @@ ds1775_raw_data tmp; if (reg <= DS1775_REG_MAX) { - /* write to the Register Select */ + /* write to the Register Select, true is for repeated start */ ret = i2c_bus.write(ds1775_write_address, ®, 1, true); /* read the two bytes of data */ if (ret == 0) { @@ -131,11 +156,6 @@ cmd[2] = tmp.lsb; ret = i2c_bus.write(ds1775_write_address, cmd, 3, false); if (ret == 0) { - if (DS1775_REG_CONFIGURATION == reg) { - ds1775_extended_format = 0; - if (tmp.uwrd & DS1775_CFG_EXTENDED_FORMAT) - ds1775_extended_format = 1; - } return DS1775_NO_ERROR; } else { printf("%s: I2C write error %d\r\n",__func__, ret); @@ -147,8 +167,29 @@ } } +/******************************************************************************/ +int ds1775_write_reg_one_byte(uint8_t value, char reg, I2C &i2c_bus) +{ + int32_t ret; + char cmd[2]; -int ds1775_write_cfg(uint16_t cfg, I2C &i2c_bus) + if (reg == DS1775_REG_CONFIGURATION) { + cmd[0] = reg; + cmd[1] = value; + ret = i2c_bus.write(ds1775_write_address, cmd, 2, false); + if (ret == 0) { + return DS1775_NO_ERROR; + } else { + printf("%s: I2C write error %d\r\n",__func__, ret); + return DS1775_ERROR; + } + } else { + printf("%s: register value invalid %x\r\n",__func__, reg); + return DS1775_ERROR; + } +} + +int ds1775_write_cfg(uint8_t cfg, I2C &i2c_bus) { return ds1775_write_reg(cfg, DS1775_REG_CONFIGURATION, i2c_bus); }
--- a/ds1775_c.h Sun Apr 07 07:43:11 2019 +0000 +++ b/ds1775_c.h Sun Apr 07 08:59:48 2019 +0000 @@ -41,12 +41,16 @@ int ds1775_init(uint8_t slaveAddress); +int ds1775_read_cfg_reg(uint8_t *value, I2C &i2c_bus); + int ds1775_read_reg(uint16_t *value, char reg, I2C &i2c_bus); float ds1775_read_reg_as_temperature(uint8_t reg, I2C &i2c_bus); int ds1775_write_reg(uint16_t value, char reg, I2C &i2c_bus); +int ds1775_write_reg_one_byte(uint8_t value, char reg, I2C &i2c_bus); + int ds1775_write_cfg(uint16_t cfg, I2C &i2c_bus); int ds1775_write_trip_low(float temperature, I2C &i2c_bus);
--- a/ds1775_cpp.cpp Sun Apr 07 07:43:11 2019 +0000 +++ b/ds1775_cpp.cpp Sun Apr 07 08:59:48 2019 +0000 @@ -56,6 +56,32 @@ } /******************************************************************************/ +int DS1775::read_cfg_reg(uint8_t *value) +{ + int32_t ret; + char data[1] = {0}; + char reg = DS1775_REG_CONFIGURATION; + + /* write to the Register Select, true is for repeated start */ + ret = m_i2c.write(m_write_address, ®, 1, true); + if (ret == 0) { + ret = m_i2c.read(m_read_address, data, 1, false); + if (ret == 0) { + *value = data[0]; + printf("%s: cfg %x\r\n", __func__, *value); + return DS1775_NO_ERROR; + } else { + printf( + "%s: failed to read data: ret: %d\r\n", __func__, ret); + } + } else { + printf("%s: failed to write to Register Select: ret: %d\r\n", + __func__, ret); + } + return DS1775_ERROR; +} + +/******************************************************************************/ int DS1775::read_reg(uint16_t *value, char reg) { int32_t ret; @@ -63,7 +89,7 @@ ds1775_raw_data tmp; if (reg <= DS1775_REG_MAX) { - /* write to the Register Select */ + /* write to the Register Select, true is for repeated start */ ret = m_i2c.write(m_write_address, ®, 1, true); /* read the two bytes of data */ if (ret == 0) { @@ -71,6 +97,8 @@ if (ret == 0) { tmp.msb = data[0]; tmp.lsb = data[1]; + printf( + "%s: MSB LSB %x %x\r\n", __func__, tmp.msb, tmp.lsb); *value = tmp.uwrd; return DS1775_NO_ERROR; } else { @@ -125,11 +153,29 @@ cmd[2] = tmp.lsb; ret = m_i2c.write(m_write_address, cmd, 3, false); if (ret == 0) { - if (DS1775_REG_CONFIGURATION == reg) { - m_extended_format = 0; - if (tmp.uwrd & DS1775_CFG_EXTENDED_FORMAT) - m_extended_format = 1; - } + return DS1775_NO_ERROR; + } else { + printf("%s: I2C write error %d\r\n",__func__, ret); + return DS1775_ERROR; + } + } else { + printf("%s: register value invalid %x\r\n",__func__, reg); + return DS1775_ERROR; + } +} + +/******************************************************************************/ +int DS1775::write_reg_one_byte(uint8_t value, char reg) +{ + int32_t ret; + char cmd[2]; + ds1775_raw_data tmp; + + if (reg == DS1775_REG_CONFIGURATION) { + cmd[0] = reg; + cmd[1] = value; + ret = m_i2c.write(m_write_address, cmd, 2, false); + if (ret == 0) { return DS1775_NO_ERROR; } else { printf("%s: I2C write error %d\r\n",__func__, ret); @@ -144,7 +190,7 @@ /******************************************************************************/ int DS1775::write_cfg(uint16_t cfg) { - return write_reg(cfg, DS1775_REG_CONFIGURATION); + return write_reg_one_byte(cfg, DS1775_REG_CONFIGURATION); } /******************************************************************************/
--- a/ds1775_cpp.h Sun Apr 07 07:43:11 2019 +0000 +++ b/ds1775_cpp.h Sun Apr 07 08:59:48 2019 +0000 @@ -114,6 +114,13 @@ /** * @brief Read register of device at slave address * @param[out] value - Read data on success + * @return 0 on success, negative number on failure + */ + int read_cfg_reg(uint8_t *value); + + /** + * @brief Read register of device at slave address + * @param[out] value - Read data on success * @param reg - Register address * @return 0 on success, negative number on failure */ @@ -163,6 +170,13 @@ */ int write_reg(uint16_t value, char reg); + /** + * @brief Write a value to a register + * @param value - value to write to the register + * @param reg - register address + * @return 0 on success, negative number on failure + */ + int write_reg_one_byte(uint8_t value, char reg); private: /** @var m_i2c * @brief I2C object