ds18b20.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef __FW_DS18B20__
  15. #define __FW_DS18B20__
  16. #include "fw_hal.h"
  17. /**
  18. * DS18B20 - Programmable Resolution 1-Wire Digital Thermometer
  19. *
  20. * To-92 Pins:
  21. * With the flat side facing you and with the leads pointing down, they are GND, DQ and Vdd
  22. *
  23. */
  24. #define DS18B20_DQ P35
  25. #define DS18B20_DQ_PULLUP() GPIO_SetPullUp(GPIO_Port_3, GPIO_Pin_5, HAL_State_ON)
  26. #define DS18B20_DQ_INPUT() GPIO_P3_SetMode(GPIO_Pin_5, GPIO_Mode_Input_HIP)
  27. #define DS18B20_DQ_OUTPUT() GPIO_P3_SetMode(GPIO_Pin_5, GPIO_Mode_InOut_OD)
  28. #define DS18B20_DQ_LOW() DS18B20_DQ=RESET
  29. #define DS18B20_DQ_HIGH() DS18B20_DQ=SET
  30. /* OneWire commands */
  31. #define ONEWIRE_CMD_RSCRATCHPAD 0xBE
  32. #define ONEWIRE_CMD_WSCRATCHPAD 0x4E
  33. #define ONEWIRE_CMD_CPYSCRATCHPAD 0x48
  34. #define ONEWIRE_CMD_RECEEPROM 0xB8
  35. #define ONEWIRE_CMD_RPWRSUPPLY 0xB4
  36. #define ONEWIRE_CMD_SEARCHROM 0xF0
  37. #define ONEWIRE_CMD_READROM 0x33
  38. #define ONEWIRE_CMD_MATCHROM 0x55
  39. #define ONEWIRE_CMD_SKIPROM 0xCC
  40. #define DS18B20_FAMILY_CODE 0x28
  41. #define DS18B20_CMD_ALARMSEARCH 0xEC
  42. /* DS18B20 read temperature command */
  43. #define DS18B20_CMD_CONVERTTEMP 0x44 /* Convert temperature */
  44. #define DS18B20_DECIMAL_STEPS_12BIT 0.0625
  45. #define DS18B20_DECIMAL_STEPS_11BIT 0.125
  46. #define DS18B20_DECIMAL_STEPS_10BIT 0.25
  47. #define DS18B20_DECIMAL_STEPS_9BIT 0.5
  48. /* Bits locations for resolution */
  49. #define DS18B20_RESOLUTION_R1 6
  50. #define DS18B20_RESOLUTION_R0 5
  51. typedef enum {
  52. DS18B20_Resolution_9bits = 9, /*!< DS18B20 9 bits resolution */
  53. DS18B20_Resolution_10bits = 10, /*!< DS18B20 10 bits resolution */
  54. DS18B20_Resolution_11bits = 11, /*!< DS18B20 11 bits resolution */
  55. DS18B20_Resolution_12bits = 12 /*!< DS18B20 12 bits resolution */
  56. } DS18B20_Resolution_t;
  57. /**
  58. * @brief Initialize DS18B20
  59. */
  60. void DS18B20_Init(void);
  61. /**
  62. * @brief Reset DS18B20
  63. * @return bit value
  64. */
  65. __BIT DS18B20_Reset(void);
  66. /**
  67. * @brief Read one bit from DS18B20
  68. * @return bit value
  69. */
  70. __BIT DS18B20_ReadBit(void);
  71. /**
  72. * @brief Read one byte from DS18B20
  73. * @return byte value
  74. */
  75. uint8_t DS18B20_ReadByte(void);
  76. /**
  77. * @brief Write one bit to DS18B20
  78. * @param b bit value
  79. */
  80. void DS18B20_WriteBit(__BIT b);
  81. /**
  82. * @brief Write one byte to DS18B20
  83. * @param byte byte value
  84. */
  85. void DS18B20_WriteByte(uint8_t byte);
  86. /**
  87. * @brief 8-bit CRC calculation
  88. *
  89. * @param addr
  90. * @param len
  91. * @return crc result
  92. */
  93. uint8_t DS18B20_Crc(uint8_t *addr, uint8_t len);
  94. /**
  95. * @brief Read SRAM scratchpad
  96. * @param buf a 9-byte buffer, Byte 8 contains the CRC code for bytes 0 through 7
  97. */
  98. void DS18B20_ReadScratchpad(uint8_t *buf);
  99. /**
  100. * @brief Start conversion on all slaves
  101. */
  102. void DS18B20_StartAll(void);
  103. /**
  104. * @brief If read bit is low, then device is not finished yet with calculation temperature
  105. * @return bit value
  106. */
  107. __BIT DS18B20_AllDone(void);
  108. /**
  109. * @brief Read 64-bit ROM: 8-bit family code "0x28", unique 48-bit serial number, 8-bit CRC
  110. * @note This command can only be used if there is a single DS18B20 on the bus.
  111. * If multiple slaves are present, a data collision will occur(a wired AND result).
  112. * @param buf a 8-byte buffer
  113. */
  114. void DS18B20_ReadRom(uint8_t *buf);
  115. /**
  116. * @brief Select a slave on the bus
  117. * @note Only the slave that exactly matches the 64-bit ROM code sequence will respond to
  118. * the function command issued by the master; all other slaves on the bus will wait
  119. * for a reset pulse.
  120. * @param addr 64-bit ROM code
  121. */
  122. void DS18B20_Select(const uint8_t* addr);
  123. /**
  124. * @brief Start conversion on selected slave
  125. * @param addr 64-bit ROM code
  126. */
  127. void DS18B20_Start(const uint8_t *addr);
  128. /**
  129. * @brief Read SRAM scratchpad from selected slave
  130. *
  131. * @param addr 64-bit ROM code
  132. * @param buf a 9-byte buffer, Byte 8 contains the CRC code for bytes 0 through 7
  133. */
  134. void DS18B20_ReadScratchpadFromAddr(const uint8_t *addr, uint8_t *buf);
  135. /**
  136. * @brief Perform one ROM search
  137. * @param buff 8-byte array for ROM bytes
  138. * @param stack 8-byte array for search stack
  139. * @param split_point deepest split point of last search
  140. * @return new split point
  141. */
  142. uint8_t DS18B20_Search(uint8_t *buff, uint8_t *stack, uint8_t split_point);
  143. #endif // __DS18B20_H_