AHT20_ATY.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * @file AHT20_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 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 functions of AHT20 for C platform
  20. *
  21. * @version
  22. * - 1_01_220804 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_220901 > ATY
  25. * -# Init tmp to zero, test AHT20_Init()
  26. * - 1_02_231229 > ATY
  27. * -# add multy addr and channel
  28. ********************************************************************************
  29. */
  30. #ifndef __AHT20_ATY_C
  31. #define __AHT20_ATY_C
  32. #include "AHT20_ATY.h"
  33. /******************************* For user *************************************/
  34. /******************************************************************************/
  35. /**
  36. * @brief AHT20 soft reset
  37. * @param channel chip channel
  38. */
  39. void AHT20_SoftReset(uint8_t channel)
  40. {
  41. uint8_t temp_uint8 = 0;
  42. I2C_WriteReg(ATH20_ADDRESS, AHT20_SOFT_RST, &temp_uint8, 0, channel);
  43. }
  44. /**
  45. * @brief read AHT20 status reg
  46. * @param channel chip channel
  47. * @return reg data
  48. */
  49. uint8_t AHT20_ReadStatusCmd(uint8_t channel)
  50. {
  51. uint8_t temp_uint8 = 0;
  52. I2C_ReadReg(ATH20_ADDRESS, AHT20_STATUS, &temp_uint8, 1, channel);
  53. return temp_uint8;
  54. }
  55. /**
  56. * @brief AHT20 chip initialization
  57. * @param channel chip channel
  58. */
  59. void AHT20_IcInitCmd(uint8_t channel)
  60. {
  61. const uint8_t temp_uint8[2] = {0x08, 0x00};
  62. // temp_uint8[0] = 0x00;
  63. // temp_uint8[1] = 0x00;
  64. // I2C_WriteReg(ATH20_ADDRESS, AHT20_NOR_MODE, temp_uint8, 2, channel);
  65. // temp_uint8[0] = 0x08;
  66. // temp_uint8[1] = 0x00;
  67. I2C_WriteReg(ATH20_ADDRESS, AHT20_INIT, temp_uint8, 2, channel);
  68. // I2C_WriteReg(ATH20_ADDRESS, 0xE1, temp_uint8, 2, channel);
  69. }
  70. /**
  71. * @brief get AHT20 busy flag
  72. * @param channel chip channel
  73. * @return busy flag: 1 - busy, 0 - leisure
  74. */
  75. uint8_t AHT20_ReadBusyCmd(uint8_t channel)
  76. {
  77. uint8_t temp_uint8 = 0;
  78. temp_uint8 = AHT20_ReadStatusCmd(channel);
  79. return (temp_uint8 >> 7) & 0x01;
  80. }
  81. /**
  82. * @brief read AHT20 calibration enable
  83. * @param channel chip channel
  84. * @return calibration state: 1 - calibrate done, 0 - not calibrated
  85. */
  86. uint8_t AHT20_ReadCalEnableCmd(uint8_t channel)
  87. {
  88. uint8_t temp_uint8 = 0;
  89. temp_uint8 = AHT20_ReadStatusCmd(channel);
  90. return (temp_uint8 >> 3) & 0x01;
  91. }
  92. /**
  93. * @brief AHT20 measure command
  94. * @param channel chip channel
  95. */
  96. void AHT20_TrigMeasure(uint8_t channel)
  97. {
  98. const uint8_t temp_uint8[2] = {0x33, 0x00};
  99. // temp_uint8[0] = 0x33;
  100. // temp_uint8[1] = 0x00;
  101. I2C_WriteReg(ATH20_ADDRESS, AHT20_TRIG_MEASURE, temp_uint8, 2, channel);
  102. }
  103. /**
  104. * @brief AHT20 measure command
  105. * @param data_t group to save origin reg data
  106. * @param channel chip channel
  107. * @todo change to Write function
  108. */
  109. void AHT20_ReadData(uint8_t* data_t, uint8_t channel)
  110. {
  111. I2C_Read(ATH20_ADDRESS, data_t, 6, channel); // todo: not tested
  112. // I2C_Start();
  113. // I2C_WriteRegByte(ATH20_ADDRESS << 1 | 1);
  114. // I2C_WaitAck(I2C_WAIT_ACK_TIME);
  115. // data_t[0] = I2C_ReadByte(1);
  116. // data_t[1] = I2C_ReadByte(1);
  117. // data_t[2] = I2C_ReadByte(1);
  118. // data_t[3] = I2C_ReadByte(1);
  119. // data_t[4] = I2C_ReadByte(1);
  120. // data_t[5] = I2C_ReadByte(0);
  121. // I2C_Stop();
  122. }
  123. /**
  124. * @brief reset AHT20 reg setting
  125. * @param regAddr reg address
  126. * @param channel chip channel
  127. * @note generally not needed
  128. */
  129. void AHT20_RegReset(uint8_t regAddr, uint8_t channel)
  130. {
  131. uint8_t temp_uint8[3] = {0};
  132. temp_uint8[0] = 0x00;
  133. temp_uint8[1] = 0x00;
  134. temp_uint8[2] = 0x00;
  135. I2C_WriteReg(ATH20_ADDRESS, regAddr, temp_uint8, 2, channel);
  136. DelayMs(5);
  137. I2C_ReadReg(ATH20_ADDRESS, AHT20_STATUS, temp_uint8, 3, channel);
  138. DelayMs(10);
  139. I2C_WriteReg(ATH20_ADDRESS, 0xB0 | regAddr, temp_uint8 + 1, 2, channel);
  140. }
  141. /**
  142. * @brief reset AHT20 reg setting
  143. * @param channel chip channel
  144. * @note generally not needed
  145. */
  146. void AHT20_RegInit(uint8_t channel)
  147. {
  148. AHT20_RegReset(0x1B, channel);
  149. AHT20_RegReset(0x1C, channel);
  150. AHT20_RegReset(0x1D, channel);
  151. }
  152. /**
  153. * @brief AHT20 all initialize
  154. * @param channel chip channel
  155. * @return errCode, 0: success, !0: error
  156. */
  157. uint8_t AHT20_Init(uint8_t channel)
  158. {
  159. uint8_t errCount = 5;
  160. while(errCount--)
  161. {
  162. DelayMs(40); // wait time after power on
  163. if((AHT20_ReadStatusCmd(channel) & 0x18) != 0x18)
  164. {
  165. AHT20_SoftReset(channel);
  166. AHT20_IcInitCmd(channel);
  167. AHT20_RegInit(channel);
  168. DelayMs(10);
  169. continue;
  170. }
  171. else
  172. break;
  173. }
  174. if(errCount == 0)
  175. return 1;
  176. errCount = 5;
  177. while(errCount--)
  178. {
  179. // check if calibrate over
  180. if(AHT20_ReadCalEnableCmd(channel) == 0)
  181. {
  182. DelayMs(10);
  183. // if not calibrated, reset to retry
  184. AHT20_IcInitCmd(channel);
  185. // AHT20_SoftReset();
  186. DelayMs(200); // time not sure
  187. }
  188. else
  189. break;
  190. }
  191. if(errCount == 0)
  192. return 2;
  193. return 0;
  194. }
  195. /*
  196. 0x1C 93 61 76 05 42
  197. TEM:25.2HUM:57.5
  198. */
  199. /**
  200. * @brief read and split data to origin hum & tem
  201. * @param ht group to save hum & tem data
  202. * @param channel chip channel
  203. * @return errCode, 0: success, !0: error
  204. */
  205. uint8_t AHT20_ReadHT(uint32_t* ht, uint8_t channel)
  206. {
  207. uint8_t errCount = 5;
  208. uint8_t temp_uint8[6] = {0};
  209. uint32_t RetuData = 0;
  210. AHT20_TrigMeasure(channel);
  211. while(errCount--)
  212. {
  213. // wait 75ms for measure finished, busy flag Bit7 set to 0
  214. DelayMs(80);
  215. AHT20_ReadData(temp_uint8, channel);
  216. if((temp_uint8[0] & 0x68) == 0x08)
  217. {
  218. // origin hum data
  219. RetuData = temp_uint8[1];
  220. RetuData = (RetuData << 8) | temp_uint8[2];
  221. RetuData = (RetuData << 8) | temp_uint8[3];
  222. RetuData = RetuData >> 4;
  223. ht[0] = RetuData;
  224. // origin tem data
  225. RetuData = temp_uint8[3] & 0x0F;
  226. RetuData = (RetuData << 8) | temp_uint8[4];
  227. RetuData = (RetuData << 8) | temp_uint8[5];
  228. ht[1] = RetuData;
  229. return 0;
  230. }
  231. }
  232. return 1;
  233. }
  234. /**
  235. * @brief calculate result from 20bit origin data, RH=%, T=C
  236. * @param ht input origin data, always bigger than 0
  237. * @param aht float data after calculate
  238. * @return errCode, 0: success, 1: out of range
  239. */
  240. uint8_t AHT20_CalcResult(uint32_t* ht, float* aht)
  241. {
  242. aht[0] = (double)ht[0] * 100 / 1024 / 1024; // hum
  243. aht[1] = (double)ht[1] * 200 / 1024 / 1024; // tem
  244. // limit data range, RH = 0~100%, Temperature = -40~85C(+50)
  245. if((aht[0] >= 0) && (aht[0] <= 100) && (aht[1] >= 10) && (aht[1] <= 135))
  246. return 0;
  247. else
  248. return 1;
  249. }
  250. /**
  251. * @brief calculate result from 20bit origin data, RH=%, T=C
  252. * @param ht data group to save th value
  253. * @param channel chip channel
  254. * @return errCode, 0: success, !0: error
  255. */
  256. uint8_t AHT20_TemHumGet(uint16_t* ht, uint8_t channel)
  257. {
  258. static uint8_t AHT20_InitFlag = 0;
  259. if(AHT20_InitFlag == 0)
  260. {
  261. if(AHT20_Init(channel) == 1)
  262. {
  263. AHT20_InitFlag = 2; // init fail
  264. }
  265. else
  266. {
  267. AHT20_InitFlag = 1;
  268. }
  269. }
  270. if(AHT20_InitFlag == 2)
  271. {
  272. #ifdef __DEBUG_AHT20_ATY
  273. printf("\r\n0xFF");
  274. #endif /* __DEBUG_AHT20_ATY */
  275. return 0xFF;
  276. }
  277. else if(AHT20_InitFlag == 1)
  278. {
  279. // Init success
  280. uint32_t tempHT_uint32[2] = {0};
  281. float tempHT_f[2] = {0};
  282. if(AHT20_ReadHT(tempHT_uint32, channel) == 0)
  283. {
  284. if((tempHT_uint32[0] && tempHT_uint32[1]) != 0)
  285. {
  286. AHT20_CalcResult(tempHT_uint32, tempHT_f);
  287. ht[0] = ((tempHT_f[0] * 1000) + 5) / 10;
  288. ht[1] = ((tempHT_f[1] * 1000) + 5) / 10;
  289. #ifdef __DEBUG_AHT20_ATY
  290. printf("\r\nHum: %.2f - Tem: %.2f", tempHT_f[0], tempHT_f[1] - 50);
  291. #endif /* __DEBUG_AHT20_ATY */
  292. }
  293. else
  294. {
  295. #ifdef __DEBUG_AHT20_ATY
  296. printf("\r\n0xFD");
  297. #endif /* __DEBUG_AHT20_ATY */
  298. return 0xFD;
  299. }
  300. }
  301. else
  302. {
  303. #ifdef __DEBUG_AHT20_ATY
  304. printf("\r\n0xFE");
  305. #endif /* __DEBUG_AHT20_ATY */
  306. return 0xFE;
  307. }
  308. }
  309. return 0;
  310. }
  311. // todo: not tested
  312. // void main()
  313. // {
  314. // SHT3X_InitBegin(addr, channel);
  315. // while(1)
  316. // {
  317. // AHT20_TemHumGet();
  318. // DelayMs(2000);
  319. // }
  320. // }
  321. #endif /* __AHT20_ATY_C */
  322. /******************************** End Of File *********************************/