Fix DS18x20 driver

This commit is contained in:
Theo Arends 2020-02-18 18:10:36 +01:00
parent 643d3d0b69
commit df34417861
1 changed files with 34 additions and 0 deletions

View File

@ -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();