SHARE
TWEET

temp

inventrkits Jan 22nd, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <OneWire.h>
  2.  
  3. // DS18S20 Temperature chip i/o
  4. OneWire ds(10);  // on pin 10
  5.  
  6. void setup(void) {
  7.   // initialize inputs/outputs
  8.   // start serial port
  9.   Serial.begin(9600);
  10. }
  11.  
  12. void loop(void) {
  13.  
  14.   //For conversion of raw data to C
  15.   int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  16.  
  17.   byte i;
  18.   byte present = 0;
  19.   byte data[12];
  20.   byte addr[8];
  21.  
  22.   if ( !ds.search(addr)) {
  23.       Serial.print("No more addresses.\n");
  24.       ds.reset_search();
  25.       return;
  26.   }
  27.  
  28.   Serial.print("R=");
  29.   for( i = 0; i < 8; i++) {
  30.     Serial.print(addr[i], HEX);
  31.     Serial.print(" ");
  32.   }
  33.  
  34.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  35.       Serial.print("CRC is not valid!\n");
  36.       return;
  37.   }
  38.  
  39.   if ( addr[0] == 0x10) {
  40.       Serial.print("Device is a DS18S20 family device.\n");
  41.   }
  42.   else if ( addr[0] == 0x28) {
  43.       Serial.print("Device is a DS18B20 family device.\n");
  44.   }
  45.   else {
  46.       Serial.print("Device family is not recognized: 0x");
  47.       Serial.println(addr[0],HEX);
  48.       return;
  49.   }
  50.  
  51.   ds.reset();
  52.   ds.select(addr);
  53.   ds.write(0x44,1);         // start conversion, with parasite power on at the end
  54.  
  55.   delay(1000);     // maybe 750ms is enough, maybe not
  56.   // we might do a ds.depower() here, but the reset will take care of it.
  57.  
  58.   present = ds.reset();
  59.   ds.select(addr);    
  60.   ds.write(0xBE);         // Read Scratchpad
  61.  
  62.   Serial.print("P=");
  63.   Serial.print(present,HEX);
  64.   Serial.print(" ");
  65.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  66.     data[i] = ds.read();
  67.     Serial.print(data[i], HEX);
  68.     Serial.print(" ");
  69.   }
  70.   Serial.print(" CRC=");
  71.   Serial.print( OneWire::crc8( data, 8), HEX);
  72.   Serial.println();
  73.  
  74.   //Conversion of raw data to C
  75.   LowByte = data[0];
  76.   HighByte = data[1];
  77.   TReading = (HighByte << 8) + LowByte;
  78.   SignBit = TReading & 0x8000;  // test most sig bit
  79.   if (SignBit) // negative
  80.   {
  81.     TReading = (TReading ^ 0xffff) + 1; // 2's comp
  82.   }
  83.   Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  84.  
  85.   Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  86.   Fract = Tc_100 % 100;
  87.  
  88.  
  89.   if (SignBit) // If its negative
  90.   {
  91.      Serial.print("-");
  92.   }
  93.   Serial.print(Whole);
  94.   Serial.print(".");
  95.   if (Fract < 10)
  96.   {
  97.      Serial.print("0");
  98.   }
  99.   Serial.print(Fract);
  100.  
  101.   Serial.print("\n");
  102.   //End conversion to C
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top