极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11143|回复: 1

丧心病狂省时间,DS18B20 时间转换从750ms到96ms

[复制链接]
发表于 2014-8-22 11:58:42 | 显示全部楼层 |阅读模式
本帖最后由 hi55234 于 2014-8-31 21:31 编辑

本来嘛,DS18B20是可以通过寄存器R0、R1调节精度的~~~


可哥们那c语言简直是0基础,一看到MOV啥的,那就1一个头2个大~~~

于是乎直接放弃,可750ms的时间真心伤不起啊~~~肿么办?

丧心病狂修改DallasTemperature.cpp

但凡涉及bit数字,的一律改为9,额差不多就这个样子:

  1. // This library is free software; you can redistribute it and/or
  2. // modify it under the terms of the GNU Lesser General Public
  3. // License as published by the Free Software Foundation; either
  4. // version 2.1 of the License, or (at your option) any later version.

  5. // Version 3.7.2 modified on Dec 6, 2011 to support Arduino 1.0
  6. // See Includes...
  7. // Modified by Jordan Hochenbaum

  8. #include "DallasTemperature.h"

  9. #if ARDUINO >= 100
  10. #include "Arduino.h"
  11. #else
  12. extern "C" {
  13. #include "WConstants.h"
  14. }
  15. #endif

  16. DallasTemperature::DallasTemperature(OneWire* _oneWire)
  17. #if REQUIRESALARMS
  18.     : _AlarmHandler(&defaultAlarmHandler)
  19. #endif
  20. {
  21.     _wire = _oneWire;
  22.     devices = 0;
  23.     parasite = false;
  24.     bitResolution = 9;
  25.     waitForConversion = true;
  26.     checkForConversion = true;
  27. }

  28. // initialise the bus
  29. void DallasTemperature::begin(void)
  30. {
  31.     DeviceAddress deviceAddress;

  32.     _wire->reset_search();
  33.     devices = 0; // Reset the number of devices when we enumerate wire devices

  34.     while (_wire->search(deviceAddress))
  35.     {
  36.         if (validAddress(deviceAddress))
  37.         {
  38.             if (!parasite && readPowerSupply(deviceAddress)) parasite = true;

  39.             ScratchPad scratchPad;

  40.             readScratchPad(deviceAddress, scratchPad);

  41.             bitResolution = max(bitResolution, getResolution(deviceAddress));

  42.             devices++;
  43.         }
  44.     }
  45. }

  46. // returns the number of devices found on the bus
  47. uint8_t DallasTemperature::getDeviceCount(void)
  48. {
  49.     return devices;
  50. }

  51. // returns true if address is valid
  52. bool DallasTemperature::validAddress(const uint8_t* deviceAddress)
  53. {
  54.     return (_wire->crc8(deviceAddress, 7) == deviceAddress[7]);
  55. }

  56. // finds an address at a given index on the bus
  57. // returns true if the device was found
  58. bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index)
  59. {
  60.     uint8_t depth = 0;

  61.     _wire->reset_search();

  62.     while (depth <= index && _wire->search(deviceAddress))
  63.     {
  64.         if (depth == index && validAddress(deviceAddress)) return true;
  65.         depth++;
  66.     }

  67.     return false;
  68. }

  69. // attempt to determine if the device at the given address is connected to the bus
  70. bool DallasTemperature::isConnected(const uint8_t* deviceAddress)
  71. {
  72.     ScratchPad scratchPad;
  73.     return isConnected(deviceAddress, scratchPad);
  74. }

  75. // attempt to determine if the device at the given address is connected to the bus
  76. // also allows for updating the read scratchpad
  77. bool DallasTemperature::isConnected(const uint8_t* deviceAddress, uint8_t* scratchPad)
  78. {
  79.     readScratchPad(deviceAddress, scratchPad);
  80.     return (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
  81. }

  82. // read device's scratch pad
  83. void DallasTemperature::readScratchPad(const uint8_t* deviceAddress, uint8_t* scratchPad)
  84. {
  85.     // send the command
  86.     _wire->reset();
  87.     _wire->select(deviceAddress);
  88.     _wire->write(READSCRATCH);

  89.     // TODO => collect all comments &  use simple loop
  90.     // byte 0: temperature LSB
  91.     // byte 1: temperature MSB
  92.     // byte 2: high alarm temp
  93.     // byte 3: low alarm temp
  94.     // byte 4: DS18S20: store for crc
  95.     //         DS18B20 & DS1822: configuration register
  96.     // byte 5: internal use & crc
  97.     // byte 6: DS18S20: COUNT_REMAIN
  98.     //         DS18B20 & DS1822: store for crc
  99.     // byte 7: DS18S20: COUNT_PER_C
  100.     //         DS18B20 & DS1822: store for crc
  101.     // byte 8: SCRATCHPAD_CRC
  102.     //
  103.     // for(int i=0; i<9; i++)
  104.     // {
  105.     //   scratchPad[i] = _wire->read();
  106.     // }


  107.     // read the response

  108.     // byte 0: temperature LSB
  109.     scratchPad[TEMP_LSB] = _wire->read();

  110.     // byte 1: temperature MSB
  111.     scratchPad[TEMP_MSB] = _wire->read();

  112.     // byte 2: high alarm temp
  113.     scratchPad[HIGH_ALARM_TEMP] = _wire->read();

  114.     // byte 3: low alarm temp
  115.     scratchPad[LOW_ALARM_TEMP] = _wire->read();

  116.     // byte 4:
  117.     // DS18S20: store for crc
  118.     // DS18B20 & DS1822: configuration register
  119.     scratchPad[CONFIGURATION] = _wire->read();

  120.     // byte 5:
  121.     // internal use & crc
  122.     scratchPad[INTERNAL_BYTE] = _wire->read();

  123.     // byte 6:
  124.     // DS18S20: COUNT_REMAIN
  125.     // DS18B20 & DS1822: store for crc
  126.     scratchPad[COUNT_REMAIN] = _wire->read();

  127.     // byte 7:
  128.     // DS18S20: COUNT_PER_C
  129.     // DS18B20 & DS1822: store for crc
  130.     scratchPad[COUNT_PER_C] = _wire->read();

  131.     // byte 8:
  132.     // SCTRACHPAD_CRC
  133.     scratchPad[SCRATCHPAD_CRC] = _wire->read();

  134.     _wire->reset();
  135. }

  136. // writes device's scratch pad
  137. void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress, const uint8_t* scratchPad)
  138. {
  139.     _wire->reset();
  140.     _wire->select(deviceAddress);
  141.     _wire->write(WRITESCRATCH);
  142.     _wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
  143.     _wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp
  144.     // DS1820 and DS18S20 have no configuration register
  145.     if (deviceAddress[0] != DS18S20MODEL) _wire->write(scratchPad[CONFIGURATION]); // configuration
  146.     _wire->reset();
  147.     _wire->select(deviceAddress); //<--this line was missing
  148.     // save the newly written values to eeprom
  149.     _wire->write(COPYSCRATCH, parasite);
  150.     if (parasite) delay(10); // 10ms delay
  151.     _wire->reset();
  152. }

  153. // reads the device's power requirements
  154. bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress)
  155. {
  156.     bool ret = false;
  157.     _wire->reset();
  158.     _wire->select(deviceAddress);
  159.     _wire->write(READPOWERSUPPLY);
  160.     if (_wire->read_bit() == 0) ret = true;
  161.     _wire->reset();
  162.     return ret;
  163. }


  164. // set resolution of all devices to 9, 10, 11, or 12 bits
  165. // if new resolution is out of range, it is constrained.
  166. void DallasTemperature::setResolution(uint8_t newResolution)
  167. {
  168.     bitResolution = constrain(newResolution, 9, 12);
  169.     DeviceAddress deviceAddress;
  170.     for (int i=0; i<devices; i++)
  171.     {
  172.         getAddress(deviceAddress, i);
  173.         setResolution(deviceAddress, bitResolution);
  174.     }
  175. }

  176. // set resolution of a device to 9, 10, 11, or 12 bits
  177. // if new resolution is out of range, 9 bits is used.
  178. bool DallasTemperature::setResolution(const uint8_t* deviceAddress, uint8_t newResolution)
  179. {
  180.     ScratchPad scratchPad;
  181.     if (isConnected(deviceAddress, scratchPad))
  182.     {
  183.         // DS1820 and DS18S20 have no resolution configuration register
  184.         if (deviceAddress[0] != DS18S20MODEL)
  185.         {
  186.             switch (newResolution)
  187.             {
  188.             case 12:
  189.                 scratchPad[CONFIGURATION] = TEMP_9_BIT;
  190.                 break;
  191.             case 11:
  192.                 scratchPad[CONFIGURATION] = TEMP_9_BIT;
  193.                 break;
  194.             case 10:
  195.                 scratchPad[CONFIGURATION] = TEMP_9_BIT;
  196.                 break;
  197.             case 9:
  198.             default:
  199.                 scratchPad[CONFIGURATION] = TEMP_9_BIT;
  200.                 break;
  201.             }
  202.             writeScratchPad(deviceAddress, scratchPad);
  203.         }
  204.         return true;  // new value set
  205.     }
  206.     return false;
  207. }

  208. // returns the global resolution
  209. uint8_t DallasTemperature::getResolution()
  210. {
  211.     return bitResolution;
  212. }

  213. // returns the current resolution of the device, 9-12
  214. // returns 0 if device not found
  215. uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress)
  216. {
  217.     // DS1820 and DS18S20 have no resolution configuration register
  218.     if (deviceAddress[0] == DS18S20MODEL) return 9;

  219.     ScratchPad scratchPad;
  220.     if (isConnected(deviceAddress, scratchPad))
  221.     {
  222.         switch (scratchPad[CONFIGURATION])
  223.         {
  224.         case TEMP_12_BIT:
  225.             return 9;

  226.         case TEMP_11_BIT:
  227.             return 9;

  228.         case TEMP_10_BIT:
  229.             return 9;

  230.         case TEMP_9_BIT:
  231.             return 9;
  232.         }
  233.     }
  234.     return 0;
  235. }


  236. // sets the value of the waitForConversion flag
  237. // TRUE : function requestTemperature() etc returns when conversion is ready
  238. // FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
  239. //        (1) programmer has to check if the needed delay has passed
  240. //        (2) but the application can do meaningful things in that time
  241. void DallasTemperature::setWaitForConversion(bool flag)
  242. {
  243.     waitForConversion = flag;
  244. }

  245. // gets the value of the waitForConversion flag
  246. bool DallasTemperature::getWaitForConversion()
  247. {
  248.     return waitForConversion;
  249. }


  250. // sets the value of the checkForConversion flag
  251. // TRUE : function requestTemperature() etc will 'listen' to an IC to determine whether a conversion is complete
  252. // FALSE: function requestTemperature() etc will wait a set time (worst case scenario) for a conversion to complete
  253. void DallasTemperature::setCheckForConversion(bool flag)
  254. {
  255.     checkForConversion = flag;
  256. }

  257. // gets the value of the waitForConversion flag
  258. bool DallasTemperature::getCheckForConversion()
  259. {
  260.     return checkForConversion;
  261. }

  262. bool DallasTemperature::isConversionAvailable(const uint8_t* deviceAddress)
  263. {
  264.     // Check if the clock has been raised indicating the conversion is complete
  265.     ScratchPad scratchPad;
  266.     readScratchPad(deviceAddress, scratchPad);
  267.     return scratchPad[0];
  268. }


  269. // sends command for all devices on the bus to perform a temperature conversion
  270. void DallasTemperature::requestTemperatures()
  271. {
  272.     _wire->reset();
  273.     _wire->skip();
  274.     _wire->write(STARTCONVO, parasite);

  275.     // ASYNC mode?
  276.     if (!waitForConversion) return;
  277.     blockTillConversionComplete(bitResolution, NULL);
  278. }

  279. // sends command for one device to perform a temperature by address
  280. // returns FALSE if device is disconnected
  281. // returns TRUE  otherwise
  282. bool DallasTemperature::requestTemperaturesByAddress(const uint8_t* deviceAddress)
  283. {
  284.     _wire->reset();
  285.     _wire->select(deviceAddress);
  286.     _wire->write(STARTCONVO, parasite);

  287.     // check device
  288.     ScratchPad scratchPad;
  289.     if (!isConnected(deviceAddress, scratchPad)) return false;

  290.     // ASYNC mode?
  291.     if (!waitForConversion) return true;
  292.     blockTillConversionComplete(getResolution(deviceAddress), deviceAddress);

  293.     return true;
  294. }

  295. // returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
  296. int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution)
  297. {
  298.     switch (bitResolution)
  299.     {
  300.     case 9:
  301.         return 94;
  302.     case 10:
  303.         return 188;
  304.     case 11:
  305.         return 375;
  306.     default:
  307.         return 750;
  308.     }
  309. }

  310. // Continue to check if the IC has responded with a temperature
  311. void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution, const uint8_t* deviceAddress)
  312. {
  313.     int delms = millisToWaitForConversion(bitResolution);
  314.     if (deviceAddress != NULL && checkForConversion && !parasite)
  315.     {
  316.         unsigned long timend = millis() + delms;
  317.         while(!isConversionAvailable(deviceAddress) && (millis() < timend));
  318.     }
  319.     else
  320.     {
  321.         delay(delms);
  322.     }
  323. }

  324. // sends command for one device to perform a temp conversion by index
  325. bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex)
  326. {
  327.     DeviceAddress deviceAddress;
  328.     getAddress(deviceAddress, deviceIndex);
  329.     return requestTemperaturesByAddress(deviceAddress);
  330. }

  331. // Fetch temperature for device index
  332. float DallasTemperature::getTempCByIndex(uint8_t deviceIndex)
  333. {
  334.     DeviceAddress deviceAddress;
  335.     if (!getAddress(deviceAddress, deviceIndex))
  336.         return DEVICE_DISCONNECTED_C;
  337.     return getTempC((uint8_t*)deviceAddress);
  338. }

  339. // Fetch temperature for device index
  340. float DallasTemperature::getTempFByIndex(uint8_t deviceIndex)
  341. {
  342.     DeviceAddress deviceAddress;
  343.     if (!getAddress(deviceAddress, deviceIndex))
  344.         return DEVICE_DISCONNECTED_F;
  345.     return getTempF((uint8_t*)deviceAddress);
  346. }

  347. // reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
  348. int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, uint8_t* scratchPad)
  349. {
  350.     int16_t fpTemperature =
  351.         (((int16_t) scratchPad[TEMP_MSB]) << 11) |
  352.         (((int16_t) scratchPad[TEMP_LSB]) << 3);

  353.     /*
  354.     DS1820 and DS18S20 have a 9-bit temperature register.

  355.     Resolutions greater than 9-bit can be calculated using the data from
  356.     the temperature, and COUNT REMAIN and COUNT PER °C registers in the
  357.     scratchpad.  The resolution of the calculation depends on the model.

  358.     While the COUNT PER °C register is hard-wired to 16 (10h) in a
  359.     DS18S20, it changes with temperature in DS1820.

  360.     After reading the scratchpad, the TEMP_READ value is obtained by
  361.     truncating the 0.5°C bit (bit 0) from the temperature data. The
  362.     extended resolution temperature can then be calculated using the
  363.     following equation:

  364.                                     COUNT_PER_C - COUNT_REMAIN
  365.     TEMPERATURE = TEMP_READ - 0.25 + --------------------------
  366.                                             COUNT_PER_C

  367.     Hagai Shatz simplified this to integer arithmetic for a 12 bits
  368.     value for a DS18S20, and James Cameron added legacy DS1820 support.

  369.     See - [url]http://myarduinotoy.blogspot.co.uk/2013/02/12bit-result-from-ds18s20.html[/url]
  370.     */

  371.     if (deviceAddress[0] == DS18S20MODEL)
  372.         fpTemperature = ((fpTemperature & 0xfff0) << 3) - 16 +
  373.             (
  374.                 ((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7) /
  375.                   scratchPad[COUNT_PER_C]
  376.             );

  377.     return fpTemperature;
  378. }


  379. // returns temperature in 1/128 degrees C or DEVICE_DISCONNECTED_RAW if the
  380. // device's scratch pad cannot be read successfully.
  381. // the numeric value of DEVICE_DISCONNECTED_RAW is defined in
  382. // DallasTemperature.h. It is a large negative number outside the
  383. // operating range of the device
  384. int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress)
  385. {
  386.     ScratchPad scratchPad;
  387.     if (isConnected(deviceAddress, scratchPad)) return calculateTemperature(deviceAddress, scratchPad);
  388.     return DEVICE_DISCONNECTED_RAW;
  389. }

  390. // returns temperature in degrees C or DEVICE_DISCONNECTED_C if the
  391. // device's scratch pad cannot be read successfully.
  392. // the numeric value of DEVICE_DISCONNECTED_C is defined in
  393. // DallasTemperature.h. It is a large negative number outside the
  394. // operating range of the device
  395. float DallasTemperature::getTempC(const uint8_t* deviceAddress)
  396. {
  397.     return rawToCelsius(getTemp(deviceAddress));
  398. }

  399. // returns temperature in degrees F or DEVICE_DISCONNECTED_F if the
  400. // device's scratch pad cannot be read successfully.
  401. // the numeric value of DEVICE_DISCONNECTED_F is defined in
  402. // DallasTemperature.h. It is a large negative number outside the
  403. // operating range of the device
  404. float DallasTemperature::getTempF(const uint8_t* deviceAddress)
  405. {
  406.     return rawToFahrenheit(getTemp(deviceAddress));
  407. }

  408. // returns true if the bus requires parasite power
  409. bool DallasTemperature::isParasitePowerMode(void)
  410. {
  411.     return parasite;
  412. }

  413. #if REQUIRESALARMS

  414. /*

  415. ALARMS:

  416. TH and TL Register Format

  417. BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
  418.   S    2^6   2^5   2^4   2^3   2^2   2^1   2^0

  419. Only bits 11 through 4 of the temperature register are used
  420. in the TH and TL comparison since TH and TL are 8-bit
  421. registers. If the measured temperature is lower than or equal
  422. to TL or higher than or equal to TH, an alarm condition exists
  423. and an alarm flag is set inside the DS18B20. This flag is
  424. updated after every temperature measurement; therefore, if the
  425. alarm condition goes away, the flag will be turned off after
  426. the next temperature conversion.

  427. */

  428. // sets the high alarm temperature for a device in degrees Celsius
  429. // accepts a float, but the alarm resolution will ignore anything
  430. // after a decimal point.  valid range is -55C - 125C
  431. void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress, char celsius)
  432. {
  433.     // make sure the alarm temperature is within the device's range
  434.     if (celsius > 125) celsius = 125;
  435.     else if (celsius < -55) celsius = -55;

  436.     ScratchPad scratchPad;
  437.     if (isConnected(deviceAddress, scratchPad))
  438.     {
  439.         scratchPad[HIGH_ALARM_TEMP] = (uint8_t)celsius;
  440.         writeScratchPad(deviceAddress, scratchPad);
  441.     }
  442. }

  443. // sets the low alarm temperature for a device in degrees Celsius
  444. // accepts a float, but the alarm resolution will ignore anything
  445. // after a decimal point.  valid range is -55C - 125C
  446. void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress, char celsius)
  447. {
  448.     // make sure the alarm temperature is within the device's range
  449.     if (celsius > 125) celsius = 125;
  450.     else if (celsius < -55) celsius = -55;

  451.     ScratchPad scratchPad;
  452.     if (isConnected(deviceAddress, scratchPad))
  453.     {
  454.         scratchPad[LOW_ALARM_TEMP] = (uint8_t)celsius;
  455.         writeScratchPad(deviceAddress, scratchPad);
  456.     }
  457. }

  458. // returns a char with the current high alarm temperature or
  459. // DEVICE_DISCONNECTED for an address
  460. char DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress)
  461. {
  462.     ScratchPad scratchPad;
  463.     if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[HIGH_ALARM_TEMP];
  464.     return DEVICE_DISCONNECTED_C;
  465. }

  466. // returns a char with the current low alarm temperature or
  467. // DEVICE_DISCONNECTED for an address
  468. char DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress)
  469. {
  470.     ScratchPad scratchPad;
  471.     if (isConnected(deviceAddress, scratchPad)) return (char)scratchPad[LOW_ALARM_TEMP];
  472.     return DEVICE_DISCONNECTED_C;
  473. }

  474. // resets internal variables used for the alarm search
  475. void DallasTemperature::resetAlarmSearch()
  476. {
  477.     alarmSearchJunction = -1;
  478.     alarmSearchExhausted = 0;
  479.     for(uint8_t i = 0; i < 7; i++)
  480.         alarmSearchAddress[i] = 0;
  481. }

  482. // This is a modified version of the OneWire::search method.
  483. //
  484. // Also added the OneWire search fix documented here:
  485. // [url]http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295[/url]
  486. //
  487. // Perform an alarm search. If this function returns a '1' then it has
  488. // enumerated the next device and you may retrieve the ROM from the
  489. // OneWire::address variable. If there are no devices, no further
  490. // devices, or something horrible happens in the middle of the
  491. // enumeration then a 0 is returned.  If a new device is found then
  492. // its address is copied to newAddr.  Use
  493. // DallasTemperature::resetAlarmSearch() to start over.
  494. bool DallasTemperature::alarmSearch(uint8_t* newAddr)
  495. {
  496.     uint8_t i;
  497.     char lastJunction = -1;
  498.     uint8_t done = 1;

  499.     if (alarmSearchExhausted) return false;
  500.     if (!_wire->reset()) return false;

  501.     // send the alarm search command
  502.     _wire->write(0xEC, 0);

  503.     for(i = 0; i < 64; i++)
  504.     {
  505.         uint8_t a = _wire->read_bit( );
  506.         uint8_t nota = _wire->read_bit( );
  507.         uint8_t ibyte = i / 8;
  508.         uint8_t ibit = 1 << (i & 7);

  509.         // I don't think this should happen, this means nothing responded, but maybe if
  510.         // something vanishes during the search it will come up.
  511.         if (a && nota) return false;

  512.         if (!a && !nota)
  513.         {
  514.             if (i == alarmSearchJunction)
  515.             {
  516.                 // this is our time to decide differently, we went zero last time, go one.
  517.                 a = 1;
  518.                 alarmSearchJunction = lastJunction;
  519.             }
  520.             else if (i < alarmSearchJunction)
  521.             {
  522.                 // take whatever we took last time, look in address
  523.                 if (alarmSearchAddress[ibyte] & ibit) a = 1;
  524.                 else
  525.                 {
  526.                     // Only 0s count as pending junctions, we've already exhausted the 0 side of 1s
  527.                     a = 0;
  528.                     done = 0;
  529.                     lastJunction = i;
  530.                 }
  531.             }
  532.             else
  533.             {
  534.                 // we are blazing new tree, take the 0
  535.                 a = 0;
  536.                 alarmSearchJunction = i;
  537.                 done = 0;
  538.             }
  539.             // OneWire search fix
  540.             // See: [url]http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295[/url]
  541.         }

  542.         if (a) alarmSearchAddress[ibyte] |= ibit;
  543.         else alarmSearchAddress[ibyte] &= ~ibit;

  544.         _wire->write_bit(a);
  545.     }

  546.     if (done) alarmSearchExhausted = 1;
  547.     for (i = 0; i < 8; i++) newAddr[i] = alarmSearchAddress[i];
  548.     return true;
  549. }

  550. // returns true if device address might have an alarm condition
  551. // (only an alarm search can verify this)
  552. bool DallasTemperature::hasAlarm(const uint8_t* deviceAddress)
  553. {
  554.     ScratchPad scratchPad;
  555.     if (isConnected(deviceAddress, scratchPad))
  556.     {
  557.         char temp = calculateTemperature(deviceAddress, scratchPad) >> 7;

  558.         // check low alarm
  559.         if (temp <= (char)scratchPad[LOW_ALARM_TEMP]) return true;

  560.         // check high alarm
  561.         if (temp >= (char)scratchPad[HIGH_ALARM_TEMP]) return true;
  562.     }

  563.     // no alarm
  564.     return false;
  565. }

  566. // returns true if any device is reporting an alarm condition on the bus
  567. bool DallasTemperature::hasAlarm(void)
  568. {
  569.     DeviceAddress deviceAddress;
  570.     resetAlarmSearch();
  571.     return alarmSearch(deviceAddress);
  572. }

  573. // runs the alarm handler for all devices returned by alarmSearch()
  574. void DallasTemperature::processAlarms(void)
  575. {
  576.     resetAlarmSearch();
  577.     DeviceAddress alarmAddr;

  578.     while (alarmSearch(alarmAddr))
  579.     {
  580.         if (validAddress(alarmAddr))
  581.             _AlarmHandler(alarmAddr);
  582.     }
  583. }

  584. // sets the alarm handler
  585. void DallasTemperature::setAlarmHandler(AlarmHandler *handler)
  586. {
  587.     _AlarmHandler = handler;
  588. }

  589. // The default alarm handler
  590. void DallasTemperature::defaultAlarmHandler(const uint8_t* deviceAddress)
  591. {
  592. }

  593. #endif

  594. // Convert float Celsius to Fahrenheit
  595. float DallasTemperature::toFahrenheit(float celsius)
  596. {
  597.     return (celsius * 1.8) + 32;
  598. }

  599. // Convert float Fahrenheit to Celsius
  600. float DallasTemperature::toCelsius(float fahrenheit)
  601. {
  602.     return (fahrenheit - 32) * 0.555555556;
  603. }

  604. // convert from raw to Celsius
  605. float DallasTemperature::rawToCelsius(int16_t raw)
  606. {
  607.     if (raw <= DEVICE_DISCONNECTED_RAW)
  608.         return DEVICE_DISCONNECTED_C;
  609.     // C = RAW/128
  610.     return (float)raw * 0.0078125;
  611. }

  612. // convert from raw to Fahrenheit
  613. float DallasTemperature::rawToFahrenheit(int16_t raw)
  614. {
  615.     if (raw <= DEVICE_DISCONNECTED_RAW)
  616.         return DEVICE_DISCONNECTED_F;
  617.     // C = RAW/128
  618.     // F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
  619.     return ((float)raw * 0.0140625) + 32;
  620. }

  621. #if REQUIRESNEW

  622. // MnetCS - Allocates memory for DallasTemperature. Allows us to instance a new object
  623. void* DallasTemperature::operator new(unsigned int size) // Implicit NSS obj size
  624. {
  625.     void * p; // void pointer
  626.     p = malloc(size); // Allocate memory
  627.     memset((DallasTemperature*)p,0,size); // Initialise memory

  628.     //!!! CANT EXPLICITLY CALL CONSTRUCTOR - workaround by using an init() methodR - workaround by using an init() method
  629.     return (DallasTemperature*) p; // Cast blank region to NSS pointer
  630. }

  631. // MnetCS 2009 -  Free the memory used by this instance
  632. void DallasTemperature::operator delete(void* p)
  633. {
  634.     DallasTemperature* pNss =  (DallasTemperature*) p; // Cast to NSS pointer
  635.     pNss->~DallasTemperature(); // Destruct the object

  636.     free(p); // Free the memory
  637. }

  638. #endif
复制代码



于是乎,实际效果用代码检测:

  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>

  3. // 数字接口10
  4. #define ONE_WIRE_BUS 10

  5. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  6. OneWire oneWire(ONE_WIRE_BUS);

  7. // Pass our oneWire reference to Dallas Temperature.
  8. DallasTemperature sensors(&oneWire);

  9. unsigned long time2;

  10. void setup(void)
  11. {
  12.   // start serial port
  13.   Serial.begin(9600);
  14.   Serial.println("Dallas Temperature IC Control Library Demo");

  15.   // Start up the library
  16.   sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
  17. }


  18. void loop(void)
  19. {

  20.   time2=millis();
  21.   sensors.requestTemperatures(); // Send the command to get temperatures

  22.   time2=millis()-time2;
  23.   Serial.print("Temperature for Device 1 is: ");
  24.   Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  25.   
  26.    Serial.print("time2 is: ");
  27.   Serial.println(time2); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  28.   
  29.   delay(1000);
  30.   
  31. }
复制代码


注意time2的效果.PNG





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-8-22 12:20:38 | 显示全部楼层
感觉上你操作寄存器后,
性能有所提升,
但是我没看出那里是修改的关键..................
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-26 05:11 , Processed in 0.043630 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表