Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 5 | mjames | 1 | //============================================================================== |
| 2 | // E - R A D I O N I C A . C O M, H.Kolomana 6/A, Djakovo 31400, Croatia |
||
| 3 | //============================================================================== |
||
| 4 | // Project : SHT21 Arduino Library (V1.0) |
||
| 5 | // File : SHT21.cpp |
||
| 6 | // Author : e-radionica.com 2017 |
||
| 7 | // Licence : Open-source ! |
||
| 8 | //============================================================================== |
||
| 9 | //============================================================================== |
||
| 10 | // Use with any SHT21 breakout. Check ours: |
||
| 11 | // https://e-radionica.com/en/sht21-humidity-and-temperature-sensor.html |
||
| 12 | // If any questions, |
||
| 13 | // just contact techsupport@e-radionica.com |
||
| 14 | //============================================================================== |
||
| 15 | |||
| 16 | |||
| 17 | // Modified for ChibiOS Mike James Feb 2020 |
||
| 18 | #include "ch.h" |
||
| 19 | #include <SHT21.h> |
||
| 20 | |||
| 21 | |||
| 22 | //============================================================================== |
||
| 23 | // PUBLIC |
||
| 24 | //============================================================================== |
||
| 25 | float SHT21::getHumidity(void) { |
||
| 26 | uint16_t result; // return variable |
||
| 27 | |||
| 28 | result = readSensor_hm(TRIGGER_RH_MEASUREMENT_HM); |
||
| 29 | |||
| 30 | return CalcRH(result); |
||
| 31 | } |
||
| 32 | |||
| 33 | float SHT21::getTemperature(void) { |
||
| 34 | uint16_t result; // return variable |
||
| 35 | |||
| 36 | result = readSensor_hm(TRIGGER_T_MEASUREMENT_HM); |
||
| 37 | |||
| 38 | return CalcT(result); |
||
| 39 | } |
||
| 40 | |||
| 41 | void SHT21::reset() { |
||
| 42 | |||
| 43 | |||
| 44 | const uint8_t buff = SOFT_RESET; |
||
| 45 | msg_t err = i2cMasterTransmit(&I2CD2, I2C_ADD, buff, sizeof(buff), 0,0); |
||
| 46 | if(err != MSG_OK) |
||
| 47 | |||
| 48 | |||
| 49 | chThdSleepMillisec(15); // wait for SHT to reset |
||
| 50 | } |
||
| 51 | |||
| 52 | msg_t SHT21::getSerialNumber(uint8_t * serialNumber) { |
||
| 53 | |||
| 54 | uint8_t serialNumber[8]; |
||
| 55 | |||
| 56 | // read memory location 1 |
||
| 57 | const uint8_t buff = { 0xFA,0xOF }; |
||
| 58 | |||
| 59 | uin8_r snRbuff[8]; |
||
| 60 | msg_t err = i2cMasterTransmit(&I2CD2, I2C_ADD, buff, sizeof(buff), &snRbuff, sizeof(snRbuff)); |
||
| 61 | if(err != MSG_OK) |
||
| 62 | return err; |
||
| 63 | |||
| 64 | serialNumber[5] = snRbuff[0]; // read SNB_3 |
||
| 65 | // CRC SNB_3 not used |
||
| 66 | serialNumber[4] = snrBuff[2]; // read SNB_2 |
||
| 67 | // CRC SNB_2 not used |
||
| 68 | serialNumber[3] = snrBuff[4]; // read SNB_1 |
||
| 69 | // CRC SNB_1 not used |
||
| 70 | serialNumber[2] = snrBuff[7]; // read SNB_0 |
||
| 71 | // CRC SNB_0 not used |
||
| 72 | |||
| 73 | // read memory location 2 |
||
| 74 | const uint8_t buff2 = {0xFC,0xC9}; |
||
| 75 | |||
| 76 | err = i2cMasterTransmit(&I2CD2, I2C_ADD, buff2, sizeof(buff2), &snRbuff, 6); |
||
| 77 | |||
| 78 | |||
| 79 | serialNumber[1] = snRbuff[0]; // read SNC_1 |
||
| 80 | serialNumber[0] = snRbuff[1]; // read SNC_0 |
||
| 81 | serialNumber[7] = snRbuff[3]; // read SNA_1 |
||
| 82 | serialNumber[6] = snRbuff[4]; // read SNA_0 |
||
| 83 | |||
| 84 | return serialNumber[return_sn]; |
||
| 85 | } |
||
| 86 | |||
| 87 | //============================================================================== |
||
| 88 | // PRIVATE |
||
| 89 | //============================================================================== |
||
| 90 | |||
| 91 | uint16_t SHT21::readSensor_hm(uint8_t command) { |
||
| 92 | uint8_t checksum; |
||
| 93 | uint8_t data[2]; |
||
| 94 | uint16_t result; |
||
| 95 | |||
| 96 | uint8_t rxBuff[3]; |
||
| 97 | err = i2cMasterTransmit(&I2CD2, I2C_ADD, &command, 1, &rxRbuff, 3); |
||
| 98 | if (err != MSG_OK) |
||
| 99 | |||
| 100 | data[0] = rxBuff[0]; // read data (MSB) |
||
| 101 | data[1] = rxBuff[1]; // read data (LSB) |
||
| 102 | checksum = rxBuff[2]; // read checksum |
||
| 103 | |||
| 104 | result = (data[0] << 8); |
||
| 105 | result += data[1]; |
||
| 106 | |||
| 107 | if(CRC_Checksum (data,2,checksum)) { |
||
| 108 | reset(); |
||
| 109 | return 1; |
||
| 110 | } |
||
| 111 | |||
| 112 | else return result; |
||
| 113 | } |
||
| 114 | |||
| 115 | float SHT21::CalcRH(uint16_t rh) { |
||
| 116 | |||
| 117 | rh &= ~0x0003; // clean last two bits |
||
| 118 | |||
| 119 | return (-6.0 + 125.0/65536 * (float)rh); // return relative humidity |
||
| 120 | } |
||
| 121 | |||
| 122 | float SHT21::CalcT(uint16_t t) { |
||
| 123 | |||
| 124 | t &= ~0x0003; // clean last two bits |
||
| 125 | |||
| 126 | return (-46.85 + 175.72/65536 * (float)t); |
||
| 127 | } |
||
| 128 | |||
| 129 | uint8_t SHT21::CRC_Checksum(uint8_t data[], uint8_t no_of_bytes, uint8_t checksum) { |
||
| 130 | uint8_t crc = 0; |
||
| 131 | uint8_t byteCtr; |
||
| 132 | |||
| 133 | //calculates 8-Bit checksum with given polynomial |
||
| 134 | for (byteCtr = 0; byteCtr < no_of_bytes; ++byteCtr) |
||
| 135 | { crc ^= (data[byteCtr]); |
||
| 136 | for (uint8_t bit = 8; bit > 0; --bit) |
||
| 137 | { if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL; |
||
| 138 | else crc = (crc << 1); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | if (crc != checksum) return 1; |
||
| 142 | else return 0; |
||
| 143 | } |
||
| 144 | |||
| 145 |