DHT20.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 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 = 1000,
  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. const SensorType AHT10 = {
  29. .typename = "AHT10",
  30. .interface = &I2C,
  31. .datatype = UT_TEMPERATURE | UT_HUMIDITY,
  32. .pollingInterval = 1000,
  33. .allocator = unitemp_DHT20_I2C_alloc,
  34. .mem_releaser = unitemp_DHT20_I2C_free,
  35. .initializer = unitemp_DHT20_init,
  36. .deinitializer = unitemp_DHT20_I2C_deinit,
  37. .updater = unitemp_DHT20_I2C_update};
  38. static uint8_t DHT20_get_status(I2CSensor* i2c_sensor) {
  39. uint8_t status[1] = {0};
  40. unitemp_i2c_readArray(i2c_sensor, 1, status);
  41. return status[0];
  42. }
  43. static uint8_t DHT20_calc_CRC8(uint8_t* message, uint8_t Num) {
  44. uint8_t i;
  45. uint8_t byte;
  46. uint8_t crc = 0xFF;
  47. for(byte = 0; byte < Num; byte++) {
  48. crc ^= (message[byte]);
  49. for(i = 8; i > 0; --i) {
  50. if(crc & 0x80)
  51. crc = (crc << 1) ^ 0x31;
  52. else
  53. crc = (crc << 1);
  54. }
  55. }
  56. return crc;
  57. }
  58. static void DHT20_reset_reg(I2CSensor* i2c_sensor, uint8_t addr) {
  59. uint8_t data[3] = {addr, 0x00, 0x00};
  60. unitemp_i2c_writeArray(i2c_sensor, 3, data);
  61. furi_delay_ms(5);
  62. unitemp_i2c_readArray(i2c_sensor, 3, data);
  63. furi_delay_ms(10);
  64. data[0] = 0xB0 | addr;
  65. unitemp_i2c_writeArray(i2c_sensor, 3, data);
  66. }
  67. bool unitemp_DHT20_I2C_alloc(Sensor* sensor, char* args) {
  68. UNUSED(args);
  69. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  70. //Адреса на шине I2C (7 бит)
  71. i2c_sensor->minI2CAdr = 0x38 << 1;
  72. i2c_sensor->maxI2CAdr = (sensor->type == &DHT20) ? (0x38 << 1) : (0x39 << 1);
  73. return true;
  74. }
  75. bool unitemp_DHT20_I2C_free(Sensor* sensor) {
  76. //Нечего высвобождать, так как ничего не было выделено
  77. UNUSED(sensor);
  78. return true;
  79. }
  80. bool unitemp_DHT20_init(Sensor* sensor) {
  81. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  82. uint8_t data[3] = {0xA8, 0x00, 0x00};
  83. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return false;
  84. furi_delay_ms(10);
  85. data[0] = (sensor->type == &DHT20) ? 0xBE : 0xE1;
  86. data[1] = 0x08;
  87. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return false;
  88. furi_delay_ms(10);
  89. return true;
  90. }
  91. bool unitemp_DHT20_I2C_deinit(Sensor* sensor) {
  92. //Нечего деинициализировать
  93. UNUSED(sensor);
  94. return true;
  95. }
  96. UnitempStatus unitemp_DHT20_I2C_update(Sensor* sensor) {
  97. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  98. if(DHT20_get_status(i2c_sensor) != 0x18) {
  99. DHT20_reset_reg(i2c_sensor, 0x1B);
  100. DHT20_reset_reg(i2c_sensor, 0x1C);
  101. DHT20_reset_reg(i2c_sensor, 0x1E);
  102. }
  103. furi_delay_ms(10);
  104. uint8_t data[7] = {0xAC, 0x33, 0x00};
  105. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT;
  106. furi_delay_ms(80);
  107. uint32_t t = furi_get_tick();
  108. while(DHT20_get_status(i2c_sensor) == 0x80) {
  109. if(furi_get_tick() - t > 10) return UT_SENSORSTATUS_TIMEOUT;
  110. }
  111. if(!unitemp_i2c_readArray(i2c_sensor, 7, data)) return UT_SENSORSTATUS_TIMEOUT;
  112. if(DHT20_calc_CRC8(data, 6) != data[6]) {
  113. return UT_SENSORSTATUS_BADCRC;
  114. }
  115. uint32_t RetuData = 0;
  116. RetuData = (RetuData | data[1]) << 8;
  117. RetuData = (RetuData | data[2]) << 8;
  118. RetuData = (RetuData | data[3]);
  119. RetuData = RetuData >> 4;
  120. sensor->hum = RetuData * 100 * 10 / 1024.0f / 1024.0f / 10.0f;
  121. RetuData = 0;
  122. RetuData = (RetuData | data[3]) << 8;
  123. RetuData = (RetuData | data[4]) << 8;
  124. RetuData = (RetuData | data[5]);
  125. RetuData = RetuData & 0xfffff;
  126. sensor->temp = (RetuData * 200 * 10.0f / 1024.0f / 1024.0f - 500) / 10.0f;
  127. return UT_SENSORSTATUS_OK;
  128. }