annotate ds18x20.c @ 409:1854abee5a59

add debug url
author Matt Johnston <matt@ucc.asn.au>
date Thu, 02 Aug 2012 22:11:13 +0800
parents 7d409dded901
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
315
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*********************************************************************************
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 Title: DS18X20-Functions via One-Wire-Bus
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 Author: Martin Thomas <[email protected]>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 http://www.siwawi.arubi.uni-kl.de/avr-projects
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 Software: avr-gcc 4.3.3 / avr-libc 1.6.7 (WinAVR 3/2010)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 Hardware: any AVR - tested with ATmega16/ATmega32/ATmega324P and 3 DS18B20
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 Partly based on code from Peter Dannegger and others.
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 changelog:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 20041124 - Extended measurements for DS18(S)20 contributed by Carsten Foss (CFO)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 200502xx - function DS18X20_read_meas_single
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 20050310 - DS18x20 EEPROM functions (can be disabled to save flash-memory)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 (DS18X20_EEPROMSUPPORT in ds18x20.h)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 20100625 - removed inner returns, added static function for read scratchpad
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 . replaced full-celcius and fractbit method with decicelsius
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 and maxres (degreeCelsius*10e-4) functions, renamed eeprom-functions,
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 delay in recall_e2 replaced by timeout-handling
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 20100714 - ow_command_skip_last_recovery used for parasite-powerd devices so the
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 strong pull-up can be enabled in time even with longer OW recovery times
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 20110209 - fix in DS18X20_format_from_maxres() by Marian Kulesza
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 **********************************************************************************/
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 #include <stdlib.h>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include <stdint.h>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include <avr/io.h>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include <avr/pgmspace.h>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include "ds18x20.h"
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #include "onewire.h"
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #include "crc8.h"
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #if DS18X20_EEPROMSUPPORT
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 // for 10ms delay in copy scratchpad
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #include <util/delay.h>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #endif /* DS18X20_EEPROMSUPPORT */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 /*----------- start of "debug-functions" ---------------*/
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #if DS18X20_VERBOSE
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #if (!DS18X20_DECICELSIUS)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #error "DS18X20_DECICELSIUS must be enabled for verbose-mode"
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #endif
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /* functions for debugging-output - undef DS18X20_VERBOSE in .h
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if you run out of program-memory */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #include <string.h>
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #include "uart.h"
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 #include "uart_addon.h"
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 static int16_t DS18X20_raw_to_decicelsius( uint8_t fc, uint8_t sp[] );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 void DS18X20_show_id_uart( uint8_t *id, size_t n )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 size_t i;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 for( i = 0; i < n; i++ ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 if ( i == 0 ) { uart_puts_P( "FC:" ); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 else if ( i == n-1 ) { uart_puts_P( "CRC:" ); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if ( i == 1 ) { uart_puts_P( "SN: " ); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 uart_puthex_byte(id[i]);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 uart_puts_P(" ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if ( i == 0 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 if ( id[0] == DS18S20_FAMILY_CODE ) { uart_puts_P ("(18S)"); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 else if ( id[0] == DS18B20_FAMILY_CODE ) { uart_puts_P ("(18B)"); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 else if ( id[0] == DS1822_FAMILY_CODE ) { uart_puts_P ("(22)"); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 else { uart_puts_P ("( ? )"); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 if ( crc8( id, OW_ROMCODE_SIZE) )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 uart_puts_P( " CRC FAIL " );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 else
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 uart_puts_P( " CRC O.K. " );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 static void show_sp_uart( uint8_t *sp, size_t n )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 size_t i;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 uart_puts_P( "SP:" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 for( i = 0; i < n; i++ ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if ( i == n-1 ) { uart_puts_P( "CRC:" ); }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 uart_puthex_byte(sp[i]);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 uart_puts_P(" ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 /*
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 convert raw value from DS18x20 to Celsius
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 input is:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 - familycode fc (0x10/0x28 see header)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 - scratchpad-buffer
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 output is:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 - cel full celsius
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 - fractions of celsius in millicelsius*(10^-1)/625 (the 4 LS-Bits)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 - subzero =0 positiv / 1 negativ
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 always returns DS18X20_OK
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 static uint8_t DS18X20_meas_to_cel( uint8_t fc, uint8_t *sp,
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 uint8_t* subzero, uint8_t* cel, uint8_t* cel_frac_bits)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 uint16_t meas;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 uint8_t i;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 meas = sp[0]; // LSB
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 meas |= ( (uint16_t)sp[1] ) << 8; // MSB
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 // only work on 12bit-base
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 if( fc == DS18S20_FAMILY_CODE ) { // 9 -> 12 bit if 18S20
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 /* Extended res. measurements for DS18S20 contributed by Carsten Foss */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 meas &= (uint16_t) 0xfffe; // Discard LSB, needed for later extended precicion calc
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 meas <<= 3; // Convert to 12-bit, now degrees are in 1/16 degrees units
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 meas += ( 16 - sp[6] ) - 4; // Add the compensation and remember to subtract 0.25 degree (4/16)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 // check for negative
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 if ( meas & 0x8000 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 *subzero=1; // mark negative
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 meas ^= 0xffff; // convert to positive => (twos complement)++
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 meas++;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 *subzero=0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 // clear undefined bits for B != 12bit
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 if ( fc == DS18B20_FAMILY_CODE || fc == DS1822_FAMILY_CODE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 i = sp[DS18B20_CONF_REG];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) { ; }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 meas &= ~(DS18B20_11_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 } else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 meas &= ~(DS18B20_10_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 } else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 meas &= ~(DS18B20_9_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 *cel = (uint8_t)(meas >> 4);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 *cel_frac_bits = (uint8_t)(meas & 0x000F);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 return DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 static void DS18X20_uart_put_temp(const uint8_t subzero,
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 const uint8_t cel, const uint8_t cel_frac_bits)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 char buffer[sizeof(int)*8+1];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 size_t i;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 uart_putc((subzero)?'-':'+');
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 uart_put_int((int)cel);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 uart_puts_P(".");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 itoa(cel_frac_bits*DS18X20_FRACCONV,buffer,10);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 for ( i = 0; i < 4-strlen(buffer); i++ ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 uart_puts_P("0");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 uart_puts(buffer);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 uart_puts_P("�C");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 /* verbose output rom-search follows read-scratchpad in one loop */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 uint8_t DS18X20_read_meas_all_verbose( void )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 uint8_t id[OW_ROMCODE_SIZE], sp[DS18X20_SP_SIZE], diff;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 uint8_t i;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 uint16_t meas;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 int16_t decicelsius;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 char s[10];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 uint8_t subzero, cel, cel_frac_bits;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 diff = ow_rom_search( diff, &id[0] );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 if( diff == OW_PRESENCE_ERR ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 uart_puts_P( "No Sensor found\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 return OW_PRESENCE_ERR; // <--- early exit!
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 if( diff == OW_DATA_ERR ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 uart_puts_P( "Bus Error\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 return OW_DATA_ERR; // <--- early exit!
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 DS18X20_show_id_uart( id, OW_ROMCODE_SIZE );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 if( id[0] == DS18B20_FAMILY_CODE || id[0] == DS18S20_FAMILY_CODE ||
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 id[0] == DS1822_FAMILY_CODE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 // temperature sensor
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 uart_putc ('\r');
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 ow_byte_wr( DS18X20_READ ); // read command
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 sp[i]=ow_byte_rd();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 show_sp_uart( sp, DS18X20_SP_SIZE );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 uart_puts_P( " CRC FAIL " );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 uart_puts_P( " CRC O.K. " );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 uart_putc ('\r');
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 meas = sp[0]; // LSB Temp. from Scrachpad-Data
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 meas |= (uint16_t) (sp[1] << 8); // MSB
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 uart_puts_P( " T_raw=");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 uart_puthex_byte( (uint8_t)(meas >> 8) );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 uart_puthex_byte( (uint8_t)meas );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 uart_puts_P( " " );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 if( id[0] == DS18S20_FAMILY_CODE ) { // 18S20
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 uart_puts_P( "S20/09" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 else if ( id[0] == DS18B20_FAMILY_CODE ||
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 id[0] == DS1822_FAMILY_CODE ) { // 18B20 or 1822
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 i=sp[DS18B20_CONF_REG];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 uart_puts_P( "B20/12" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 uart_puts_P( "B20/11" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 uart_puts_P( " B20/10 " );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 uart_puts_P( "B20/09" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 uart_puts_P(" ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 DS18X20_meas_to_cel( id[0], sp, &subzero, &cel, &cel_frac_bits );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 DS18X20_uart_put_temp( subzero, cel, cel_frac_bits );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 decicelsius = DS18X20_raw_to_decicelsius( id[0], sp );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 if ( decicelsius == DS18X20_INVALID_DECICELSIUS ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 uart_puts_P("* INVALID *");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 uart_puts_P(" conv: ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 uart_put_int(decicelsius);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 uart_puts_P(" deci�C ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 DS18X20_format_from_decicelsius( decicelsius, s, 10 );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 uart_puts_P(" fmt: ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 uart_puts(s);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 uart_puts_P(" �C ");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 uart_puts("\r");
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 } // if meas-sensor
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 } // loop all sensors
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 uart_puts_P( "\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 return DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 #endif /* DS18X20_VERBOSE */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 #if DS18X20_VERBOSE
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 #define uart_puts_P_verbose(s__) uart_puts_P(s__)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 #else
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 #define uart_puts_P_verbose(s__)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 #endif
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 /*----------- end of "debug-functions" ---------------*/
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 /* find DS18X20 Sensors on 1-Wire-Bus
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 input/ouput: diff is the result of the last rom-search
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 *diff = OW_SEARCH_FIRST for first call
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 output: id is the rom-code of the sensor found */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 uint8_t DS18X20_find_sensor( uint8_t *diff, uint8_t id[] )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 uint8_t go;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 go = 1;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 do {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 *diff = ow_rom_search( *diff, &id[0] );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 if ( *diff == OW_PRESENCE_ERR || *diff == OW_DATA_ERR ||
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 *diff == OW_LAST_DEVICE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 go = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 if ( id[0] == DS18B20_FAMILY_CODE || id[0] == DS18S20_FAMILY_CODE ||
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 id[0] == DS1822_FAMILY_CODE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 go = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 } while (go);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 /* get power status of DS18x20
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 input: id = rom_code
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 returns: DS18X20_POWER_EXTERN or DS18X20_POWER_PARASITE */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 uint8_t DS18X20_get_power_status( uint8_t id[] )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 uint8_t pstat;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 ow_command( DS18X20_READ_POWER_SUPPLY, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 pstat = ow_bit_io( 1 );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 return ( pstat ) ? DS18X20_POWER_EXTERN : DS18X20_POWER_PARASITE;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 /* start measurement (CONVERT_T) for all sensors if input id==NULL
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 or for single sensor where id is the rom-code */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 uint8_t DS18X20_start_meas( uint8_t with_power_extern, uint8_t id[])
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 if( ow_input_pin_state() ) { // only send if bus is "idle" = high
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 if ( with_power_extern != DS18X20_POWER_EXTERN ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 ow_command_with_parasite_enable( DS18X20_CONVERT_T, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 /* not longer needed: ow_parasite_enable(); */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 ow_command( DS18X20_CONVERT_T, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 uart_puts_P_verbose( "DS18X20_start_meas: Short Circuit!\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 ret = DS18X20_START_FAIL;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 // returns 1 if conversion is in progress, 0 if finished
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 // not available when parasite powered.
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 uint8_t DS18X20_conversion_in_progress(void)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 return ow_bit_io( 1 ) ? DS18X20_CONVERSION_DONE : DS18X20_CONVERTING;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351 static uint8_t read_scratchpad( uint8_t id[], uint8_t sp[], uint8_t n )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 uint8_t i;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 ow_command( DS18X20_READ, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 for ( i = 0; i < n; i++ ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 sp[i] = ow_byte_rd();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 ret = DS18X20_ERROR_CRC;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 #if DS18X20_DECICELSIUS
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 /* convert scratchpad data to physical value in unit decicelsius */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 static int16_t DS18X20_raw_to_decicelsius( uint8_t familycode, uint8_t sp[] )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375 uint16_t measure;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 uint8_t negative;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377 int16_t decicelsius;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 uint16_t fract;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 measure = sp[0] | (sp[1] << 8);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 //measure = 0xFF5E; // test -10.125
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 //measure = 0xFE6F; // test -25.0625
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 if( familycode == DS18S20_FAMILY_CODE ) { // 9 -> 12 bit if 18S20
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 /* Extended measurements for DS18S20 contributed by Carsten Foss */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 measure &= (uint16_t)0xfffe; // Discard LSB, needed for later extended precicion calc
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387 measure <<= 3; // Convert to 12-bit, now degrees are in 1/16 degrees units
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 measure += (16 - sp[6]) - 4; // Add the compensation and remember to subtract 0.25 degree (4/16)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 // check for negative
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 if ( measure & 0x8000 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393 negative = 1; // mark negative
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 measure ^= 0xffff; // convert to positive => (twos complement)++
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 measure++;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 negative = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 // clear undefined bits for DS18B20 != 12bit resolution
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 if ( familycode == DS18B20_FAMILY_CODE || familycode == DS1822_FAMILY_CODE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 switch( sp[DS18B20_CONF_REG] & DS18B20_RES_MASK ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 case DS18B20_9_BIT:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 measure &= ~(DS18B20_9_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 case DS18B20_10_BIT:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 measure &= ~(DS18B20_10_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 case DS18B20_11_BIT:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 measure &= ~(DS18B20_11_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 default:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 // 12 bit - all bits valid
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 decicelsius = (measure >> 4);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 decicelsius *= 10;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 // decicelsius += ((measure & 0x000F) * 640 + 512) / 1024;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 // 625/1000 = 640/1024
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 fract = ( measure & 0x000F ) * 640;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 if ( !negative ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 fract += 512;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 fract /= 1024;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429 decicelsius += fract;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 if ( negative ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 decicelsius = -decicelsius;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 if ( /* decicelsius == 850 || */ decicelsius < -550 || decicelsius > 1250 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436 return DS18X20_INVALID_DECICELSIUS;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 return decicelsius;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 /* format decicelsius-value into string, itoa method inspired
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 by code from Chris Takahashi for the MSP430 libc, BSD-license
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 modifications mthomas: variable-types, fixed radix 10, use div(),
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 insert decimal-point */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 uint8_t DS18X20_format_from_decicelsius( int16_t decicelsius, char str[], uint8_t n)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 uint8_t sign = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 char temp[7];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 int8_t temp_loc = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 uint8_t str_loc = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 div_t dt;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 // range from -550:-55.0�C to 1250:+125.0�C -> min. 6+1 chars
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 if ( n >= (6+1) && decicelsius > -1000 && decicelsius < 10000 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 if ( decicelsius < 0) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 sign = 1;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 decicelsius = -decicelsius;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 // construct a backward string of the number.
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 do {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 dt = div(decicelsius,10);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 temp[temp_loc++] = dt.rem + '0';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 decicelsius = dt.quot;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 } while ( decicelsius > 0 );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 if ( sign ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 temp[temp_loc] = '-';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 ///temp_loc--;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 temp[temp_loc] = '+';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 // reverse the string.into the output
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 while ( temp_loc >=0 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 str[str_loc++] = temp[(uint8_t)temp_loc--];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 if ( temp_loc == 0 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 str[str_loc++] = DS18X20_DECIMAL_CHAR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 str[str_loc] = '\0';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 /* reads temperature (scratchpad) of sensor with rom-code id
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 output: decicelsius
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 returns DS18X20_OK on success */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 uint8_t DS18X20_read_decicelsius( uint8_t id[], int16_t *decicelsius )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 uint8_t sp[DS18X20_SP_SIZE];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 if ( ret == DS18X20_OK ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 *decicelsius = DS18X20_raw_to_decicelsius( id[0], sp );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 /* reads temperature (scratchpad) of sensor without id (single sensor)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 output: decicelsius
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512 returns DS18X20_OK on success */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 uint8_t DS18X20_read_decicelsius_single( uint8_t familycode, int16_t *decicelsius )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 uint8_t sp[DS18X20_SP_SIZE];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 ret = read_scratchpad( NULL, sp, DS18X20_SP_SIZE );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 if ( ret == DS18X20_OK ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 *decicelsius = DS18X20_raw_to_decicelsius( familycode, sp );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 #endif /* DS18X20_DECICELSIUS */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 #if DS18X20_MAX_RESOLUTION
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 static int32_t DS18X20_raw_to_maxres( uint8_t familycode, uint8_t sp[] )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532 uint16_t measure;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 uint8_t negative;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 int32_t temperaturevalue;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 measure = sp[0] | (sp[1] << 8);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537 //measure = 0xFF5E; // test -10.125
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 //measure = 0xFE6F; // test -25.0625
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 if( familycode == DS18S20_FAMILY_CODE ) { // 9 -> 12 bit if 18S20
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541 /* Extended measurements for DS18S20 contributed by Carsten Foss */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 measure &= (uint16_t)0xfffe; // Discard LSB, needed for later extended precicion calc
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 measure <<= 3; // Convert to 12-bit, now degrees are in 1/16 degrees units
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 measure += ( 16 - sp[6] ) - 4; // Add the compensation and remember to subtract 0.25 degree (4/16)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 // check for negative
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 if ( measure & 0x8000 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 negative = 1; // mark negative
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 measure ^= 0xffff; // convert to positive => (twos complement)++
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 measure++;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 negative = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 // clear undefined bits for DS18B20 != 12bit resolution
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 if ( familycode == DS18B20_FAMILY_CODE || familycode == DS1822_FAMILY_CODE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 switch( sp[DS18B20_CONF_REG] & DS18B20_RES_MASK ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 case DS18B20_9_BIT:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 measure &= ~(DS18B20_9_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563 case DS18B20_10_BIT:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 measure &= ~(DS18B20_10_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 case DS18B20_11_BIT:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 measure &= ~(DS18B20_11_BIT_UNDF);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 default:
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 // 12 bit - all bits valid
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571 break;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 temperaturevalue = (measure >> 4);
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 temperaturevalue *= 10000;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 temperaturevalue +=( measure & 0x000F ) * DS18X20_FRACCONV;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 if ( negative ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 temperaturevalue = -temperaturevalue;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583 return temperaturevalue;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 uint8_t DS18X20_read_maxres( uint8_t id[], int32_t *temperaturevalue )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588 uint8_t sp[DS18X20_SP_SIZE];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 ret = read_scratchpad( id, sp, DS18X20_SP_SIZE );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 if ( ret == DS18X20_OK ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 *temperaturevalue = DS18X20_raw_to_maxres( id[0], sp );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 uint8_t DS18X20_read_maxres_single( uint8_t familycode, int32_t *temperaturevalue )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 uint8_t sp[DS18X20_SP_SIZE];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 ret = read_scratchpad( NULL, sp, DS18X20_SP_SIZE );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605 if ( ret == DS18X20_OK ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 *temperaturevalue = DS18X20_raw_to_maxres( familycode, sp );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 uint8_t DS18X20_format_from_maxres( int32_t temperaturevalue, char str[], uint8_t n)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614 uint8_t sign = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 char temp[10];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616 int8_t temp_loc = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 uint8_t str_loc = 0;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618 ldiv_t ldt;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 // range from -550000:-55.0000�C to 1250000:+125.0000�C -> min. 9+1 chars
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 if ( n >= (9+1) && temperaturevalue > -1000000L && temperaturevalue < 10000000L ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 if ( temperaturevalue < 0) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625 sign = 1;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626 temperaturevalue = -temperaturevalue;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 do {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 ldt = ldiv( temperaturevalue, 10 );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631 temp[temp_loc++] = ldt.rem + '0';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 temperaturevalue = ldt.quot;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633 } while ( temperaturevalue > 0 );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635 // mk 20110209
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 if ((temp_loc < 4)&&(temp_loc > 1)) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637 temp[temp_loc++] = '0';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 } // mk end
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 if ( sign ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 temp[temp_loc] = '-';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 temp[temp_loc] = '+';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646 while ( temp_loc >= 0 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 str[str_loc++] = temp[(uint8_t)temp_loc--];
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 if ( temp_loc == 3 ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 str[str_loc++] = DS18X20_DECIMAL_CHAR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 str[str_loc] = '\0';
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 #endif /* DS18X20_MAX_RESOLUTION */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 #if DS18X20_EEPROMSUPPORT
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667 uint8_t DS18X20_write_scratchpad( uint8_t id[],
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 uint8_t th, uint8_t tl, uint8_t conf)
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673 if( ow_input_pin_state() ) { // only send if bus is "idle" = high
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 ow_command( DS18X20_WRITE_SCRATCHPAD, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675 ow_byte_wr( th );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
676 ow_byte_wr( tl );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677 if ( id[0] == DS18B20_FAMILY_CODE || id[0] == DS1822_FAMILY_CODE ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678 ow_byte_wr( conf ); // config only available on DS18B20 and DS1822
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
680 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683 uart_puts_P_verbose( "DS18X20_write_scratchpad: Short Circuit!\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
690 uint8_t DS18X20_read_scratchpad( uint8_t id[], uint8_t sp[], uint8_t n )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
694 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695 if( ow_input_pin_state() ) { // only send if bus is "idle" = high
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 ret = read_scratchpad( id, sp, n );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 uart_puts_P_verbose( "DS18X20_read_scratchpad: Short Circuit!\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
702
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
705
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
706 uint8_t DS18X20_scratchpad_to_eeprom( uint8_t with_power_extern,
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
707 uint8_t id[] )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712 if( ow_input_pin_state() ) { // only send if bus is "idle" = high
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713 if ( with_power_extern != DS18X20_POWER_EXTERN ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 ow_command_with_parasite_enable( DS18X20_COPY_SCRATCHPAD, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 /* not longer needed: ow_parasite_enable(); */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
717 ow_command( DS18X20_COPY_SCRATCHPAD, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719 _delay_ms(DS18X20_COPYSP_DELAY); // wait for 10 ms
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
720 if ( with_power_extern != DS18X20_POWER_EXTERN ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721 ow_parasite_disable();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
723 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
725 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726 uart_puts_P_verbose( "DS18X20_copy_scratchpad: Short Circuit!\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727 ret = DS18X20_START_FAIL;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
732
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 uint8_t DS18X20_eeprom_to_scratchpad( uint8_t id[] )
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734 {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
735 uint8_t ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
736 uint8_t retry_count=255;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 ow_reset();
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 if( ow_input_pin_state() ) { // only send if bus is "idle" = high
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
740 ow_command( DS18X20_RECALL_E2, id );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
741 while( retry_count-- && !( ow_bit_io( 1 ) ) ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
742 ;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
743 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744 if ( retry_count ) {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 ret = DS18X20_OK;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 } else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 uart_puts_P_verbose( "DS18X20_recall_E2: timeout!\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 else {
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
752 uart_puts_P_verbose( "DS18X20_recall_E2: Short Circuit!\r" );
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
753 ret = DS18X20_ERROR;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756 return ret;
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757 }
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759 #endif /* DS18X20_EEPROMSUPPORT */
7d409dded901 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760