Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
22 | jlesech | 1 | #include <OneWire.h> |
2 | |||
3 | // OneWire DS18S20, DS18B20, DS1822 Temperature Example |
||
4 | // |
||
5 | // http://www.pjrc.com/teensy/td_libs_OneWire.html |
||
6 | // |
||
7 | // The DallasTemperature library can do all this work for you! |
||
8 | // http://milesburton.com/Dallas_Temperature_Control_Library |
||
9 | |||
10 | OneWire ds(10); // on pin 10 |
||
11 | |||
12 | void setup(void) { |
||
13 | Serial.begin(9600); |
||
14 | } |
||
15 | |||
16 | void loop(void) { |
||
17 | byte i; |
||
18 | byte present = 0; |
||
19 | byte type_s; |
||
20 | byte data[12]; |
||
21 | byte addr[8]; |
||
22 | float celsius, fahrenheit; |
||
23 | |||
24 | if ( !ds.search(addr)) { |
||
25 | Serial.println("No more addresses."); |
||
26 | Serial.println(); |
||
27 | ds.reset_search(); |
||
28 | delay(250); |
||
29 | return; |
||
30 | } |
||
31 | |||
32 | Serial.print("ROM ="); |
||
33 | for( i = 0; i < 8; i++) { |
||
34 | Serial.write(' '); |
||
35 | Serial.print(addr[i], HEX); |
||
36 | } |
||
37 | |||
38 | if (OneWire::crc8(addr, 7) != addr[7]) { |
||
39 | Serial.println("CRC is not valid!"); |
||
40 | return; |
||
41 | } |
||
42 | Serial.println(); |
||
43 | |||
44 | // the first ROM byte indicates which chip |
||
45 | switch (addr[0]) { |
||
46 | case 0x10: |
||
47 | Serial.println(" Chip = DS18S20"); // or old DS1820 |
||
48 | type_s = 1; |
||
49 | break; |
||
50 | case 0x28: |
||
51 | Serial.println(" Chip = DS18B20"); |
||
52 | type_s = 0; |
||
53 | break; |
||
54 | case 0x22: |
||
55 | Serial.println(" Chip = DS1822"); |
||
56 | type_s = 0; |
||
57 | break; |
||
58 | default: |
||
59 | Serial.println("Device is not a DS18x20 family device."); |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | ds.reset(); |
||
64 | ds.select(addr); |
||
65 | ds.write(0x44,1); // start conversion, with parasite power on at the end |
||
66 | |||
67 | delay(1000); // maybe 750ms is enough, maybe not |
||
68 | // we might do a ds.depower() here, but the reset will take care of it. |
||
69 | |||
70 | present = ds.reset(); |
||
71 | ds.select(addr); |
||
72 | ds.write(0xBE); // Read Scratchpad |
||
73 | |||
74 | Serial.print(" Data = "); |
||
75 | Serial.print(present,HEX); |
||
76 | Serial.print(" "); |
||
77 | for ( i = 0; i < 9; i++) { // we need 9 bytes |
||
78 | data[i] = ds.read(); |
||
79 | Serial.print(data[i], HEX); |
||
80 | Serial.print(" "); |
||
81 | } |
||
82 | Serial.print(" CRC="); |
||
83 | Serial.print(OneWire::crc8(data, 8), HEX); |
||
84 | Serial.println(); |
||
85 | |||
86 | // convert the data to actual temperature |
||
87 | |||
88 | unsigned int raw = (data[1] << 8) | data[0]; |
||
89 | if (type_s) { |
||
90 | raw = raw << 3; // 9 bit resolution default |
||
91 | if (data[7] == 0x10) { |
||
92 | // count remain gives full 12 bit resolution |
||
93 | raw = (raw & 0xFFF0) + 12 - data[6]; |
||
94 | } |
||
95 | } else { |
||
96 | byte cfg = (data[4] & 0x60); |
||
97 | if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms |
||
98 | else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms |
||
99 | else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms |
||
100 | // default is 12 bit resolution, 750 ms conversion time |
||
101 | } |
||
102 | celsius = (float)raw / 16.0; |
||
103 | fahrenheit = celsius * 1.8 + 32.0; |
||
104 | Serial.print(" Temperature = "); |
||
105 | Serial.print(celsius); |
||
106 | Serial.print(" Celsius, "); |
||
107 | Serial.print(fahrenheit); |
||
108 | Serial.println(" Fahrenheit"); |
||
109 | } |