Mercurial > templog
annotate simple_ds18b20.c @ 53:56b1f02f470c
add dump program
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 24 Jun 2012 17:22:26 +0800 |
parents | 878be5e353a0 |
children | ca08442635ca |
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 | |
128 static void | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
129 printhex_nibble(const unsigned char b, FILE *stream) |
12 | 130 { |
131 unsigned char c = b & 0x0f; | |
132 if ( c > 9 ) { | |
133 c += 'A'-10; | |
134 } | |
135 else { | |
136 c += '0'; | |
137 } | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
138 fputc(c, stream); |
12 | 139 } |
140 | |
141 void | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
142 printhex_byte(const unsigned char b, FILE *stream) |
12 | 143 { |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
144 printhex_nibble( b >> 4, stream); |
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
145 printhex_nibble( b, stream); |
12 | 146 } |
147 | |
148 void | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
149 printhex(uint8_t *id, uint8_t n, FILE *stream) |
12 | 150 { |
151 for (uint8_t i = 0; i < n; i++) | |
152 { | |
153 if (i > 0) | |
154 { | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
155 fputc(' ', stream); |
12 | 156 } |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
157 printhex_byte(id[i], stream); |
12 | 158 } |
159 } | |
160 | |
161 | |
162 uint8_t | |
163 simple_ds18b20_read_all() | |
164 { | |
165 uint8_t id[OW_ROMCODE_SIZE]; | |
166 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) | |
167 { | |
168 diff = ow_rom_search( diff, &id[0] ); | |
169 | |
170 if( diff == OW_PRESENCE_ERR ) { | |
171 printf_P( PSTR("No Sensor found\r") ); | |
172 return OW_PRESENCE_ERR; // <--- early exit! | |
173 } | |
174 | |
175 if( diff == OW_DATA_ERR ) { | |
176 printf_P( PSTR("Bus Error\r") ); | |
177 return OW_DATA_ERR; // <--- early exit! | |
178 } | |
179 | |
180 int16_t decicelsius; | |
181 uint8_t ret = simple_ds18b20_read_decicelsius(NULL, &decicelsius); | |
182 if (ret != DS18X20_OK) | |
183 { | |
184 printf_P(PSTR("Failed reading\r")); | |
185 return OW_DATA_ERR; | |
186 } | |
187 | |
188 printf_P(PSTR("DS18B20 %d: "), diff); | |
189 if (crc8(id, OW_ROMCODE_SIZE)) | |
190 { | |
191 printf_P(PSTR("CRC fail")); | |
192 } | |
20
878be5e353a0
Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents:
13
diff
changeset
|
193 printhex(id, OW_ROMCODE_SIZE, stdout); |
12 | 194 printf_P(PSTR(" %d.%d ÂșC\n"), decicelsius/10, decicelsius % 10); |
195 } | |
196 printf_P(PSTR("Done sensors\n")); | |
197 return DS18X20_OK; | |
198 } | |
199 |