ds18b20.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 noInterrupts()
  20. #define interrupts()
  21. #define DEVICE_DISCONNECTED_C -127
  22. #define DEVICE_DISCONNECTED_F -196.6
  23. #define DEVICE_DISCONNECTED_RAW -7040
  24. #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
  25. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  26. typedef uint8_t DeviceAddress[8];
  27. typedef uint8_t ScratchPad[9];
  28. // Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0
  29. // Tiny 2x16 entry CRC table created by Arjen Lentz
  30. // See http://lentz.com.au/blog/calculating-crc-with-a-tiny-32-entry-lookup-table
  31. static const uint8_t dscrc2x16_table[] = {
  32. 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
  33. 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
  34. 0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
  35. 0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74
  36. };
  37. /* *INDENT-OFF* */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* *INDENT-ON* */
  42. void ds18b20_init(int GPIO);
  43. #define ds18b20_send ds18b20_write
  44. #define ds18b20_send_byte ds18b20_write_byte
  45. #define ds18b20_RST_PULSE ds18b20_reset
  46. void ds18b20_write(char bit);
  47. unsigned char ds18b20_read(void);
  48. void ds18b20_write_byte(char data);
  49. unsigned char ds18b20_read_byte(void);
  50. unsigned char ds18b20_reset(void);
  51. bool ds18b20_setResolution(const DeviceAddress tempSensorAddresses[], int numAddresses, uint8_t newResolution);
  52. bool ds18b20_isConnected(const DeviceAddress *deviceAddress, uint8_t *scratchPad);
  53. void ds18b20_writeScratchPad(const DeviceAddress *deviceAddress, const uint8_t *scratchPad);
  54. bool ds18b20_readScratchPad(const DeviceAddress *deviceAddress, uint8_t *scratchPad);
  55. void ds18b20_select(const DeviceAddress *address);
  56. uint8_t ds18b20_crc8(const uint8_t *addr, uint8_t len);
  57. bool ds18b20_isAllZeros(const uint8_t * const scratchPad);
  58. bool isConversionComplete();
  59. uint16_t millisToWaitForConversion();
  60. void ds18b20_requestTemperatures();
  61. float ds18b20_getTempF(const DeviceAddress *deviceAddress);
  62. float ds18b20_getTempC(const DeviceAddress *deviceAddress);
  63. int16_t calculateTemperature(const DeviceAddress *deviceAddress, uint8_t* scratchPad);
  64. float ds18b20_get_temp(void);
  65. void reset_search();
  66. bool search(uint8_t *newAddr, bool search_mode);
  67. /* *INDENT-OFF* */
  68. #ifdef __cplusplus
  69. }
  70. #endif
  71. /* *INDENT-ON* */
  72. #endif