Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let pins float when writing (cf data sheet section 'write time slots') #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions OneWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ uint8_t OneWire::reset(void)
delayMicroseconds(480);
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); // allow it to float
delayMicroseconds(70);
delayMicroseconds(70); // must be more than 60us, and less than (15+60)us
r = !DIRECT_READ(reg, mask);
interrupts();
delayMicroseconds(410);
Expand All @@ -201,19 +201,28 @@ void OneWire::write_bit(uint8_t v)
volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg;

if (v & 1) {
// Data sheet: To generate a Write 1 time slot, after
// pulling the 1-Wire bus low, the bus master must
// release the 1-Wire bus within 15us. When the bus is
// released, the 5kOhm pullup resistor will pull the
// bus high.
noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
delayMicroseconds(10);
DIRECT_WRITE_HIGH(reg, mask); // drive output high
DIRECT_MODE_INPUT(reg, mask); // release
interrupts();
delayMicroseconds(55);
} else {
// To generate a Write 0 time slot, after pulling the
// 1-Wire bus low, the bus master must continue to
// hold the bus low for the duration of the time slot
// (at least 60us).
noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
delayMicroseconds(65);
DIRECT_WRITE_HIGH(reg, mask); // drive output high
DIRECT_MODE_INPUT(reg, mask); // release
interrupts();
delayMicroseconds(5);
}
Expand All @@ -232,8 +241,10 @@ uint8_t OneWire::read_bit(void)
noInterrupts();
DIRECT_MODE_OUTPUT(reg, mask);
DIRECT_WRITE_LOW(reg, mask);
// must be low for minimum of 1us
delayMicroseconds(3);
DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
// DS18B20 data is valid for 15us after falling edge
delayMicroseconds(10);
r = DIRECT_READ(reg, mask);
interrupts();
Expand Down