LibDriver DS18B20
Loading...
Searching...
No Matches
driver_ds18b20.c
Go to the documentation of this file.
1
37
38#include "driver_ds18b20.h"
39
43#define CHIP_NAME "Maxim Integrated DS18B20"
44#define MANUFACTURER_NAME "Maxim Integrated"
45#define SUPPLY_VOLTAGE_MIN 3.0f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 4.0f
48#define TEMPERATURE_MIN -55.0f
49#define TEMPERATURE_MAX 125.0f
50#define DRIVER_VERSION 2000
51
55#define DS18B20_CMD_SEARCH_ROM 0xF0
56#define DS18B20_CMD_READ_ROM 0x33
57#define DS18B20_CMD_MATCH_ROM 0x55
58#define DS18B20_CMD_SKIP_ROM 0xCC
59#define DS18B20_CMD_ALARM_SEARCH 0xEC
60#define DS18B20_CMD_CONVERT_T 0x44
61#define DS18B20_CMD_WRITE_SCRATCHPAD 0x4E
62#define DS18B20_CMD_READ_SCRATCHPAD 0xBE
63#define DS18B20_CMD_COPY_SCRATCHPAD 0x48
64#define DS18B20_CMD_RECALL_EE 0xB8
65#define DS18B20_CMD_READ_POWER_SUPPLY 0xB4
66
70const uint8_t gc_ds18b20_crc_table[256] =
71{
72 0X00, 0X5E, 0XBC, 0XE2, 0X61, 0X3F, 0XDD, 0X83, 0XC2, 0X9C, 0X7E, 0X20, 0XA3,
73 0XFD, 0X1F, 0X41, 0X9D, 0XC3, 0X21, 0X7F, 0XFC, 0XA2, 0X40, 0X1E, 0X5F, 0X01,
74 0XE3, 0XBD, 0X3E, 0X60, 0X82, 0XDC, 0X23, 0X7D, 0X9F, 0XC1, 0X42, 0X1C, 0XFE,
75 0XA0, 0XE1, 0XBF, 0X5D, 0X03, 0X80, 0XDE, 0X3C, 0X62, 0XBE, 0XE0, 0X02, 0X5C,
76 0XDF, 0X81, 0X63, 0X3D, 0X7C, 0X22, 0XC0, 0X9E, 0X1D, 0X43, 0XA1, 0XFF, 0X46,
77 0X18, 0XFA, 0XA4, 0X27, 0X79, 0X9B, 0XC5, 0X84, 0XDA, 0X38, 0X66, 0XE5, 0XBB,
78 0X59, 0X07, 0XDB, 0X85, 0X67, 0X39, 0XBA, 0XE4, 0X06, 0X58, 0X19, 0X47, 0XA5,
79 0XFB, 0X78, 0X26, 0XC4, 0X9A, 0X65, 0X3B, 0XD9, 0X87, 0X04, 0X5A, 0XB8, 0XE6,
80 0XA7, 0XF9, 0X1B, 0X45, 0XC6, 0X98, 0X7A, 0X24, 0XF8, 0XA6, 0X44, 0X1A, 0X99,
81 0XC7, 0X25, 0X7B, 0X3A, 0X64, 0X86, 0XD8, 0X5B, 0X05, 0XE7, 0XB9, 0X8C, 0XD2,
82 0X30, 0X6E, 0XED, 0XB3, 0X51, 0X0F, 0X4E, 0X10, 0XF2, 0XAC, 0X2F, 0X71, 0X93,
83 0XCD, 0X11, 0X4F, 0XAD, 0XF3, 0X70, 0X2E, 0XCC, 0X92, 0XD3, 0X8D, 0X6F, 0X31,
84 0XB2, 0XEC, 0X0E, 0X50, 0XAF, 0XF1, 0X13, 0X4D, 0XCE, 0X90, 0X72, 0X2C, 0X6D,
85 0X33, 0XD1, 0X8F, 0X0C, 0X52, 0XB0, 0XEE, 0X32, 0X6C, 0X8E, 0XD0, 0X53, 0X0D,
86 0XEF, 0XB1, 0XF0, 0XAE, 0X4C, 0X12, 0X91, 0XCF, 0X2D, 0X73, 0XCA, 0X94, 0X76,
87 0X28, 0XAB, 0XF5, 0X17, 0X49, 0X08, 0X56, 0XB4, 0XEA, 0X69, 0X37, 0XD5, 0X8B,
88 0X57, 0X09, 0XEB, 0XB5, 0X36, 0X68, 0X8A, 0XD4, 0X95, 0XCB, 0X29, 0X77, 0XF4,
89 0XAA, 0X48, 0X16, 0XE9, 0XB7, 0X55, 0X0B, 0X88, 0XD6, 0X34, 0X6A, 0X2B, 0X75,
90 0X97, 0XC9, 0X4A, 0X14, 0XF6, 0XA8, 0X74, 0X2A, 0XC8, 0X96, 0X15, 0X4B, 0XA9,
91 0XF7, 0XB6, 0XE8, 0X0A, 0X54, 0XD7, 0X89, 0X6B, 0X35,
92};
93
104static uint8_t a_ds18b20_check_crc(uint8_t *buf, uint8_t len, uint8_t crc)
105{
106 uint8_t i;
107 uint8_t crc8 = 0;
108
109 for (i = 0; i < len; i++)
110 {
111 crc8 = gc_ds18b20_crc_table[crc8 ^ buf[i]]; /* calculate crc */
112 }
113 if (crc8 == crc) /* check crc */
114 {
115 return 0; /* return right */
116 }
117 else
118 {
119 return 1; /* return wrong */
120 }
121}
122
131static uint8_t a_ds18b20_reset(ds18b20_handle_t *handle)
132{
133 uint8_t retry = 0;
134 uint8_t res;
135
136 handle->disable_irq(); /* disable irq */
137 if (handle->bus_write(0) != 0) /* write 0 */
138 {
139 handle->enable_irq(); /* enable irq */
140 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
141
142 return 1; /* return error */
143 }
144 handle->delay_us(750); /* wait 750 us */
145 if (handle->bus_write(1) != 0) /* write 1 */
146 {
147 handle->enable_irq(); /* enable irq */
148 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
149
150 return 1; /* return error */
151 }
152 handle->delay_us(15); /* wait 15 us */
153 res = 1; /* reset res */
154 while ((res != 0) && (retry < 200)) /* wait 200 us */
155 {
156 if (handle->bus_read((uint8_t *)&res) != 0) /* read 1 bit */
157 {
158 handle->enable_irq(); /* enable irq */
159 handle->debug_print("ds18b20: bus read failed.\n"); /* read failed */
160
161 return 1; /* return error */
162 }
163 retry++; /* retry times++ */
164 handle->delay_us(1); /* delay 1 us */
165 }
166 if (retry >= 200) /* if retry times is over 200 times */
167 {
168 handle->enable_irq(); /* enable irq */
169 handle->debug_print("ds18b20: bus read no response.\n"); /* no response */
170
171 return 1; /* return error */
172 }
173 else
174 {
175 retry = 0; /* reset retry */
176 }
177 res = 0; /* reset res */
178 while ((res == 0)&& (retry < 240)) /* wait 240 us */
179 {
180 if (handle->bus_read((uint8_t *)&res) != 0) /* read one bit */
181 {
182 handle->enable_irq(); /* enable irq */
183 handle->debug_print("ds18b20: bus read failed.\n"); /* read failed */
184
185 return 1; /* return error */
186 }
187 retry++; /* retry times++ */
188 handle->delay_us(1); /* delay 1 us */
189 }
190 if (retry >= 240) /* if retry times is over 240 times */
191 {
192 handle->enable_irq(); /* enable irq */
193 handle->debug_print("ds18b20: bus read no response.\n"); /* no response */
194
195 return 1; /* return error */
196 }
197 handle->enable_irq(); /* enable irq */
198
199 return 0; /* success return 0 */
200}
201
211static uint8_t a_ds18b20_read_bit(ds18b20_handle_t *handle, uint8_t *data)
212{
213 if (handle->bus_write(0) != 0) /* write 0 */
214 {
215 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
216
217 return 1; /* return error */
218 }
219 handle->delay_us(2); /* wait 2 us */
220 if (handle->bus_write(1) != 0) /* write 1 */
221 {
222 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
223
224 return 1; /* return error */
225 }
226 handle->delay_us(12); /* wait 12 us */
227 if (handle->bus_read(data) != 0) /* read 1 bit */
228 {
229 handle->debug_print("ds18b20: bus read failed.\n"); /* read failed */
230
231 return 1; /* return error */
232 }
233 handle->delay_us(50); /* wait 50 us */
234
235 return 0; /* success return 0 */
236}
237
247static uint8_t a_ds18b20_read_byte(ds18b20_handle_t *handle, uint8_t *byte)
248{
249 uint8_t i, j;
250
251 *byte = 0; /* set byte 0 */
252 handle->disable_irq(); /* disable irq */
253 for (i = 1; i <= 8; i++)
254 {
255 if (a_ds18b20_read_bit(handle, (uint8_t *)&j) != 0) /* read 1 bit */
256 {
257 handle->enable_irq(); /* enable irq */
258 handle->debug_print("ds18b20: bus read byte failed.\n"); /* read byte failed */
259
260 return 1; /* return error */
261 }
262 *byte = (j << 7) | ((*byte) >> 1); /* set MSB */
263 }
264 handle->enable_irq(); /* enable irq */
265
266 return 0; /* success return 0 */
267}
268
278static uint8_t a_ds18b20_write_byte(ds18b20_handle_t *handle, uint8_t byte)
279{
280 uint8_t j;
281 uint8_t test_b;
282
283 handle->disable_irq(); /* disable irq */
284 for (j = 1; j <= 8; j++) /* run 8 times, 8 bits = 1 Byte */
285 {
286 test_b = byte & 0x01; /* get 1 bit */
287 byte = byte >> 1; /* right shift 1 bit */
288 if (test_b != 0) /* write 1 */
289 {
290 if (handle->bus_write(0) != 0) /* write 0 */
291 {
292 handle->enable_irq(); /* enable irq */
293 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
294
295 return 1; /* return error */
296 }
297 handle->delay_us(2); /* wait 2 us */
298 if (handle->bus_write(1) != 0) /* write 1 */
299 {
300 handle->enable_irq(); /* enable irq */
301 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
302
303 return 1; /* return error */
304 }
305 handle->delay_us(60); /* wait 60 us */
306 }
307 else /* write 0 */
308 {
309 if (handle->bus_write(0) != 0) /* write 0 */
310 {
311 handle->enable_irq(); /* enable irq */
312 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
313
314 return 1; /* return error */
315 }
316 handle->delay_us(60); /* wait 60 us */
317 if (handle->bus_write(1) != 0) /* write 1 */
318 {
319 handle->enable_irq(); /* enable irq */
320 handle->debug_print("ds18b20: bus write failed.\n"); /* write failed */
321
322 return 1; /* return error */
323 }
324 handle->delay_us(2); /* wait 2 us */
325 }
326 }
327 handle->enable_irq(); /* enable irq */
328
329 return 0; /* success return 0 */
330}
331
343{
344 if (handle == NULL) /* check handle */
345 {
346 return 2; /* return error */
347 }
348 if (handle->inited != 1) /* check handle initialization */
349 {
350 return 3; /* return error */
351 }
352
353 handle->mode = (uint8_t)mode; /* set mode */
354
355 return 0; /* success return 0 */
356}
357
369{
370 if (handle == NULL) /* check handle */
371 {
372 return 2; /* return error */
373 }
374 if (handle->inited != 1) /* check handle initialization */
375 {
376 return 3; /* return error */
377 }
378
379 *mode = (ds18b20_mode_t)(handle->mode); /* get mode */
380
381 return 0; /* success return 0 */
382}
383
394uint8_t ds18b20_set_rom(ds18b20_handle_t *handle, uint8_t rom[8])
395{
396 if (handle == NULL) /* check handle */
397 {
398 return 2; /* return error */
399 }
400 if (handle->inited != 1) /* check handle initialization */
401 {
402 return 3; /* return error */
403 }
404
405 memcpy(handle->rom, rom , 8); /* copy rom */
406
407 return 0; /* success return 0 */
408}
409
420uint8_t ds18b20_get_rom(ds18b20_handle_t *handle, uint8_t rom[8])
421{
422 uint8_t i;
423
424 if (handle == NULL) /* check handle */
425 {
426 return 2; /* return error */
427 }
428 if (handle->inited != 1) /* check handle initialization */
429 {
430 return 3; /* return error */
431 }
432
433 if (a_ds18b20_reset(handle) != 0) /* reset bus */
434 {
435 handle->debug_print("ds18b20: bus rest failed.\n"); /* reset bus failed */
436
437 return 1; /* return error */
438 }
439 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_ROM) != 0) /* write read rom command */
440 {
441 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
442
443 return 1; /* return error */
444 }
445 for (i = 0; i < 8; i++) /* read 8 bytes */
446 {
447 if (a_ds18b20_read_byte(handle, (uint8_t *)&rom[i]) != 0) /* read 1 byte */
448 {
449 handle->debug_print("ds18b20: read rom failed.\n"); /* read failed */
450
451 return 1; /* return error */
452 }
453 }
454
455 return 0; /* success return 0 */
456}
457
470{
471 uint8_t i, buf[9];
472
473 if (handle == NULL) /* check handle */
474 {
475 return 2; /* return error */
476 }
477 if (handle->inited != 1) /* check handle initialization */
478 {
479 return 3; /* return error */
480 }
481
482 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
483 {
484 if (a_ds18b20_reset(handle) != 0) /* reset bus */
485 {
486 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
487
488 return 1; /* return error */
489 }
490 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
491 {
492 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
493
494 return 1; /* return error */
495 }
496 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* sent read scratchpad command */
497 {
498 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
499
500 return 1; /* return error */
501 }
502 for (i = 0; i < 9; i++)
503 {
504 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read 9 bytes */
505 {
506 handle->debug_print("ds18b20: read data failed.\n"); /* read data failed */
507
508 return 1; /* return error */
509 }
510 }
511 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
512 {
513 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
514
515 return 1; /* return error */
516 }
517 if (a_ds18b20_reset(handle) != 0) /* reset bus */
518 {
519 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
520
521 return 1; /* return error */
522 }
523 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* write skip rom command */
524 {
525 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
526
527 return 1; /* return error */
528 }
529 if (a_ds18b20_write_byte(handle, DS18B20_CMD_WRITE_SCRATCHPAD) != 0) /* write scratchpad command */
530 {
531 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
532
533 return 1; /* return error */
534 }
535 buf[4] &= ~(3 << 5); /* clear resolution bits */
536 buf[4] |= resolution << 5; /* set resolution bits */
537 for (i = 0; i < 3; i++)
538 {
539 if (a_ds18b20_write_byte(handle, buf[2+i]) != 0) /* write command */
540 {
541 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
542
543 return 1; /* return error */
544 }
545 }
546
547 return 0; /* success return 0 */
548 }
549 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
550 {
551 if (a_ds18b20_reset(handle) != 0) /* reset bus */
552 {
553 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
554
555 return 1; /* return error */
556 }
557 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* send match rom command */
558 {
559 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
560
561 return 1; /* return error */
562 }
563 for (i = 0; i < 8; i++)
564 {
565 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
566 {
567 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
568
569 return 1; /* return error */
570 }
571 }
572 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* send read scratchpad command */
573 {
574 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
575
576 return 1; /* return error */
577 }
578 for (i = 0; i < 9; i++)
579 {
580 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read 9 bytes */
581 {
582 handle->debug_print("ds18b20: read data failed.\n"); /* read data failed */
583
584 return 1; /* return error */
585 }
586 }
587 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
588 {
589 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
590
591 return 1; /* return error */
592 }
593 if (a_ds18b20_reset(handle) != 0) /* reset bus */
594 {
595 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
596
597 return 1; /* return error */
598 }
599 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* match rom */
600 {
601 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
602
603 return 1; /* return error */
604 }
605 for (i = 0; i < 8; i++)
606 {
607 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
608 {
609 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
610
611 return 1; /* return error */
612 }
613 }
614 if (a_ds18b20_write_byte(handle, DS18B20_CMD_WRITE_SCRATCHPAD) != 0) /* write scratchpad command */
615 {
616 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
617
618 return 1; /* return error */
619 }
620 buf[4] &= ~(3 << 5); /* clear resolution bits */
621 buf[4] |= resolution << 5; /* set resolution bits */
622 for (i = 0; i < 3; i++)
623 {
624 if (a_ds18b20_write_byte(handle, buf[2 + i]) != 0) /* write command */
625 {
626 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
627
628 return 1; /* return error */
629 }
630 }
631
632 return 0; /* success return 0 */
633 }
634 else
635 {
636 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode invalid */
637
638 return 1; /* return error */
639 }
640}
641
654{
655 uint8_t i, buf[9];
656
657 if (handle == NULL) /* check handle */
658 {
659 return 2; /* return error */
660 }
661 if (handle->inited != 1) /* check handle initialization */
662 {
663 return 3; /* return error */
664 }
665
666 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
667 {
668 if (a_ds18b20_reset(handle) != 0) /* reset bus */
669 {
670 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus */
671
672 return 1; /* return error */
673 }
674 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
675 {
676 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
677
678 return 1; /* return error */
679 }
680 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* write read scratchpad command */
681 {
682 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
683
684 return 1; /* return error */
685 }
686 for (i = 0; i < 9; i++) /* read 9 bytes */
687 {
688 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read bytes */
689 {
690 handle->debug_print("ds18b20: read data failed.\n"); /* read data failed */
691
692 return 1; /* return error */
693 }
694 }
695 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
696 {
697 handle->debug_print("ds18b20: crc check error.\n"); /* crc check failed */
698
699 return 1; /* return error */
700 }
701 *resolution = (ds18b20_resolution_t)(buf[4] >> 5); /* get resolution */
702
703 return 0; /* success return 0 */
704 }
705 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
706 {
707 if (a_ds18b20_reset(handle) != 0) /* reset bus */
708 {
709 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset failed */
710
711 return 1; /* return error */
712 }
713 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
714 {
715 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
716
717 return 1; /* return error */
718 }
719 for (i = 0; i < 8; i++)
720 {
721 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
722 {
723 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
724
725 return 1; /* return error */
726 }
727 }
728 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* sent read scratchpad command */
729 {
730 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
731
732 return 1; /* return error */
733 }
734 for (i = 0; i < 9; i++)
735 {
736 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read 9 bytes */
737 {
738 handle->debug_print("ds18b20: read data failed.\n"); /* read failed */
739
740 return 1; /* return error */
741 }
742 }
743 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
744 {
745 handle->debug_print("ds18b20: crc check error.\n"); /* check crc failed */
746
747 return 1; /* return error */
748 }
749 *resolution = (ds18b20_resolution_t)(buf[4] >> 5); /* get resolution */
750
751 return 0; /* success return 0 */
752 }
753 else
754 {
755 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode invalid */
756
757 return 1; /* return error */
758 }
759}
760
773uint8_t ds18b20_scratchpad_set_alarm_threshold(ds18b20_handle_t *handle, int8_t threshold_high, int8_t threshold_low)
774{
775 uint8_t i, buf[9];
776
777 if (handle == NULL) /* check handle */
778 {
779 return 2; /* return error */
780 }
781 if (handle->inited != 1) /* check handle initialization */
782 {
783 return 3; /* return error */
784 }
785
786 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
787 {
788 if (a_ds18b20_reset(handle) != 0) /* reset bus */
789 {
790 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
791
792 return 1; /* return error */
793 }
794 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
795 {
796 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
797
798 return 1; /* return error */
799 }
800 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* sent read scratchpad command */
801 {
802 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
803
804 return 1; /* return error */
805 }
806 for (i = 0; i < 9; i++)
807 {
808 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read 9 bytes */
809 {
810 handle->debug_print("ds18b20: read data failed.\n"); /* read data failed */
811
812 return 1; /* return error */
813 }
814 }
815 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
816 {
817 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
818
819 return 1; /* return error */
820 }
821 if (a_ds18b20_reset(handle) != 0) /* reset bus */
822 {
823 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
824
825 return 1; /* return error */
826 }
827 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
828 {
829 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
830
831 return 1; /* return error */
832 }
833 if (a_ds18b20_write_byte(handle, DS18B20_CMD_WRITE_SCRATCHPAD) != 0) /* sent write scratchpad command */
834 {
835 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
836
837 return 1; /* return error */
838 }
839 buf[2] = (uint8_t)threshold_high; /* set high threshold */
840 buf[3] = (uint8_t)threshold_low; /* set low threshold */
841 for (i = 0; i < 3; i++)
842 {
843 if (a_ds18b20_write_byte(handle, buf[2 + i]) != 0) /* write command */
844 {
845 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
846
847 return 1; /* return error */
848 }
849 }
850
851 return 0; /* success return 0 */
852 }
853 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
854 {
855 if (a_ds18b20_reset(handle) != 0) /* reset bus */
856 {
857 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
858
859 return 1; /* return error */
860 }
861 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
862 {
863 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
864
865 return 1; /* return error */
866 }
867 for (i = 0; i < 8; i++)
868 {
869 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
870 {
871 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
872
873 return 1; /* return error */
874 }
875 }
876 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* sent read scratchpad command */
877 {
878 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
879
880 return 1; /* return error */
881 }
882 for (i = 0; i < 9; i++)
883 {
884 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read 9 byte */
885 {
886 handle->debug_print("ds18b20: read data failed.\n"); /* read data failed */
887
888 return 1; /* return error */
889 }
890 }
891 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
892 {
893 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
894
895 return 1; /* return error */
896 }
897 if (a_ds18b20_reset(handle) != 0) /* reset bus */
898 {
899 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
900
901 return 1; /* return error */
902 }
903 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
904 {
905 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
906
907 return 1; /* return error */
908 }
909 for (i = 0; i < 8; i++)
910 {
911 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
912 {
913 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
914
915 return 1; /* return error */
916 }
917 }
918 if (a_ds18b20_write_byte(handle, DS18B20_CMD_WRITE_SCRATCHPAD) != 0) /* write scratchpad command */
919 {
920 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
921
922 return 1; /* return error */
923 }
924 buf[2] = (uint8_t)threshold_high; /* set high threshold */
925 buf[3] = (uint8_t)threshold_low; /* set low threshold */
926 for (i = 0; i < 3; i++)
927 {
928 if (a_ds18b20_write_byte(handle, buf[2 + i]) != 0) /* write command */
929 {
930 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
931
932 return 1; /* return error */
933 }
934 }
935
936 return 0; /* success return 0 */
937 }
938 else
939 {
940 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
941
942 return 1; /* return error */
943 }
944}
945
958uint8_t ds18b20_scrachpad_get_alarm_threshold(ds18b20_handle_t *handle, int8_t *threshold_high, int8_t *threshold_low)
959{
960 uint8_t i, buf[9];
961
962 if (handle == NULL) /* check handle */
963 {
964 return 2; /* return error */
965 }
966 if (handle->inited != 1) /* check handle initialization */
967 {
968 return 3; /* return error */
969 }
970
971 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
972 {
973 if (a_ds18b20_reset(handle) != 0) /* reset bus */
974 {
975 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
976
977 return 1; /* return error */
978 }
979 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
980 {
981 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
982
983 return 1; /* return error */
984 }
985 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* sent read scratchpad command */
986 {
987 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
988
989 return 1; /* return error */
990 }
991 for (i = 0; i < 9; i++)
992 {
993 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read 9 bytes */
994 {
995 handle->debug_print("ds18b20: read data failed.\n"); /* read data failed */
996
997 return 1; /* return error */
998 }
999 }
1000 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
1001 {
1002 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
1003
1004 return 1; /* return error */
1005 }
1006 *threshold_high = (int8_t)(buf[2]); /* get high threshold */
1007 *threshold_low = (int8_t)(buf[3]); /* get low threshold */
1008
1009 return 0; /* success return 0 */
1010 }
1011 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1012 {
1013 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1014 {
1015 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1016
1017 return 1; /* return error */
1018 }
1019 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* write match rom command */
1020 {
1021 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1022
1023 return 1; /* return error */
1024 }
1025 for (i = 0; i < 8; i++)
1026 {
1027 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1028 {
1029 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1030
1031 return 1; /* return error */
1032 }
1033 }
1034 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* sent read scratchpad command */
1035 {
1036 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1037
1038 return 1; /* return error */
1039 }
1040 for (i = 0; i < 9; i++) /* read 9 bytes */
1041 {
1042 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read bytes */
1043 {
1044 handle->debug_print("ds18b20: read data failed.\n"); /* read byte failed */
1045
1046 return 1; /* return error */
1047 }
1048 }
1049 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
1050 {
1051 handle->debug_print("ds18b20: crc check error.\n"); /* crc error */
1052
1053 return 1; /* return error */
1054 }
1055 *threshold_high = (int8_t)(buf[2]); /* get high threshold */
1056 *threshold_low = (int8_t)(buf[3]); /* get low threshold */
1057
1058 return 0; /* success return 0 */
1059 }
1060 else
1061 {
1062 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode invalid */
1063
1064 return 1; /* return error */
1065 }
1066}
1067
1079{
1080 uint8_t i;
1081
1082 if (handle == NULL) /* check handle */
1083 {
1084 return 2; /* return error */
1085 }
1086 if (handle->inited != 1) /* check handle initialization */
1087 {
1088 return 3; /* return error */
1089 }
1090
1091 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
1092 {
1093 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1094 {
1095 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1096
1097 return 1; /* return error */
1098 }
1099 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
1100 {
1101 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1102
1103 return 1; /* return error */
1104 }
1105 if (a_ds18b20_write_byte(handle, DS18B20_CMD_COPY_SCRATCHPAD) != 0) /* write copy scratchpad command */
1106 {
1107 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1108
1109 return 1; /* return error */
1110 }
1111
1112 return 0; /* success return 0 */
1113 }
1114 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1115 {
1116 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1117 {
1118 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
1119
1120 return 1; /* return error */
1121 }
1122 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* write match rom command */
1123 {
1124 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1125
1126 return 1; /* return error */
1127 }
1128 for (i = 0; i < 8; i++)
1129 {
1130 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1131 {
1132 handle->debug_print("ds18b20: write command failed.\n"); /* write command */
1133
1134 return 1; /* return error */
1135 }
1136 }
1137 if (a_ds18b20_write_byte(handle, DS18B20_CMD_COPY_SCRATCHPAD) != 0) /* write copy scratchpad command */
1138 {
1139 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1140
1141 return 1; /* return error */
1142 }
1143
1144 return 0; /* success return 0 */
1145 }
1146 else
1147 {
1148 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
1149
1150 return 1; /* return error */
1151 }
1152}
1153
1165{
1166 uint8_t i;
1167
1168 if (handle == NULL) /* check handle */
1169 {
1170 return 2; /* return error */
1171 }
1172 if (handle->inited != 1) /* check handle initialization */
1173 {
1174 return 3; /* return error */
1175 }
1176
1177 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if we use in skip rom */
1178 {
1179 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1180 {
1181 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1182
1183 return 1; /* return error */
1184 }
1185 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
1186 {
1187 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1188
1189 return 1; /* return error */
1190 }
1191 if (a_ds18b20_write_byte(handle, DS18B20_CMD_RECALL_EE) != 0) /* write recall ee command */
1192 {
1193 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1194
1195 return 1; /* return error */
1196 }
1197
1198 return 0; /* success return 0 */
1199 }
1200 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1201 {
1202 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1203 {
1204 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1205
1206 return 1; /* return error */
1207 }
1208 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
1209 {
1210 handle->debug_print("ds18b20: write command failed.\n"); /* write command */
1211
1212 return 1; /* return error */
1213 }
1214 for (i = 0; i < 8; i++)
1215 {
1216 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1217 {
1218 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1219
1220 return 1; /* return error */
1221 }
1222 }
1223 if (a_ds18b20_write_byte(handle, DS18B20_CMD_RECALL_EE) != 0) /* sent recall ee command */
1224 {
1225 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1226
1227 return 1; /* return error */
1228 }
1229
1230 return 0; /* success return 0 */
1231 }
1232 else
1233 {
1234 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
1235
1236 return 1; /* return error */
1237 }
1238}
1239
1251uint8_t ds18b20_alarm_convert_to_register(ds18b20_handle_t *handle, float temp, int8_t *reg)
1252{
1253 if (handle == NULL) /* check handle */
1254 {
1255 return 2; /* return error */
1256 }
1257 if (handle->inited != 1) /* check handle initialization */
1258 {
1259 return 3; /* return error */
1260 }
1261
1262 *reg = (int8_t)(temp); /* convert real data to register data */
1263
1264 return 0; /* success return 0 */
1265}
1266
1278uint8_t ds18b20_alarm_convert_to_data(ds18b20_handle_t *handle, int8_t reg, float *temp)
1279{
1280 if (handle == NULL) /* check handle */
1281 {
1282 return 2; /* return error */
1283 }
1284 if (handle->inited != 1) /* check handle initialization */
1285 {
1286 return 3; /* return error */
1287 }
1288
1289 *temp = (float)(reg); /* convert raw data to real data */
1290
1291 return 0; /* success return 0 */
1292}
1293
1306{
1307 if (handle == NULL) /* check handle */
1308 {
1309 return 2; /* return error */
1310 }
1311 if (handle->debug_print == NULL) /* check debug_print */
1312 {
1313 return 3; /* return error */
1314 }
1315 if (handle->bus_init == NULL) /* check bus_init */
1316 {
1317 handle->debug_print("ds18b20: bus_init is null.\n"); /* bus_init is null */
1318
1319 return 3; /* return error */
1320 }
1321 if (handle->bus_deinit == NULL) /* check bus_deinit */
1322 {
1323 handle->debug_print("ds18b20: bus_deinit is null.\n"); /* bus_read is null */
1324
1325 return 3; /* return error */
1326 }
1327 if (handle->bus_read == NULL) /* check bus_read */
1328 {
1329 handle->debug_print("ds18b20: bus_read is null.\n"); /* bus_read is null */
1330
1331 return 3; /* return error */
1332 }
1333 if (handle->bus_write == NULL) /* check bus_write */
1334 {
1335 handle->debug_print("ds18b20: bus_write is null.\n"); /* bus_write is null */
1336
1337 return 3; /* return error */
1338 }
1339 if (handle->delay_ms == NULL) /* check delay_ms */
1340 {
1341 handle->debug_print("ds18b20: delay_ms is null.\n"); /* delay_ms is null */
1342
1343 return 3; /* return error */
1344 }
1345 if (handle->delay_us == NULL) /* check delay_us */
1346 {
1347 handle->debug_print("ds18b20: delay_us is null.\n"); /* delay_us is null */
1348
1349 return 3; /* return error */
1350 }
1351 if (handle->enable_irq == NULL) /* check enable_irq */
1352 {
1353 handle->debug_print("ds18b20: enable_irq is null.\n"); /* enable_irq is null */
1354
1355 return 3; /* return error */
1356 }
1357 if (handle->disable_irq == NULL) /* check disable_irq */
1358 {
1359 handle->debug_print("ds18b20: disable_irq is null.\n"); /* disable_irq is null */
1360
1361 return 3; /* return error */
1362 }
1363
1364 if (handle->bus_init() != 0) /* initialize bus */
1365 {
1366 handle->debug_print("ds18b20: bus init failed.\n"); /* bus innit failed */
1367
1368 return 1; /* return error */
1369 }
1370 if (a_ds18b20_reset(handle) != 0) /* reset chip */
1371 {
1372 handle->debug_print("ds18b20: reset failed.\n"); /* reset chip failed */
1373 (void)handle->bus_deinit(); /* close bus */
1374
1375 return 4; /* return error */
1376 }
1377 handle->inited = 1; /* flag finish initialization */
1378
1379 return 0; /* success return 0 */
1380}
1381
1393{
1394 if (handle == NULL) /* check handle */
1395 {
1396 return 2; /* return error */
1397 }
1398 if (handle->inited != 1) /* check handle initialization */
1399 {
1400 return 3; /* return error */
1401 }
1402
1403 if (handle->bus_deinit() != 0) /* close bus */
1404 {
1405 handle->debug_print("ds18b20: deinit failed.\n"); /* deinit failed */
1406
1407 return 1; /* return error */
1408 }
1409 handle->inited = 0; /* flag close */
1410
1411 return 0; /* success return 0 */
1412}
1413
1426uint8_t ds18b20_read(ds18b20_handle_t *handle, int16_t *raw, float *temp)
1427{
1428 uint8_t i, buf[9];
1429 uint8_t res;
1430 uint32_t cnt;
1431
1432 if (handle == NULL) /* check handle */
1433 {
1434 return 2; /* return error */
1435 }
1436 if (handle->inited != 1) /* check handle initialization */
1437 {
1438 return 3; /* return error */
1439 }
1440
1441 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
1442 {
1443 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1444 {
1445 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
1446
1447 return 1; /* return error */
1448 }
1449 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
1450 {
1451 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1452
1453 return 1; /* return error */
1454 }
1455 if (a_ds18b20_write_byte(handle, DS18B20_CMD_CONVERT_T) != 0) /* sent convert temp command */
1456 {
1457 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1458
1459 return 1; /* return error */
1460 }
1461 cnt = 0; /* reset cnt */
1462 res = 0; /* reset res */
1463 while ((res == 0) && (cnt < 100)) /* wait 1 s */
1464 {
1465 if (a_ds18b20_read_bit(handle, (uint8_t *)&res) != 0) /* read 1 bit */
1466 {
1467 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
1468
1469 return 1; /* return error */
1470 }
1471 handle->delay_ms(10); /* delay 10 ms */
1472 cnt++; /* cnt++ */
1473 }
1474 if (cnt >= 100) /* if cnt is over 100 times */
1475 {
1476 handle->debug_print("ds18b20: bus read timeout.\n"); /* bus read timeout */
1477
1478 return 1; /* reset error */
1479 }
1480 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1481 {
1482 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
1483
1484 return 1; /* return error */
1485 }
1486 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* send skip rom command */
1487 {
1488 handle->debug_print("ds18b20: write command failed.\n"); /* write command */
1489
1490 return 1; /* return error */
1491 }
1492 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* write read scratchpad command */
1493 {
1494 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1495
1496 return 1; /* return error */
1497 }
1498 for (i = 0; i < 9; i++) /* read 9 bytes */
1499 {
1500 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read bytes */
1501 {
1502 handle->debug_print("ds18b20: read byte failed.\n"); /* read byte failed */
1503
1504 return 1; /* return error */
1505 }
1506 }
1507 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
1508 {
1509 handle->debug_print("ds18b20: crc check error.\n"); /* crc check failed */
1510
1511 return 1; /* return error */
1512 }
1513 *raw = (int16_t)(((uint16_t)buf[1]) << 8) | buf[0]; /* get raw data */
1514 if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_9BIT) /* if 9 bit resolution */
1515 {
1516 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1517 {
1518 *raw = (*raw ) >> 3; /* right shift 3 */
1519 *raw = (*raw) | 0xE000U; /* set negative part */
1520 }
1521 else /* if positive */
1522 {
1523 *raw = (*raw ) >> 3; /* right shift 3 */
1524 }
1525 *temp = (float)(*raw) * 0.5f; /* convert to real data */
1526 }
1527 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_10BIT) /* if 10 bit resolution */
1528 {
1529 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1530 {
1531 *raw = (*raw ) >> 2; /* right shift 2 */
1532 *raw = (*raw) | 0xC000U; /* set negative part */
1533 }
1534 else
1535 {
1536 *raw = (*raw ) >> 2; /* right shift 2 */
1537 }
1538 *temp = (float)(*raw) * 0.25f; /* convert to real data */
1539 }
1540 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_11BIT) /* if 11 bit resolution */
1541 {
1542 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1543 {
1544 *raw = (*raw ) >> 1; /* right shift 1 */
1545 *raw = (*raw) | 0x8000U; /* set negative part */
1546 }
1547 else
1548 {
1549 *raw = (*raw ) >> 1; /* right shift 1 */
1550 }
1551 *temp = (float)(*raw) * 0.125f; /* convert to real data */
1552 }
1553 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_12BIT) /* if 12 bit resolution */
1554 {
1555 *raw = (*raw ) >> 0; /* right shift 0 */
1556 *temp = (float)(*raw) * 0.0625f; /* convert to real data */
1557 }
1558 else
1559 {
1560 handle->debug_print("ds18b20: resolution invalid.\n"); /* resolution is invalid */
1561
1562 return 1; /* return error */
1563 }
1564
1565 return 0; /* success return 0 */
1566 }
1567 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1568 {
1569 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1570 {
1571 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1572
1573 return 1; /* return error */
1574 }
1575 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
1576 {
1577 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1578
1579 return 1; /* return error */
1580 }
1581 for (i = 0; i < 8; i++)
1582 {
1583 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1584 {
1585 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1586
1587 return 1; /* return error */
1588 }
1589 }
1590 if (a_ds18b20_write_byte(handle, DS18B20_CMD_CONVERT_T) != 0) /* sent convert temp command */
1591 {
1592 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1593
1594 return 1; /* return error */
1595 }
1596 cnt = 0; /* reset cnt */
1597 res = 0; /* reset res */
1598 while ((res == 0) && (cnt < 100)) /* read max 1s */
1599 {
1600 if (a_ds18b20_read_bit(handle, (uint8_t *)&res) != 0) /* read 1 bit */
1601 {
1602 handle->debug_print("ds18b20: read bit failed.\n"); /* read 1 bit failed */
1603
1604 return 1; /* return error */
1605 }
1606 handle->delay_ms(10); /* delay 10 ms */
1607 cnt++; /* cnt++ */
1608 }
1609 if (cnt >= 100) /* if cnt is over 100 times */
1610 {
1611 handle->debug_print("ds18b20: bus read timeout.\n"); /* read timeout */
1612
1613 return 1; /* return error */
1614 }
1615 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1616 {
1617 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1618
1619 return 1; /* return error */
1620 }
1621 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
1622 {
1623 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1624
1625 return 1; /* return error */
1626 }
1627 for (i = 0; i < 8; i++)
1628 {
1629 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1630 {
1631 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1632
1633 return 1; /* return error */
1634 }
1635 }
1636 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* send read scratchpad command */
1637 {
1638 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1639
1640 return 1; /* return error */
1641 }
1642 for (i = 0; i < 9; i++) /* read 9 bytes */
1643 {
1644 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read byte */
1645 {
1646 handle->debug_print("ds18b20: read byte failed.\n"); /* read failed */
1647
1648 return 1; /* return error */
1649 }
1650 }
1651 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
1652 {
1653 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
1654
1655 return 1; /* return error */
1656 }
1657 *raw = (int16_t)(((uint16_t)buf[1]) << 8) | buf[0]; /* get raw data */
1658 if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_9BIT) /* if 9 bit resolution */
1659 {
1660 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1661 {
1662 *raw = (*raw ) >> 3; /* right shift 3 */
1663 *raw = (*raw) | 0xE000U; /* set negative part */
1664 }
1665 else /* if positive */
1666 {
1667 *raw = (*raw ) >> 3; /* right shift 3 */
1668 }
1669 *temp = (float)(*raw) * 0.5f; /* convert to real data */
1670 }
1671 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_10BIT) /* if 10 bit resolution */
1672 {
1673 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1674 {
1675 *raw = (*raw ) >> 2; /* right shift 2 */
1676 *raw = (*raw) | 0xC000U; /* set negative part */
1677 }
1678 else
1679 {
1680 *raw = (*raw ) >> 2; /* right shift 2 */
1681 }
1682 *temp = (float)(*raw) * 0.25f; /* convert to real data */
1683 }
1684 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_11BIT) /* if 11 bit resolution */
1685 {
1686 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1687 {
1688 *raw = (*raw ) >> 1; /* right shift 1 */
1689 *raw = (*raw) | 0x8000U; /* set negative part */
1690 }
1691 else
1692 {
1693 *raw = (*raw ) >> 1; /* right shift 1 */
1694 }
1695 *temp = (float)(*raw) * 0.125f; /* convert to real data */
1696 }
1697 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_12BIT) /* if 12 bit resolution */
1698 {
1699 *raw = (*raw ) >> 0; /* right shift 0 */
1700 *temp = (float)(*raw) * 0.0625f; /* convert to real data */
1701 }
1702 else
1703 {
1704 handle->debug_print("ds18b20: resolution invalid.\n"); /* resolution is invalid */
1705
1706 return 1; /* return error */
1707 }
1708
1709 return 0; /* success return 0 */
1710 }
1711 else
1712 {
1713 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
1714
1715 return 1; /* return error */
1716 }
1717}
1718
1728static uint8_t a_ds18b20_read_2bit(ds18b20_handle_t *handle, uint8_t *data)
1729{
1730 uint8_t i;
1731 uint8_t res;
1732
1733 *data = 0; /* reset data */
1734 handle->disable_irq(); /* disable irq */
1735 for (i = 0; i < 2; i++) /* read 2 bit */
1736 {
1737 *data <<= 1; /* left shift 1 */
1738 if (a_ds18b20_read_bit(handle, (uint8_t *)&res) != 0) /* read one bit */
1739 {
1740 handle->enable_irq(); /* enable irq */
1741 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
1742
1743 return 1; /* return error */
1744 }
1745 *data = (*data) | res; /* get 1 bit */
1746 }
1747 handle->enable_irq(); /* enable irq */
1748
1749 return 0; /* success return 0 */
1750}
1751
1752
1762static uint8_t a_ds18b20_write_bit(ds18b20_handle_t *handle, uint8_t bit)
1763{
1764 handle->disable_irq(); /* disable irq */
1765 if (handle->bus_write(0) != 0) /* write 0 */
1766 {
1767 handle->enable_irq(); /* enable irq */
1768 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1769
1770 return 1; /* return error */
1771 }
1772 handle->delay_us(12); /* wait 12 us */
1773 if (handle->bus_write(bit) != 0) /* write bit */
1774 {
1775 handle->enable_irq(); /* enable irq */
1776 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1777
1778 return 1; /* return error */
1779 }
1780 handle->delay_us(30); /* wait 30 us */
1781 if (handle->bus_write(1) != 0) /* write 1 */
1782 {
1783 handle->enable_irq(); /* enable irq */
1784 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1785
1786 return 1; /* return error */
1787 }
1788 handle->delay_us(5); /* wait 5 us */
1789 handle->enable_irq(); /* enable irq */
1790
1791 return 0; /* success return 0 */
1792}
1793
1805static uint8_t a_ds18b20_search(ds18b20_handle_t *handle, uint8_t (*pid)[8], uint8_t cmd, uint8_t *number)
1806{
1807 uint8_t k, l = 0, conflict_bit, m, n;
1808 uint8_t buffer[DS18B20_MAX_SEARCH_SIZE];
1809 uint8_t ss[64];
1810 uint8_t s = 0;
1811 uint8_t num = 0;
1812
1813 if ((*number) > DS18B20_MAX_SEARCH_SIZE) /* check number */
1814 {
1815 handle->debug_print("ds18b20: number is over DS18B20_MAX_SEARCH_SIZE.\n"); /* number is over */
1816
1817 return 1; /* return error */
1818 }
1819 memset((uint8_t *)buffer, 0, DS18B20_MAX_SEARCH_SIZE); /* clear buffer */
1820 memset((uint8_t *)ss, 0, sizeof(uint8_t) * 64); /* clear buffer */
1821 do
1822 {
1823 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1824 {
1825 handle->debug_print("ds18b20: reset failed.\n"); /* reset bus failed */
1826
1827 return 1; /* return error */
1828 }
1829 if (a_ds18b20_write_byte(handle, cmd) != 0) /* write 1 byte */
1830 {
1831 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1832
1833 return 1; /* return error */
1834 }
1835 for (m = 0; m < 8; m++) /* read 8 byte */
1836 {
1837 for (n = 0; n < 8; n++) /* read 8 bit */
1838 {
1839 if (a_ds18b20_read_2bit(handle, (uint8_t *)&k) != 0) /* read 2 bit */
1840 {
1841 handle->debug_print("ds18b20: read 2bit failed.\n"); /* read 2 bit failed */
1842
1843 return 1; /* return error */
1844 }
1845 k = k & 0x03; /* get valid bits */
1846 s = s >> 1; /* right shift 1 bit */
1847 if (k == 0x01) /* 0000 0001 */
1848 {
1849 if (a_ds18b20_write_bit(handle, 0) != 0) /* write 0 */
1850 {
1851 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1852
1853 return 1; /* return error */
1854 }
1855 ss[(m * 8 + n)] = 0; /* set 0 */
1856 }
1857 else if (k == 0x02) /* 0000 0010 */
1858 {
1859 s = s | 0x80; /* set 7 bit */
1860 if (a_ds18b20_write_bit(handle, 1) != 0) /* write 1 */
1861 {
1862 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1863
1864 return 1; /* return error */
1865 }
1866 ss[(m * 8 + n)] = 1; /* set 1 */
1867 }
1868 else if (k == 0x00) /* if 0000 */
1869 {
1870 conflict_bit = (uint8_t)(m * 8 + n + 1); /* flag conflict bit */
1871 if (conflict_bit > buffer[l]) /* check buffer */
1872 {
1873 if (a_ds18b20_write_bit(handle, 0) != 0) /* write 0 */
1874 {
1875 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1876
1877 return 1; /* return error */
1878 }
1879 ss[(m * 8 + n)] = 0; /* set 0 */
1880 buffer[++l] = conflict_bit; /* set conflict bit */
1881 }
1882 else if (conflict_bit < buffer[l]) /* if > buffer */
1883 {
1884 s = s|((ss[(m * 8 + n)] & 0x01) << 7); /* get s */
1885 if (a_ds18b20_write_bit(handle, ss[(m*8+n)]) != 0) /* write data */
1886 {
1887 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1888
1889 return 1; /* return error */
1890 }
1891 }
1892 else if (conflict_bit == buffer[l]) /* if == buffer */
1893 {
1894 s = s | 0x80; /* set 7 bit */
1895 if (a_ds18b20_write_bit(handle, 1) != 0) /* write 1 */
1896 {
1897 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1898
1899 return 1; /* return error */
1900 }
1901 ss[(m * 8 + n)] = 1; /* set 1 */
1902 l = l-1; /* l-- */
1903 }
1904 else
1905 {
1906
1907 }
1908 }
1909 else
1910 {
1911 *number = num; /* save num */
1912
1913 return 0; /* success return 0 */
1914 }
1915 handle->delay_us(5); /* delay 5 us */
1916 }
1917 pid[num][m] = s; /* save s */
1918 s = 0; /* reset s */
1919 }
1920 num++; /* num++ */
1921 if (num > (*number)) /* check num range */
1922 {
1923 break; /* break */
1924 }
1925 } while (buffer[l] != 0); /* check buffer[l] */
1926 *number = num; /* set number */
1927
1928 return 0; /* success return 0 */
1929}
1930
1941uint8_t ds18b20_search_rom(ds18b20_handle_t *handle, uint8_t (*rom)[8], uint8_t *num)
1942{
1943 if (handle == NULL) /* check handle */
1944 {
1945 return 2; /* return error */
1946 }
1947 if (handle->inited != 1) /* check handle initialization */
1948 {
1949 return 3; /* return error */
1950 }
1951
1952 return a_ds18b20_search(handle, rom, DS18B20_CMD_SEARCH_ROM, num); /* return search result */
1953}
1954
1965uint8_t ds18b20_search_alarm(ds18b20_handle_t *handle, uint8_t (*rom)[8], uint8_t *num)
1966{
1967 if (handle == NULL) /* check handle */
1968 {
1969 return 2; /* return error */
1970 }
1971 if (handle->inited != 1) /* check handle initialization */
1972 {
1973 return 3; /* return error */
1974 }
1975
1976 return a_ds18b20_search(handle, rom, DS18B20_CMD_ALARM_SEARCH, num); /* return search result */
1977}
1978
1989{
1990 uint8_t i;
1991
1992 if (handle == NULL) /* check handle */
1993 {
1994 return 2; /* return error */
1995 }
1996 if (handle->inited != 1) /* check handle initialization */
1997 {
1998 return 3; /* return error */
1999 }
2000
2001 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
2002 {
2003 if (a_ds18b20_reset(handle) != 0) /* bus reset */
2004 {
2005 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
2006
2007 return 1; /* return error */
2008 }
2009 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
2010 {
2011 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2012
2013 return 1; /* return error */
2014 }
2015 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_POWER_SUPPLY) != 0) /* write read power supply command */
2016 {
2017 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2018
2019 return 1; /* return error */
2020 }
2021 if (a_ds18b20_read_bit(handle, (uint8_t *)power_mode) != 0) /* get power mode */
2022 {
2023 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
2024
2025 return 1; /* return error */
2026 }
2027
2028 return 0; /* success return 0 */
2029 }
2030 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
2031 {
2032 if (a_ds18b20_reset(handle) != 0) /* bus reset */
2033 {
2034 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
2035
2036 return 1; /* return error */
2037 }
2038 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
2039 {
2040 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2041
2042 return 1; /* return error */
2043 }
2044 for (i = 0; i < 8; i++)
2045 {
2046 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
2047 {
2048 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2049
2050 return 1; /* return error */
2051 }
2052 }
2053 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_POWER_SUPPLY) != 0) /* write read power supply */
2054 {
2055 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2056
2057 return 1; /* return error */
2058 }
2059 if (a_ds18b20_read_bit(handle, (uint8_t *)power_mode) != 0) /* get power mode */
2060 {
2061 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
2062
2063 return 1; /* return error */
2064 }
2065
2066 return 0; /* success return 0 */
2067 }
2068 else
2069 {
2070 handle->debug_print("ds18b20: mode invalid.\n"); /* mode is invalid */
2071
2072 return 1; /* return error */
2073 }
2074}
2075
2085{
2086 if (info == NULL) /* check handle */
2087 {
2088 return 2; /* return error */
2089 }
2090
2091 memset(info, 0, sizeof(ds18b20_info_t)); /* initialize ds18b20 info structure */
2092 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2093 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2094 strncpy(info->interface, "GPIO", 8); /* copy interface name */
2095 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2096 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2097 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2098 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2099 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2100 info->driver_version = DRIVER_VERSION; /* set driver version */
2101
2102 return 0; /* success return 0 */
2103}
#define MAX_CURRENT
const uint8_t gc_ds18b20_crc_table[256]
crc table
#define DS18B20_CMD_READ_SCRATCHPAD
#define DS18B20_CMD_WRITE_SCRATCHPAD
#define DS18B20_CMD_SKIP_ROM
#define SUPPLY_VOLTAGE_MAX
#define DS18B20_CMD_MATCH_ROM
#define DS18B20_CMD_READ_ROM
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define DS18B20_CMD_RECALL_EE
#define DS18B20_CMD_CONVERT_T
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define DS18B20_CMD_SEARCH_ROM
chip command definition
#define DS18B20_CMD_COPY_SCRATCHPAD
#define DS18B20_CMD_READ_POWER_SUPPLY
#define DS18B20_CMD_ALARM_SEARCH
driver ds18b20 header file
uint8_t ds18b20_alarm_convert_to_data(ds18b20_handle_t *handle, int8_t reg, float *temp)
convert the register data to the alarm temperature
uint8_t ds18b20_search_alarm(ds18b20_handle_t *handle, uint8_t(*rom)[8], uint8_t *num)
search the ds18b20 alarm rom
#define DS18B20_MAX_SEARCH_SIZE
ds18b20 max search size definition
uint8_t ds18b20_get_power_mode(ds18b20_handle_t *handle, ds18b20_power_mode_t *power_mode)
get the power mode
uint8_t ds18b20_search_rom(ds18b20_handle_t *handle, uint8_t(*rom)[8], uint8_t *num)
search the ds18b20 rom
uint8_t ds18b20_scrachpad_get_alarm_threshold(ds18b20_handle_t *handle, int8_t *threshold_high, int8_t *threshold_low)
get the alarm threshold in the scratchpad
uint8_t ds18b20_scratchpad_set_alarm_threshold(ds18b20_handle_t *handle, int8_t threshold_high, int8_t threshold_low)
set the alarm threshold in the scratchpad
uint8_t ds18b20_alarm_convert_to_register(ds18b20_handle_t *handle, float temp, int8_t *reg)
convert the alarm temperature to the register data
uint8_t ds18b20_scratchpad_get_resolution(ds18b20_handle_t *handle, ds18b20_resolution_t *resolution)
get the resolution in the scratchpad
ds18b20_power_mode_t
ds18b20 power mode enumeration definition
uint8_t ds18b20_init(ds18b20_handle_t *handle)
initialize the chip
uint8_t ds18b20_set_rom(ds18b20_handle_t *handle, uint8_t rom[8])
set the handle rom
uint8_t ds18b20_copy_eeprom_to_scratchpad(ds18b20_handle_t *handle)
copy the eeprom content to the scratchpad
uint8_t ds18b20_copy_scratchpad_to_eeprom(ds18b20_handle_t *handle)
copy the scratchpad content to the eeprom
ds18b20_resolution_t
ds18b20 resolution enumeration definition
uint8_t ds18b20_info(ds18b20_info_t *info)
get chip's information
uint8_t ds18b20_read(ds18b20_handle_t *handle, int16_t *raw, float *temp)
read data from the chip
uint8_t ds18b20_scratchpad_set_resolution(ds18b20_handle_t *handle, ds18b20_resolution_t resolution)
set the resolution in the scratchpad
struct ds18b20_handle_s ds18b20_handle_t
ds18b20 handle structure definition
ds18b20_mode_t
ds18b20 mode enumeration definition
uint8_t ds18b20_deinit(ds18b20_handle_t *handle)
close the chip
uint8_t ds18b20_get_mode(ds18b20_handle_t *handle, ds18b20_mode_t *mode)
get the chip mode
uint8_t ds18b20_set_mode(ds18b20_handle_t *handle, ds18b20_mode_t mode)
set the chip mode
struct ds18b20_info_s ds18b20_info_t
ds18b20 info structure definition
uint8_t ds18b20_get_rom(ds18b20_handle_t *handle, uint8_t rom[8])
get the chip rom
@ DS18B20_RESOLUTION_11BIT
@ DS18B20_RESOLUTION_9BIT
@ DS18B20_RESOLUTION_10BIT
@ DS18B20_RESOLUTION_12BIT
@ DS18B20_MODE_SKIP_ROM
@ DS18B20_MODE_MATCH_ROM
void(* enable_irq)(void)
uint8_t(* bus_deinit)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
void(* delay_us)(uint32_t us)
void(* disable_irq)(void)
uint8_t(* bus_read)(uint8_t *value)
uint8_t(* bus_write)(uint8_t value)
uint8_t(* bus_init)(void)
uint32_t driver_version
char manufacturer_name[32]