ds18b20.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <esp_system.h>
  14. #ifndef DS18B20_H_
  15. #define DS18B20_H_
  16. // warning: 'taskENTER_CRITICAL(mux)' is deprecated in ESP-IDF, consider using 'portENTER_CRITICAL(mux)'
  17. #define noInterrupts() portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;taskENTER_CRITICAL(&mux)
  18. #define interrupts() taskEXIT_CRITICAL(&mux)
  19. #define DEVICE_DISCONNECTED_C -127
  20. #define DEVICE_DISCONNECTED_F -196.6
  21. #define DEVICE_DISCONNECTED_RAW -7040
  22. #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  23. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  24. typedef uint8_t DeviceAddress[8];
  25. typedef uint8_t ScratchPad[9];
  26. // Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0
  27. // Tiny 2x16 entry CRC table created by Arjen Lentz
  28. // See http://lentz.com.au/blog/calculating-crc-with-a-tiny-32-entry-lookup-table
  29. static const uint8_t dscrc2x16_table[] = {
  30. 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
  31. 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
  32. 0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
  33. 0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74
  34. };
  35. /* *INDENT-OFF* */
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /* *INDENT-ON* */
  40. void ds18b20_init(int GPIO);
  41. #define ds18b20_send ds18b20_write
  42. #define ds18b20_send_byte ds18b20_write_byte
  43. #define ds18b20_RST_PULSE ds18b20_reset
  44. void ds18b20_write(char bit);
  45. unsigned char ds18b20_read(void);
  46. void ds18b20_write_byte(char data);
  47. unsigned char ds18b20_read_byte(void);
  48. unsigned char ds18b20_reset(void);
  49. bool ds18b20_setResolution(const DeviceAddress tempSensorAddresses[], int numAddresses, uint8_t newResolution);
  50. bool ds18b20_isConnected(const DeviceAddress *deviceAddress, uint8_t *scratchPad);
  51. void ds18b20_writeScratchPad(const DeviceAddress *deviceAddress, const uint8_t *scratchPad);
  52. bool ds18b20_readScratchPad(const DeviceAddress *deviceAddress, uint8_t *scratchPad);
  53. void ds18b20_select(const DeviceAddress *address);
  54. uint8_t ds18b20_crc8(const uint8_t *addr, uint8_t len);
  55. bool ds18b20_isAllZeros(const uint8_t * const scratchPad);
  56. bool isConversionComplete();
  57. uint16_t millisToWaitForConversion();
  58. void ds18b20_requestTemperatures();
  59. float ds18b20_getTempF(const DeviceAddress *deviceAddress);
  60. float ds18b20_getTempC(const DeviceAddress *deviceAddress);
  61. int16_t calculateTemperature(const DeviceAddress *deviceAddress, uint8_t* scratchPad);
  62. float ds18b20_get_temp(void);
  63. void reset_search();
  64. bool search(uint8_t *newAddr, bool search_mode);
  65. /* *INDENT-OFF* */
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. /* *INDENT-ON* */
  70. #endif