annotate old/simple_ds18b20.c @ 527:dd8895652485

porting to asyncio
author Matt Johnston <matt@ucc.asn.au>
date Fri, 20 Mar 2015 20:12:25 +0800
parents 95043860e437
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 // Matt Johnston 2012
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 // Based on ds18x20.c by Martin Thomas, in turn based on code by
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 // Peter // Dannegger and others.
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 //
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 #include <stdio.h>
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 #include <avr/pgmspace.h>
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #include "ds18x20.h"
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 #include "onewire.h"
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 #include "crc8.h"
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #include "simple_ds18b20.h"
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 uint8_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 simple_ds18b20_start_meas(uint8_t id[])
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 uint8_t ret;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 ow_reset();
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 if( ow_input_pin_state() ) { // only send if bus is "idle" = high
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 ow_command_with_parasite_enable(DS18X20_CONVERT_T, id);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 ret = DS18X20_OK;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 ret = DS18X20_START_FAIL;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 return ret;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 static uint8_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 read_scratchpad( uint8_t id[], uint8_t sp[], uint8_t n )
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 uint8_t i;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 uint8_t ret;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 ow_command( DS18X20_READ, id );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 for ( i = 0; i < n; i++ ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 sp[i] = ow_byte_rd();
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 ret = DS18X20_ERROR_CRC;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 } else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 ret = DS18X20_OK;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 return ret;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
414
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
50 int16_t
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
51 ds18b20_raw16_to_decicelsius(uint16_t measure)
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 uint8_t negative;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 int16_t decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 uint16_t fract;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 // check for negative
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 if ( measure & 0x8000 ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 negative = 1; // mark negative
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 measure ^= 0xffff; // convert to positive => (twos complement)++
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 measure++;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 negative = 0;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 decicelsius = (measure >> 4);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 decicelsius *= 10;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 // decicelsius += ((measure & 0x000F) * 640 + 512) / 1024;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 // 625/1000 = 640/1024
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 fract = ( measure & 0x000F ) * 640;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 if ( !negative ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 fract += 512;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 fract /= 1024;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 decicelsius += fract;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if ( negative ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 decicelsius = -decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
419
a60a015732ea Ignore 85º setting too
Matt Johnston <matt@ucc.asn.au>
parents: 414
diff changeset
83 if ( decicelsius == 850 || decicelsius < -550 || decicelsius > 1250 ) {
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 return DS18X20_INVALID_DECICELSIUS;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 } else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 return decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 uint8_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 simple_ds18b20_read_decicelsius( uint8_t id[], int16_t *decicelsius )
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 {
414
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
93 uint16_t reading;
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 uint8_t ret;
414
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
95
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
96 ret = simple_ds18b20_read_raw(id, &reading);
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
97 if (ret == DS18X20_OK)
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
98 {
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
99 *decicelsius = ds18b20_raw16_to_decicelsius(reading);
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
100 }
a7e228609f20 untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 381
diff changeset
101 return ret;
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
381
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
104 uint8_t
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
105 simple_ds18b20_read_raw( uint8_t id[], uint16_t *reading )
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
106 {
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
107 uint8_t sp[DS18X20_SP_SIZE];
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
108 uint8_t ret;
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
109
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
110 if (id)
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
111 {
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
112 ow_reset();
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
113 }
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
114 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE );
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
115 if ( ret == DS18X20_OK ) {
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
116 *reading = sp[0] | (sp[1] << 8);
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
117 }
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
118 return ret;
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
119 }
83c83014e5e3 report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 326
diff changeset
120
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 static void
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
122 printhex_nibble(const unsigned char b, FILE *stream)
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 unsigned char c = b & 0x0f;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 if ( c > 9 ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 c += 'A'-10;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 c += '0';
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 }
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
131 fputc(c, stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 void
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
135 printhex_byte(const unsigned char b, FILE *stream)
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 {
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
137 printhex_nibble( b >> 4, stream);
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
138 printhex_nibble( b, stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 void
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
142 printhex(uint8_t *id, uint8_t n, FILE *stream)
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 for (uint8_t i = 0; i < n; i++)
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 if (i > 0)
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 {
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
148 fputc(' ', stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 }
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
150 printhex_byte(id[i], stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 uint8_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 simple_ds18b20_read_all()
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 uint8_t id[OW_ROMCODE_SIZE];
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 diff = ow_rom_search( diff, &id[0] );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 if( diff == OW_PRESENCE_ERR ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 printf_P( PSTR("No Sensor found\r") );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 return OW_PRESENCE_ERR; // <--- early exit!
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 if( diff == OW_DATA_ERR ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 printf_P( PSTR("Bus Error\r") );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 return OW_DATA_ERR; // <--- early exit!
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 int16_t decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 uint8_t ret = simple_ds18b20_read_decicelsius(NULL, &decicelsius);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 if (ret != DS18X20_OK)
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 printf_P(PSTR("Failed reading\r"));
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 return OW_DATA_ERR;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 printf_P(PSTR("DS18B20 %d: "), diff);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 if (crc8(id, OW_ROMCODE_SIZE))
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 printf_P(PSTR("CRC fail"));
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
186 printhex(id, OW_ROMCODE_SIZE, stdout);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 printf_P(PSTR(" %d.%d ºC\n"), decicelsius/10, decicelsius % 10);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 printf_P(PSTR("Done sensors\n"));
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 return DS18X20_OK;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192