hello

Dependents:   sht31_II ina-hack-test rapidtouchscreenprototype EMBEDDED_SYSTEM_PROJECT_GROUP_9 ... more

Files at this revision

API Documentation at this revision

Comitter:
GeofferyOmlette
Date:
Wed Jul 20 16:51:28 2016 +0000
Child:
1:756e26f0b067
Commit message:
hello

Changed in this revision

Sht31.cpp Show annotated file Show diff for this revision Revisions of this file
Sht31.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sht31.cpp	Wed Jul 20 16:51:28 2016 +0000
@@ -0,0 +1,128 @@
+/***************************************************
+  This is a library for the SHT31 Digital Humidity & Temp Sht31
+
+  Designed specifically to work with the SHT31 Digital Sht31 from Adafruit
+  ----> https://www.adafruit.com/products/2857
+
+  These displays use I2C to communicate, 2 pins are required to
+  interface
+  Adafruit invests time and resources providing this open source code,
+  please support Adafruit and open-source hardware by purchasing
+  products from Adafruit!
+
+  Written by Limor Fried/Ladyada for Adafruit Industries.
+  BSD license, all text above must be included in any redistribution
+ ****************************************************/
+
+#include "Sht31.h"
+#include "mbed.h"
+
+Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) {
+    _i2caddr = 0x44;
+     printf("\r\n");
+    reset();
+    readStatus();
+}
+
+float Sht31::readTemperature(void) {
+    if (! readTempHum()) return NAN;
+    return temp;
+}
+
+float Sht31::readHumidity(void) {
+    if (! readTempHum()) return NAN;
+    return humidity;
+}
+
+void Sht31::reset(void) {
+    _i2c.start();
+    _i2c.write(SHT31_SOFTRESET);
+    _i2c.stop();
+    wait_ms(10);
+}
+
+uint16_t Sht31::readStatus(void) {
+    writeCommand(SHT31_READSTATUS);
+    char val[1];
+    _i2c.start();
+    _i2c.read(_i2caddr, val, 1);
+    _i2c.stop();
+    uint16_t stat = val[0];
+    stat <<= 8;
+    _i2c.start();
+    _i2c.read(_i2caddr, val, 1);
+    _i2c.stop();
+    stat |= val[0];
+    printf("0x%X\r\n", stat);
+    return stat;
+}
+
+void Sht31::writeCommand(uint16_t cmd) {
+    char buf[2];
+    _i2c.start();
+    buf[0] = (cmd >> 8);
+    buf[1] = (cmd & 0xFF);
+    _i2c.write(_i2caddr, buf, 2);
+    _i2c.stop();
+}
+
+
+bool Sht31::readTempHum(void) {
+    char readbuffer[6];
+
+    writeCommand(SHT31_MEAS_HIGHREP);
+
+    wait_ms(500);
+    _i2c.start();
+    _i2c.read(_i2caddr, readbuffer, 6);
+    _i2c.stop();
+     for (uint8_t i = 0; i < 6; i++) {
+         printf("0x%Xd\r\n", readbuffer[i]);
+     }
+    uint16_t ST, SRH;
+    ST = readbuffer[0];
+    ST <<= 8;
+    ST |= readbuffer[1];
+
+    if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) {
+        return false;
+    }
+
+    SRH = readbuffer[3];
+    SRH <<= 8;
+    SRH |= readbuffer[4];
+
+    if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) {
+        return false;
+    }
+
+    printf("ST = %d\r\n", ST);
+    double stemp = ST;
+    stemp *= 175;
+    stemp /= 0xffff;
+    stemp = -45 + stemp;
+    temp = stemp;
+
+    printf("SRH = %d\r\n", SRH);
+    double shum = SRH;
+    shum *= 100;
+    shum /= 0xFFFF;
+
+    humidity = shum;
+
+    return true;
+}
+
+uint8_t Sht31::crc8(const uint8_t *data, int len) {
+    const uint8_t POLYNOMIAL(0x31);
+    uint8_t crc(0xFF);
+
+    for ( int j = len; j; --j ) {
+        crc ^= *data++;
+
+        for ( int i = 8; i; --i ) {
+            crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
+        }
+    }
+    return crc;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sht31.h	Wed Jul 20 16:51:28 2016 +0000
@@ -0,0 +1,53 @@
+/***************************************************
+  This is a library for the SHT31 Digital Humidity & Temp Sht31
+
+  Designed specifically to work with the SHT31 Digital Sht31 from Adafruit
+  ----> https://www.adafruit.com/products/2857
+
+  These displays use I2C to communicate, 2 pins are required to
+  interface
+  Adafruit invests time and resources providing this open source code,
+  please support Adafruit and open-source hardware by purchasing
+  products from Adafruit!
+
+  Written by Limor Fried/Ladyada for Adafruit Industries.
+  BSD license, all text above must be included in any redistribution
+ ****************************************************/
+
+#ifndef Sht31_H
+#define Sht31_H
+
+#include "mbed.h"
+
+#define SHT31_DEFAULT_ADDR          0x44
+#define SHT31_MEAS_HIGHREP_STRETCH  0x2C06
+#define SHT31_MEAS_MEDREP_STRETCH   0x2C0D
+#define SHT31_MEAS_LOWREP_STRETCH   0x2C10
+#define SHT31_MEAS_HIGHREP          0x2400
+#define SHT31_MEAS_MEDREP           0x240B
+#define SHT31_MEAS_LOWREP           0x2416
+#define SHT31_READSTATUS            0xF32D
+#define SHT31_CLEARSTATUS           0x3041
+#define SHT31_SOFTRESET             0x30A2
+#define SHT31_HEATEREN              0x306D
+#define SHT31_HEATERDIS             0x3066
+
+class Sht31 {
+public:
+    Sht31(PinName sda, PinName scl);
+    float readTemperature(void);
+    float readHumidity(void);
+
+private:
+    void reset(void);
+    uint16_t readStatus(void);
+    void writeCommand(uint16_t cmd);
+    bool readTempHum(void);
+    uint8_t crc8(const uint8_t *data, int len);
+
+    I2C _i2c;
+    int _i2caddr;
+    float humidity, temp;
+};
+
+#endif