DHT20.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "DHT20.h"
  16. #include "../interfaces/I2CSensor.h"
  17. const SensorType DHT20 = {
  18. .typename = "DHT20",
  19. .altname = "DHT20/AM2108/AHT20",
  20. .interface = &I2C,
  21. .datatype = UT_TEMPERATURE | UT_HUMIDITY,
  22. .pollingInterval = 2000,
  23. .allocator = unitemp_DHT20_I2C_alloc,
  24. .mem_releaser = unitemp_DHT20_I2C_free,
  25. .initializer = unitemp_DHT20_init,
  26. .deinitializer = unitemp_DHT20_I2C_deinit,
  27. .updater = unitemp_DHT20_I2C_update};
  28. static uint8_t DHT20_get_status(I2CSensor* i2c_sensor) {
  29. uint8_t status[1] = {0};
  30. unitemp_i2c_readArray(i2c_sensor, 1, status);
  31. return status[0];
  32. }
  33. static uint8_t DHT20_calc_CRC8(uint8_t* message, uint8_t Num) {
  34. uint8_t i;
  35. uint8_t byte;
  36. uint8_t crc = 0xFF;
  37. for(byte = 0; byte < Num; byte++) {
  38. crc ^= (message[byte]);
  39. for(i = 8; i > 0; --i) {
  40. if(crc & 0x80)
  41. crc = (crc << 1) ^ 0x31;
  42. else
  43. crc = (crc << 1);
  44. }
  45. }
  46. return crc;
  47. }
  48. static void DHT20_reset_reg(I2CSensor* i2c_sensor, uint8_t addr) {
  49. uint8_t data[3] = {addr, 0x00, 0x00};
  50. unitemp_i2c_writeArray(i2c_sensor, 3, data);
  51. furi_delay_ms(5);
  52. unitemp_i2c_readArray(i2c_sensor, 3, data);
  53. furi_delay_ms(10);
  54. data[0] = 0xB0 | addr;
  55. unitemp_i2c_writeArray(i2c_sensor, 3, data);
  56. }
  57. bool unitemp_DHT20_I2C_alloc(Sensor* sensor, char* args) {
  58. UNUSED(args);
  59. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  60. //Адреса на шине I2C (7 бит)
  61. i2c_sensor->minI2CAdr = 0x38 << 1;
  62. i2c_sensor->maxI2CAdr = 0x38 << 1;
  63. return true;
  64. }
  65. bool unitemp_DHT20_I2C_free(Sensor* sensor) {
  66. //Нечего высвобождать, так как ничего не было выделено
  67. UNUSED(sensor);
  68. return true;
  69. }
  70. bool unitemp_DHT20_init(Sensor* sensor) {
  71. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  72. uint8_t data[3] = {0xA8, 0x00, 0x00};
  73. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return false;
  74. furi_delay_ms(10);
  75. data[0] = 0xBE;
  76. data[1] = 0x08;
  77. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return false;
  78. furi_delay_ms(10);
  79. return true;
  80. }
  81. bool unitemp_DHT20_I2C_deinit(Sensor* sensor) {
  82. //Нечего деинициализировать
  83. UNUSED(sensor);
  84. return true;
  85. }
  86. UnitempStatus unitemp_DHT20_I2C_update(Sensor* sensor) {
  87. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  88. if(DHT20_get_status(i2c_sensor) != 0x18) {
  89. DHT20_reset_reg(i2c_sensor, 0x1B);
  90. DHT20_reset_reg(i2c_sensor, 0x1C);
  91. DHT20_reset_reg(i2c_sensor, 0x1E);
  92. }
  93. furi_delay_ms(10);
  94. uint8_t data[7] = {0xAC, 0x33, 0x00};
  95. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT;
  96. furi_delay_ms(80);
  97. uint32_t t = furi_get_tick();
  98. while(DHT20_get_status(i2c_sensor) == 0x80) {
  99. if(furi_get_tick() - t > 10) return UT_SENSORSTATUS_TIMEOUT;
  100. }
  101. if(!unitemp_i2c_readArray(i2c_sensor, 7, data)) return UT_SENSORSTATUS_TIMEOUT;
  102. if(DHT20_calc_CRC8(data, 6) != data[6]) {
  103. return UT_SENSORSTATUS_BADCRC;
  104. }
  105. uint32_t RetuData = 0;
  106. RetuData = (RetuData | data[1]) << 8;
  107. RetuData = (RetuData | data[2]) << 8;
  108. RetuData = (RetuData | data[3]);
  109. RetuData = RetuData >> 4;
  110. sensor->hum = RetuData * 100 * 10 / 1024.0f / 1024.0f / 10.0f;
  111. RetuData = 0;
  112. RetuData = (RetuData | data[3]) << 8;
  113. RetuData = (RetuData | data[4]) << 8;
  114. RetuData = (RetuData | data[5]);
  115. RetuData = RetuData & 0xfffff;
  116. sensor->temp = (RetuData * 200 * 10.0f / 1024.0f / 1024.0f - 500) / 10.0f;
  117. return UT_SENSORSTATUS_OK;
  118. }