OneWire应用 单总线温度传感器DS18系列

Posted myautomation

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OneWire应用 单总线温度传感器DS18系列相关的知识,希望对你有一定的参考价值。

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.  分辨率9-12位,可设置报警,适用于1-Wire 总线,测量温度-55-125摄氏度
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 microprocessor 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.  一个控制器可连接多个传感器,可应用于许多领域

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)

 技术分享图片

 64-BIT LASERED ROM CODE   64位光刻ROM编码
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(8位家族编码). The next 48 bits contain a unique serial number(48位序列码). The most significant 8 bits contain a cyclic redundancy check (CRC) byte that is calculated from the first 56 bits of the ROM code(8位冗余检验码). 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

  

示例程序

 

  1 #include <OneWire.h>
  2 
  3                                                                                              
  4 
  5 // OneWire DS18S20, DS18B20, DS1822 Temperature Example
  6 
  7 //
  8 
  9 // 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  
 18 
 19 OneWire  ds(10);   // on pin 10,定义单总线设备ds,连接于10号引脚
 20 
 21  
 22 
 23 void setup(void)
 24 
 25 {
 26 
 27   Serial.begin(9600);
 28 
 29 }
 30 
 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];     //传感器的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       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 do 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   Serial.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 }

 














以上是关于OneWire应用 单总线温度传感器DS18系列的主要内容,如果未能解决你的问题,请参考以下文章

ESP3213.DS18B20温度传感器实验(OneWire和DallasTemperature库)

ESP3214.DHT11湿度传感器实验(DHT操作库)

ESP3214.DHT11湿度传感器实验(DHT操作库)

一起玩转树莓派——DS18B20测温模块应用

DS18B20温度传感器

单片机练习 - DS18B20温度转换与显示