Rev 6 | Details | Compare with Previous | 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; |
||
6 | mjames | 45 | msg_t err = i2cMasterTransmit(m_driver, I2C_ADD, &buff, sizeof(buff), 0,0); |
5 | mjames | 46 | if(err != MSG_OK) |
47 | |||
48 | |||
6 | mjames | 49 | chThdSleepMilliseconds(15); // wait for SHT to reset |
5 | mjames | 50 | } |
51 | |||
52 | msg_t SHT21::getSerialNumber(uint8_t * serialNumber) { |
||
53 | |||
54 | |||
55 | // read memory location 1 |
||
6 | mjames | 56 | const uint8_t buff []= { 0xFA,0x0F }; |
5 | mjames | 57 | |
6 | mjames | 58 | uint8_t rxbuff[8]; |
59 | msg_t err = i2cMasterTransmit(m_driver, I2C_ADD, buff, sizeof(buff), &rxbuff[0], sizeof(rxbuff)); |
||
5 | mjames | 60 | if(err != MSG_OK) |
6 | mjames | 61 | return err; |
5 | mjames | 62 | |
6 | mjames | 63 | serialNumber[5] = rxbuff[0]; // read SNB_3 |
5 | mjames | 64 | // CRC SNB_3 not used |
6 | mjames | 65 | serialNumber[4] = rxbuff[2]; // read SNB_2 |
5 | mjames | 66 | // CRC SNB_2 not used |
6 | mjames | 67 | serialNumber[3] = rxbuff[4]; // read SNB_1 |
5 | mjames | 68 | // CRC SNB_1 not used |
6 | mjames | 69 | serialNumber[2] = rxbuff[7]; // read SNB_0 |
5 | mjames | 70 | // CRC SNB_0 not used |
71 | |||
72 | // read memory location 2 |
||
6 | mjames | 73 | const uint8_t buff2[] = {0xFC,0xC9}; |
5 | mjames | 74 | |
6 | mjames | 75 | err = i2cMasterTransmit(m_driver, I2C_ADD, buff2, sizeof(buff2), &rxbuff[0], 6); |
5 | mjames | 76 | |
77 | |||
6 | mjames | 78 | serialNumber[1] = rxbuff[0]; // read SNC_1 |
79 | serialNumber[0] = rxbuff[1]; // read SNC_0 |
||
80 | serialNumber[7] = rxbuff[3]; // read SNA_1 |
||
81 | serialNumber[6] = rxbuff[4]; // read SNA_0 |
||
5 | mjames | 82 | |
6 | mjames | 83 | return MSG_OK; |
5 | mjames | 84 | } |
85 | |||
86 | //============================================================================== |
||
87 | // PRIVATE |
||
88 | //============================================================================== |
||
89 | |||
90 | uint16_t SHT21::readSensor_hm(uint8_t command) { |
||
91 | uint8_t checksum; |
||
92 | uint8_t data[2]; |
||
93 | uint16_t result; |
||
94 | |||
95 | uint8_t rxBuff[3]; |
||
6 | mjames | 96 | |
97 | msg_t err = i2cMasterTransmit(m_driver, I2C_ADD, &command, 1, &rxBuff[0], 3); |
||
5 | mjames | 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 |