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
70static const uint8_t gcs_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 = gcs_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_scratchpad_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 handle->delay_ms(12); /* delay 12ms */
1112
1113 return 0; /* success return 0 */
1114 }
1115 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1116 {
1117 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1118 {
1119 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
1120
1121 return 1; /* return error */
1122 }
1123 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* write match rom command */
1124 {
1125 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1126
1127 return 1; /* return error */
1128 }
1129 for (i = 0; i < 8; i++)
1130 {
1131 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1132 {
1133 handle->debug_print("ds18b20: write command failed.\n"); /* write command */
1134
1135 return 1; /* return error */
1136 }
1137 }
1138 if (a_ds18b20_write_byte(handle, DS18B20_CMD_COPY_SCRATCHPAD) != 0) /* write copy scratchpad command */
1139 {
1140 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1141
1142 return 1; /* return error */
1143 }
1144 handle->delay_ms(12); /* delay 12ms */
1145
1146 return 0; /* success return 0 */
1147 }
1148 else
1149 {
1150 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
1151
1152 return 1; /* return error */
1153 }
1154}
1155
1167{
1168 uint8_t i;
1169
1170 if (handle == NULL) /* check handle */
1171 {
1172 return 2; /* return error */
1173 }
1174 if (handle->inited != 1) /* check handle initialization */
1175 {
1176 return 3; /* return error */
1177 }
1178
1179 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if we use in skip rom */
1180 {
1181 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1182 {
1183 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1184
1185 return 1; /* return error */
1186 }
1187 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
1188 {
1189 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1190
1191 return 1; /* return error */
1192 }
1193 if (a_ds18b20_write_byte(handle, DS18B20_CMD_RECALL_EE) != 0) /* write recall ee command */
1194 {
1195 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1196
1197 return 1; /* return error */
1198 }
1199
1200 return 0; /* success return 0 */
1201 }
1202 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1203 {
1204 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1205 {
1206 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1207
1208 return 1; /* return error */
1209 }
1210 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
1211 {
1212 handle->debug_print("ds18b20: write command failed.\n"); /* write command */
1213
1214 return 1; /* return error */
1215 }
1216 for (i = 0; i < 8; i++)
1217 {
1218 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1219 {
1220 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1221
1222 return 1; /* return error */
1223 }
1224 }
1225 if (a_ds18b20_write_byte(handle, DS18B20_CMD_RECALL_EE) != 0) /* sent recall ee command */
1226 {
1227 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1228
1229 return 1; /* return error */
1230 }
1231
1232 return 0; /* success return 0 */
1233 }
1234 else
1235 {
1236 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
1237
1238 return 1; /* return error */
1239 }
1240}
1241
1253uint8_t ds18b20_alarm_convert_to_register(ds18b20_handle_t *handle, float temp, int8_t *reg)
1254{
1255 if (handle == NULL) /* check handle */
1256 {
1257 return 2; /* return error */
1258 }
1259 if (handle->inited != 1) /* check handle initialization */
1260 {
1261 return 3; /* return error */
1262 }
1263
1264 *reg = (int8_t)(temp); /* convert real data to register data */
1265
1266 return 0; /* success return 0 */
1267}
1268
1280uint8_t ds18b20_alarm_convert_to_data(ds18b20_handle_t *handle, int8_t reg, float *temp)
1281{
1282 if (handle == NULL) /* check handle */
1283 {
1284 return 2; /* return error */
1285 }
1286 if (handle->inited != 1) /* check handle initialization */
1287 {
1288 return 3; /* return error */
1289 }
1290
1291 *temp = (float)(reg); /* convert raw data to real data */
1292
1293 return 0; /* success return 0 */
1294}
1295
1308{
1309 if (handle == NULL) /* check handle */
1310 {
1311 return 2; /* return error */
1312 }
1313 if (handle->debug_print == NULL) /* check debug_print */
1314 {
1315 return 3; /* return error */
1316 }
1317 if (handle->bus_init == NULL) /* check bus_init */
1318 {
1319 handle->debug_print("ds18b20: bus_init is null.\n"); /* bus_init is null */
1320
1321 return 3; /* return error */
1322 }
1323 if (handle->bus_deinit == NULL) /* check bus_deinit */
1324 {
1325 handle->debug_print("ds18b20: bus_deinit is null.\n"); /* bus_read is null */
1326
1327 return 3; /* return error */
1328 }
1329 if (handle->bus_read == NULL) /* check bus_read */
1330 {
1331 handle->debug_print("ds18b20: bus_read is null.\n"); /* bus_read is null */
1332
1333 return 3; /* return error */
1334 }
1335 if (handle->bus_write == NULL) /* check bus_write */
1336 {
1337 handle->debug_print("ds18b20: bus_write is null.\n"); /* bus_write is null */
1338
1339 return 3; /* return error */
1340 }
1341 if (handle->delay_ms == NULL) /* check delay_ms */
1342 {
1343 handle->debug_print("ds18b20: delay_ms is null.\n"); /* delay_ms is null */
1344
1345 return 3; /* return error */
1346 }
1347 if (handle->delay_us == NULL) /* check delay_us */
1348 {
1349 handle->debug_print("ds18b20: delay_us is null.\n"); /* delay_us is null */
1350
1351 return 3; /* return error */
1352 }
1353 if (handle->enable_irq == NULL) /* check enable_irq */
1354 {
1355 handle->debug_print("ds18b20: enable_irq is null.\n"); /* enable_irq is null */
1356
1357 return 3; /* return error */
1358 }
1359 if (handle->disable_irq == NULL) /* check disable_irq */
1360 {
1361 handle->debug_print("ds18b20: disable_irq is null.\n"); /* disable_irq is null */
1362
1363 return 3; /* return error */
1364 }
1365
1366 if (handle->bus_init() != 0) /* initialize bus */
1367 {
1368 handle->debug_print("ds18b20: bus init failed.\n"); /* bus innit failed */
1369
1370 return 1; /* return error */
1371 }
1372 if (a_ds18b20_reset(handle) != 0) /* reset chip */
1373 {
1374 handle->debug_print("ds18b20: reset failed.\n"); /* reset chip failed */
1375 (void)handle->bus_deinit(); /* close bus */
1376
1377 return 4; /* return error */
1378 }
1379 handle->inited = 1; /* flag finish initialization */
1380
1381 return 0; /* success return 0 */
1382}
1383
1395{
1396 if (handle == NULL) /* check handle */
1397 {
1398 return 2; /* return error */
1399 }
1400 if (handle->inited != 1) /* check handle initialization */
1401 {
1402 return 3; /* return error */
1403 }
1404
1405 if (handle->bus_deinit() != 0) /* close bus */
1406 {
1407 handle->debug_print("ds18b20: deinit failed.\n"); /* deinit failed */
1408
1409 return 1; /* return error */
1410 }
1411 handle->inited = 0; /* flag close */
1412
1413 return 0; /* success return 0 */
1414}
1415
1428uint8_t ds18b20_read(ds18b20_handle_t *handle, int16_t *raw, float *temp)
1429{
1430 uint8_t i, buf[9];
1431 uint8_t res;
1432 uint32_t cnt;
1433
1434 if (handle == NULL) /* check handle */
1435 {
1436 return 2; /* return error */
1437 }
1438 if (handle->inited != 1) /* check handle initialization */
1439 {
1440 return 3; /* return error */
1441 }
1442
1443 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
1444 {
1445 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1446 {
1447 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
1448
1449 return 1; /* return error */
1450 }
1451 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
1452 {
1453 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1454
1455 return 1; /* return error */
1456 }
1457 if (a_ds18b20_write_byte(handle, DS18B20_CMD_CONVERT_T) != 0) /* sent convert temp command */
1458 {
1459 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1460
1461 return 1; /* return error */
1462 }
1463 cnt = 0; /* reset cnt */
1464 res = 0; /* reset res */
1465 while ((res == 0) && (cnt < 100)) /* wait 1 s */
1466 {
1467 if (a_ds18b20_read_bit(handle, (uint8_t *)&res) != 0) /* read 1 bit */
1468 {
1469 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
1470
1471 return 1; /* return error */
1472 }
1473 handle->delay_ms(10); /* delay 10 ms */
1474 cnt++; /* cnt++ */
1475 }
1476 if (cnt >= 100) /* if cnt is over 100 times */
1477 {
1478 handle->debug_print("ds18b20: bus read timeout.\n"); /* bus read timeout */
1479
1480 return 1; /* reset error */
1481 }
1482 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1483 {
1484 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
1485
1486 return 1; /* return error */
1487 }
1488 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* send skip rom command */
1489 {
1490 handle->debug_print("ds18b20: write command failed.\n"); /* write command */
1491
1492 return 1; /* return error */
1493 }
1494 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* write read scratchpad command */
1495 {
1496 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1497
1498 return 1; /* return error */
1499 }
1500 for (i = 0; i < 9; i++) /* read 9 bytes */
1501 {
1502 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read bytes */
1503 {
1504 handle->debug_print("ds18b20: read byte failed.\n"); /* read byte failed */
1505
1506 return 1; /* return error */
1507 }
1508 }
1509 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
1510 {
1511 handle->debug_print("ds18b20: crc check error.\n"); /* crc check failed */
1512
1513 return 1; /* return error */
1514 }
1515 *raw = (int16_t)(((uint16_t)buf[1]) << 8) | buf[0]; /* get raw data */
1516 if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_9BIT) /* if 9 bit resolution */
1517 {
1518 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1519 {
1520 *raw = (*raw ) >> 3; /* right shift 3 */
1521 *raw = (*raw) | 0xE000U; /* set negative part */
1522 }
1523 else /* if positive */
1524 {
1525 *raw = (*raw ) >> 3; /* right shift 3 */
1526 }
1527 *temp = (float)(*raw) * 0.5f; /* convert to real data */
1528 }
1529 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_10BIT) /* if 10 bit resolution */
1530 {
1531 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1532 {
1533 *raw = (*raw ) >> 2; /* right shift 2 */
1534 *raw = (*raw) | 0xC000U; /* set negative part */
1535 }
1536 else
1537 {
1538 *raw = (*raw ) >> 2; /* right shift 2 */
1539 }
1540 *temp = (float)(*raw) * 0.25f; /* convert to real data */
1541 }
1542 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_11BIT) /* if 11 bit resolution */
1543 {
1544 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1545 {
1546 *raw = (*raw ) >> 1; /* right shift 1 */
1547 *raw = (*raw) | 0x8000U; /* set negative part */
1548 }
1549 else
1550 {
1551 *raw = (*raw ) >> 1; /* right shift 1 */
1552 }
1553 *temp = (float)(*raw) * 0.125f; /* convert to real data */
1554 }
1555 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_12BIT) /* if 12 bit resolution */
1556 {
1557 *raw = (*raw ) >> 0; /* right shift 0 */
1558 *temp = (float)(*raw) * 0.0625f; /* convert to real data */
1559 }
1560 else
1561 {
1562 handle->debug_print("ds18b20: resolution invalid.\n"); /* resolution is invalid */
1563
1564 return 1; /* return error */
1565 }
1566
1567 return 0; /* success return 0 */
1568 }
1569 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
1570 {
1571 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1572 {
1573 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1574
1575 return 1; /* return error */
1576 }
1577 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
1578 {
1579 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1580
1581 return 1; /* return error */
1582 }
1583 for (i = 0; i < 8; i++)
1584 {
1585 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1586 {
1587 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1588
1589 return 1; /* return error */
1590 }
1591 }
1592 if (a_ds18b20_write_byte(handle, DS18B20_CMD_CONVERT_T) != 0) /* sent convert temp command */
1593 {
1594 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1595
1596 return 1; /* return error */
1597 }
1598 cnt = 0; /* reset cnt */
1599 res = 0; /* reset res */
1600 while ((res == 0) && (cnt < 100)) /* read max 1s */
1601 {
1602 if (a_ds18b20_read_bit(handle, (uint8_t *)&res) != 0) /* read 1 bit */
1603 {
1604 handle->debug_print("ds18b20: read bit failed.\n"); /* read 1 bit failed */
1605
1606 return 1; /* return error */
1607 }
1608 handle->delay_ms(10); /* delay 10 ms */
1609 cnt++; /* cnt++ */
1610 }
1611 if (cnt >= 100) /* if cnt is over 100 times */
1612 {
1613 handle->debug_print("ds18b20: bus read timeout.\n"); /* read timeout */
1614
1615 return 1; /* return error */
1616 }
1617 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1618 {
1619 handle->debug_print("ds18b20: bus reset failed.\n"); /* reset bus failed */
1620
1621 return 1; /* return error */
1622 }
1623 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
1624 {
1625 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1626
1627 return 1; /* return error */
1628 }
1629 for (i = 0; i < 8; i++)
1630 {
1631 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
1632 {
1633 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1634
1635 return 1; /* return error */
1636 }
1637 }
1638 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_SCRATCHPAD) != 0) /* send read scratchpad command */
1639 {
1640 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1641
1642 return 1; /* return error */
1643 }
1644 for (i = 0; i < 9; i++) /* read 9 bytes */
1645 {
1646 if (a_ds18b20_read_byte(handle, (uint8_t *)&buf[i]) != 0) /* read byte */
1647 {
1648 handle->debug_print("ds18b20: read byte failed.\n"); /* read failed */
1649
1650 return 1; /* return error */
1651 }
1652 }
1653 if (a_ds18b20_check_crc((uint8_t *)buf, 8, buf[8]) != 0) /* check crc */
1654 {
1655 handle->debug_print("ds18b20: crc check error.\n"); /* crc check error */
1656
1657 return 1; /* return error */
1658 }
1659 *raw = (int16_t)(((uint16_t)buf[1]) << 8) | buf[0]; /* get raw data */
1660 if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_9BIT) /* if 9 bit resolution */
1661 {
1662 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1663 {
1664 *raw = (*raw ) >> 3; /* right shift 3 */
1665 *raw = (*raw) | 0xE000U; /* set negative part */
1666 }
1667 else /* if positive */
1668 {
1669 *raw = (*raw ) >> 3; /* right shift 3 */
1670 }
1671 *temp = (float)(*raw) * 0.5f; /* convert to real data */
1672 }
1673 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_10BIT) /* if 10 bit resolution */
1674 {
1675 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1676 {
1677 *raw = (*raw ) >> 2; /* right shift 2 */
1678 *raw = (*raw) | 0xC000U; /* set negative part */
1679 }
1680 else
1681 {
1682 *raw = (*raw ) >> 2; /* right shift 2 */
1683 }
1684 *temp = (float)(*raw) * 0.25f; /* convert to real data */
1685 }
1686 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_11BIT) /* if 11 bit resolution */
1687 {
1688 if ((((uint16_t)(*raw)) & (1 << 15)) != 0) /* if negative */
1689 {
1690 *raw = (*raw ) >> 1; /* right shift 1 */
1691 *raw = (*raw) | 0x8000U; /* set negative part */
1692 }
1693 else
1694 {
1695 *raw = (*raw ) >> 1; /* right shift 1 */
1696 }
1697 *temp = (float)(*raw) * 0.125f; /* convert to real data */
1698 }
1699 else if (((buf[4] >> 5) & 0x03) == DS18B20_RESOLUTION_12BIT) /* if 12 bit resolution */
1700 {
1701 *raw = (*raw ) >> 0; /* right shift 0 */
1702 *temp = (float)(*raw) * 0.0625f; /* convert to real data */
1703 }
1704 else
1705 {
1706 handle->debug_print("ds18b20: resolution invalid.\n"); /* resolution is invalid */
1707
1708 return 1; /* return error */
1709 }
1710
1711 return 0; /* success return 0 */
1712 }
1713 else
1714 {
1715 handle->debug_print("ds18b20: mode invalid.\n"); /* ds18b20 mode is invalid */
1716
1717 return 1; /* return error */
1718 }
1719}
1720
1730static uint8_t a_ds18b20_read_2bit(ds18b20_handle_t *handle, uint8_t *data)
1731{
1732 uint8_t i;
1733 uint8_t res;
1734
1735 *data = 0; /* reset data */
1736 handle->disable_irq(); /* disable irq */
1737 for (i = 0; i < 2; i++) /* read 2 bit */
1738 {
1739 *data <<= 1; /* left shift 1 */
1740 if (a_ds18b20_read_bit(handle, (uint8_t *)&res) != 0) /* read one bit */
1741 {
1742 handle->enable_irq(); /* enable irq */
1743 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
1744
1745 return 1; /* return error */
1746 }
1747 *data = (*data) | res; /* get 1 bit */
1748 }
1749 handle->enable_irq(); /* enable irq */
1750
1751 return 0; /* success return 0 */
1752}
1753
1754
1764static uint8_t a_ds18b20_write_bit(ds18b20_handle_t *handle, uint8_t bit)
1765{
1766 handle->disable_irq(); /* disable irq */
1767 if (handle->bus_write(0) != 0) /* write 0 */
1768 {
1769 handle->enable_irq(); /* enable irq */
1770 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1771
1772 return 1; /* return error */
1773 }
1774 handle->delay_us(12); /* wait 12 us */
1775 if (handle->bus_write(bit) != 0) /* write bit */
1776 {
1777 handle->enable_irq(); /* enable irq */
1778 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1779
1780 return 1; /* return error */
1781 }
1782 handle->delay_us(30); /* wait 30 us */
1783 if (handle->bus_write(1) != 0) /* write 1 */
1784 {
1785 handle->enable_irq(); /* enable irq */
1786 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1787
1788 return 1; /* return error */
1789 }
1790 handle->delay_us(5); /* wait 5 us */
1791 handle->enable_irq(); /* enable irq */
1792
1793 return 0; /* success return 0 */
1794}
1795
1807static uint8_t a_ds18b20_search(ds18b20_handle_t *handle, uint8_t (*pid)[8], uint8_t cmd, uint8_t *number)
1808{
1809 uint8_t k, l = 0, conflict_bit, m, n;
1810 uint8_t buffer[DS18B20_MAX_SEARCH_SIZE];
1811 uint8_t ss[64];
1812 uint8_t s = 0;
1813 uint8_t num = 0;
1814
1815 if ((*number) > DS18B20_MAX_SEARCH_SIZE) /* check number */
1816 {
1817 handle->debug_print("ds18b20: number is over DS18B20_MAX_SEARCH_SIZE.\n"); /* number is over */
1818
1819 return 1; /* return error */
1820 }
1821 memset((uint8_t *)buffer, 0, DS18B20_MAX_SEARCH_SIZE); /* clear buffer */
1822 memset((uint8_t *)ss, 0, sizeof(uint8_t) * 64); /* clear buffer */
1823 do
1824 {
1825 if (a_ds18b20_reset(handle) != 0) /* reset bus */
1826 {
1827 handle->debug_print("ds18b20: reset failed.\n"); /* reset bus failed */
1828
1829 return 1; /* return error */
1830 }
1831 if (a_ds18b20_write_byte(handle, cmd) != 0) /* write 1 byte */
1832 {
1833 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
1834
1835 return 1; /* return error */
1836 }
1837 for (m = 0; m < 8; m++) /* read 8 byte */
1838 {
1839 for (n = 0; n < 8; n++) /* read 8 bit */
1840 {
1841 if (a_ds18b20_read_2bit(handle, (uint8_t *)&k) != 0) /* read 2 bit */
1842 {
1843 handle->debug_print("ds18b20: read 2bit failed.\n"); /* read 2 bit failed */
1844
1845 return 1; /* return error */
1846 }
1847 k = k & 0x03; /* get valid bits */
1848 s = s >> 1; /* right shift 1 bit */
1849 if (k == 0x01) /* 0000 0001 */
1850 {
1851 if (a_ds18b20_write_bit(handle, 0) != 0) /* write 0 */
1852 {
1853 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1854
1855 return 1; /* return error */
1856 }
1857 ss[(m * 8 + n)] = 0; /* set 0 */
1858 }
1859 else if (k == 0x02) /* 0000 0010 */
1860 {
1861 s = s | 0x80; /* set 7 bit */
1862 if (a_ds18b20_write_bit(handle, 1) != 0) /* write 1 */
1863 {
1864 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1865
1866 return 1; /* return error */
1867 }
1868 ss[(m * 8 + n)] = 1; /* set 1 */
1869 }
1870 else if (k == 0x00) /* if 0000 */
1871 {
1872 conflict_bit = (uint8_t)(m * 8 + n + 1); /* flag conflict bit */
1873 if (conflict_bit > buffer[l]) /* check buffer */
1874 {
1875 if (a_ds18b20_write_bit(handle, 0) != 0) /* write 0 */
1876 {
1877 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1878
1879 return 1; /* return error */
1880 }
1881 ss[(m * 8 + n)] = 0; /* set 0 */
1882 buffer[++l] = conflict_bit; /* set conflict bit */
1883 }
1884 else if (conflict_bit < buffer[l]) /* if > buffer */
1885 {
1886 s = s|((ss[(m * 8 + n)] & 0x01) << 7); /* get s */
1887 if (a_ds18b20_write_bit(handle, ss[(m*8+n)]) != 0) /* write data */
1888 {
1889 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1890
1891 return 1; /* return error */
1892 }
1893 }
1894 else if (conflict_bit == buffer[l]) /* if == buffer */
1895 {
1896 s = s | 0x80; /* set 7 bit */
1897 if (a_ds18b20_write_bit(handle, 1) != 0) /* write 1 */
1898 {
1899 handle->debug_print("ds18b20: write bit failed.\n"); /* write bit failed */
1900
1901 return 1; /* return error */
1902 }
1903 ss[(m * 8 + n)] = 1; /* set 1 */
1904 l = l-1; /* l-- */
1905 }
1906 else
1907 {
1908
1909 }
1910 }
1911 else
1912 {
1913 *number = num; /* save num */
1914
1915 return 0; /* success return 0 */
1916 }
1917 handle->delay_us(5); /* delay 5 us */
1918 }
1919 pid[num][m] = s; /* save s */
1920 s = 0; /* reset s */
1921 }
1922 num++; /* num++ */
1923 if (num > (*number)) /* check num range */
1924 {
1925 break; /* break */
1926 }
1927 } while (buffer[l] != 0); /* check buffer[l] */
1928 *number = num; /* set number */
1929
1930 return 0; /* success return 0 */
1931}
1932
1943uint8_t ds18b20_search_rom(ds18b20_handle_t *handle, uint8_t (*rom)[8], uint8_t *num)
1944{
1945 if (handle == NULL) /* check handle */
1946 {
1947 return 2; /* return error */
1948 }
1949 if (handle->inited != 1) /* check handle initialization */
1950 {
1951 return 3; /* return error */
1952 }
1953
1954 return a_ds18b20_search(handle, rom, DS18B20_CMD_SEARCH_ROM, num); /* return search result */
1955}
1956
1967uint8_t ds18b20_search_alarm(ds18b20_handle_t *handle, uint8_t (*rom)[8], uint8_t *num)
1968{
1969 if (handle == NULL) /* check handle */
1970 {
1971 return 2; /* return error */
1972 }
1973 if (handle->inited != 1) /* check handle initialization */
1974 {
1975 return 3; /* return error */
1976 }
1977
1978 return a_ds18b20_search(handle, rom, DS18B20_CMD_ALARM_SEARCH, num); /* return search result */
1979}
1980
1991{
1992 uint8_t i;
1993
1994 if (handle == NULL) /* check handle */
1995 {
1996 return 2; /* return error */
1997 }
1998 if (handle->inited != 1) /* check handle initialization */
1999 {
2000 return 3; /* return error */
2001 }
2002
2003 if (handle->mode == DS18B20_MODE_SKIP_ROM) /* if use skip rom mode */
2004 {
2005 if (a_ds18b20_reset(handle) != 0) /* bus reset */
2006 {
2007 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
2008
2009 return 1; /* return error */
2010 }
2011 if (a_ds18b20_write_byte(handle, DS18B20_CMD_SKIP_ROM) != 0) /* sent skip rom command */
2012 {
2013 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2014
2015 return 1; /* return error */
2016 }
2017 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_POWER_SUPPLY) != 0) /* write read power supply command */
2018 {
2019 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2020
2021 return 1; /* return error */
2022 }
2023 if (a_ds18b20_read_bit(handle, (uint8_t *)power_mode) != 0) /* get power mode */
2024 {
2025 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
2026
2027 return 1; /* return error */
2028 }
2029
2030 return 0; /* success return 0 */
2031 }
2032 else if (handle->mode == DS18B20_MODE_MATCH_ROM) /* if we use match rom mode */
2033 {
2034 if (a_ds18b20_reset(handle) != 0) /* bus reset */
2035 {
2036 handle->debug_print("ds18b20: bus reset failed.\n"); /* bus reset failed */
2037
2038 return 1; /* return error */
2039 }
2040 if (a_ds18b20_write_byte(handle, DS18B20_CMD_MATCH_ROM) != 0) /* sent match rom command */
2041 {
2042 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2043
2044 return 1; /* return error */
2045 }
2046 for (i = 0; i < 8; i++)
2047 {
2048 if (a_ds18b20_write_byte(handle, handle->rom[i]) != 0) /* send rom */
2049 {
2050 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2051
2052 return 1; /* return error */
2053 }
2054 }
2055 if (a_ds18b20_write_byte(handle, DS18B20_CMD_READ_POWER_SUPPLY) != 0) /* write read power supply */
2056 {
2057 handle->debug_print("ds18b20: write command failed.\n"); /* write command failed */
2058
2059 return 1; /* return error */
2060 }
2061 if (a_ds18b20_read_bit(handle, (uint8_t *)power_mode) != 0) /* get power mode */
2062 {
2063 handle->debug_print("ds18b20: read bit failed.\n"); /* read a bit failed */
2064
2065 return 1; /* return error */
2066 }
2067
2068 return 0; /* success return 0 */
2069 }
2070 else
2071 {
2072 handle->debug_print("ds18b20: mode invalid.\n"); /* mode is invalid */
2073
2074 return 1; /* return error */
2075 }
2076}
2077
2087{
2088 if (info == NULL) /* check handle */
2089 {
2090 return 2; /* return error */
2091 }
2092
2093 memset(info, 0, sizeof(ds18b20_info_t)); /* initialize ds18b20 info structure */
2094 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2095 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2096 strncpy(info->interface, "GPIO", 8); /* copy interface name */
2097 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2098 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2099 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2100 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2101 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2102 info->driver_version = DRIVER_VERSION; /* set driver version */
2103
2104 return 0; /* success return 0 */
2105}
#define MAX_CURRENT
#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_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_scratchpad_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_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]