Azuria

Arduino - OneWire kode

May 2nd, 2011
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <OneWire.h>
  2.  
  3. OneWire ds(8); // DS18x20 Temperature chip i/o One-wire
  4. OneWire ds1(12);
  5.  
  6. //Tempsensor variables
  7. byte address0[8] = {0x10, 0xB6, 0xBE, 0x30, 0x2, 0x8, 0x0, 0xD2}; // External DS18B20 GPS Sensor 28 73 CD F5 2 0 0 AE
  8. byte address1[12] = {0x10, 0x6E, 0xBD, 0x30, 0x2, 0x8, 0x0, 0xF9};
  9. int temp0 = 0, temp1 =0;
  10.  
  11. // gets temperature data from onewire sensor network, need to supply byte address, it'll check to see what type of sensor and convert appropriately
  12.   int getTempdata(byte sensorAddress[8]) {
  13.   int HighByte, LowByte, TReading, SignBit, Tc_100, Whole;
  14.   byte data[12], i, present = 0;
  15.  
  16.     ds.reset();
  17.     ds.select(sensorAddress);
  18.     ds.write(0x44,1);         // start conversion, with parasite power on at the end
  19.    
  20.     delay(3000);     // maybe 750ms is enough, maybe not
  21.     // we might do a ds.depower() here, but the reset will take care of it.
  22.  
  23.   present = ds.reset();
  24.   ds.select(sensorAddress);    
  25.   ds.write(0xBE);         // Read Scratchpad
  26.  
  27.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  28.     data[i] = ds.read();
  29.   }
  30.   LowByte = data[0];
  31.   HighByte = data[1];
  32.   TReading = (HighByte << 8) + LowByte;
  33.   SignBit = TReading & 0x8000;  // test most sig bit
  34.   if (SignBit) // negative
  35.   {
  36.     TReading = (TReading ^ 0xffff) + 1; // 2's comp
  37.   }
  38.  
  39.   if (sensorAddress[0] == 0x10) {
  40.     Tc_100 = TReading * 50;    // multiply by (100 * 0.0625) or 6.25
  41.   }
  42.   else {
  43.     Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  44.   }
  45.  
  46.  
  47.   Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  48.  
  49.     if (SignBit) // If its negative
  50.   {
  51.      Whole = Whole * -1;
  52.   }
  53.   return Whole;
  54. }
  55.  
  56. void setup(void) {
  57.   // initialize inputs/outputs
  58.   // start serial port
  59.   Serial.begin(9600);
  60. }
  61. void loop(void) {
  62.  
  63.   temp0 = getTempdata(address0);
  64.   temp1 = getTempdata(address1);
  65.  
  66. char msg[100];
  67.  
  68. snprintf(msg, 100, "Internal Temperature: %i\n",  "External Temperature: %i\n", temp0, temp1);
  69.  
  70. Serial.print(msg);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment