Mercurial > templog
annotate simple_ds18b20.c @ 108:b523761181f5 avr-http
Add some replacement functions for testing
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 15 Sep 2012 21:43:15 +0800 |
parents | ca08442635ca |
children | 90d1ebb941ab |
rev | line source |
---|---|
12 | 1 // Matt Johnston 2012 |
2 // Based on ds18x20.c by Martin Thomas, in turn based on code by | |
3 // Peter // Dannegger and others. | |
4 // | |
5 #include <stdio.h> | |
6 #include <avr/pgmspace.h> | |
7 | |
8 #include "ds18x20.h" | |
9 #include "onewire.h" | |
10 #include "crc8.h" | |
11 | |
12 #include "simple_ds18b20.h" | |
13 | |
14 uint8_t | |
15 simple_ds18b20_start_meas(uint8_t id[]) | |
16 { | |
17 uint8_t ret; | |
18 | |
19 ow_reset(); | |
20 if( ow_input_pin_state() ) { // only send if bus is "idle" = high | |
21 ow_command_with_parasite_enable(DS18X20_CONVERT_T, id); | |
22 ret = DS18X20_OK; | |
23 } | |
24 else { | |
25 ret = DS18X20_START_FAIL; | |
26 } | |
27 | |
28 return ret; | |
29 } | |
30 | |
31 static uint8_t | |
32 read_scratchpad( uint8_t id[], uint8_t sp[], uint8_t n ) | |
33 { | |
34 uint8_t i; | |
35 uint8_t ret; | |
36 | |
37 ow_command( DS18X20_READ, id ); | |
38 for ( i = 0; i < n; i++ ) { | |
39 sp[i] = ow_byte_rd(); | |
40 } | |
41 if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) { | |
42 ret = DS18X20_ERROR_CRC; | |
43 } else { | |
44 ret = DS18X20_OK; | |
45 } | |
46 | |
47 return ret; | |
48 } | |
49 | |
50 static int16_t | |
51 ds18b20_raw_to_decicelsius( uint8_t sp[] ) | |
52 { | |
53 uint16_t measure; | |
54 uint8_t negative; | |
55 int16_t decicelsius; | |
56 uint16_t fract; | |
57 | |
58 measure = sp[0] | (sp[1] << 8); | |
59 //measure = 0xFF5E; // test -10.125 | |
60 //measure = 0xFE6F; // test -25.0625 | |
61 | |
62 // check for negative | |
63 if ( measure & 0x8000 ) { | |
64 negative = 1; // mark negative | |
65 measure ^= 0xffff; // convert to positive => (twos complement)++ | |
66 measure++; | |
67 } | |
68 else { | |
69 negative = 0; | |
70 } | |
71 | |
72 // clear undefined bits for DS18B20 != 12bit resolution | |
73 switch( sp[DS18B20_CONF_REG] & DS18B20_RES_MASK ) { | |
74 case DS18B20_9_BIT: | |
75 measure &= ~(DS18B20_9_BIT_UNDF); | |
76 break; | |
77 case DS18B20_10_BIT: | |
78 measure &= ~(DS18B20_10_BIT_UNDF); | |
79 break; | |
80 case DS18B20_11_BIT: | |
81 measure &= ~(DS18B20_11_BIT_UNDF); | |
82 break; | |
83 default: | |
84 // 12 bit - all bits valid | |
85 break; | |
86 } | |
87 | |
88 decicelsius = (measure >> 4); | |
89 decicelsius *= 10; | |
90 | |
91 // decicelsius += ((measure & 0x000F) * 640 + 512) / 1024; | |
92 // 625/1000 = 640/1024 | |
93 fract = ( measure & 0x000F ) * 640; | |
94 if ( !negative ) { | |
95 fract += 512; | |
96 } | |
97 fract /= 1024; | |
98 decicelsius += fract; | |
99 | |
100 if ( negative ) { | |
101 decicelsius = -decicelsius; | |
102 } | |
103 | |
104 if ( /* decicelsius == 850 || */ decicelsius < -550 || decicelsius > 1250 ) { | |
105 return DS18X20_INVALID_DECICELSIUS; | |
106 } else { | |
107 return decicelsius; | |
108 } | |
109 } | |
110 | |
111 uint8_t | |
112 simple_ds18b20_read_decicelsius( uint8_t id[], int16_t *decicelsius ) | |
113 { | |
114 uint8_t sp[DS18X20_SP_SIZE]; | |
115 uint8_t ret; | |
116 | |
117 if (id) | |
118 { | |
119 ow_reset(); | |
120 } | |
121 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE ); | |
122 if ( ret == DS18X20_OK ) { | |
123 *decicelsius = ds18b20_raw_to_decicelsius( sp ); | |
124 } | |
125 return ret; | |
126 } | |
127 | |
75
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
128 uint8_t |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
129 simple_ds18b20_read_raw( uint8_t id[], uint16_t *reading ) |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
130 { |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
131 uint8_t sp[DS18X20_SP_SIZE]; |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
132 uint8_t ret; |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
133 |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
134 if (id) |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
135 { |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
136 ow_reset(); |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
137 } |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
138 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE ); |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
139 if ( ret == DS18X20_OK ) { |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
140 *reading = sp[0] | (sp[1] << 8); |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
141 } |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
142 return ret; |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
143 } |
ca08442635ca
report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents:
20
diff
changeset
|
144 |
12 | 145 static void |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
146 printhex_nibble(const unsigned char b, FILE *stream) |
12 | 147 { |
148 unsigned char c = b & 0x0f; | |
149 if ( c > 9 ) { | |
150 c += 'A'-10; | |
151 } | |
152 else { | |
153 c += '0'; | |
154 } | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
155 fputc(c, stream); |
12 | 156 } |
157 | |
158 void | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
159 printhex_byte(const unsigned char b, FILE *stream) |
12 | 160 { |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
161 printhex_nibble( b >> 4, stream); |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
162 printhex_nibble( b, stream); |
12 | 163 } |
164 | |
165 void | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
166 printhex(uint8_t *id, uint8_t n, FILE *stream) |
12 | 167 { |
168 for (uint8_t i = 0; i < n; i++) | |
169 { | |
170 if (i > 0) | |
171 { | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
172 fputc(' ', stream); |
12 | 173 } |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
174 printhex_byte(id[i], stream); |
12 | 175 } |
176 } | |
177 | |
178 | |
179 uint8_t | |
180 simple_ds18b20_read_all() | |
181 { | |
182 uint8_t id[OW_ROMCODE_SIZE]; | |
183 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) | |
184 { | |
185 diff = ow_rom_search( diff, &id[0] ); | |
186 | |
187 if( diff == OW_PRESENCE_ERR ) { | |
188 printf_P( PSTR("No Sensor found\r") ); | |
189 return OW_PRESENCE_ERR; // <--- early exit! | |
190 } | |
191 | |
192 if( diff == OW_DATA_ERR ) { | |
193 printf_P( PSTR("Bus Error\r") ); | |
194 return OW_DATA_ERR; // <--- early exit! | |
195 } | |
196 | |
197 int16_t decicelsius; | |
198 uint8_t ret = simple_ds18b20_read_decicelsius(NULL, &decicelsius); | |
199 if (ret != DS18X20_OK) | |
200 { | |
201 printf_P(PSTR("Failed reading\r")); | |
202 return OW_DATA_ERR; | |
203 } | |
204 | |
205 printf_P(PSTR("DS18B20 %d: "), diff); | |
206 if (crc8(id, OW_ROMCODE_SIZE)) | |
207 { | |
208 printf_P(PSTR("CRC fail")); | |
209 } | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
210 printhex(id, OW_ROMCODE_SIZE, stdout); |
12 | 211 printf_P(PSTR(" %d.%d ÂșC\n"), decicelsius/10, decicelsius % 10); |
212 } | |
213 printf_P(PSTR("Done sensors\n")); | |
214 return DS18X20_OK; | |
215 } | |
216 |