annotate simple_ds18b20.c @ 98:dbcbda1e2da2

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