Skip to content

Commit

Permalink
Adding support for hardware (DS2482) based OWI bus manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelpatel committed Oct 8, 2017
1 parent a732841 commit 1b4e7c1
Show file tree
Hide file tree
Showing 12 changed files with 741 additions and 90 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
# Arduino-OWI
The OWI library has been developed to support the implementation of
1-wire device drivers. The library includes an example device driver
and a bus scanner.
1-wire bus managers and device drivers. The library includes a GPIO
based software and DS2482 based hardware bus manager, and device
driver for DS18B20.

Version: 1.5
The examples directory contains device search, bus scanner,
thermometer alarm search, and example sketches for DS18B20, DS1990A
and DS2482.

Version: 1.6

## Classes

* [Abstract One-Wire Bus Manager and Device Interface, OWI](./src/OWI.h)
* [Software One-Wire Interface, Software::OWI](./src/Software/OWI.h)
* [Software One-Wire Bus Manager, GPIO, Software::OWI](./src/Software/OWI.h)
* [Hardware One-Wire Bus Manager, DS2482, Hardware::OWI](./src/Hardware/OWI.h)
* [Programmable Resolution 1-Wire Digital Thermometer, DS18B20](./src/Driver/DS18B20.h)

## Example Sketches

* [DS18B20](./examples/DS18B20)
* [DS1990A](./examples/DS1990A)
* [DS2482](./examples/DS2482)
* [Alarm](./examples/Alarm)
* [Scanner](./examples/Scanner)
* [Search](./examples/Search)
* [Alarm](./examples/Alarm)

## Dependencies

* [Arduino-GPIO](https://github.com/mikaelpatel/Arduino-GPIO)
* [Arduino-TWI](https://github.com/mikaelpatel/Arduino-TWI)
25 changes: 18 additions & 7 deletions examples/Alarm/Alarm.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
#include "GPIO.h"
#include "OWI.h"
#include "Software/OWI.h"
#include "Driver/DS18B20.h"

#if defined(ARDUINO_attiny)
#include "Software/Serial.h"
Software::Serial<BOARD::D0> Serial;
Software::OWI<BOARD::D1> owi;
#else
// Configure: Software/Hardware OWI Bus Manager
#define USE_SOFTWARE_OWI
#if defined(USE_SOFTWARE_OWI)
#include "Software/OWI.h"
Software::OWI<BOARD::D7> owi;

#else
#include "Hardware/OWI.h"
// Configure: Software/Hardware TWI Bus Manager
// #define USE_SOFTWARE_TWI
#if defined(USE_SOFTWARE_TWI)
#include "Software/TWI.h"
Software::TWI<BOARD::D18,BOARD::D19> twi;
#else
#include "Hardware/TWI.h"
Hardware::TWI twi;
#endif
Hardware::OWI owi(twi);
#endif

DS18B20 sensor(owi);
Expand Down Expand Up @@ -50,7 +61,7 @@ void loop()
bool triggered = false;
static uint32_t timestamp = 0;
if (!sensor.convert_request(true)) return;
delay(1000);
delay(sensor.conversion_time());
do {
last = owi.alarm_search(rom, last);
if (last == owi.ERROR) break;
Expand Down
26 changes: 19 additions & 7 deletions examples/DS18B20/DS18B20.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
#include "GPIO.h"
#include "OWI.h"
#include "Software/OWI.h"
#include "Driver/DS18B20.h"

#if defined(ARDUINO_attiny)
#include "Software/Serial.h"
Software::Serial<BOARD::D0> Serial;
Software::OWI<BOARD::D1> owi;
#else
// Configure: Software/Hardware OWI Bus Manager
#define USE_SOFTWARE_OWI
#if defined(USE_SOFTWARE_OWI)
#include "Software/OWI.h"
Software::OWI<BOARD::D7> owi;

#else
#include "Hardware/OWI.h"
// Configure: Software/Hardware TWI Bus Manager
// #define USE_SOFTWARE_TWI
#if defined(USE_SOFTWARE_TWI)
#include "Software/TWI.h"
Software::TWI<BOARD::D18,BOARD::D19> twi;
#else
#include "Hardware/TWI.h"
Hardware::TWI twi;
#endif
Hardware::OWI owi(twi);
#endif

DS18B20 sensor(owi);
Expand All @@ -25,6 +36,7 @@ void loop()
// Print list of sensors, rom code, and temperature

if (!sensor.convert_request(true)) return;
delay(sensor.conversion_time());

int8_t last = owi.FIRST;
uint8_t* rom = sensor.rom();
Expand Down Expand Up @@ -79,5 +91,5 @@ void loop()
} while (last != owi.LAST);

Serial.println();
delay(5000);
delay(4000);
}
122 changes: 122 additions & 0 deletions examples/DS2482/DS2482.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "GPIO.h"
#include "OWI.h"
#include "TWI.h"
#include "Hardware/OWI.h"
#include <util/crc16.h>

#define USE_SOFTWARE_TWI
#if defined(USE_SOFTWARE_TWI)
#include "Software/TWI.h"
Software::TWI<BOARD::D18,BOARD::D19> twi;
#else
#include "Hardware/TWI.h"
Hardware::TWI twi;
#endif

Hardware::OWI owi(twi);

// DS18B20 Commands
enum {
CONVERT_T = 0x44,
READ_SCRATCHPAD = 0xbe
};

// DS18B20 Scratchpad Memory
struct scratchpad_t {
int16_t temperature;
int8_t high_trigger;
int8_t low_trigger;
uint8_t configuration;
uint8_t reserved[3];
uint8_t crc;
};

#define TRACE(expr) \
do { \
Serial.print(#expr "="); \
Serial.println(expr); \
} while (0)

void setup()
{
Serial.begin(57600);
while (!Serial);

TRACE(owi.device_reset());
TRACE(owi.device_config());
TRACE(owi.set_read_pointer(owi.READ_DATA_REGISTER));
TRACE(owi.set_read_pointer(owi.CONFIGURATION_REGISTER));
TRACE(owi.set_read_pointer(owi.CHANNEL_SELECTION_REGISTER));
TRACE(owi.set_read_pointer(owi.STATUS_REGISTER));

uint8_t rom[owi.ROM_MAX] = { 0 };
uint8_t crc = 0;
TRACE(owi.reset());
Serial.print(F("rom="));
owi.write(owi.READ_ROM);
for (size_t i = 0; i < sizeof(rom); i++) {
rom[i] = owi.read(8);
if (rom[i] < 0x10) Serial.print(0);
Serial.print(rom[i], HEX);
crc = _crc_ibutton_update(crc, rom[i]);
}
Serial.print(F(", crc="));
Serial.println(crc);

uint8_t value = 0;
uint8_t bits = 0;
uint8_t ix = 0;
uint8_t res = 0;
uint8_t dir = 0;
bool id;
bool nid;
crc = 0;
TRACE(owi.reset());
Serial.print(F("rom="));
owi.write(owi.SEARCH_ROM);
do {
res = owi.triplet(dir);
id = (res & 1) != 0;
nid = (res & 2) != 0;
value = (value >> 1);
if (dir) value |= 0x80;
bits += 1;
if (bits == CHARBITS) {
rom[ix] = value;
if (rom[ix] < 0x10) Serial.print(0);
Serial.print(rom[ix], HEX);
crc = _crc_ibutton_update(crc, rom[ix]);
ix += 1;
bits = 0;
value = 0;
}
} while (id != nid);
Serial.print(F(", crc="));
Serial.println(crc);
}

void loop()
{
owi.reset();
owi.write(owi.SKIP_ROM);
owi.write(CONVERT_T);
delay(750);

owi.reset();
owi.write(owi.SKIP_ROM);
owi.write(READ_SCRATCHPAD);

scratchpad_t scratchpad;
uint8_t* p = (uint8_t*) &scratchpad;
uint8_t crc = 0;
for (size_t i = 0; i < sizeof(scratchpad); i++) {
p[i] = owi.read();
crc = _crc_ibutton_update(crc, p[i]);
}
if (crc == 0) {
float temperature = scratchpad.temperature * 0.0625;
TRACE(temperature);
} else
TRACE(crc);
delay(2000);
}
22 changes: 16 additions & 6 deletions examples/Scanner/Scanner.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#include "OWI.h"
#include "GPIO.h"

// Configure: Software/Hardware OWI Bus Manager
#define USE_SOFTWARE_OWI
#if defined(USE_SOFTWARE_OWI)
#include "Software/OWI.h"
Software::OWI<BOARD::D7> owi;

#if defined(ARDUINO_attiny)
#include "Software/Serial.h"
Software::Serial<BOARD::D0> Serial;
Software::OWI<BOARD::D1> owi;
#else
Software::OWI<BOARD::D7> owi;
#include "Hardware/OWI.h"
// Configure: Software/Hardware TWI Bus Manager
// #define USE_SOFTWARE_TWI
#if defined(USE_SOFTWARE_TWI)
#include "Software/TWI.h"
Software::TWI<BOARD::D18,BOARD::D19> twi;
#else
#include "Hardware/TWI.h"
Hardware::TWI twi;
#endif
Hardware::OWI owi(twi);
#endif

void setup()
Expand All @@ -23,7 +34,6 @@ void loop()
uint8_t rom[owi.ROM_MAX] = { 0 };
int8_t last = owi.FIRST;
int id = 0;
size_t i;

do {
last = owi.search_rom(0, rom, last);
Expand Down
33 changes: 23 additions & 10 deletions examples/Search/Search.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#include "OWI.h"
#include "GPIO.h"

// Configure: Software/Hardware OWI Bus Manager
#define USE_SOFTWARE_OWI
#if defined(USE_SOFTWARE_OWI)
#include "Software/OWI.h"
Software::OWI<BOARD::D7> owi;

#if defined(ARDUINO_attiny)
#include "Software/Serial.h"
Software::Serial<BOARD::D0> Serial;
Software::OWI<BOARD::D1> owi;
#else
Software::OWI<BOARD::D7> owi;
#include "Hardware/OWI.h"
// Configure: Software/Hardware TWI Bus Manager
// #define USE_SOFTWARE_TWI
#if defined(USE_SOFTWARE_TWI)
#include "Software/TWI.h"
Software::TWI<BOARD::D18,BOARD::D19> twi;
#else
#include "Hardware/TWI.h"
Hardware::TWI twi;
#endif
Hardware::OWI owi(twi);
#endif

void setup()
Expand All @@ -26,14 +37,16 @@ void loop()
do {
last = owi.search_rom(0, rom, last);
if (last == owi.ERROR) break;
int pos = Serial.print(i++);
pos += Serial.print(':');
Serial.print(i++);
Serial.print(':');
int8_t pos = 0;
for (size_t i = 0; i < sizeof(rom); i++)
for (uint8_t mask = 0x80; mask != 0; mask >>= 1)
for (uint8_t mask = 0x80; mask != 0; mask >>= 1, pos++) {
Serial.print((rom[i] & mask) != 0);
if (pos == last) Serial.print('*');
}
if (pos == last) Serial.print('*');
Serial.println();
for (int i = 0; i < last - 1 + pos; i++) Serial.print('-');
Serial.println('*');
} while (last != owi.LAST);
Serial.println();

Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Arduino-OWI
version=1.5
version=1.6
author=Mikael Patel
maintainer=Mikael Patel <[email protected]>
sentence=One-Wire Interface (OWI) library for Arduino.
paragraph=The OWI library has been developed to support the implementation of 1-wire device drivers. Includes abstract OWI bus manager class, GPIO based Software::OWI bus manager and device driver for DS18B20.
paragraph=The OWI library has been developed to support the implementation of 1-wire device drivers. Includes abstract OWI bus manager class, GPIO based Software::OWI bus manager, DS2482 based Hardware::OWI bus manager, and device driver for DS18B20.
category=Communication
url=https://github.com/mikaelpatel/Arduino-OWI
architectures=avr,sam
5 changes: 3 additions & 2 deletions mainpage.dox
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

The OWI library has been developed to support the implementation of
1-wire device drivers. Includes abstract OWI bus manager class, GPIO
based Software::OWI bus manager and device driver for DS18B20.
based Software::OWI bus manager, DS2482 based Hardware::OWI bus
manager, and device driver for DS18B20.

Version: 1.5
Version: 1.6
*/

/** @page License
Expand Down
Loading

0 comments on commit 1b4e7c1

Please sign in to comment.