Arduino DS18B20温度检测
一.实物图
注:电阻选取4.7k欧
二.事例代码
注:先下载Onewire库到arduino libraries目录下,然后就有例子
#include <OneWire.h> // OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary) void setup(void) {
Serial.begin(9600);
} void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit; if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
} Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
} if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println(); // the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
} ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it. present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println(); // Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
}
三.串口读取数据
Arduino DS18B20温度检测的更多相关文章
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- 一种快速查询多点DS18B20温度的方法(转)
源:http://hi.baidu.com/james_xiao/item/79b961c90623093e45941623 一种快速查询多点DS18B20温度的方法 引言 为了满足实时性要 ...
- ZigBee学习五 无线温度检测
ZigBee学习五 无线温度检测 1)修改公用头文件GenericApp.h typedef union h{ uint8 TEMP[4]; struct RFRXBUF { unsigned cha ...
- 基于FS4412的DS18B20温度采集编程实现(1-时序分析)
作者:秦老师,华清远见嵌入式学院讲师. 一.DS18B20简介 DS18B20是常用的数字温度计.DS18B20数字温度计提供9至12位(可配置)温度读数,表明该设备的温度. 信息通过单总线接口被发送 ...
- Arduino+DS18b20+OLED Display
DS18b20获取到温度数值保存到变量中,然后和天气图标还有滚动字幕一起发送到OLED 屏幕上显示 需要用到的库均可在Arduino库管理器下载. 电路图: 图中屏幕接线已在代码中写出,温度传感器da ...
- 基于PLC-C#串口通讯,温度检测和转速监控的c#/.Net实现。
我司为五金加工企业,其中有一条喷涂车间和流水线,客户要求能实时监控炉温温度.流水线速,并设置上下限值,达到上下限时报警. 开始考虑过USB的温度采集器,但是却没有找到带USB的光电开关,并且线路长度受 ...
- DS18B20温度获取
https://detail.tmall.com/item.htm?id=40083203373&spm=a1z09.2.0.0.31cd2e8d1sb06V&_u=e1qf7bf56 ...
- 嵌入式 Web Server 温度检测系统
1. Web Server 服务器后台表单处理程序:使用 CGI 程序接口编写后台程序的 Web 服务器. 2. Boa 服务器
- 单片机C语言实现的采用DS18B20的温度检测装置
这几天老师布置了一个课程设计题目:采用51单片机控制的DS18B20温度检测系统.大概花了我一个礼拜的时间,幸好我的C语言学得还可以,最后还是让我搞出来了,真是高兴,我是采用STC-52单片机和DS1 ...
随机推荐
- HDU 5442 Favorite Donut
Favorite Donut Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- broker监控dataguard配置
使用broker查看dataguard信息时有告警 DGMGRL> show configuration; Configuration - DRTEST Protection Mode: Max ...
- Android欢迎页面以及引导页面
开发环境:Windows 10 x64,Android Studio 3.0 很多APP都会在启动主界面(MainActivity)之前显示一个短暂的欢迎页面,设置微博,知乎,百度之类APP还是在欢迎 ...
- Spring MVC-集成(Integration)-Hibernate验证器示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_hibernate_validator.htm 说明:示例基于Spring MVC ...
- 开源GIS软件 3
OpenWLANMap 与 OpenStreetMap 项目类似,OpenWLANMap 将变成一个开源的替代产品,提供 WLANs 的数据库.定位信息等. 开源排水管网GIS系统 udpnGIS 邢 ...
- poj 1635
有根树同构.参考论文<hash在....> #include <iostream> #include <fstream> #include <algorith ...
- linux sh 脚本调用外部命令
参考:http://blog.csdn.net/lhb_blog/article/details/22083649 ------------------------------------------ ...
- nyoj 1238 最少换乘 (河南省第八届acm程序设计大赛)
题目1238 题目信息 执行结果 本题排行 pid=1238" style="text-decoration:none; color:rgb(55,119,188)"&g ...
- 2014年辛星解读css第二节
第一节我们简单介绍了一下CSS的工作流程,我相信读者会有一个大体的认识,那么接下来我们将会深入的研究一下CSS的细节问题,这些问题的涉及将会使我们的工作更加完好. *************凝视*** ...
- Mahout算法调用展示平台2.1
软件版本号: windows7: Tomcat7.JDK7.Spring4.0.2.Struts2.3.Hibernate4.3.myeclipse10.0.easyui:Linux(centos6. ...