comparison simple_ds18b20.c @ 12:3c27bfbd7f3a

Add simple_ds18b20.c etc
author Matt Johnston <matt@ucc.asn.au>
date Sat, 19 May 2012 21:47:09 +0800
parents
children 4838bfcb3504
comparison
equal deleted inserted replaced
11:06bedbe8540d 12:3c27bfbd7f3a
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
129 printhex_nibble(const unsigned char b)
130 {
131 unsigned char c = b & 0x0f;
132 if ( c > 9 ) {
133 c += 'A'-10;
134 }
135 else {
136 c += '0';
137 }
138 putchar(c);
139 }
140
141 void
142 printhex_byte( const unsigned char b )
143 {
144 printhex_nibble( b >> 4 );
145 printhex_nibble( b );
146 }
147
148 void
149 printhex(uint8_t *id, uint8_t n)
150 {
151 for (uint8_t i = 0; i < n; i++)
152 {
153 if (i > 0)
154 {
155 putchar(' ');
156 }
157 printhex_byte(id[i]);
158 }
159 }
160
161
162 uint8_t
163 simple_ds18b20_read_all()
164 {
165 uint8_t id[OW_ROMCODE_SIZE];
166 uint8_t sp[DS18X20_SP_SIZE];
167 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
168 {
169 diff = ow_rom_search( diff, &id[0] );
170
171 if( diff == OW_PRESENCE_ERR ) {
172 printf_P( PSTR("No Sensor found\r") );
173 return OW_PRESENCE_ERR; // <--- early exit!
174 }
175
176 if( diff == OW_DATA_ERR ) {
177 printf_P( PSTR("Bus Error\r") );
178 return OW_DATA_ERR; // <--- early exit!
179 }
180
181 int16_t decicelsius;
182 uint8_t ret = simple_ds18b20_read_decicelsius(NULL, &decicelsius);
183 if (ret != DS18X20_OK)
184 {
185 printf_P(PSTR("Failed reading\r"));
186 return OW_DATA_ERR;
187 }
188
189 printf_P(PSTR("DS18B20 %d: "), diff);
190 if (crc8(id, OW_ROMCODE_SIZE))
191 {
192 printf_P(PSTR("CRC fail"));
193 }
194 printhex(id, OW_ROMCODE_SIZE);
195 printf_P(PSTR(" %d.%d ÂșC\n"), decicelsius/10, decicelsius % 10);
196 }
197 printf_P(PSTR("Done sensors\n"));
198 return DS18X20_OK;
199 }
200