1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033 |
-
- #include "DallasTemperature.h"
- #if ARDUINO >= 100
- #include "Arduino.h"
- #else
- extern "C" {
- #include "WConstants.h"
- }
- #endif
- #define STARTCONVO 0x44
- #define COPYSCRATCH 0x48
- #define READSCRATCH 0xBE
- #define WRITESCRATCH 0x4E
- #define RECALLSCRATCH 0xB8
- #define READPOWERSUPPLY 0xB4
- #define ALARMSEARCH 0xEC
- #define TEMP_LSB 0
- #define TEMP_MSB 1
- #define HIGH_ALARM_TEMP 2
- #define LOW_ALARM_TEMP 3
- #define CONFIGURATION 4
- #define INTERNAL_BYTE 5
- #define COUNT_REMAIN 6
- #define COUNT_PER_C 7
- #define SCRATCHPAD_CRC 8
- #define DSROM_FAMILY 0
- #define DSROM_CRC 7
- #define TEMP_9_BIT 0x1F
- #define TEMP_10_BIT 0x3F
- #define TEMP_11_BIT 0x5F
- #define TEMP_12_BIT 0x7F
- #define MAX_CONVERSION_TIMEOUT 750
- #define NO_ALARM_HANDLER ((AlarmHandler *)0)
- DallasTemperature::DallasTemperature() {
- #if REQUIRESALARMS
- setAlarmHandler(NO_ALARM_HANDLER);
- #endif
- useExternalPullup = false;
- }
- DallasTemperature::DallasTemperature(OneWire* _oneWire) : DallasTemperature() {
- setOneWire(_oneWire);
- }
- bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
- switch (deviceAddress[DSROM_FAMILY]) {
- case DS18S20MODEL:
- case DS18B20MODEL:
- case DS1822MODEL:
- case DS1825MODEL:
- case DS28EA00MODEL:
- return true;
- default:
- return false;
- }
- }
- DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
- setPullupPin(_pullupPin);
- }
- void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
- useExternalPullup = true;
- pullupPin = _pullupPin;
- pinMode(pullupPin, OUTPUT);
- deactivateExternalPullup();
- }
- void DallasTemperature::setOneWire(OneWire* _oneWire) {
- _wire = _oneWire;
- devices = 0;
- ds18Count = 0;
- parasite = false;
- bitResolution = 9;
- waitForConversion = true;
- checkForConversion = true;
- autoSaveScratchPad = true;
- }
- void DallasTemperature::begin(void) {
- DeviceAddress deviceAddress;
- _wire->reset_search();
- devices = 0;
- ds18Count = 0;
- while (_wire->search(deviceAddress)) {
- if (validAddress(deviceAddress)) {
- devices++;
- if (validFamily(deviceAddress)) {
- ds18Count++;
- if (!parasite && readPowerSupply(deviceAddress))
- parasite = true;
- uint8_t b = getResolution(deviceAddress);
- if (b > bitResolution) bitResolution = b;
- }
- }
- }
- }
- uint8_t DallasTemperature::getDeviceCount(void) {
- return devices;
- }
- uint8_t DallasTemperature::getDS18Count(void) {
- return ds18Count;
- }
- bool DallasTemperature::validAddress(const uint8_t* deviceAddress) {
- return (_wire->crc8(deviceAddress, 7) == deviceAddress[DSROM_CRC]);
- }
- bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) {
- uint8_t depth = 0;
- _wire->reset_search();
- while (depth <= index && _wire->search(deviceAddress)) {
- if (depth == index && validAddress(deviceAddress))
- return true;
- depth++;
- }
- return false;
- }
- bool DallasTemperature::isConnected(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- return isConnected(deviceAddress, scratchPad);
- }
- bool DallasTemperature::isConnected(const uint8_t* deviceAddress,
- uint8_t* scratchPad) {
- bool b = readScratchPad(deviceAddress, scratchPad);
- return b && !isAllZeros(scratchPad) && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
- }
- bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress,
- uint8_t* scratchPad) {
-
- int b = _wire->reset();
- if (b == 0)
- return false;
- _wire->select(deviceAddress);
- _wire->write(READSCRATCH);
-
-
-
-
-
-
-
-
-
-
-
-
-
- for (uint8_t i = 0; i < 9; i++) {
- scratchPad[i] = _wire->read();
- }
- b = _wire->reset();
- return (b == 1);
- }
- void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress,
- const uint8_t* scratchPad) {
- _wire->reset();
- _wire->select(deviceAddress);
- _wire->write(WRITESCRATCH);
- _wire->write(scratchPad[HIGH_ALARM_TEMP]);
- _wire->write(scratchPad[LOW_ALARM_TEMP]);
-
- if (deviceAddress[DSROM_FAMILY] != DS18S20MODEL)
- _wire->write(scratchPad[CONFIGURATION]);
- if (autoSaveScratchPad)
- saveScratchPad(deviceAddress);
- else
- _wire->reset();
- }
- bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress)
- {
- bool parasiteMode = false;
- _wire->reset();
- if (deviceAddress == nullptr)
- _wire->skip();
- else
- _wire->select(deviceAddress);
- _wire->write(READPOWERSUPPLY);
- if (_wire->read_bit() == 0)
- parasiteMode = true;
- _wire->reset();
- return parasiteMode;
- }
- void DallasTemperature::setResolution(uint8_t newResolution) {
- bitResolution = constrain(newResolution, 9, 12);
- DeviceAddress deviceAddress;
- for (uint8_t i = 0; i < devices; i++) {
- getAddress(deviceAddress, i);
- setResolution(deviceAddress, bitResolution, true);
- }
- }
- bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
- uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
- bool success = false;
-
- if (deviceAddress[DSROM_FAMILY] == DS18S20MODEL)
- {
- success = true;
- }
- else
- {
-
- newResolution = constrain(newResolution, 9, 12);
- uint8_t newValue = 0;
- ScratchPad scratchPad;
-
- if (isConnected(deviceAddress, scratchPad))
- {
- switch (newResolution) {
- case 12:
- newValue = TEMP_12_BIT;
- break;
- case 11:
- newValue = TEMP_11_BIT;
- break;
- case 10:
- newValue = TEMP_10_BIT;
- break;
- case 9:
- default:
- newValue = TEMP_9_BIT;
- break;
- }
-
- if (scratchPad[CONFIGURATION] != newValue)
- {
- scratchPad[CONFIGURATION] = newValue;
- writeScratchPad(deviceAddress, scratchPad);
- }
-
- success = true;
- }
- }
-
- if (skipGlobalBitResolutionCalculation == false)
- {
- bitResolution = newResolution;
- if (devices > 1)
- {
- for (uint8_t i = 0; i < devices; i++)
- {
- if (bitResolution == 12) break;
- DeviceAddress deviceAddr;
- getAddress(deviceAddr, i);
- uint8_t b = getResolution(deviceAddr);
- if (b > bitResolution) bitResolution = b;
- }
- }
- }
- return success;
- }
- uint8_t DallasTemperature::getResolution() {
- return bitResolution;
- }
- uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {
-
- if (deviceAddress[DSROM_FAMILY] == DS18S20MODEL)
- return 12;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- switch (scratchPad[CONFIGURATION]) {
- case TEMP_12_BIT:
- return 12;
- case TEMP_11_BIT:
- return 11;
- case TEMP_10_BIT:
- return 10;
- case TEMP_9_BIT:
- return 9;
- }
- }
- return 0;
- }
- void DallasTemperature::setWaitForConversion(bool flag) {
- waitForConversion = flag;
- }
- bool DallasTemperature::getWaitForConversion() {
- return waitForConversion;
- }
- void DallasTemperature::setCheckForConversion(bool flag) {
- checkForConversion = flag;
- }
- bool DallasTemperature::getCheckForConversion() {
- return checkForConversion;
- }
- bool DallasTemperature::isConversionComplete() {
- uint8_t b = _wire->read_bit();
- return (b == 1);
- }
- void DallasTemperature::requestTemperatures() {
- _wire->reset();
- _wire->skip();
- _wire->write(STARTCONVO, parasite);
-
- if (!waitForConversion)
- return;
- blockTillConversionComplete(bitResolution);
- }
- bool DallasTemperature::requestTemperaturesByAddress(
- const uint8_t* deviceAddress) {
- uint8_t bitResolution = getResolution(deviceAddress);
- if (bitResolution == 0) {
- return false;
- }
- _wire->reset();
- _wire->select(deviceAddress);
- _wire->write(STARTCONVO, parasite);
-
- if (!waitForConversion)
- return true;
- blockTillConversionComplete(bitResolution);
- return true;
- }
- void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {
- if (checkForConversion && !parasite) {
- unsigned long start = millis();
- while (!isConversionComplete() && (millis() - start < MAX_CONVERSION_TIMEOUT ))
- yield();
- } else {
- unsigned long delms = millisToWaitForConversion(bitResolution);
- activateExternalPullup();
- delay(delms);
- deactivateExternalPullup();
- }
- }
- int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
- switch (bitResolution) {
- case 9:
- return 94;
- case 10:
- return 188;
- case 11:
- return 375;
- default:
- return 750;
- }
- }
- bool DallasTemperature::saveScratchPadByIndex(uint8_t deviceIndex) {
-
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) return false;
-
- return saveScratchPad(deviceAddress);
-
- }
- bool DallasTemperature::saveScratchPad(const uint8_t* deviceAddress) {
-
- if (_wire->reset() == 0)
- return false;
-
- if (deviceAddress == nullptr)
- _wire->skip();
- else
- _wire->select(deviceAddress);
-
- _wire->write(COPYSCRATCH,parasite);
-
-
- if (!parasite) {
- delay(20);
- } else {
- activateExternalPullup();
- delay(20);
- deactivateExternalPullup();
- }
-
- return _wire->reset() == 1;
-
- }
- bool DallasTemperature::recallScratchPadByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) return false;
-
- return recallScratchPad(deviceAddress);
- }
- bool DallasTemperature::recallScratchPad(const uint8_t* deviceAddress) {
-
- if (_wire->reset() == 0)
- return false;
-
- if (deviceAddress == nullptr)
- _wire->skip();
- else
- _wire->select(deviceAddress);
-
- _wire->write(RECALLSCRATCH,parasite);
-
- unsigned long start = millis();
- while (_wire->read_bit() == 0) {
-
- if (millis() - start > 20) return false;
- yield();
- }
-
- return _wire->reset() == 1;
-
- }
- void DallasTemperature::setAutoSaveScratchPad(bool flag) {
- autoSaveScratchPad = flag;
- }
- bool DallasTemperature::getAutoSaveScratchPad() {
- return autoSaveScratchPad;
- }
- void DallasTemperature::activateExternalPullup() {
- if(useExternalPullup)
- digitalWrite(pullupPin, LOW);
- }
- void DallasTemperature::deactivateExternalPullup() {
- if(useExternalPullup)
- digitalWrite(pullupPin, HIGH);
- }
- bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- getAddress(deviceAddress, deviceIndex);
- return requestTemperaturesByAddress(deviceAddress);
- }
- float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) {
- return DEVICE_DISCONNECTED_C;
- }
- return getTempC((uint8_t*) deviceAddress);
- }
- float DallasTemperature::getTempFByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) {
- return DEVICE_DISCONNECTED_F;
- }
- return getTempF((uint8_t*) deviceAddress);
- }
- int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
- uint8_t* scratchPad) {
- int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
- | (((int16_t) scratchPad[TEMP_LSB]) << 3);
-
- if ((deviceAddress[DSROM_FAMILY] == DS18S20MODEL) && (scratchPad[COUNT_PER_C] != 0)) {
- fpTemperature = ((fpTemperature & 0xfff0) << 3) - 32
- + (((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7)
- / scratchPad[COUNT_PER_C]);
- }
- return fpTemperature;
- }
- int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad))
- return calculateTemperature(deviceAddress, scratchPad);
- return DEVICE_DISCONNECTED_RAW;
- }
- float DallasTemperature::getTempC(const uint8_t* deviceAddress) {
- return rawToCelsius(getTemp(deviceAddress));
- }
- float DallasTemperature::getTempF(const uint8_t* deviceAddress) {
- return rawToFahrenheit(getTemp(deviceAddress));
- }
- bool DallasTemperature::isParasitePowerMode(void) {
- return parasite;
- }
- void DallasTemperature::setUserData(const uint8_t* deviceAddress,
- int16_t data) {
-
- if (getUserData(deviceAddress) == data)
- return;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- scratchPad[HIGH_ALARM_TEMP] = data >> 8;
- scratchPad[LOW_ALARM_TEMP] = data & 255;
- writeScratchPad(deviceAddress, scratchPad);
- }
- }
- int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress) {
- int16_t data = 0;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- data = scratchPad[HIGH_ALARM_TEMP] << 8;
- data += scratchPad[LOW_ALARM_TEMP];
- }
- return data;
- }
- int16_t DallasTemperature::getUserDataByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- getAddress(deviceAddress, deviceIndex);
- return getUserData((uint8_t*) deviceAddress);
- }
- void DallasTemperature::setUserDataByIndex(uint8_t deviceIndex, int16_t data) {
- DeviceAddress deviceAddress;
- getAddress(deviceAddress, deviceIndex);
- setUserData((uint8_t*) deviceAddress, data);
- }
- float DallasTemperature::toFahrenheit(float celsius) {
- return (celsius * 1.8f) + 32.0f;
- }
- float DallasTemperature::toCelsius(float fahrenheit) {
- return (fahrenheit - 32.0f) * 0.555555556f;
- }
- float DallasTemperature::rawToCelsius(int16_t raw) {
- if (raw <= DEVICE_DISCONNECTED_RAW)
- return DEVICE_DISCONNECTED_C;
-
- return (float) raw * 0.0078125f;
- }
- float DallasTemperature::rawToFahrenheit(int16_t raw) {
- if (raw <= DEVICE_DISCONNECTED_RAW)
- return DEVICE_DISCONNECTED_F;
-
-
- return ((float) raw * 0.0140625f) + 32.0f;
- }
- bool DallasTemperature::isAllZeros(const uint8_t * const scratchPad, const size_t length) {
- for (size_t i = 0; i < length; i++) {
- if (scratchPad[i] != 0) {
- return false;
- }
- }
- return true;
- }
- #if REQUIRESALARMS
- void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress,
- int8_t celsius) {
-
- if (getHighAlarmTemp(deviceAddress) == celsius)
- return;
-
- if (celsius > 125)
- celsius = 125;
- else if (celsius < -55)
- celsius = -55;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- scratchPad[HIGH_ALARM_TEMP] = (uint8_t) celsius;
- writeScratchPad(deviceAddress, scratchPad);
- }
- }
- void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress,
- int8_t celsius) {
-
- if (getLowAlarmTemp(deviceAddress) == celsius)
- return;
-
- if (celsius > 125)
- celsius = 125;
- else if (celsius < -55)
- celsius = -55;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- scratchPad[LOW_ALARM_TEMP] = (uint8_t) celsius;
- writeScratchPad(deviceAddress, scratchPad);
- }
- }
- int8_t DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad))
- return (int8_t) scratchPad[HIGH_ALARM_TEMP];
- return DEVICE_DISCONNECTED_C;
- }
- int8_t DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad))
- return (int8_t) scratchPad[LOW_ALARM_TEMP];
- return DEVICE_DISCONNECTED_C;
- }
- void DallasTemperature::resetAlarmSearch() {
- alarmSearchJunction = -1;
- alarmSearchExhausted = 0;
- for (uint8_t i = 0; i < 7; i++) {
- alarmSearchAddress[i] = 0;
- }
- }
- bool DallasTemperature::alarmSearch(uint8_t* newAddr) {
- uint8_t i;
- int8_t lastJunction = -1;
- uint8_t done = 1;
- if (alarmSearchExhausted)
- return false;
- if (!_wire->reset())
- return false;
-
- _wire->write(0xEC, 0);
- for (i = 0; i < 64; i++) {
- uint8_t a = _wire->read_bit();
- uint8_t nota = _wire->read_bit();
- uint8_t ibyte = i / 8;
- uint8_t ibit = 1 << (i & 7);
-
-
- if (a && nota)
- return false;
- if (!a && !nota) {
- if (i == alarmSearchJunction) {
-
- a = 1;
- alarmSearchJunction = lastJunction;
- } else if (i < alarmSearchJunction) {
-
- if (alarmSearchAddress[ibyte] & ibit) {
- a = 1;
- } else {
-
- a = 0;
- done = 0;
- lastJunction = i;
- }
- } else {
-
- a = 0;
- alarmSearchJunction = i;
- done = 0;
- }
-
-
- }
- if (a)
- alarmSearchAddress[ibyte] |= ibit;
- else
- alarmSearchAddress[ibyte] &= ~ibit;
- _wire->write_bit(a);
- }
- if (done)
- alarmSearchExhausted = 1;
- for (i = 0; i < 8; i++)
- newAddr[i] = alarmSearchAddress[i];
- return true;
- }
- bool DallasTemperature::hasAlarm(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- int8_t temp = calculateTemperature(deviceAddress, scratchPad) >> 7;
-
- if (temp <= (int8_t) scratchPad[LOW_ALARM_TEMP])
- return true;
-
- if (temp >= (int8_t) scratchPad[HIGH_ALARM_TEMP])
- return true;
- }
-
- return false;
- }
- bool DallasTemperature::hasAlarm(void) {
- DeviceAddress deviceAddress;
- resetAlarmSearch();
- return alarmSearch(deviceAddress);
- }
- void DallasTemperature::processAlarms(void) {
- if (!hasAlarmHandler())
- {
- return;
- }
- resetAlarmSearch();
- DeviceAddress alarmAddr;
- while (alarmSearch(alarmAddr)) {
- if (validAddress(alarmAddr)) {
- _AlarmHandler(alarmAddr);
- }
- }
- }
- void DallasTemperature::setAlarmHandler(const AlarmHandler *handler) {
- _AlarmHandler = handler;
- }
- bool DallasTemperature::hasAlarmHandler()
- {
- return _AlarmHandler != NO_ALARM_HANDLER;
- }
- #endif
- #if REQUIRESNEW
- void* DallasTemperature::operator new(unsigned int size) {
- void * p;
- p = malloc(size);
- memset((DallasTemperature*)p,0,size);
-
- return (DallasTemperature*) p;
- }
- void DallasTemperature::operator delete(void* p) {
- DallasTemperature* pNss = (DallasTemperature*) p;
- pNss->~DallasTemperature();
- free(p);
- }
- #endif
|