2018-01-1818:20:48
感觉自己最近有点凌乱,一个很简单的问题都能困扰自己很久。以前能很好使用和调试的DS18B20温度传感器,今天愣是搞了很久,妈卖批。
仅仅一个上拉电阻就困扰了我很久,同时也颠覆了我一直以来“电阻”无用的理论。有一些敏感元件,电阻的作用不容小觑。
调试代码简单精简版本如下,极客工坊大神修改版
1 #include "DS18B20_S.h"
2 //传感器设定为10位模式,每次转换时间<187.5ms,如果需要12位模式,请修改库文件of ds.set(0x7F);
3 DS18B20_S ds(9);//pin9
4 void setup() {
5 Serial.begin(9600);
6 }
7 void loop() {
8 ds.start();//开始测量(所有传感器)
9 float a=ds.get();
10 delay(200);//2根线连接模式
11 Serial.print("c0=");
12 if(a>200){ //CRC 校验错误
13 Serial.println("CRC error");
14 }
15 else{
16 Serial.println(a);
17 }
18
19 //下面的不重要
20 // void set(byte n);//set(0x1F) 9位模式 delay(94) ;
21 //0x3F 10位模式 delay(188);
22 //0x5F 11位模式 delay(375);
23 //0x7F 12 位模式 delay(750);
24 }
中贝斯特代码《通用版代码》用到了#include <OneWire.h>#include <DallasTemperature.h>库文件
1 //上拉电阻!!!!!!!!!!!!!!!!!
2 #include <OneWire.h>
3 #include <DallasTemperature.h>
4
5 // Data wire is plugged into port 2 on the Arduino
6 #define ONE_WIRE_BUS 2
7
8 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
9 OneWire oneWire(ONE_WIRE_BUS);
10
11 // Pass our oneWire reference to Dallas Temperature.
12 DallasTemperature sensors(&oneWire);
13
14 void setup(void)
15 {
16 // start serial port
17 Serial.begin(9600);
18 Serial.println("Dallas Temperature IC Control Library Demo");
19
20 // Start up the library
21 sensors.begin();
22 }
23
24 void loop(void)
25 {
26 // call sensors.requestTemperatures() to issue a global temperature
27 // request to all devices on the bus
28 Serial.print("Requesting temperatures...");
29 sensors.requestTemperatures(); // Send the command to get temperatures
30 Serial.println("DONE");
31
32 Serial.print("Temperature for the device 1 (index 0) is: ");
33 Serial.println(sensors.getTempCByIndex(0));
34 }
来源:oschina
链接:https://my.oschina.net/u/4323338/blog/4209579