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系列的更多相关文章

  1. 【蓝桥杯单片机11】单总线温度传感器DS18B20的基本操作

    [蓝桥杯单片机11]单总线温度传感器DS18B20的基本操作 广东职业技术学院 欧浩源 单总线数字温度传感器DS18B20几乎成了各类单片机甚至ARM实验板的标配模块来,在蓝桥杯的往届省赛和国赛中,这 ...

  2. 玩转X-CTR100 l STM32F4 l DS18B20单总线温度传感器

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 扩展DS1 ...

  3. EFM32JG系列MCU内部温度传感器使用方法

    在很多电子类应用场合中,我们经常需要采集产品工作的周围环境温度,一般采取的方式有两种: 1)外加温度传感器 2)采用MCU内部温度传感器 外加温度传感器会增加产品的成本以及布板空间,所以在很多场合,我 ...

  4. 「雕爷学编程」Arduino动手做(39)——DS18B20温度传感器

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  5. Arduion学习(三)驱动温度传感器

    一.实验目的: 1.将温度值打印显示在串口监视器 1.将温度值打印显示在串口,不同温度段显示不同的灯光,并在温度过高或过低时利用蜂鸣器报警. 二.实验准备: 1.查阅相关资料,了解本次实验所用到的引脚 ...

  6. 单片机学习(十二)1-Wire通信协议和DS18B20温度传感器

    目录 一.DS18B20 1. DS18B20简介 2. 电路原理图 3. 内部结构 内部完整结构框图 存储器结构 二.单总线(1-Wire BUS) 1. 单总线简介 2. 电路规范 3. 单总线的 ...

  7. DS18B20温度传感器知识点总结

    2018-01-1818:20:48 感觉自己最近有点凌乱,一个很简单的问题都能困扰自己很久.以前能很好使用和调试的DS18B20温度传感器,今天愣是搞了很久,妈卖批. 仅仅一个上拉电阻就困扰了我很久 ...

  8. Arduino入门笔记(6):温度传感器及感温杯实验

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.Arduino板 :https://item.taob ...

  9. 树莓派 Zero W+温度传感器DS18B20

    树莓派 Zero W+温度传感器DS18B20 作者:陈拓chentuo@ms.xab.ac.cn 2018.05.28/2018.06.01 0.  概述 用树莓派 Zero W读取DS18B20温 ...

随机推荐

  1. php高效读取和写入

    /** * 删除非空目录里面所有文件和子目录 * @param string $dir * @return boolean */ function fn_rmdir($dir) { //先删除目录下的 ...

  2. React.Fragment

    React 中一个常见模式是为一个组件返回多个元素.Fragments 可以让你聚合一个子元素列表,并且不在DOM中增加额外节点. Fragments 看起来像空的 JSX 标签: render() ...

  3. pymssql 介绍

    pymssql包是Python语言用于连接SQL Server数据库的驱动程序(或者称作DB API),它是最终和数据库进行交互的工具.SQLAlchemy包就是利用pymssql包实现和SQL Se ...

  4. 【C#】Random类中构造方法、时间种子与随机数序列的关系

    Random类 构造函数 1) Random random = new Random(); // 无参数构造函数使用系统时钟生成其种子值 然而,系统时钟取值范围有限,因此在小规模计算中,可能无法使用不 ...

  5. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  6. Python 中的数字到底是什么?

    花下猫语:在 Python 中,不同类型的数字可以直接做算术运算,并不需要作显式的类型转换.但是,它的"隐式类型转换"可能跟其它语言不同,因为 Python 中的数字是一种特殊的对 ...

  7. Construct a Matrix (矩阵快速幂+构造)

    There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...

  8. 微服务架构组件梳理之Netflix停更之后该何去何从

    自2018年底,Netflix陆续宣布Eureka.Hystrix等框架进入维护状态,不再进行新功能的开发. 恰逢最近我打算对公司的办公项目进行微服务架构升级,所以恶补了一番微服务相关知识,在这里进行 ...

  9. logback1.11的一个bug:有线程持续不断写入log文件,log文件就不会按设定以日期切换。

    此Bug的解决方案请见:https://www.cnblogs.com/xiandedanteng/p/12205422.html logback是log4j的后继者,作者也是同一人,但其中的bug不 ...

  10. CDH5.16.1集群企业真正离线部署

    一.准备工作 1.离线部署主要分为三块: MySQL离线部署 CM离线部署 Parcel文件离线源部署 2.规划 节点 MySQL部署组件 Parcel文件离线源 CM服务进程 大数据组件 hadoo ...