3-phase PV router
Loading...
Searching...
No Matches
ds18b20.h
Go to the documentation of this file.
1
23
24#ifndef DS18B20_H
25#define DS18B20_H
26
27#include <stdint.h>
28
29// Dallas DS18B20 commands
30inline constexpr uint8_t CONVERT_TEMPERATURE{ 0x44 };
31inline constexpr uint8_t READ_SCRATCHPAD{ 0xBE };
32inline constexpr uint8_t WRITE_SCRATCH{ 0x4E };
33
34inline constexpr int16_t OUTOFRANGE_TEMPERATURE{ 30200 };
35inline constexpr int16_t TEMP_RANGE_LOW{ -5500 };
36inline constexpr int16_t TEMP_RANGE_HIGH{ 12500 };
37
38inline constexpr int16_t DEVICE_DISCONNECTED_RAW{ -7040 };
39
40namespace DS18B20
41{
43using ScratchPad = uint8_t[9];
44
45inline constexpr int16_t POWER_ON_VALUE{ 8500 };
46inline constexpr int16_t POWER_ON_JUMP{ 500 };
47
59constexpr uint8_t crc8(const uint8_t* data, uint8_t len)
60{
61 uint8_t crc{ 0 };
62 while (len--)
63 {
64 uint8_t inbyte{ *data++ };
65 for (uint8_t i = 8; i != 0; --i)
66 {
67 const bool mix{ ((crc ^ inbyte) & 0x01) != 0 };
68 crc >>= 1;
69 if (mix)
70 {
71 crc ^= 0x8C;
72 }
73 inbyte >>= 1;
74 }
75 }
76 return crc;
77}
78
88constexpr int16_t toHundredths(uint8_t lsb, uint8_t msb)
89{
90 const int16_t x16{ static_cast< int16_t >((msb << 8) | lsb) };
91 return static_cast< int16_t >((x16 * 6) + (x16 >> 2));
92}
93
101constexpr int16_t decode(const ScratchPad& buf)
102{
103 if (crc8(buf, 8) != buf[8])
104 {
106 }
107
108 const int16_t result{ toHundredths(buf[0], buf[1]) };
109 if (result < TEMP_RANGE_LOW || result > TEMP_RANGE_HIGH)
110 {
112 }
113 return result;
114}
115
127constexpr int16_t skipPowerOnValue(int16_t reading, int16_t previous)
128{
129 if (POWER_ON_VALUE == reading)
130 {
131 const int delta{ reading - previous };
132 if (delta > POWER_ON_JUMP || delta < -POWER_ON_JUMP)
133 {
135 }
136 }
137 return reading;
138}
139} // namespace DS18B20
140
146constexpr bool isValidTemperature(int16_t temperature_x100)
147{
148 return (OUTOFRANGE_TEMPERATURE != temperature_x100) && (DEVICE_DISCONNECTED_RAW != temperature_x100);
149}
150
151namespace DS18B20
152{
162{
163 int16_t lastValid{ 0 };
164
171 int16_t operator()(const int16_t reading)
172 {
173 const int16_t kept{ skipPowerOnValue(reading, lastValid) };
174 if (isValidTemperature(kept))
175 {
176 lastValid = kept;
177 }
178 return kept;
179 }
180};
181} // namespace DS18B20
182
183#endif /* DS18B20_H */
constexpr uint8_t WRITE_SCRATCH
Definition ds18b20.h:32
constexpr uint8_t CONVERT_TEMPERATURE
Definition ds18b20.h:30
constexpr int16_t TEMP_RANGE_LOW
Definition ds18b20.h:35
constexpr int16_t DEVICE_DISCONNECTED_RAW
Definition ds18b20.h:38
constexpr uint8_t READ_SCRATCHPAD
Definition ds18b20.h:31
constexpr int16_t OUTOFRANGE_TEMPERATURE
Definition ds18b20.h:34
constexpr bool isValidTemperature(int16_t temperature_x100)
Whether a temperature reading is valid, and so worth sending.
Definition ds18b20.h:146
constexpr int16_t TEMP_RANGE_HIGH
Definition ds18b20.h:36
uint8_t i
constexpr uint8_t crc8(const uint8_t *data, uint8_t len)
The Dallas/Maxim CRC-8 (x^8 + x^5 + x^4 + 1), as OneWire::crc8.
Definition ds18b20.h:59
constexpr int16_t skipPowerOnValue(int16_t reading, int16_t previous)
The reading to keep, the power-on value skipped.
Definition ds18b20.h:127
constexpr int16_t decode(const ScratchPad &buf)
The reading of a scratchpad, its CRC checked.
Definition ds18b20.h:101
constexpr int16_t POWER_ON_JUMP
Definition ds18b20.h:46
constexpr int16_t POWER_ON_VALUE
Definition ds18b20.h:45
constexpr int16_t toHundredths(uint8_t lsb, uint8_t msb)
The temperature of the register, in hundredths of °C.
Definition ds18b20.h:88
uint8_t[9] ScratchPad
The scratchpad: temperature LSB and MSB, TH, TL, configuration, 3 reserved bytes, CRC.
Definition ds18b20.h:43
The readings of one sensor, the power-on value skipped.
Definition ds18b20.h:162
int16_t operator()(const int16_t reading)
The reading to keep.
Definition ds18b20.h:171