From 3280c7d177f07282ecd72b9aa881325136b55569 Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Mon, 1 Oct 2018 14:28:21 +0000 Subject: [PATCH] Add W1_PARASITE_POWER optimization for DS18X20 --- sonoff/user_config.h | 1 + sonoff/xsns_05_ds18x20.ino | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/sonoff/user_config.h b/sonoff/user_config.h index 72aa97d53..ee77854bc 100644 --- a/sonoff/user_config.h +++ b/sonoff/user_config.h @@ -271,6 +271,7 @@ // -- One wire sensors ---------------------------- // WARNING: Select none for default one DS18B20 sensor or enable one of the following two options for multiple sensors #define USE_DS18x20 // Optional for more than one DS18x20 sensors with id sort, single scan and read retry (+1k3 code) +//#define W1_PARASITE_POWER // If using USE_DS18x20 then optimize for parasite powered sensors //#define USE_DS18x20_LEGACY // Optional for more than one DS18x20 sensors with dynamic scan using library OneWire (+1k5 code) // -- I2C sensors --------------------------------- diff --git a/sonoff/xsns_05_ds18x20.ino b/sonoff/xsns_05_ds18x20.ino index 869deac8e..474f26e2e 100644 --- a/sonoff/xsns_05_ds18x20.ino +++ b/sonoff/xsns_05_ds18x20.ino @@ -49,6 +49,10 @@ struct DS18X20STRUCT { uint8_t ds18x20_sensors = 0; uint8_t ds18x20_pin = 0; char ds18x20_types[12]; +#ifdef W1_PARASITE_POWER +uint8_t ds18x20_sensor_curr = 0; +unsigned long w1_power_until = 0; +#endif /*********************************************************************************************\ * Embedded tuned OneWire library @@ -285,7 +289,14 @@ void Ds18x20Init() void Ds18x20Convert() { OneWireReset(); +#ifdef W1_PARASITE_POWER + // With parasite power address one sensor at a time + if (++ds18x20_sensor_curr >= ds18x20_sensors) + ds18x20_sensor_curr = 0; + OneWireSelect(ds18x20_sensor[ds18x20_sensor_curr].address); +#else OneWireWrite(W1_SKIP_ROM); // Address all Sensors on Bus +#endif OneWireWrite(W1_CONVERT_TEMP); // start conversion, no parasite power on at the end // delay(750); // 750ms should be enough for 12bit conv } @@ -334,6 +345,9 @@ bool Ds18x20Read(uint8_t sensor) OneWireWrite(data[4]); // Configuration Register OneWireSelect(ds18x20_sensor[index].address); OneWireWrite(W1_WRITE_EEPROM); // Save scratchpad to EEPROM +#ifdef W1_PARASITE_POWER + w1_power_until = millis() + 10; // 10ms specified duration for EEPROM write +#endif } temp12 = (data[1] << 8) + data[0]; if (temp12 > 2047) { @@ -374,7 +388,18 @@ void Ds18x20Name(uint8_t sensor) void Ds18x20EverySecond() { - if (uptime &1) { +#ifdef W1_PARASITE_POWER + // skip access if there is still an eeprom write ongoing + unsigned long now = millis(); + if (now < w1_power_until) + return; +#endif + if (uptime & 1 +#ifdef W1_PARASITE_POWER + // if more than 1 sensor and only parasite power: convert every cycle + || ds18x20_sensors >= 2 +#endif + ) { // 2mS Ds18x20Convert(); // Start conversion, takes up to one second } else {