annotate simple_ds18b20.c @ 143:0895f5ad7731

copied fridge logic from main.c
author Matt Johnston <matt@ucc.asn.au>
date Tue, 20 Nov 2012 22:03:10 +0800
parents 295a4b875677
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 // Matt Johnston 2012
3c27bfbd7f3a 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
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 // Peter // Dannegger and others.
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 //
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 #include <stdio.h>
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 #include <avr/pgmspace.h>
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #include "ds18x20.h"
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 #include "onewire.h"
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 #include "crc8.h"
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #include "simple_ds18b20.h"
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 uint8_t
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 simple_ds18b20_start_meas(uint8_t id[])
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 uint8_t ret;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 ow_reset();
3c27bfbd7f3a 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
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 ow_command_with_parasite_enable(DS18X20_CONVERT_T, id);
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 ret = DS18X20_OK;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 else {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 ret = DS18X20_START_FAIL;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 return ret;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 static uint8_t
3c27bfbd7f3a 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 )
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 uint8_t i;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 uint8_t ret;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 ow_command( DS18X20_READ, id );
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 for ( i = 0; i < n; i++ ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 sp[i] = ow_byte_rd();
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 ret = DS18X20_ERROR_CRC;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 } else {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 ret = DS18X20_OK;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 return ret;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
115
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
50 int16_t
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
51 ds18b20_raw16_to_decicelsius(uint16_t measure)
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 uint8_t negative;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 int16_t decicelsius;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 uint16_t fract;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 // check for negative
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 if ( measure & 0x8000 ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 negative = 1; // mark negative
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 measure ^= 0xffff; // convert to positive => (twos complement)++
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 measure++;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 else {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 negative = 0;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 decicelsius = (measure >> 4);
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 decicelsius *= 10;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 // decicelsius += ((measure & 0x000F) * 640 + 512) / 1024;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 // 625/1000 = 640/1024
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 fract = ( measure & 0x000F ) * 640;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 if ( !negative ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 fract += 512;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 fract /= 1024;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 decicelsius += fract;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if ( negative ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 decicelsius = -decicelsius;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
120
295a4b875677 Ignore 85º setting too
Matt Johnston <matt@ucc.asn.au>
parents: 115
diff changeset
83 if ( decicelsius == 850 || decicelsius < -550 || decicelsius > 1250 ) {
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 return DS18X20_INVALID_DECICELSIUS;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 } else {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 return decicelsius;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 uint8_t
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 simple_ds18b20_read_decicelsius( uint8_t id[], int16_t *decicelsius )
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 {
115
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
93 uint16_t reading;
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 uint8_t ret;
115
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
95
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
96 ret = simple_ds18b20_read_raw(id, &reading);
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
97 if (ret == DS18X20_OK)
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
98 {
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
99 *decicelsius = ds18b20_raw16_to_decicelsius(reading);
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
100 }
90d1ebb941ab untested fridge control
Matt Johnston <matt@ucc.asn.au>
parents: 75
diff changeset
101 return ret;
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
75
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
104 uint8_t
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
105 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
106 {
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
107 uint8_t sp[DS18X20_SP_SIZE];
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
108 uint8_t ret;
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
109
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
110 if (id)
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
111 {
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
112 ow_reset();
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
113 }
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
114 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE );
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
115 if ( ret == DS18X20_OK ) {
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
116 *reading = sp[0] | (sp[1] << 8);
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
117 }
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
118 return ret;
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
119 }
ca08442635ca report raw ds18b20 values instead
Matt Johnston <matt@ucc.asn.au>
parents: 20
diff changeset
120
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 static void
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
122 printhex_nibble(const unsigned char b, FILE *stream)
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 unsigned char c = b & 0x0f;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 if ( c > 9 ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 c += 'A'-10;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 else {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 c += '0';
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 }
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
131 fputc(c, stream);
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 void
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
135 printhex_byte(const unsigned char b, FILE *stream)
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 {
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
137 printhex_nibble( b >> 4, stream);
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
138 printhex_nibble( b, stream);
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 void
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
142 printhex(uint8_t *id, uint8_t n, FILE *stream)
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 for (uint8_t i = 0; i < n; i++)
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 if (i > 0)
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 {
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
148 fputc(' ', stream);
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 }
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
150 printhex_byte(id[i], stream);
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 uint8_t
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 simple_ds18b20_read_all()
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 uint8_t id[OW_ROMCODE_SIZE];
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 diff = ow_rom_search( diff, &id[0] );
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 if( diff == OW_PRESENCE_ERR ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 printf_P( PSTR("No Sensor found\r") );
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 return OW_PRESENCE_ERR; // <--- early exit!
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 if( diff == OW_DATA_ERR ) {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 printf_P( PSTR("Bus Error\r") );
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 return OW_DATA_ERR; // <--- early exit!
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 int16_t decicelsius;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 uint8_t ret = simple_ds18b20_read_decicelsius(NULL, &decicelsius);
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 if (ret != DS18X20_OK)
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 printf_P(PSTR("Failed reading\r"));
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 return OW_DATA_ERR;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 printf_P(PSTR("DS18B20 %d: "), diff);
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 if (crc8(id, OW_ROMCODE_SIZE))
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 {
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 printf_P(PSTR("CRC fail"));
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
20
878be5e353a0 Untested - calculate crc in uart_putchar
Matt Johnston <matt@ucc.asn.au>
parents: 13
diff changeset
186 printhex(id, OW_ROMCODE_SIZE, stdout);
12
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 printf_P(PSTR(" %d.%d ºC\n"), decicelsius/10, decicelsius % 10);
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 printf_P(PSTR("Done sensors\n"));
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 return DS18X20_OK;
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 }
3c27bfbd7f3a Add simple_ds18b20.c etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192