Skip to content

Commit

Permalink
ability to use internal pullup resistor
Browse files Browse the repository at this point in the history
  • Loading branch information
maxicus committed Nov 5, 2015
1 parent d388e36 commit 59e1825
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion OneWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ uint8_t OneWire::reset(void)

noInterrupts();
DIRECT_MODE_INPUT(reg, mask);
#if ONEWIRE_INTERNAL_PULLUP
DIRECT_WRITE_HIGH(reg , mask); // enable pull-up resistor
#endif

interrupts();
// wait until the wire is high... just in case
do {
Expand All @@ -162,6 +166,10 @@ uint8_t OneWire::reset(void)
delayMicroseconds(480);
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); // allow it to float
#if ONEWIRE_INTERNAL_PULLUP
DIRECT_WRITE_HIGH(reg , mask); // enable pull-up resistor
#endif

delayMicroseconds(70);
r = !DIRECT_READ(reg, mask);
interrupts();
Expand Down Expand Up @@ -212,6 +220,9 @@ uint8_t OneWire::read_bit(void)
DIRECT_WRITE_LOW(reg, mask);
delayMicroseconds(3);
DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
#if ONEWIRE_INTERNAL_PULLUP
DIRECT_WRITE_HIGH(reg , mask); // enable pull-up resistor
#endif
delayMicroseconds(10);
r = DIRECT_READ(reg, mask);
interrupts();
Expand All @@ -235,7 +246,9 @@ void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {
if ( !power) {
noInterrupts();
DIRECT_MODE_INPUT(baseReg, bitmask);
DIRECT_WRITE_LOW(baseReg, bitmask);
#if !(ONEWIRE_INTERNAL_PULLUP)
DIRECT_WRITE_LOW(baseReg, bitmask); // otherwise it's left high
#endif
interrupts();
}
}
Expand All @@ -246,7 +259,9 @@ void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0
if (!power) {
noInterrupts();
DIRECT_MODE_INPUT(baseReg, bitmask);
#if !ONEWIRE_INTERNAL_PULLUP
DIRECT_WRITE_LOW(baseReg, bitmask);
#endif
interrupts();
}
}
Expand Down Expand Up @@ -293,6 +308,7 @@ void OneWire::depower()
{
noInterrupts();
DIRECT_MODE_INPUT(baseReg, bitmask);
DIRECT_WRITE_LOW(baseReg, bitmask); // disable pull-up too
interrupts();
}

Expand Down
8 changes: 8 additions & 0 deletions OneWire.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
#define ONEWIRE_CRC16 1
#endif

// Use internal microcontroller pull-up resistor instead of
// regular 4.7KOhms one connected externally
// http://wp.josh.com/2014/06/23/no-external-pull-up-needed-for-ds18b20-temp-sensor/
// for details
#ifndef ONEWIRE_INTERNAL_PULLUP
#define ONEWIRE_INTERNAL_PULLUP 1
#endif

#define FALSE 0
#define TRUE 1

Expand Down

0 comments on commit 59e1825

Please sign in to comment.