diff --git a/keywords.txt b/keywords.txt index 7704c70..9381235 100644 --- a/keywords.txt +++ b/keywords.txt @@ -18,9 +18,16 @@ end KEYWORD2 readTemperature KEYWORD2 readHumidity KEYWORD2 +enableDataReady KEYWORD2 +disableDataReady KEYWORD2 +setOpenDrain KEYWORD2 +setPushPull KEYWORD2 +setActiveHigh KEYWORD2 +setActiveLow KEYWORD2 + ######################################### # Constants ######################################### -FAHRENHEIT LITERAL1 +FAHRENHEIT LITERAL1 CELSIUS LITERAL1 diff --git a/src/HTS.cpp b/src/HTS.cpp index 80a11f0..827875f 100644 --- a/src/HTS.cpp +++ b/src/HTS.cpp @@ -26,6 +26,7 @@ #define HTS221_WHO_AM_I_REG 0x0f #define HTS221_CTRL1_REG 0x20 #define HTS221_CTRL2_REG 0x21 +#define HTS221_CTRL3_REG 0x22 //Dara Ready (b7 0 active high)(open drain, b6 1)(b2, 1 enable data ready) #define HTS221_STATUS_REG 0x27 #define HTS221_HUMIDITY_OUT_L_REG 0x28 #define HTS221_TEMP_OUT_L_REG 0x2a @@ -39,6 +40,7 @@ #define HTS221_T0_OUT_REG 0x3c #define HTS221_T1_OUT_REG 0x3e + HTS221Class::HTS221Class(TwoWire& wire) : _wire(&wire) { @@ -56,8 +58,12 @@ int HTS221Class::begin() readHTS221Calibration(); - // turn on the HTS221 and enable Block Data Update - i2cWrite(HTS221_CTRL1_REG, 0x84); + // enable HTS221 + i2cWrite(HTS221_CTRL1_REG, 0x80); + + // Default DREADY configuration + disableDataReady(); + setPushPull(); return 1; } @@ -70,9 +76,39 @@ void HTS221Class::end() _wire->end(); } +void HTS221Class::enableDataReady(){ + uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b11111000; + i2cWrite(HTS221_CTRL3_REG, data | 0b1 << 2); +} + +void HTS221Class::disableDataReady(){ + uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b11111000; + i2cWrite(HTS221_CTRL3_REG, data | 0b0 << 2); +} + +void HTS221Class::setOpenDrain(){ + uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b10111100; + i2cWrite(HTS221_CTRL3_REG, data | 0b1 << 6); +} + +void HTS221Class::setPushPull(){ + uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b10111100; + i2cWrite(HTS221_CTRL3_REG, data); +} + +void HTS221Class::setActiveHigh(){ + uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b01111111; + i2cWrite(HTS221_CTRL3_REG, data); +} + +void HTS221Class::setActiveLow(){ + uint8_t data = i2cRead(HTS221_CTRL3_REG) & 0b01111111; + i2cWrite(HTS221_CTRL3_REG, data | 0b1 << 7); +} + float HTS221Class::readTemperature(int units) { - // Wait for ONE_SHOT bit to be cleared by the hardware + // Wait for ONE_SHOT bit to be cleared by the hardware while (i2cRead(HTS221_CTRL2_REG) & 0x01); // trigger one shot diff --git a/src/HTS.h b/src/HTS.h index af7c2ee..e0a8868 100644 --- a/src/HTS.h +++ b/src/HTS.h @@ -22,7 +22,7 @@ #include #include - +#define HTS221_CTRL3_REG 0x22 enum { FAHRENHEIT, CELSIUS @@ -38,6 +38,13 @@ class HTS221Class { float readTemperature(int units = CELSIUS); float readHumidity(); + void enableDataReady(); //Outputs pin3 DREADY + void disableDataReady(); + void setOpenDrain(); + void setPushPull(); + void setActiveHigh(); + void setActiveLow(); + private: int i2cRead(uint8_t reg); int i2cRead16(uint8_t reg) {