working with mbed os 5.5

Dependents:   Mbed-Connect-Cloud-Demo Mbed-Connect-Cloud-Demo mbed-cloud-connect-sensor-laser-distance2 mbed-cloud-connect-sensor-laser-distance2 ... more

Fork of Sht31 by Robert Taylor

Committer:
GeofferyOmlette
Date:
Wed Jul 20 16:51:28 2016 +0000
Revision:
0:c90aa4f69539
Child:
1:756e26f0b067
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GeofferyOmlette 0:c90aa4f69539 1 /***************************************************
GeofferyOmlette 0:c90aa4f69539 2 This is a library for the SHT31 Digital Humidity & Temp Sht31
GeofferyOmlette 0:c90aa4f69539 3
GeofferyOmlette 0:c90aa4f69539 4 Designed specifically to work with the SHT31 Digital Sht31 from Adafruit
GeofferyOmlette 0:c90aa4f69539 5 ----> https://www.adafruit.com/products/2857
GeofferyOmlette 0:c90aa4f69539 6
GeofferyOmlette 0:c90aa4f69539 7 These displays use I2C to communicate, 2 pins are required to
GeofferyOmlette 0:c90aa4f69539 8 interface
GeofferyOmlette 0:c90aa4f69539 9 Adafruit invests time and resources providing this open source code,
GeofferyOmlette 0:c90aa4f69539 10 please support Adafruit and open-source hardware by purchasing
GeofferyOmlette 0:c90aa4f69539 11 products from Adafruit!
GeofferyOmlette 0:c90aa4f69539 12
GeofferyOmlette 0:c90aa4f69539 13 Written by Limor Fried/Ladyada for Adafruit Industries.
GeofferyOmlette 0:c90aa4f69539 14 BSD license, all text above must be included in any redistribution
GeofferyOmlette 0:c90aa4f69539 15 ****************************************************/
GeofferyOmlette 0:c90aa4f69539 16
GeofferyOmlette 0:c90aa4f69539 17 #include "Sht31.h"
GeofferyOmlette 0:c90aa4f69539 18 #include "mbed.h"
GeofferyOmlette 0:c90aa4f69539 19
GeofferyOmlette 0:c90aa4f69539 20 Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) {
GeofferyOmlette 0:c90aa4f69539 21 _i2caddr = 0x44;
GeofferyOmlette 0:c90aa4f69539 22 printf("\r\n");
GeofferyOmlette 0:c90aa4f69539 23 reset();
GeofferyOmlette 0:c90aa4f69539 24 readStatus();
GeofferyOmlette 0:c90aa4f69539 25 }
GeofferyOmlette 0:c90aa4f69539 26
GeofferyOmlette 0:c90aa4f69539 27 float Sht31::readTemperature(void) {
GeofferyOmlette 0:c90aa4f69539 28 if (! readTempHum()) return NAN;
GeofferyOmlette 0:c90aa4f69539 29 return temp;
GeofferyOmlette 0:c90aa4f69539 30 }
GeofferyOmlette 0:c90aa4f69539 31
GeofferyOmlette 0:c90aa4f69539 32 float Sht31::readHumidity(void) {
GeofferyOmlette 0:c90aa4f69539 33 if (! readTempHum()) return NAN;
GeofferyOmlette 0:c90aa4f69539 34 return humidity;
GeofferyOmlette 0:c90aa4f69539 35 }
GeofferyOmlette 0:c90aa4f69539 36
GeofferyOmlette 0:c90aa4f69539 37 void Sht31::reset(void) {
GeofferyOmlette 0:c90aa4f69539 38 _i2c.start();
GeofferyOmlette 0:c90aa4f69539 39 _i2c.write(SHT31_SOFTRESET);
GeofferyOmlette 0:c90aa4f69539 40 _i2c.stop();
GeofferyOmlette 0:c90aa4f69539 41 wait_ms(10);
GeofferyOmlette 0:c90aa4f69539 42 }
GeofferyOmlette 0:c90aa4f69539 43
GeofferyOmlette 0:c90aa4f69539 44 uint16_t Sht31::readStatus(void) {
GeofferyOmlette 0:c90aa4f69539 45 writeCommand(SHT31_READSTATUS);
GeofferyOmlette 0:c90aa4f69539 46 char val[1];
GeofferyOmlette 0:c90aa4f69539 47 _i2c.start();
GeofferyOmlette 0:c90aa4f69539 48 _i2c.read(_i2caddr, val, 1);
GeofferyOmlette 0:c90aa4f69539 49 _i2c.stop();
GeofferyOmlette 0:c90aa4f69539 50 uint16_t stat = val[0];
GeofferyOmlette 0:c90aa4f69539 51 stat <<= 8;
GeofferyOmlette 0:c90aa4f69539 52 _i2c.start();
GeofferyOmlette 0:c90aa4f69539 53 _i2c.read(_i2caddr, val, 1);
GeofferyOmlette 0:c90aa4f69539 54 _i2c.stop();
GeofferyOmlette 0:c90aa4f69539 55 stat |= val[0];
GeofferyOmlette 0:c90aa4f69539 56 printf("0x%X\r\n", stat);
GeofferyOmlette 0:c90aa4f69539 57 return stat;
GeofferyOmlette 0:c90aa4f69539 58 }
GeofferyOmlette 0:c90aa4f69539 59
GeofferyOmlette 0:c90aa4f69539 60 void Sht31::writeCommand(uint16_t cmd) {
GeofferyOmlette 0:c90aa4f69539 61 char buf[2];
GeofferyOmlette 0:c90aa4f69539 62 _i2c.start();
GeofferyOmlette 0:c90aa4f69539 63 buf[0] = (cmd >> 8);
GeofferyOmlette 0:c90aa4f69539 64 buf[1] = (cmd & 0xFF);
GeofferyOmlette 0:c90aa4f69539 65 _i2c.write(_i2caddr, buf, 2);
GeofferyOmlette 0:c90aa4f69539 66 _i2c.stop();
GeofferyOmlette 0:c90aa4f69539 67 }
GeofferyOmlette 0:c90aa4f69539 68
GeofferyOmlette 0:c90aa4f69539 69
GeofferyOmlette 0:c90aa4f69539 70 bool Sht31::readTempHum(void) {
GeofferyOmlette 0:c90aa4f69539 71 char readbuffer[6];
GeofferyOmlette 0:c90aa4f69539 72
GeofferyOmlette 0:c90aa4f69539 73 writeCommand(SHT31_MEAS_HIGHREP);
GeofferyOmlette 0:c90aa4f69539 74
GeofferyOmlette 0:c90aa4f69539 75 wait_ms(500);
GeofferyOmlette 0:c90aa4f69539 76 _i2c.start();
GeofferyOmlette 0:c90aa4f69539 77 _i2c.read(_i2caddr, readbuffer, 6);
GeofferyOmlette 0:c90aa4f69539 78 _i2c.stop();
GeofferyOmlette 0:c90aa4f69539 79 for (uint8_t i = 0; i < 6; i++) {
GeofferyOmlette 0:c90aa4f69539 80 printf("0x%Xd\r\n", readbuffer[i]);
GeofferyOmlette 0:c90aa4f69539 81 }
GeofferyOmlette 0:c90aa4f69539 82 uint16_t ST, SRH;
GeofferyOmlette 0:c90aa4f69539 83 ST = readbuffer[0];
GeofferyOmlette 0:c90aa4f69539 84 ST <<= 8;
GeofferyOmlette 0:c90aa4f69539 85 ST |= readbuffer[1];
GeofferyOmlette 0:c90aa4f69539 86
GeofferyOmlette 0:c90aa4f69539 87 if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) {
GeofferyOmlette 0:c90aa4f69539 88 return false;
GeofferyOmlette 0:c90aa4f69539 89 }
GeofferyOmlette 0:c90aa4f69539 90
GeofferyOmlette 0:c90aa4f69539 91 SRH = readbuffer[3];
GeofferyOmlette 0:c90aa4f69539 92 SRH <<= 8;
GeofferyOmlette 0:c90aa4f69539 93 SRH |= readbuffer[4];
GeofferyOmlette 0:c90aa4f69539 94
GeofferyOmlette 0:c90aa4f69539 95 if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) {
GeofferyOmlette 0:c90aa4f69539 96 return false;
GeofferyOmlette 0:c90aa4f69539 97 }
GeofferyOmlette 0:c90aa4f69539 98
GeofferyOmlette 0:c90aa4f69539 99 printf("ST = %d\r\n", ST);
GeofferyOmlette 0:c90aa4f69539 100 double stemp = ST;
GeofferyOmlette 0:c90aa4f69539 101 stemp *= 175;
GeofferyOmlette 0:c90aa4f69539 102 stemp /= 0xffff;
GeofferyOmlette 0:c90aa4f69539 103 stemp = -45 + stemp;
GeofferyOmlette 0:c90aa4f69539 104 temp = stemp;
GeofferyOmlette 0:c90aa4f69539 105
GeofferyOmlette 0:c90aa4f69539 106 printf("SRH = %d\r\n", SRH);
GeofferyOmlette 0:c90aa4f69539 107 double shum = SRH;
GeofferyOmlette 0:c90aa4f69539 108 shum *= 100;
GeofferyOmlette 0:c90aa4f69539 109 shum /= 0xFFFF;
GeofferyOmlette 0:c90aa4f69539 110
GeofferyOmlette 0:c90aa4f69539 111 humidity = shum;
GeofferyOmlette 0:c90aa4f69539 112
GeofferyOmlette 0:c90aa4f69539 113 return true;
GeofferyOmlette 0:c90aa4f69539 114 }
GeofferyOmlette 0:c90aa4f69539 115
GeofferyOmlette 0:c90aa4f69539 116 uint8_t Sht31::crc8(const uint8_t *data, int len) {
GeofferyOmlette 0:c90aa4f69539 117 const uint8_t POLYNOMIAL(0x31);
GeofferyOmlette 0:c90aa4f69539 118 uint8_t crc(0xFF);
GeofferyOmlette 0:c90aa4f69539 119
GeofferyOmlette 0:c90aa4f69539 120 for ( int j = len; j; --j ) {
GeofferyOmlette 0:c90aa4f69539 121 crc ^= *data++;
GeofferyOmlette 0:c90aa4f69539 122
GeofferyOmlette 0:c90aa4f69539 123 for ( int i = 8; i; --i ) {
GeofferyOmlette 0:c90aa4f69539 124 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
GeofferyOmlette 0:c90aa4f69539 125 }
GeofferyOmlette 0:c90aa4f69539 126 }
GeofferyOmlette 0:c90aa4f69539 127 return crc;
GeofferyOmlette 0:c90aa4f69539 128 }