Onewire App Single Bus Temperature Sensor DS18 Series

OneWire DS18S20, DS18B20, DS1822 Temperature

DS18B20

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of -10°C to +85°C. In addition, the DS18B20 can derive power directly from the data line (“parasite power”) , eliminating the need for an external power supply. Resolution9-12bits, alarm can be set, suitable for1-Wire Bus, measure temperature-55-125Celsius
Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one micropro cessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems. One controller can be connected to multiple A sensor that can be used in many fields

FEATURES Features
§ Unique 1-Wire? Interface Requires Only One Port Pin for Communication
§ Each Device has a Unique 64-Bit Serial Code Stored in an On-Board ROM
§ Multidrop Capability Simplifies Distributed Temperature-Sensing Applications
§ Requires No External Components
§ Can Be Powered from Data Line; Power Supply Range is 3.0V to 5.5V
§ Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
§ ±0.5° C Accuracy from -10°C to +85°C
§ Thermometer Resolution is User Selectable from 9 to 12 Bits
§ Converts Temperature to 12-Bit Digital Word in 750ms (Max)

Share a picture

64-BIT LASERED ROM CODE 64Bit Lithography strong>ROMCode
Each DS18B20 contains a unique 64–bit code (see Figure 6) stored in ROM. The least significant 8 bits of the ROM code contain the DS18B20’s 1-Wire family code: 28h (8Bit Family Code). The next 48 bits contain a unique serial number ( 48Bit Sequence Code). The most significant 8 bits contain a cyclic redundancy check (CRC) byte that is calculated from the first 56 bits of the ROM code ( 8Bit Redundancy Check Code). A detailed explanation of the CRC bits is provided in the CRC Generation section. The 64-bit ROM code and associated ROM function control logic allow the DS18B20 to operate as a 1-Wire device using the protocol detailed in the 1-Wire Bus System section.
Figure 6. 64-Bit Lasered ROM Code< /s trong>

Sample program

p>

 1 #include 
2
3
4
5 // OneWire DS18S20, DS18B20, DS1822 Temperature Example
6
7 //
8
9< /span> // http://www.pjrc.com/teensy/td_libs_OneWire .html
10
11 //
12
13 // The DallasTemperature library can do all this work for you!
14
15 // http:// milesburton.com/Dallas_Temperature_Control_Library
16
17 < br /> 18
19 OneWire ds(10); // on pin 10, define single bus device ds, connect to Pin 10
20
21
22
23 void setup(void)
24
25 {
26
27 Serial.begin(9600);
28
29 }
30 < br /> 31
32
33 void loop(void)
34
35 {
36
37 byte i;
38
39 byte present = 0;
40
41 byte type_s;
42
43 byte data[12 ];
44
45 byte addr[8]; //The ROM of the sensor, a total of 8 bytes of data, 64 bits
46
47 float celsius, fahrenheit;
< /span> 48
49
50
51 if (!ds.search (addr)) //If you don’t find ds, just keep querying
52
53 {
< /span> 54
55 Serial.println("No mo re addresses.");
56
57 Serial.println();
58
59 ds.reset_search();
60
61 span> delay(250);
62
63 return;
64
65 < span style="color: #000000"> }
66
67
68
69 Serial.print("ROM ="); // The address of ds will be displayed
70
71 for( i = 0; i < 8; i++)
72
73 {
74
75 Serial.write(' ');
76< /span>
77 Serial.print(addr[i], HEX);
78
< span style="color: #008080"> 79
}
80
81
82
83 if (OneWire::crc8(addr, 7) != addr[7]) // CRC check failed
84
85 {
86
87 Serial.println("CRC is not valid!");
88
89 < span style="color: #0000ff">return
;
90
91 }
92
93 Serial.println();
94
95
96
97 // the first ROM byte indicates which chip, according to addr[0] to determine the DS18 series model
98
99 switch (addr[0< /span>])
100
101 {
102
103 case 0x10:
104
105 Serial.println(" Chip = DS18S20"); // or old DS1820
106< /span>
107 type_s = 1;
108
109 break ;
110
111 case 0x28: //0x28 is the family code of DS18B20
112
113 Serial.println(" Chip = DS18B20");
114
115 type_s = 0 ;
116
117 break;
118
119 case 0x22 span>:
120
121 Serial.println(" Chip = DS1822");
122 < br />123 type_s = 0;< br />124
125 break;
126
< span style="color: #008080">127
default:
128
129 Serial.println("Device is not a DS18x20 family device.");
130
131 return;
132 < br />133 }
134
135
136
< span style="color: #008080">137
ds.reset(); //Reset
138
139 ds.select(addr); // Select device
140
141 ds.write(0x44 ,1);
142
143 // start conversion, with parasite power on at the end,
144
145 //The internal temperature change of the sensor is stored in the temporary memory, and 0x44 is the protocol of the sensor Coding
146
147
< span style="color: #008080">148

149 delay(1000< /span>); // maybe 750ms is enough, maybe not
150
151 // we might do a ds.depower() here, but the reset will take care of it.
152
153
154
155< /span> present = ds.reset(); //Return 1 means there is a device.< br />156
157 ds.select(addr);
158
159 ds.read(0xBE); / / Read Scratchpad reads the scratchpad, 0xBE is the protocol code of the sensor
160
161
162
163 Serial.print(" Data = ");
164
165 Serial.print(present,HEX);
166
167 Serial.print(" ");
168< /span>
169 for (i = 0; i <9; i++)
170
171 {// we need 9 bytes
172
173 data[i] =< span style="color: #000000"> ds.read();
174
175 Serial.print(data[i], HEX);
176
177 Serial.print(" ");
178
179 }
180
1 81 Serial.print(" CRC=");
182
< span style="color: #008080">183
Serial.print(OneWire::crc8(data, 8), HEX);
184
185 span> Serial.println();
186
187
188
189< /span> // convert the data to actual temperature
190
191
192
193 unsigned int raw = (data[1] << < span style="color: #800080">8
) | data[0];
194
195 if (type_s) //DS18S20 corresponding temperature conversion
196
197 {
198
199 raw = raw << < span style="color: #800080">3
; // 9 bit resolution default span>
200
201 if (data[< span style="color: #800080">7
] == 0x10)
202
203 {
204
205 // count remain gives full 12 bit resolution
206
207 raw = (raw & 0xFFF0) + 12-data[6];
208
209 }
210
211 }
212
213 else //DS18B20, DS1822 corresponding temperature conversion< br />214
215 {
216
217 byte cfg = (data[4] & 0x60 );
218
219 if (cfg == 0x00) raw = raw << < span style="color: #800080">3
; // 9 bit resolution, 93.75 ms
220
221 else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
222
223 else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
224
225 // default is 12 bit resolution, 750 ms conversion time
226 < br / >227 }
228< /span>
229 celsius = (float)raw / 16.0; //Celsius
230
231 fahrenheit = celsius * 1.8 + 32.0; //Fahrenheit temperature
232
233 Serial. print(" Temperature = ");
234
235 Serial.print(celsius);
236
237 Serial.print(" Celsius, < span style="color: #800000">"
);
238 span>
239 Serial.print(fahrenheit);
240
241 Serial.println(" Fahrenheit");< br />242
243 }

 1 #include 
2
3
4
5 // OneWire DS18S20, DS18B20, DS1822 Temperature Example
6
7 //
< span style="color: #008080"> 8
9 // http://www.pjrc.com/teensy/td_libs_OneWire.html
10
11 //
< span style="color: #008080"> 12

13 // The Dal lasTemperature library can do all this work for you!
14
15 // http://milesburton.com/Dallas_Temperature_Control_Library
16
17
18
19 OneWire ds(10 ); // on pin 10, define single bus device ds, connected to pin 10
20
21
22
23 void setup(void)
2 4
25 {
26
27 Serial.begin(9600);
28
29 }
30
31
32
33 void loop(void)
34
35 {
36
37 byte i;
38
39 < span style="color: #0000ff">byte
present = 0;
< /span> 40
41 byte type_s;
42
43 byte data[12];
44
45< /span> byte addr[8]; //传感器的ROM,共8个字节的数据,64位
46
47 float celsius, fahrenheit;
48
49
50
51 if ( !ds.search(addr)) //查询不到ds就一直查询
52
53 {
54
55 Serial.println("No more addresses.");
56
57 Serial.println();
58

59 ds.reset_search();
60
61 delay(250);
62
63 return;
64
65 }
66
67
68
69 Serial.print("ROM ="); //查询到则显示ds的地址
70
71 for( i = 0; i < 8; i++)
72
73 {
74
75 Serial.write( );
76
77 Serial.print(addr[i], HEX);
78
79 }
80
81
82
83 if (OneWire::crc8(addr, 7) != addr[7]) // CRC检验失败的情况
84
85 {
86
87 Serial.println("CRC is not valid!");
88
89 return;
90
91 }
92
93 Serial.println();
94
95
96
97 // the first ROM byte indicates which chip ,根据addr[0]决定DS18系列的型号
98
99 switch (addr[0])
100
101 {
102
103 case 0x10:
104
105 Serial.println(" Chip = DS18S20"); // or old DS1820
106
107 type_s = 1;
108
109 break;
110
111 case 0x28: //0x28是DS18B20的家族编码
112
113 Serial.println(" Chip = DS18B20");
114
115 type_s = 0;
116
117 break;
118
119 case 0x22:
120
121< /span> Serial.println(" Chip = DS1822");
122
123 type_s = 0;
124
125 break;
126
127 default:
128
129 Serial.println("Device is not a DS18x20 family device.");
130
131 return;
132
133 }
134
135
136
137 ds.reset(); //复位
138
139 ds.select(addr); // 选择设备
140
141 ds.write(0x44,1);
142
143 // start conversion, with parasite power on at the end,
144
145 //传感器内部温度变换保存在暂存器,0x44是传感器的协议编码
146
147
148
149 delay(1000); // maybe 750ms is enough, maybe not
150
151 // we might d o a ds.depower() here, but the reset will take care of it.
152
153
154
155 present = ds.reset(); //返回1表示有设备存在
156
157 ds.select(addr);
158
159 ds.read(0xBE); // Read Scratchpad 读暂存器,0xBE是传感器的协议编码
160
161
162
163 Se rial.print(" Data = ");
164
165 Serial.print(present,HEX);
166
167 Serial.print(" ");
168
169 for ( i = 0; i < 9; i++)
170
171 { // we need 9 bytes
172
173 data[i] = ds.read();
174
175 Serial.print(data[i], HEX);
176
177 Serial.print(" ");
178
179 }
180
181 Serial.print(" CRC=");
182
183 Serial.print(OneWire::crc8(data, 8), HEX);
184
185 Serial.println();
186
187
188
189 // convert the data to actual temperature
190
191
192
193 unsigned int raw = (data[1] << 8) | data[0];
194

195 if (type_s) //DS18S20对应的温度转换
196
197 {
198
199 raw = raw << 3; // 9 bit resolution default
200
201 if (data[7] == 0x10)
202
203 {
204
205 // count remain gives full 12 bit resolution
206
207 raw = (raw & 0xFFF0) + 12 - data[6];
208
209 }
210
211 }
212
213 else //DS18B20,DS1822对应的温度转换
214
215 {
216
217 byte cfg = (data[4] & 0x60);
218
219 if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
220
221 else if (cfg == 0x20) raw = raw << 2 ; // 10 bit res, 187.5 ms
222
223 else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
224
225 // default is 12 bit resolution, 750 ms conversion time
226
227 }
228
229 celsius = (float)raw / 16.0; //摄氏温度
230
231 fahrenheit = celsius * 1.8 + 32.0; //华氏温度
232
233 Serial.print(" Temperature = ");
234
235 Serial.print(celsius);
236
237 Serial.print(" Celsius, "
);
238
239 Serial.print(fahrenheit);
240
241 Serial.println(" Fahrenheit");
242
243 }

Leave a Comment

Your email address will not be published.