Contiki 3.x
ds18b20.c
1 /*
2 
3  Contiki library for DS18B20 temperature sensor -
4  For more details see http://xxx
5 
6  Author -
7  Author -
8 
9  License - GPLv3
10 
11  */
12 
13 #include "ds18b20.h"
14 
15 /* probe_for_ds18b20 probes for the sensor. Returns 0 on failure, 1 on success */
16 /* Assumptions: only one sensor on the "1-wire bus", on port WSN_DS18B20_PORT */
17 /* BUG: THIS CODE DOES NOT WORK AS INTENDED! IT RETURNS "1" EVEN WHEN THERE IS NO */
18 /* SENSOR CONNECTED. */
19 
20 uint8_t
21 ds18b20_probe(void)
22 {
23  uint8_t result = 0;
24 
25  /* Reset 1W-bus */
26 
27  /* Pull PIN low for 480 microseconds (us) */
28  /* Start with setting bit DS18B20_1_PIN to 0 */
29  OW_SET_PIN_LOW();
30  /* then set direction to OUT by setting DS18B20_1_DDR bit to 1 */
31  OW_SET_OUTPUT();
32  /* Delay 480 us */
33  clock_delay_usec(480);
34  /* See if sensor responds. First release the bus and switch to INput mode */
35  /* by setting DS18B20_1_DDR bit to 0 */
36  OW_SET_INPUT();
37  /* Activate internal pull-up by setting pin to HIGH (when in INput mode) */
38  /* OW_SET_PIN_HIGH(); */
39  /* Wait for the pin to go HIGH for 64 us */
40  clock_delay_usec(64);
41  /* Now the sensor, if present, pulls the pin LOW for 60-240 us */
42  /* Detect 0 on PIND bit DS18B20_1_PIN. Invert the result so a presence (aka a 0) */
43  /* sets "result" to 1 (for success) */
44  result = !OW_GET_PIN_STATE();
45 
46  /* The sensor releases the pin so it goes HIGH after 240 us, add some */
47  /* for the signal to stabilize, say 300 usecs to be on the safe side? */
48  if(result) {
49  clock_delay_usec(300);
50  /* Now the bus should be HIGH again */
51  result = OW_GET_PIN_STATE();
52  }
53 
54  return result;
55 }
56 /* Write 1 or 0 on the bus */
57 
58 void
59 write_bit(uint8_t bit)
60 {
61  /* Set pin to 0 */
62  OW_SET_OUTPUT();
63  OW_SET_PIN_LOW();
64 
65  /* Pin should be 0 for at least 1 us */
67 
68  /* If we're writing a 1, let interna pull-up pull the bus high */
69  /* within 15 us of setting the bus to low */
70  if(bit) {
71  /* Internal pull-up is activated by setting direction to IN and the */
72  /* setting the pin to HIGH */
73  OW_SET_INPUT();
74  OW_SET_PIN_HIGH();
75  }
76  /* OK, now the bus is either LOW, or pulled HIGH by the internal pull-up */
77  /* Let this state remain for 60 us, then release the bus */
78  clock_delay_usec(60);
79 
80  /* Release the bus */
81  OW_SET_PIN_HIGH();
82  OW_SET_INPUT();
83 
84  /* Allow > 1 us between read/write operations */
86 }
87 /* */
88 /* Read one bit of information from the bus, and return it as 1 or 0 */
89 /* */
90 
91 uint8_t
92 read_bit(void)
93 {
94  uint8_t bit = 0;
95 
96  /* Set pin to 0 */
97  OW_SET_OUTPUT();
98  OW_SET_PIN_LOW();
99 
100  /* Pin should be 0 for at least 1 us */
101  clock_delay_usec(2);
102 
103  /* Now read the bus, start by setting in/out direction and activating internal */
104  /* pull-up resistor */
105  OW_SET_INPUT();
106  OW_SET_PIN_HIGH();
107 
108  /* ds18b20 either keeps the pin down or releases the bus and the */
109  /* bus then goes high because of the interna pull-up resistor */
110  /* Check whichever happens before 15 us has passed */
111  clock_delay_usec(15 - 2 - 1);
112  bit = OW_GET_PIN_STATE();
113 
114  /* The complete read cycle must last at least 60 us. We have now spent */
115  /* about 14-15 us in delays, so add another delay to reach >= 60 us */
116  clock_delay_usec(50);
117 
118  /* Release bus */
119  OW_SET_PIN_HIGH();
120  OW_SET_INPUT();
121 
122  /* Allow > 1 us between read/write operations */
123  clock_delay_usec(2);
124 
125  return bit ? 1 : 0;
126 }
127 /* */
128 /* Read one byte of information. A byte is read least significant bit first */
129 /* */
130 
131 uint8_t
132 read_byte(void)
133 {
134  uint8_t result = 0;
135  uint8_t bit;
136  int i;
137 
138  for(i = 0; i < 8; i++) {
139  bit = read_bit();
140  result += (bit << i);
141  }
142  return result;
143 }
144 /* */
145 /* Write one byte of information. A byte is written least significant bit first */
146 /* */
147 
148 void
149 write_byte(uint8_t byte)
150 {
151  int i;
152 
153  for(i = 0; i < 8; i++) {
154  write_bit((byte >> i) & 1);
155  }
156 }
157 /* */
158 /* ds18b20_get_temp returns the temperature in "temp" (in degrees celsius) */
159 /* Returns 0 on failure (and then "temp" is left unchanged */
160 /* Returns 1 on success, and sets temp */
161 /* */
162 
163 uint8_t
164 ds18b20_get_temp(float *temp)
165 {
166  uint8_t result = 0;
167 
168  /* Reset bus by probing. Probe returns 1 on success/presence of sensor */
169  if(ds18b20_probe()) {
170  /* write command "skip rom" since we only have one sensor on the wire! */
171  write_byte(DS18B20_COMMAND_SKIP_ROM);
172 
173  /* write command to start measurement */
174  write_byte(DS18B20_COMMAND_START_CONVERSION);
175 
176  /* Wait for conversion to complete */
177  /* Conversion is 12-bit by default. */
178  /* Since we have external power to the sensor (ie not in "parasitic power" mode) */
179  /* the bus is held LOW by the sensor while the conversion is going on, and then HIGH */
180  /* when conversion is finished. */
181  OW_SET_INPUT();
182  int count = 0;
183  while(!OW_GET_PIN_STATE()) {
184  clock_delay_msec(10);
185  count++;
186  /* Longest conversion time is 750 ms (12-bit resolution) */
187  /* So if count > 80 (for a little margin!), we return -274.0 */
188  /* which indicates failure to read the temperature. */
189  if(count > 80) {
190  return 0;
191  }
192  }
193 
194  /* The result is stored in the "scratch pad", a 9 byte memory block. */
195  /* The first two bytes are the conversion result. Reading the scratch pad */
196  /* can be terminated by sending a reset signal (but we read all 9 bytes) */
197  (void)ds18b20_probe();
198  write_byte(DS18B20_COMMAND_SKIP_ROM);
199  write_byte(DS18B20_COMMAND_READ_SCRATCH_PAD);
200  uint8_t i, sp_arr[9];
201  for(i = 0; i < 9; i++) {
202  sp_arr[i] = read_byte();
203  }
204 
205  /* Check CRC, if mismatch, return 0 (failure to read temperature) */
206  uint8_t crc_cal = crc8_ds18b20(sp_arr, 8);
207 
208  if(crc_cal != sp_arr[8]) {
209  return 0;
210  }
211 
212  /* OK, now decode what the temperature reading is. This code assumes 12-bit resolution, */
213  /* so this must be modified if the code is modified to use any other resolution! */
214  int16_t temp_res;
215  uint8_t temp_lsb = sp_arr[0];
216  uint8_t temp_msb = sp_arr[1];
217 
218  temp_res = (int16_t)temp_msb << 8 | temp_lsb;
219  *temp = (float)temp_res * 0.0625;
220 
221  result = 1;
222  }
223  return result;
224 }
225 /* */
226 /* crc8 algorithm for ds18b20 */
227 /* http://www.miscel.dk/MiscEl/CRCcalculations.html */
228 /* */
229 
230 uint8_t
231 crc8_ds18b20(uint8_t *buf, uint8_t buf_len)
232 {
233  uint8_t result = 0;
234  uint8_t i, b;
235 
236  for(i = 0; i < buf_len; i++) {
237  result = result ^ buf[i];
238  for(b = 1; b < 9; b++) {
239  if(result & 0x1) {
240  result = (result >> 1) ^ 0x8C;
241  } else {
242  result = result >> 1;
243  }
244  }
245  }
246  return result;
247 }
void clock_delay_msec(uint16_t howlong)
Delay up to 65535 milliseconds.
Definition: clock.c:260
static volatile clock_time_t count
These routines define the AVR-specific calls declared in /core/sys/clock.h CLOCK_SECOND is the number...
Definition: clock.c:80
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:94