to github
[svn42.git] / rf433ctl / OneWire / examples / sample / sample.pde
1 #include <OneWire.h>
2
3 /* DS18S20 Temperature chip i/o */
4
5 OneWire  ds(8);  // on pin 10
6
7 void setup(void) {
8   Serial.begin(9600);
9 }
10
11 void loop(void) {
12   byte i;
13   byte present = 0;
14   byte data[12];
15   byte addr[8];
16   
17   if ( !ds.search(addr)) {
18     Serial.print("No more addresses.\n");
19     ds.reset_search();
20     delay(250);
21     return;
22   }
23   
24   Serial.print("R=");
25   for( i = 0; i < 8; i++) {
26     Serial.print(addr[i], HEX);
27     Serial.print(" ");
28   }
29
30   if ( OneWire::crc8( addr, 7) != addr[7]) {
31       Serial.print("CRC is not valid!\n");
32       return;
33   }
34   
35   if ( addr[0] != 0x10) {
36       Serial.print("Device is not a DS18S20 family device.\n");
37       return;
38   }
39
40   // The DallasTemperature library can do all this work for you!
41
42   ds.reset();
43   ds.select(addr);
44   ds.write(0x44,1);         // start conversion, with parasite power on at the end
45   
46   delay(1000);     // maybe 750ms is enough, maybe not
47   // we might do a ds.depower() here, but the reset will take care of it.
48   
49   present = ds.reset();
50   ds.select(addr);    
51   ds.write(0xBE);         // Read Scratchpad
52
53   Serial.print("P=");
54   Serial.print(present,HEX);
55   Serial.print(" ");
56   for ( i = 0; i < 9; i++) {           // we need 9 bytes
57     data[i] = ds.read();
58     Serial.print(data[i], HEX);
59     Serial.print(" ");
60   }
61   Serial.print(" CRC=");
62   Serial.print( OneWire::crc8( data, 8), HEX);
63   Serial.println();
64 }