SHT3X_ATY.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. * @file SHT3X_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of SHT3X tem&hum transistor for all devices
  20. *
  21. * @version
  22. * - 1_01_200318 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __SHT3X_ATY_C
  27. #define __SHT3X_ATY_C
  28. #include "SHT3X_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. float temperatureSHT3X = 0.0;
  32. float humiditySHT3X = 0.0;
  33. /* 0: 0-65T, 10-90H, Best accuracy
  34. 1: -40-0/65-120T, 0-10/90-100H, not good accuracy, warning
  35. 2: out of limit data, error
  36. */
  37. uint8_t warningFlag = 0;
  38. /**
  39. * @brief calc crc with SHT3X data
  40. * @param sht3xData data to calc
  41. * @param nbrOfBytes length of data to calc
  42. * @return uint8_t crc value
  43. */
  44. uint8_t SHT3X_CalcCrc(uint8_t* sht3xData, uint8_t nbrOfBytes)
  45. {
  46. uint8_t bit_t; // bit mask
  47. uint8_t crc = 0xFF; // calculated checksum
  48. uint8_t byteCtr; // byte counter
  49. // calculates 8-Bit checksum with given polynomial
  50. for(byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
  51. {
  52. crc ^= sht3xData[byteCtr];
  53. for(bit_t = 8; bit_t > 0; --bit_t)
  54. {
  55. if(crc & 0x80)
  56. crc = (crc << 1) ^ POLYNOMIAL;
  57. else
  58. crc = (crc << 1);
  59. }
  60. }
  61. return crc;
  62. }
  63. /**
  64. * @brief check crc with SHT3X data
  65. * @param sht3xData data to check
  66. * @param nbrOfBytes length of data to check
  67. * @param checksum compare value with calculated crc
  68. * @return !0: wrong, 0: success
  69. */
  70. uint8_t SHT3X_CheckCrc(uint8_t* sht3xData, uint8_t nbrOfBytes, uint8_t checksum)
  71. {
  72. // calculates 8-Bit checksum
  73. uint8_t crc = SHT3X_CalcCrc(sht3xData, nbrOfBytes);
  74. if(crc != checksum)
  75. return 1;
  76. else
  77. return 0;
  78. }
  79. /**
  80. * @brief write command to SHT3X
  81. * @param cmd command to send
  82. */
  83. void SHT3X_WriteCommand(sht3xCommands cmd)
  84. {
  85. uint8_t cmd_t[2];
  86. cmd_t[0] = cmd >> 8;
  87. cmd_t[1] = cmd & 0xFF;
  88. I2C_Write(SHT3X_ADDR, cmd_t, 2);
  89. }
  90. /**
  91. * @brief start periodic measurment
  92. * @param repeatability repeatability
  93. * @param frequency frequency
  94. * @note use depending on the required repeatability and frequency,
  95. * the corresponding command
  96. */
  97. void SHT3X_StartPeriodicMeasurment(sht3xRepeatability repeatability, sht3xFrequency frequency)
  98. {
  99. switch(repeatability)
  100. {
  101. case REPEATAB_LOW: // low repeatability
  102. switch(frequency)
  103. {
  104. case FREQUENCY_HZ5: // low repeatability, 0.5 Hz
  105. SHT3X_WriteCommand(CMD_MEAS_PERI_05_L);
  106. break;
  107. case FREQUENCY_1HZ: // low repeatability, 1.0 Hz
  108. SHT3X_WriteCommand(CMD_MEAS_PERI_1_L);
  109. break;
  110. case FREQUENCY_2HZ: // low repeatability, 2.0 Hz
  111. SHT3X_WriteCommand(CMD_MEAS_PERI_2_L);
  112. break;
  113. case FREQUENCY_4HZ: // low repeatability, 4.0 Hz
  114. SHT3X_WriteCommand(CMD_MEAS_PERI_4_L);
  115. break;
  116. case FREQUENCY_10HZ: // low repeatability, 10.0 Hz
  117. SHT3X_WriteCommand(CMD_MEAS_PERI_10_L);
  118. break;
  119. default:
  120. break;
  121. }
  122. break;
  123. case REPEATAB_MEDIUM: // medium repeatability
  124. switch(frequency)
  125. {
  126. case FREQUENCY_HZ5: // medium repeatability, 0.5 Hz
  127. SHT3X_WriteCommand(CMD_MEAS_PERI_05_M);
  128. break;
  129. case FREQUENCY_1HZ: // medium repeatability, 1.0 Hz
  130. SHT3X_WriteCommand(CMD_MEAS_PERI_1_M);
  131. break;
  132. case FREQUENCY_2HZ: // medium repeatability, 2.0 Hz
  133. SHT3X_WriteCommand(CMD_MEAS_PERI_2_M);
  134. break;
  135. case FREQUENCY_4HZ: // medium repeatability, 4.0 Hz
  136. SHT3X_WriteCommand(CMD_MEAS_PERI_4_M);
  137. break;
  138. case FREQUENCY_10HZ: // medium repeatability, 10.0 Hz
  139. SHT3X_WriteCommand(CMD_MEAS_PERI_10_M);
  140. break;
  141. default:
  142. break;
  143. }
  144. break;
  145. case REPEATAB_HIGH: // high repeatability
  146. switch(frequency)
  147. {
  148. case FREQUENCY_HZ5: // high repeatability, 0.5 Hz
  149. SHT3X_WriteCommand(CMD_MEAS_PERI_05_H);
  150. break;
  151. case FREQUENCY_1HZ: // high repeatability, 1.0 Hz
  152. SHT3X_WriteCommand(CMD_MEAS_PERI_1_H);
  153. break;
  154. case FREQUENCY_2HZ: // high repeatability, 2.0 Hz
  155. SHT3X_WriteCommand(CMD_MEAS_PERI_2_H);
  156. break;
  157. case FREQUENCY_4HZ: // high repeatability, 4.0 Hz
  158. SHT3X_WriteCommand(CMD_MEAS_PERI_4_H);
  159. break;
  160. case FREQUENCY_10HZ: // high repeatability, 10.0 Hz
  161. SHT3X_WriteCommand(CMD_MEAS_PERI_10_H);
  162. break;
  163. default:
  164. break;
  165. }
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. /**
  172. * @brief calc real value from 16 bit data
  173. * @param temperature temperature 16 bit origin value
  174. * @param humidity humidity 16 bit origin value
  175. */
  176. void SHT3X_ValuePorcess(uint16_t* temperature, uint16_t* humidity)
  177. {
  178. temperatureSHT3X = -45 + 175 * (1.0 * (*temperature) / 65535);
  179. humiditySHT3X = 100 * (1.0 * (*humidity) / 65535);
  180. warningFlag = 0;
  181. if(temperatureSHT3X < 0 || temperatureSHT3X>65)
  182. warningFlag = 1;
  183. else if(temperatureSHT3X < -40)
  184. {
  185. warningFlag = 2;
  186. temperatureSHT3X = -40;
  187. }
  188. else if(temperatureSHT3X > 120)
  189. {
  190. warningFlag = 2;
  191. temperatureSHT3X = 120;
  192. }
  193. if(humiditySHT3X < 10 || humiditySHT3X>90)
  194. warningFlag = 1;
  195. else if(humiditySHT3X < 0)
  196. {
  197. warningFlag = 2;
  198. humiditySHT3X = 0;
  199. }
  200. else if(humiditySHT3X > 100)
  201. {
  202. warningFlag = 2;
  203. humiditySHT3X = 100;
  204. }
  205. }
  206. /**
  207. * @brief read all data from SHT3X
  208. * @param temperature temperature 16 bit origin value
  209. * @param humidity humidity 16 bit origin value
  210. */
  211. void SHT3X_ReadDataAndCrc(uint16_t* temperature, uint16_t* humidity)
  212. {
  213. uint8_t bytes[6]; // read 2 data array and 1 checksum byte
  214. // read two data bytes and one checksum byte
  215. I2C_Read(SHT3X_ADDR, bytes, 6);
  216. // verify checksum then combine the two bytes to a 16-bit value
  217. if(!SHT3X_CheckCrc(bytes, 2, bytes[2]))
  218. *temperature = (bytes[0] << 8) | bytes[1];
  219. // else
  220. // *temperature = 0;
  221. if(!SHT3X_CheckCrc(bytes + 3, 2, bytes[5]))
  222. *humidity = (bytes[3] << 8) | bytes[4];
  223. // else
  224. // *humidity = 0;
  225. SHT3X_ValuePorcess(temperature, humidity);
  226. }
  227. /**
  228. * @brief read data and calc real value
  229. */
  230. void SHT3X_ReadMeasurementBuffer(void)
  231. {
  232. uint16_t hexValueTemp = 0;
  233. uint16_t hexValueHumi = 0;
  234. SHT3X_WriteCommand(CMD_FETCH_DATA);
  235. SHT3X_ReadDataAndCrc(&hexValueTemp, &hexValueHumi);
  236. }
  237. /**
  238. * @brief start measurement in clock stretching mode
  239. * @param repeatability repeatability
  240. * @note use depending on the required repeatability, the corresponding command
  241. */
  242. void SHT3X_GetTempAndHumiClkStretch(sht3xRepeatability repeatability)
  243. {
  244. uint16_t hexTempValue = 0;
  245. uint16_t hexHumiValue = 0;
  246. switch(repeatability)
  247. {
  248. case REPEATAB_LOW:
  249. SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_L);
  250. break;
  251. case REPEATAB_MEDIUM:
  252. SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_M);
  253. break;
  254. case REPEATAB_HIGH:
  255. SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_H);
  256. break;
  257. default:
  258. break;
  259. }
  260. SHT3X_ReadDataAndCrc(&hexTempValue, &hexHumiValue);
  261. }
  262. /**
  263. * @brief init SHT3X, check device
  264. * @note put at main init
  265. */
  266. void SHT3X_InitBegin(void)
  267. {
  268. uint8_t i = 0;
  269. SHT3X_StartPeriodicMeasurment(REPEATAB_HIGH, FREQUENCY_10HZ);
  270. for(i = 0; i < 9; i++)
  271. {
  272. DelayMs(110);
  273. SHT3X_ReadMeasurementBuffer();
  274. // SHT3X_GetTempAndHumiClkStretch(REPEATAB_LOW);
  275. }
  276. // SHT3X_StartPeriodicMeasurment(REPEATAB_LOW, FREQUENCY_HZ1);
  277. SHT3X_StartPeriodicMeasurment(REPEATAB_HIGH, FREQUENCY_10HZ);
  278. }
  279. // void main()
  280. // {
  281. // SHT3X_InitBegin();
  282. // while(1)
  283. // {
  284. // SHT3X_ReadMeasurementBuffer();
  285. // UartSendStr("\r\nHT: ");
  286. // UartSendStr(TemperatureStr);
  287. // UartSendStr(" - ");
  288. // UartSendStr(HumidityStr);
  289. // DelayMs(2000);
  290. // }
  291. // }
  292. // // declare 5byte group, last with '\0' for show
  293. // uint8_t Temperature[5] = {0, 0, '.', 0};
  294. // uint8_t Humidity[5] = {0, 0, '.', 0};
  295. // uint8_t TemperatureStr[5] = {0, 0, '.', 0};
  296. // uint8_t HumidityStr[5] = {0, 0, '.', 0};
  297. // uint8_t temperatureHexI = 0; // integer
  298. // uint8_t temperatureHexD = 0; // decimal
  299. // uint8_t humidityHexI = 0;
  300. // uint8_t humidityHexD = 0;
  301. // temperature = ((uint8_t)decTempValue << 8) + ((uint16_t)(decTempValue * 100) % 100);
  302. // humidity = ((uint8_t)decHumiValue << 8) + ((uint16_t)(decHumiValue * 100) % 100);
  303. // temperatureHexI = temperature >> 8;
  304. // temperatureHexD = temperature;
  305. // humidityHexI = humidity >> 8;
  306. // humidityHexD = humidity;
  307. // // todo higher than 100 or below 0
  308. // // todo only get origin data, do not change type here
  309. // // todo AHT20 deal the same, and change I2C function to standard
  310. // // if()
  311. // Temperature[0] = temperatureHexI / 10;
  312. // Temperature[1] = temperatureHexI % 10;
  313. // Temperature[3] = temperatureHexD / 10;
  314. // Humidity[0] = humidityHexI / 10;
  315. // Humidity[1] = humidityHexI % 10;
  316. // Humidity[3] = humidityHexD / 10;
  317. // TemperatureStr[0] = Temperature[0] + '0';
  318. // TemperatureStr[1] = Temperature[1] + '0';
  319. // TemperatureStr[3] = Temperature[3] + '0';
  320. // HumidityStr[0] = Humidity[0] + '0';
  321. // HumidityStr[1] = Humidity[1] + '0';
  322. // HumidityStr[3] = Humidity[3] + '0';
  323. #endif /* __SHT3X_ATY_C */
  324. /******************************** End Of File *********************************/