LM75.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "LM75.h"
  16. #include "../interfaces/I2CSensor.h"
  17. #define LM75_REG_TEMP 0x00
  18. #define LM75_REG_CONFIG 0x01
  19. #define LM75_REG_THYST 0x02
  20. #define LM75_REG_TOS 0x03
  21. #define LM75_CONFIG_SHUTDOWN 0b00000001
  22. #define LM75_CONFIG_INTERRUPT 0b00000010
  23. #define LM75_CONFIG_OSPOLARITY_HIGH 0b00000100
  24. #define LM75_CONFIG_FAULTQUEUE_1 0b00000000
  25. #define LM75_CONFIG_FAULTQUEUE_2 0b00001000
  26. #define LM75_CONFIG_FAULTQUEUE_4 0b00010000
  27. #define LM75_CONFIG_FAULTQUEUE_6 0b00011000
  28. const SensorType LM75 = {
  29. .typename = "LM75",
  30. .interface = &I2C,
  31. .datatype = UT_DATA_TYPE_TEMP,
  32. .pollingInterval = 500,
  33. .allocator = unitemp_LM75_alloc,
  34. .mem_releaser = unitemp_LM75_free,
  35. .initializer = unitemp_LM75_init,
  36. .deinitializer = unitemp_LM75_deinit,
  37. .updater = unitemp_LM75_update};
  38. bool unitemp_LM75_alloc(Sensor* sensor, char* args) {
  39. UNUSED(args);
  40. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  41. //Адреса на шине I2C (7 бит)
  42. i2c_sensor->minI2CAdr = 0b1001000 << 1;
  43. i2c_sensor->maxI2CAdr = 0b1001111 << 1;
  44. return true;
  45. }
  46. bool unitemp_LM75_free(Sensor* sensor) {
  47. //Нечего высвобождать, так как ничего не было выделено
  48. UNUSED(sensor);
  49. return true;
  50. }
  51. bool unitemp_LM75_init(Sensor* sensor) {
  52. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  53. //Выход если не удалось записать значение в датчик
  54. if(!unitemp_i2c_writeReg(i2c_sensor, LM75_REG_CONFIG, LM75_CONFIG_FAULTQUEUE_1)) return false;
  55. return true;
  56. }
  57. bool unitemp_LM75_deinit(Sensor* sensor) {
  58. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  59. if(!unitemp_i2c_writeReg(
  60. i2c_sensor, LM75_REG_CONFIG, LM75_CONFIG_FAULTQUEUE_1 | LM75_CONFIG_SHUTDOWN))
  61. return false;
  62. return true;
  63. }
  64. UnitempStatus unitemp_LM75_update(Sensor* sensor) {
  65. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  66. uint8_t buff[2];
  67. if(!unitemp_i2c_readRegArray(i2c_sensor, LM75_REG_TEMP, 2, buff))
  68. return UT_SENSORSTATUS_TIMEOUT;
  69. int16_t raw = (((uint16_t)buff[0] << 8) | buff[1]);
  70. sensor->temp = raw / 32 * 0.125;
  71. return UT_SENSORSTATUS_OK;
  72. }