annotate simple_ds18b20.c @ 370:aaa94f90eaa1

merge
author Matt Johnston <matt@ucc.asn.au>
date Tue, 26 Jun 2012 21:21:51 +0800
parents f6b5941b4c34
children ca08442635ca
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
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 static int16_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 ds18b20_raw_to_decicelsius( uint8_t sp[] )
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 uint16_t measure;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 uint8_t negative;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 int16_t decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 uint16_t fract;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 measure = sp[0] | (sp[1] << 8);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 //measure = 0xFF5E; // test -10.125
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 //measure = 0xFE6F; // test -25.0625
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 // check for negative
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 if ( measure & 0x8000 ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 negative = 1; // mark negative
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 measure ^= 0xffff; // convert to positive => (twos complement)++
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 measure++;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 negative = 0;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 // clear undefined bits for DS18B20 != 12bit resolution
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 switch( sp[DS18B20_CONF_REG] & DS18B20_RES_MASK ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 case DS18B20_9_BIT:
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 measure &= ~(DS18B20_9_BIT_UNDF);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 break;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 case DS18B20_10_BIT:
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 measure &= ~(DS18B20_10_BIT_UNDF);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 break;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 case DS18B20_11_BIT:
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 measure &= ~(DS18B20_11_BIT_UNDF);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 break;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 default:
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 // 12 bit - all bits valid
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 break;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 }
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 decicelsius = (measure >> 4);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 decicelsius *= 10;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 // decicelsius += ((measure & 0x000F) * 640 + 512) / 1024;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 // 625/1000 = 640/1024
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 fract = ( measure & 0x000F ) * 640;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 if ( !negative ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 fract += 512;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 fract /= 1024;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 decicelsius += fract;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 if ( negative ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 decicelsius = -decicelsius;
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
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 if ( /* decicelsius == 850 || */ decicelsius < -550 || decicelsius > 1250 ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 return DS18X20_INVALID_DECICELSIUS;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 } else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 return decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 uint8_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 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
113 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 uint8_t sp[DS18X20_SP_SIZE];
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 uint8_t ret;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 if (id)
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 ow_reset();
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 if ( ret == DS18X20_OK ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 *decicelsius = ds18b20_raw_to_decicelsius( sp );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 return ret;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 }
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 static void
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
129 printhex_nibble(const unsigned char b, FILE *stream)
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 unsigned char c = b & 0x0f;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 if ( c > 9 ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 c += 'A'-10;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 else {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 c += '0';
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
138 fputc(c, 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_byte(const unsigned char b, FILE *stream)
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 {
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
144 printhex_nibble( b >> 4, stream);
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
145 printhex_nibble( b, stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 void
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
149 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
150 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 for (uint8_t i = 0; i < n; i++)
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 if (i > 0)
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 {
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
155 fputc(' ', stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 }
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
157 printhex_byte(id[i], stream);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 }
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
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 uint8_t
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 simple_ds18b20_read_all()
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 uint8_t id[OW_ROMCODE_SIZE];
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 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
167 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 diff = ow_rom_search( diff, &id[0] );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 if( diff == OW_PRESENCE_ERR ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 printf_P( PSTR("No Sensor found\r") );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 return OW_PRESENCE_ERR; // <--- early exit!
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 if( diff == OW_DATA_ERR ) {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 printf_P( PSTR("Bus Error\r") );
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 return OW_DATA_ERR; // <--- early exit!
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 }
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 int16_t decicelsius;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 uint8_t ret = simple_ds18b20_read_decicelsius(NULL, &decicelsius);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 if (ret != DS18X20_OK)
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("Failed reading\r"));
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 return OW_DATA_ERR;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 printf_P(PSTR("DS18B20 %d: "), diff);
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 if (crc8(id, OW_ROMCODE_SIZE))
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 {
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 printf_P(PSTR("CRC fail"));
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 }
326
f6b5941b4c34 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 319
diff changeset
193 printhex(id, OW_ROMCODE_SIZE, stdout);
318
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 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
195 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 printf_P(PSTR("Done sensors\n"));
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 return DS18X20_OK;
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 }
31199b2941f6 Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199