sensor.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "stm8s.h"
  2. #include "sensor.h"
  3. #include "i2c.h"
  4. #include "delay.h"
  5. /* Defines */
  6. #define AHT_I2C_ADDR 0x38
  7. #define CMD_GET_STATUS 0x71
  8. #define CMD_INIT 0xbe
  9. #define CMD_MEASURE 0xac
  10. #define CMD_MEASURE_CTL 0x33
  11. #define CMD_MEASURE_NOP 0x00
  12. #define CMD_SRESET 0xba
  13. #define CMD_CALIBR1 0x1b
  14. #define CMD_CALIBR2 0x1c
  15. #define CMD_CALIBR3 0x1e
  16. #define STATUS_BUSY 0x80
  17. #define STATUS_CMD 0x40
  18. #define STATUS_CYC 0x20
  19. #define STATUS_CAL 0x08
  20. #define STATUS_CALB 0x18
  21. /* Functions prototypes */
  22. static uint8_t Calc_CRC8(uint8_t *message, uint8_t Num);
  23. static aht20_st_t JH_Reset_REG(uint8_t addr);
  24. /**
  25. * Initialization
  26. */
  27. aht20_st_t AHT20_Init(void)
  28. {
  29. uint8_t buf[2];
  30. // Just powered on, it takes time for the product chip to be ready internally,
  31. // the delay is 100~500ms, and 500ms is recommended
  32. Delay(500);
  33. // When power on, send 0x71 to read the status word for the first time,
  34. // and judge whether the status word is 0x18.
  35. i2c_start();
  36. buf[0] = CMD_GET_STATUS;
  37. if (! i2c_send(AHT_I2C_ADDR, 1, buf)) {
  38. return AHT_St_Err;
  39. }
  40. /* get one byte */
  41. if (! i2c_request(AHT_I2C_ADDR, 1, buf)) {
  42. return AHT_St_Err;
  43. }
  44. if ((buf[0] & STATUS_CALB) != STATUS_CALB) {
  45. //reinitialize registers
  46. aht20_st_t rest;
  47. rest = JH_Reset_REG(0x1b);
  48. if (rest != AHT_St_OK) {
  49. return AHT_St_Err;
  50. }
  51. rest = JH_Reset_REG(0x1c);
  52. if (rest != AHT_St_OK) {
  53. return AHT_St_Err;
  54. }
  55. rest = JH_Reset_REG(0x1e);
  56. if (rest != AHT_St_OK) {
  57. return AHT_St_Err;
  58. }
  59. Delay(1);
  60. }
  61. return AHT_St_OK;
  62. }
  63. aht20_st_t AHT20_SoftReset(void)
  64. {
  65. uint8_t buf[2];
  66. i2c_start();
  67. buf[0] = CMD_SRESET;
  68. if (! i2c_send(AHT_I2C_ADDR, 1, buf)) {
  69. return AHT_St_Err;
  70. }
  71. i2c_stop();
  72. return AHT_St_OK;
  73. }
  74. aht20_st_t AHT20_StartMeasure(void)
  75. {
  76. uint8_t buf[4];
  77. i2c_start();
  78. buf[0] = CMD_MEASURE;
  79. buf[1] = CMD_MEASURE_CTL;
  80. buf[2] = CMD_MEASURE_NOP;
  81. if (! i2c_send(AHT_I2C_ADDR, 3, buf)) {
  82. return AHT_St_Err;
  83. }
  84. i2c_stop();
  85. return AHT_St_OK;
  86. }
  87. aht20_st_t AHT20_GetData(aht20_t * data)
  88. {
  89. i2c_start();
  90. /* Now read 7 bytes of data */
  91. uint8_t buf[8];
  92. if (! i2c_request(AHT_I2C_ADDR, 7, buf)) {
  93. return AHT_St_Err;
  94. }
  95. if ((buf[0] & STATUS_BUSY) != 0) {
  96. return AHT_St_Bsy;
  97. }
  98. /* Calculate values */
  99. uint32_t result;
  100. /* Humidity = Srh * 100% / 2^20 */
  101. result = buf[1];
  102. result <<= 8;
  103. result |= buf[2];
  104. result <<= 8;
  105. result |= buf[3];
  106. result >>= 4;
  107. result *= 1000;
  108. result += 524288;
  109. result /= 256;
  110. result /= 256;
  111. result /= 16;
  112. data->Humidity = (uint16_t)result;
  113. /* Temperature = St * 200 / 2^20 - 50 */
  114. result = buf[3] & 0xf;
  115. result <<= 8;
  116. result |= buf[4];
  117. result <<= 8;
  118. result |= buf[5];
  119. result *= 2000;
  120. result += 524288;
  121. result /= 256;
  122. result /= 256;
  123. result /= 16;
  124. data->Temperature = (int16_t)(result - 500);
  125. return AHT_St_OK;
  126. }
  127. /**
  128. * CRC check type: CRC8/MAXIM
  129. * Polynomial: X8+X5+X4+1
  130. * Poly: 0011 0001 0x31
  131. * When the high bit is placed in the back, it becomes 1000 1100 0x8c
  132. */
  133. static uint8_t Calc_CRC8(uint8_t *message, uint8_t Num)
  134. {
  135. uint8_t i;
  136. uint8_t byte;
  137. uint8_t crc = 0xFF;
  138. for (byte=0; byte<Num; byte++) {
  139. crc ^= (message[byte]);
  140. for (i=8; i>0; --i) {
  141. if (crc & 0x80) {
  142. crc = (crc << 1) ^ 0x31;
  143. } else {
  144. crc = (crc << 1);
  145. }
  146. }
  147. }
  148. return crc;
  149. }
  150. /* Reset register */
  151. static aht20_st_t JH_Reset_REG(uint8_t addr) {
  152. uint8_t buf[4];
  153. i2c_start();
  154. buf[0] = addr;
  155. buf[1] = 0;
  156. buf[2] = 0;
  157. if (! i2c_send(AHT_I2C_ADDR, 3, buf)) {
  158. return AHT_St_Err;
  159. }
  160. i2c_stop();
  161. Delay(5); //Delay about 5ms
  162. i2c_start();
  163. if (! i2c_request(AHT_I2C_ADDR, 3, buf)) {
  164. return AHT_St_Err;
  165. }
  166. Delay(10); //Delay about 10ms
  167. i2c_start();
  168. buf[0] = (0xB0|addr);
  169. if (! i2c_send(AHT_I2C_ADDR, 3, buf)) {
  170. return AHT_St_Err;
  171. }
  172. i2c_stop();
  173. return AHT_St_OK;
  174. }