ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
ds18b20.c
Go to the documentation of this file.
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
15#include "ds18b20.h"
16
17void DS18B20_Init(void)
18{
19 /* Pull up 2 seconds for possible capacitor charging */
23 SYS_Delay(1000);
25 SYS_Delay(1);
27 SYS_Delay(1000);
28}
29
30__BIT DS18B20_Reset(void)
31{
32 __BIT b;
33
34 /* Line low, and wait 480us */
37 SYS_DelayUs(500);
38 /* Release line and wait for 70us */
40 SYS_DelayUs(70);
41 /* Check bit value, success if low */
42 b = DS18B20_DQ;
43 /* Delay for 410 us */
44 SYS_DelayUs(410);
45 /* Return value of presence pulse, 0 = OK, 1 = ERROR */
46 return b;
47}
48
49__BIT DS18B20_ReadBit(void)
50{
51 __BIT b = RESET;
52
53 /* Line low */
56 SYS_DelayUs(2);
57
58 /* Release line */
60 SYS_DelayUs(10);
61
62 /* Read line value */
63 if (DS18B20_DQ) {
64 /* Bit is HIGH */
65 b = SET;
66 }
67
68 /* Wait 50us to complete 60us period */
69 SYS_DelayUs(50);
70
71 /* Return bit value */
72 return b;
73}
74
76{
77 uint8_t i = 8, byte = 0;
78 while (i--)
79 {
80 byte >>= 1;
81 if (DS18B20_ReadBit())
82 {
83 byte |= 0x80;
84 }
85 }
86 return byte;
87}
88
89void DS18B20_WriteBit(__BIT b)
90{
91 if (b)
92 {
93 /* Set line low */
96 SYS_DelayUs(10);
97
98 /* Bit high */
100
101 /* Wait for 55 us and release the line */
102 SYS_DelayUs(55);
104 }
105 else
106 {
107 /* Set line low */
110 SYS_DelayUs(65);
111
112 /* Bit high */
114
115 /* Wait for 5 us and release the line */
116 SYS_DelayUs(5);
118 }
119}
120
122{
123 uint8_t i = 8;
124 /* Write 8 bits */
125 while (i--)
126 {
127 /* LSB bit is first */
128 DS18B20_WriteBit(byte & 0x01);
129 byte >>= 1;
130 }
131}
132
134{
135 uint8_t i = 0;
136 /* Reset line */
138 /* Skip ROM */
140 /* Read scratchpad command by onewire protocol */
142
143 /* Get data */
144 for (i = 0; i < 9; i++)
145 {
146 /* Read byte by byte */
147 *buf++ = DS18B20_ReadByte();
148 }
149}
150
152{
153 uint8_t crc = 0, inbyte, i, mix;
154
155 while (len--)
156 {
157 inbyte = *addr++;
158 for (i = 8; i; i--)
159 {
160 mix = (crc ^ inbyte) & 0x01;
161 crc >>= 1;
162 if (mix)
163 {
164 crc ^= 0x8C;
165 }
166 inbyte >>= 1;
167 }
168 }
169 /* Return calculated CRC */
170 return crc;
171}
172
174{
175 /* Reset pulse */
177 /* Skip rom */
179 /* Start conversion on all connected devices */
181}
182
184{
185 /* If read bit is low, then device is not finished yet with calculation temperature */
186 return DS18B20_ReadBit();
187}
188
190{
191 uint8_t i = 0;
192 /* Reset pulse */
194 /* Read rom */
196 /* Get data */
197 for (i = 0; i < 8; i++)
198 {
199 /* Read byte by byte */
200 *buf++ = DS18B20_ReadByte();
201 }
202}
203
205{
206 uint8_t len = 8;
208 while (len--)
209 {
211 }
212}
213
215{
216 /* Reset pulse */
218 /* Select ROM number */
220 /* Start conversion on selected device */
222}
223
225{
226 uint8_t i = 0;
227 /* Reset line */
229 /* Select ROM number */
231 /* Read scratchpad command by onewire protocol */
233
234 /* Get data */
235 for (i = 0; i < 9; i++)
236 {
237 /* Read byte by byte */
238 *buf++ = DS18B20_ReadByte();
239 }
240}
241
243{
244 uint8_t len = 64, pos = 0;
245 /* Start from deepest point */
246 split_point = (split_point == 0x00)? 0xFF : split_point;
247 /* Reset line */
249 /* Start searching */
251
252 while (len--)
253 {
254 // Read the value and its complement value of this bit
255 __BIT pb = DS18B20_ReadBit();
256 __BIT cb = DS18B20_ReadBit();
257 if (pb && cb) // no device
258 {
259 return 0;
260 }
261 else if (pb) // bit = 1
262 {
263 *(buff + pos / 8) |= 0x01 << (pos % 8);
265 // confirm: set this bit to 1
266 *(stack + pos / 8) |= 0x01 << (pos % 8);
267 }
268 else if (cb) // bit = 0
269 {
270 *(buff + pos / 8) &= ~(0x01 << (pos % 8));
272 // confirm: set this bit to 1
273 *(stack + pos / 8) |= 0x01 << (pos % 8);
274 }
275 else // bit can be 0 or 1, possible split point
276 {
277 if (split_point == 0xFF || pos > split_point)
278 {
279 // new split point, try 0
280 *(buff + pos / 8) &= ~(0x01 << (pos % 8));
282 // unconfirm: set this bit to 0
283 *(stack + pos / 8) &= ~(0x01 << (pos % 8));
284 // record this new split point
285 split_point = pos;
286 }
287 else if (pos == split_point)
288 {
289 // reach split point, try 1
290 *(buff + pos / 8) |= 0x01 << (pos % 8);
292 // confirm: set this bit to 1
293 *(stack + pos / 8) |= 0x01 << (pos % 8);
294 }
295 else // middle point, use existing bit
296 {
297 DS18B20_WriteBit(*(buff + pos / 8) >> (pos % 8) & 0x01);
298 }
299 }
300 pos++;
301 }
302 // Relocate split point, move it to the last *unconfirmed* bit of stack
303 while (split_point > 0 && *(stack + split_point / 8) >> (split_point % 8) & 0x01 == 0x01) split_point--;
304 return split_point;
305}
uint8_t pos
__XDATA uint8_t buff[7]
Definition: ds3231.c:17
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
void SYS_DelayUs(uint16_t t)
Definition: fw_sys.c:75
@ RESET
Definition: fw_types.h:84
@ SET
Definition: fw_types.h:85
unsigned char uint8_t
Definition: fw_types.h:18
uint8_t addr[8]
Definition: main.c:28
uint8_t __XDATA i
void DS18B20_WriteByte(uint8_t byte)
Write one byte to DS18B20.
Definition: ds18b20.c:121
void DS18B20_ReadScratchpadFromAddr(const uint8_t *addr, uint8_t *buf)
Read SRAM scratchpad from selected slave.
Definition: ds18b20.c:224
uint8_t DS18B20_Crc(uint8_t *addr, uint8_t len)
8-bit CRC calculation
Definition: ds18b20.c:151
uint8_t DS18B20_ReadByte(void)
Read one byte from DS18B20.
Definition: ds18b20.c:75
__BIT DS18B20_ReadBit(void)
Read one bit from DS18B20.
Definition: ds18b20.c:49
void DS18B20_StartAll(void)
Start conversion on all slaves.
Definition: ds18b20.c:173
void DS18B20_WriteBit(__BIT b)
Write one bit to DS18B20.
Definition: ds18b20.c:89
void DS18B20_ReadRom(uint8_t *buf)
Read 64-bit ROM: 8-bit family code "0x28", unique 48-bit serial number, 8-bit CRC.
Definition: ds18b20.c:189
void DS18B20_ReadScratchpad(uint8_t *buf)
Read SRAM scratchpad.
Definition: ds18b20.c:133
void DS18B20_Init(void)
Initialize DS18B20.
Definition: ds18b20.c:17
void DS18B20_Select(const uint8_t *addr)
Select a slave on the bus.
Definition: ds18b20.c:204
uint8_t DS18B20_Search(uint8_t *buff, uint8_t *stack, uint8_t split_point)
Perform one ROM search.
Definition: ds18b20.c:242
__BIT DS18B20_AllDone(void)
If read bit is low, then device is not finished yet with calculation temperature.
Definition: ds18b20.c:183
__BIT DS18B20_Reset(void)
Reset DS18B20.
Definition: ds18b20.c:30
void DS18B20_Start(const uint8_t *addr)
Start conversion on selected slave.
Definition: ds18b20.c:214
#define ONEWIRE_CMD_SEARCHROM
Definition: ds18b20.h:41
#define DS18B20_DQ_INPUT()
Definition: ds18b20.h:30
#define DS18B20_DQ
Definition: ds18b20.h:28
#define ONEWIRE_CMD_SKIPROM
Definition: ds18b20.h:44
#define DS18B20_CMD_CONVERTTEMP
Definition: ds18b20.h:50
#define DS18B20_DQ_PULLUP()
Definition: ds18b20.h:29
#define ONEWIRE_CMD_READROM
Definition: ds18b20.h:42
#define DS18B20_DQ_OUTPUT()
Definition: ds18b20.h:31
#define ONEWIRE_CMD_MATCHROM
Definition: ds18b20.h:43
#define ONEWIRE_CMD_RSCRATCHPAD
Definition: ds18b20.h:36