AHT20_ATY.c 7.8 KB

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