Skip to content

Commit 1b4e7c1

Browse files
committed
Adding support for hardware (DS2482) based OWI bus manager
1 parent a732841 commit 1b4e7c1

File tree

12 files changed

+741
-90
lines changed

12 files changed

+741
-90
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
# Arduino-OWI
22
The OWI library has been developed to support the implementation of
3-
1-wire device drivers. The library includes an example device driver
4-
and a bus scanner.
3+
1-wire bus managers and device drivers. The library includes a GPIO
4+
based software and DS2482 based hardware bus manager, and device
5+
driver for DS18B20.
56

6-
Version: 1.5
7+
The examples directory contains device search, bus scanner,
8+
thermometer alarm search, and example sketches for DS18B20, DS1990A
9+
and DS2482.
10+
11+
Version: 1.6
712

813
## Classes
914

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

1420
## Example Sketches
1521

1622
* [DS18B20](./examples/DS18B20)
1723
* [DS1990A](./examples/DS1990A)
24+
* [DS2482](./examples/DS2482)
25+
* [Alarm](./examples/Alarm)
1826
* [Scanner](./examples/Scanner)
1927
* [Search](./examples/Search)
20-
* [Alarm](./examples/Alarm)
2128

2229
## Dependencies
2330

2431
* [Arduino-GPIO](https://github.com/mikaelpatel/Arduino-GPIO)
32+
* [Arduino-TWI](https://github.com/mikaelpatel/Arduino-TWI)

examples/Alarm/Alarm.ino

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
#include "GPIO.h"
22
#include "OWI.h"
3-
#include "Software/OWI.h"
43
#include "Driver/DS18B20.h"
54

6-
#if defined(ARDUINO_attiny)
7-
#include "Software/Serial.h"
8-
Software::Serial<BOARD::D0> Serial;
9-
Software::OWI<BOARD::D1> owi;
10-
#else
5+
// Configure: Software/Hardware OWI Bus Manager
6+
#define USE_SOFTWARE_OWI
7+
#if defined(USE_SOFTWARE_OWI)
8+
#include "Software/OWI.h"
119
Software::OWI<BOARD::D7> owi;
10+
11+
#else
12+
#include "Hardware/OWI.h"
13+
// Configure: Software/Hardware TWI Bus Manager
14+
// #define USE_SOFTWARE_TWI
15+
#if defined(USE_SOFTWARE_TWI)
16+
#include "Software/TWI.h"
17+
Software::TWI<BOARD::D18,BOARD::D19> twi;
18+
#else
19+
#include "Hardware/TWI.h"
20+
Hardware::TWI twi;
21+
#endif
22+
Hardware::OWI owi(twi);
1223
#endif
1324

1425
DS18B20 sensor(owi);
@@ -50,7 +61,7 @@ void loop()
5061
bool triggered = false;
5162
static uint32_t timestamp = 0;
5263
if (!sensor.convert_request(true)) return;
53-
delay(1000);
64+
delay(sensor.conversion_time());
5465
do {
5566
last = owi.alarm_search(rom, last);
5667
if (last == owi.ERROR) break;

examples/DS18B20/DS18B20.ino

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
#include "GPIO.h"
22
#include "OWI.h"
3-
#include "Software/OWI.h"
43
#include "Driver/DS18B20.h"
54

6-
#if defined(ARDUINO_attiny)
7-
#include "Software/Serial.h"
8-
Software::Serial<BOARD::D0> Serial;
9-
Software::OWI<BOARD::D1> owi;
10-
#else
5+
// Configure: Software/Hardware OWI Bus Manager
6+
#define USE_SOFTWARE_OWI
7+
#if defined(USE_SOFTWARE_OWI)
8+
#include "Software/OWI.h"
119
Software::OWI<BOARD::D7> owi;
10+
11+
#else
12+
#include "Hardware/OWI.h"
13+
// Configure: Software/Hardware TWI Bus Manager
14+
// #define USE_SOFTWARE_TWI
15+
#if defined(USE_SOFTWARE_TWI)
16+
#include "Software/TWI.h"
17+
Software::TWI<BOARD::D18,BOARD::D19> twi;
18+
#else
19+
#include "Hardware/TWI.h"
20+
Hardware::TWI twi;
21+
#endif
22+
Hardware::OWI owi(twi);
1223
#endif
1324

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

2738
if (!sensor.convert_request(true)) return;
39+
delay(sensor.conversion_time());
2840

2941
int8_t last = owi.FIRST;
3042
uint8_t* rom = sensor.rom();
@@ -79,5 +91,5 @@ void loop()
7991
} while (last != owi.LAST);
8092

8193
Serial.println();
82-
delay(5000);
94+
delay(4000);
8395
}

examples/DS2482/DS2482.ino

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "GPIO.h"
2+
#include "OWI.h"
3+
#include "TWI.h"
4+
#include "Hardware/OWI.h"
5+
#include <util/crc16.h>
6+
7+
#define USE_SOFTWARE_TWI
8+
#if defined(USE_SOFTWARE_TWI)
9+
#include "Software/TWI.h"
10+
Software::TWI<BOARD::D18,BOARD::D19> twi;
11+
#else
12+
#include "Hardware/TWI.h"
13+
Hardware::TWI twi;
14+
#endif
15+
16+
Hardware::OWI owi(twi);
17+
18+
// DS18B20 Commands
19+
enum {
20+
CONVERT_T = 0x44,
21+
READ_SCRATCHPAD = 0xbe
22+
};
23+
24+
// DS18B20 Scratchpad Memory
25+
struct scratchpad_t {
26+
int16_t temperature;
27+
int8_t high_trigger;
28+
int8_t low_trigger;
29+
uint8_t configuration;
30+
uint8_t reserved[3];
31+
uint8_t crc;
32+
};
33+
34+
#define TRACE(expr) \
35+
do { \
36+
Serial.print(#expr "="); \
37+
Serial.println(expr); \
38+
} while (0)
39+
40+
void setup()
41+
{
42+
Serial.begin(57600);
43+
while (!Serial);
44+
45+
TRACE(owi.device_reset());
46+
TRACE(owi.device_config());
47+
TRACE(owi.set_read_pointer(owi.READ_DATA_REGISTER));
48+
TRACE(owi.set_read_pointer(owi.CONFIGURATION_REGISTER));
49+
TRACE(owi.set_read_pointer(owi.CHANNEL_SELECTION_REGISTER));
50+
TRACE(owi.set_read_pointer(owi.STATUS_REGISTER));
51+
52+
uint8_t rom[owi.ROM_MAX] = { 0 };
53+
uint8_t crc = 0;
54+
TRACE(owi.reset());
55+
Serial.print(F("rom="));
56+
owi.write(owi.READ_ROM);
57+
for (size_t i = 0; i < sizeof(rom); i++) {
58+
rom[i] = owi.read(8);
59+
if (rom[i] < 0x10) Serial.print(0);
60+
Serial.print(rom[i], HEX);
61+
crc = _crc_ibutton_update(crc, rom[i]);
62+
}
63+
Serial.print(F(", crc="));
64+
Serial.println(crc);
65+
66+
uint8_t value = 0;
67+
uint8_t bits = 0;
68+
uint8_t ix = 0;
69+
uint8_t res = 0;
70+
uint8_t dir = 0;
71+
bool id;
72+
bool nid;
73+
crc = 0;
74+
TRACE(owi.reset());
75+
Serial.print(F("rom="));
76+
owi.write(owi.SEARCH_ROM);
77+
do {
78+
res = owi.triplet(dir);
79+
id = (res & 1) != 0;
80+
nid = (res & 2) != 0;
81+
value = (value >> 1);
82+
if (dir) value |= 0x80;
83+
bits += 1;
84+
if (bits == CHARBITS) {
85+
rom[ix] = value;
86+
if (rom[ix] < 0x10) Serial.print(0);
87+
Serial.print(rom[ix], HEX);
88+
crc = _crc_ibutton_update(crc, rom[ix]);
89+
ix += 1;
90+
bits = 0;
91+
value = 0;
92+
}
93+
} while (id != nid);
94+
Serial.print(F(", crc="));
95+
Serial.println(crc);
96+
}
97+
98+
void loop()
99+
{
100+
owi.reset();
101+
owi.write(owi.SKIP_ROM);
102+
owi.write(CONVERT_T);
103+
delay(750);
104+
105+
owi.reset();
106+
owi.write(owi.SKIP_ROM);
107+
owi.write(READ_SCRATCHPAD);
108+
109+
scratchpad_t scratchpad;
110+
uint8_t* p = (uint8_t*) &scratchpad;
111+
uint8_t crc = 0;
112+
for (size_t i = 0; i < sizeof(scratchpad); i++) {
113+
p[i] = owi.read();
114+
crc = _crc_ibutton_update(crc, p[i]);
115+
}
116+
if (crc == 0) {
117+
float temperature = scratchpad.temperature * 0.0625;
118+
TRACE(temperature);
119+
} else
120+
TRACE(crc);
121+
delay(2000);
122+
}

examples/Scanner/Scanner.ino

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#include "OWI.h"
22
#include "GPIO.h"
3+
4+
// Configure: Software/Hardware OWI Bus Manager
5+
#define USE_SOFTWARE_OWI
6+
#if defined(USE_SOFTWARE_OWI)
37
#include "Software/OWI.h"
8+
Software::OWI<BOARD::D7> owi;
49

5-
#if defined(ARDUINO_attiny)
6-
#include "Software/Serial.h"
7-
Software::Serial<BOARD::D0> Serial;
8-
Software::OWI<BOARD::D1> owi;
910
#else
10-
Software::OWI<BOARD::D7> owi;
11+
#include "Hardware/OWI.h"
12+
// Configure: Software/Hardware TWI Bus Manager
13+
// #define USE_SOFTWARE_TWI
14+
#if defined(USE_SOFTWARE_TWI)
15+
#include "Software/TWI.h"
16+
Software::TWI<BOARD::D18,BOARD::D19> twi;
17+
#else
18+
#include "Hardware/TWI.h"
19+
Hardware::TWI twi;
20+
#endif
21+
Hardware::OWI owi(twi);
1122
#endif
1223

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

2838
do {
2939
last = owi.search_rom(0, rom, last);

examples/Search/Search.ino

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#include "OWI.h"
22
#include "GPIO.h"
3+
4+
// Configure: Software/Hardware OWI Bus Manager
5+
#define USE_SOFTWARE_OWI
6+
#if defined(USE_SOFTWARE_OWI)
37
#include "Software/OWI.h"
8+
Software::OWI<BOARD::D7> owi;
49

5-
#if defined(ARDUINO_attiny)
6-
#include "Software/Serial.h"
7-
Software::Serial<BOARD::D0> Serial;
8-
Software::OWI<BOARD::D1> owi;
910
#else
10-
Software::OWI<BOARD::D7> owi;
11+
#include "Hardware/OWI.h"
12+
// Configure: Software/Hardware TWI Bus Manager
13+
// #define USE_SOFTWARE_TWI
14+
#if defined(USE_SOFTWARE_TWI)
15+
#include "Software/TWI.h"
16+
Software::TWI<BOARD::D18,BOARD::D19> twi;
17+
#else
18+
#include "Hardware/TWI.h"
19+
Hardware::TWI twi;
20+
#endif
21+
Hardware::OWI owi(twi);
1122
#endif
1223

1324
void setup()
@@ -26,14 +37,16 @@ void loop()
2637
do {
2738
last = owi.search_rom(0, rom, last);
2839
if (last == owi.ERROR) break;
29-
int pos = Serial.print(i++);
30-
pos += Serial.print(':');
40+
Serial.print(i++);
41+
Serial.print(':');
42+
int8_t pos = 0;
3143
for (size_t i = 0; i < sizeof(rom); i++)
32-
for (uint8_t mask = 0x80; mask != 0; mask >>= 1)
44+
for (uint8_t mask = 0x80; mask != 0; mask >>= 1, pos++) {
3345
Serial.print((rom[i] & mask) != 0);
46+
if (pos == last) Serial.print('*');
47+
}
48+
if (pos == last) Serial.print('*');
3449
Serial.println();
35-
for (int i = 0; i < last - 1 + pos; i++) Serial.print('-');
36-
Serial.println('*');
3750
} while (last != owi.LAST);
3851
Serial.println();
3952

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Arduino-OWI
2-
version=1.5
2+
version=1.6
33
author=Mikael Patel
44
maintainer=Mikael Patel <mikael.patel@gmail.com>
55
sentence=One-Wire Interface (OWI) library for Arduino.
6-
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.
6+
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.
77
category=Communication
88
url=https://github.com/mikaelpatel/Arduino-OWI
99
architectures=avr,sam

mainpage.dox

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

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

7-
Version: 1.5
8+
Version: 1.6
89
*/
910

1011
/** @page License

0 commit comments

Comments
 (0)