From df3441786186cc2155ba76b0e1d2a2bb035cc93e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 18 Feb 2020 18:10:36 +0100 Subject: [PATCH] Fix DS18x20 driver --- tasmota/xsns_05_ds18x20.ino | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tasmota/xsns_05_ds18x20.ino b/tasmota/xsns_05_ds18x20.ino index ba87e3a5d..5b46d5519 100644 --- a/tasmota/xsns_05_ds18x20.ino +++ b/tasmota/xsns_05_ds18x20.ino @@ -126,6 +126,8 @@ void OneWireWriteBit(uint8_t v) delayMicroseconds(delay_high[v]); } +/* +// Fails for reasons unknown to me uint8_t OneWireReadBit(void) { if (!ds18x20_dual_mode) { @@ -143,6 +145,35 @@ uint8_t OneWireReadBit(void) delayMicroseconds(53); return r; } +*/ +// Works fine in contrast to above. Why? +void OneWireReadBit1(void) +{ + pinMode(ds18x20_pin, OUTPUT); + digitalWrite(ds18x20_pin, LOW); + delayMicroseconds(3); + pinMode(ds18x20_pin, Settings.flag3.ds18x20_internal_pullup ? INPUT_PULLUP : INPUT); // SetOption74 - Enable internal pullup for single DS18x20 sensor +} + +void OneWireReadBit2(void) +{ + digitalWrite(ds18x20_pin_out, LOW); + delayMicroseconds(3); + digitalWrite(ds18x20_pin_out, HIGH); +} + +uint8_t OneWireReadBit(void) +{ + if (!ds18x20_dual_mode) { + OneWireReadBit1(); + } else { + OneWireReadBit2(); + } + delayMicroseconds(10); + uint8_t r = digitalRead(ds18x20_pin); + delayMicroseconds(53); + return r; +} /*------------------------------------------------------------------------------------------*/ @@ -284,6 +315,7 @@ void Ds18x20Init(void) uint64_t ids[DS18X20_MAX_SENSORS]; ds18x20_pin = pin[GPIO_DSB]; + if (pin[GPIO_DSB_OUT] < 99) { ds18x20_pin_out = pin[GPIO_DSB_OUT]; ds18x20_dual_mode = true; // Dual pins mode as used by Shelly @@ -419,6 +451,8 @@ void Ds18x20Name(uint8_t sensor) void Ds18x20EverySecond(void) { + if (!ds18x20_sensors) { return; } + #ifdef W1_PARASITE_POWER // skip access if there is still an eeprom write ongoing unsigned long now = millis();