annotate ds18x20.h @ 125:397ac5c079bf

avoid bad values
author Matt Johnston <matt@ucc.asn.au>
date Wed, 10 Oct 2012 22:58:35 +0800
parents 7da9a3f23592
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #ifndef DS18X20_H_
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #define DS18X20_H_
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 #ifdef __cplusplus
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 extern "C" {
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 #endif
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #include <stdlib.h>
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 #include <stdint.h>
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 // DS18x20 EERPROM support disabled(0) or enabled(1) :
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #define DS18X20_EEPROMSUPPORT 1
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 // decicelsius functions disabled(0) or enabled(1):
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #define DS18X20_DECICELSIUS 1
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 // max. resolution functions disabled(0) or enabled(1):
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #define DS18X20_MAX_RESOLUTION 1
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 // extended output via UART disabled(0) or enabled(1) :
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #define DS18X20_VERBOSE 1
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 /* return values */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #define DS18X20_OK 0x00
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #define DS18X20_ERROR 0x01
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 #define DS18X20_START_FAIL 0x02
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #define DS18X20_ERROR_CRC 0x03
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #define DS18X20_INVALID_DECICELSIUS 2000
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #define DS18X20_POWER_PARASITE 0x00
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #define DS18X20_POWER_EXTERN 0x01
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #define DS18X20_CONVERSION_DONE 0x00
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #define DS18X20_CONVERTING 0x01
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 /* DS18X20 specific values (see datasheet) */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #define DS18S20_FAMILY_CODE 0x10
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define DS18B20_FAMILY_CODE 0x28
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define DS1822_FAMILY_CODE 0x22
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #define DS18X20_CONVERT_T 0x44
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define DS18X20_READ 0xBE
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #define DS18X20_WRITE 0x4E
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #define DS18X20_EE_WRITE 0x48
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #define DS18X20_EE_RECALL 0xB8
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #define DS18X20_READ_POWER_SUPPLY 0xB4
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 #define DS18B20_CONF_REG 4
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #define DS18B20_9_BIT 0
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #define DS18B20_10_BIT (1<<5)
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 #define DS18B20_11_BIT (1<<6)
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 #define DS18B20_12_BIT ((1<<6)|(1<<5))
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 #define DS18B20_RES_MASK ((1<<6)|(1<<5))
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 // undefined bits in LSB if 18B20 != 12bit
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 #define DS18B20_9_BIT_UNDF ((1<<0)|(1<<1)|(1<<2))
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 #define DS18B20_10_BIT_UNDF ((1<<0)|(1<<1))
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 #define DS18B20_11_BIT_UNDF ((1<<0))
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #define DS18B20_12_BIT_UNDF 0
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 // conversion times in milliseconds
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 #define DS18B20_TCONV_12BIT 750
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 #define DS18B20_TCONV_11BIT DS18B20_TCONV_12_BIT/2
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 #define DS18B20_TCONV_10BIT DS18B20_TCONV_12_BIT/4
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 #define DS18B20_TCONV_9BIT DS18B20_TCONV_12_BIT/8
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 #define DS18S20_TCONV DS18B20_TCONV_12_BIT
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 // constant to convert the fraction bits to cel*(10^-4)
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 #define DS18X20_FRACCONV 625
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 // scratchpad size in bytes
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 #define DS18X20_SP_SIZE 9
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 // DS18X20 EEPROM-Support
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 #define DS18X20_WRITE_SCRATCHPAD 0x4E
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 #define DS18X20_COPY_SCRATCHPAD 0x48
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 #define DS18X20_RECALL_E2 0xB8
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 #define DS18X20_COPYSP_DELAY 10 /* ms */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 #define DS18X20_TH_REG 2
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 #define DS18X20_TL_REG 3
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 #define DS18X20_DECIMAL_CHAR '.'
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 extern uint8_t DS18X20_find_sensor(uint8_t *diff,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 uint8_t id[]);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 extern uint8_t DS18X20_get_power_status(uint8_t id[]);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 extern uint8_t DS18X20_start_meas( uint8_t with_external,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 uint8_t id[]);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 // returns 1 if conversion is in progress, 0 if finished
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 // not available when parasite powered
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 extern uint8_t DS18X20_conversion_in_progress(void);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 #if DS18X20_DECICELSIUS
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 extern uint8_t DS18X20_read_decicelsius( uint8_t id[],
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 int16_t *decicelsius );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 extern uint8_t DS18X20_read_decicelsius_single( uint8_t familycode,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 int16_t *decicelsius );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 extern uint8_t DS18X20_format_from_decicelsius( int16_t decicelsius,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 char s[], uint8_t n);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 #endif /* DS18X20_DECICELSIUS */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 #if DS18X20_MAX_RESOLUTION
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 // temperature unit for max. resolution is �C * 10e-4
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 // examples: -250625 -> -25.0625�C, 1250000 -> 125.0000 �C
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 extern uint8_t DS18X20_read_maxres( uint8_t id[],
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 int32_t *temperaturevalue );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 extern uint8_t DS18X20_read_maxres_single( uint8_t familycode,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 int32_t *temperaturevalue );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 extern uint8_t DS18X20_format_from_maxres( int32_t temperaturevalue,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 char s[], uint8_t n);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 #endif /* DS18X20_MAX_RESOLUTION */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 #if DS18X20_EEPROMSUPPORT
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 // write th, tl and config-register to scratchpad (config ignored on DS18S20)
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 uint8_t DS18X20_write_scratchpad( uint8_t id[],
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 uint8_t th, uint8_t tl, uint8_t conf);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 // read scratchpad into array SP
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 uint8_t DS18X20_read_scratchpad( uint8_t id[], uint8_t sp[], uint8_t n);
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 // copy values int scratchpad into DS18x20 eeprom
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 uint8_t DS18X20_scratchpad_to_eeprom( uint8_t with_power_extern,
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 uint8_t id[] );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 // copy values from DS18x20 eeprom into scratchpad
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 uint8_t DS18X20_eeprom_to_scratchpad( uint8_t id[] );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 #endif /* DS18X20_EEPROMSUPPORT */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 #if DS18X20_VERBOSE
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 extern void DS18X20_show_id_uart( uint8_t *id, size_t n );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 extern uint8_t DS18X20_read_meas_all_verbose( void );
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 #endif /* DS18X20_VERBOSE */
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 #ifdef __cplusplus
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 #endif
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139
7da9a3f23592 Import ds18x20 code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #endif