Arduino-OWI
One-Wire Interface (OWI) library for Arduino
ROM.ino
Go to the documentation of this file.
1 #include "GPIO.h"
2 #include "OWI.h"
3 #include "Software/OWI.h"
4 #include "assert.h"
5 
7 
8 void setup()
9 {
10  Serial.begin(57600);
11  while (!Serial);
12 }
13 
14 void loop()
15 {
16  // The Standard ROM functions:
17  // 1. Read ROM from single device on the bus.
18  // 2. Skip ROM to address all devices
19  // 3. Match ROM to address a specific device.
20  // 4. Search ROM to iterate devices.
21  // 5. Alarm Search to iterate devices with alarm set.
22  // 6. Match LABEL to use short addressing (Extended function).
23 
24  uint8_t ROM[OWI::ROM_MAX];
25  uint8_t label;
26  int8_t last;
27  uint8_t id;
28  bool res;
29 
30  // Read ROM from slave, and print
31  ASSERT(owi.read_rom(ROM));
32  Serial.print(F("read_rom:"));
33  print(ROM);
34  Serial.println();
35 
36  // Address slave with skip rom
37  ASSERT(res = owi.skip_rom());
38  Serial.print(F("skip_rom:res="));
39  Serial.println(res);
40  owi.write(0);
41 
42  // Address slave with match rom
43  ASSERT(res = owi.match_rom(ROM));
44  Serial.print(F("match_rom:res="));
45  Serial.println(res);
46  owi.write(0);
47 
48  // Address slave with search rom
49  id = 0;
50  last = owi.FIRST;
51  memset(ROM, 0, sizeof(ROM));
52  do {
53  last = owi.search_rom(1, ROM, last);
54  if (last == owi.ERROR) break;
55  label = 10;
56  owi.write(0x15);
57  owi.write(label);
58  Serial.print(F("search_rom["));
59  Serial.print(id);
60  Serial.print(F("]:"));
61  print(ROM);
62  Serial.print(F(":label_rom:label="));
63  Serial.println(label);
64  id += 1;
65  } while (last != owi.LAST);
66 
67  // Address slave with alarm search
68  last = owi.FIRST;
69  id = 0;
70  memset(ROM, 0, sizeof(ROM));
71  do {
72  last = owi.alarm_search(ROM, last);
73  if (last == owi.ERROR) break;
74  owi.write(0x16);
75  label = owi.read();
76  Serial.print(F("alarm_search["));
77  Serial.print(id++);
78  Serial.print(F("]:"));
79  print(ROM);
80  Serial.print(F(":read_label:label="));
81  Serial.println(label);
82  } while (last != owi.LAST);
83 
84  // Address slave with match label
85  ASSERT(res = owi.match_label(10));
86  owi.write(0);
87  Serial.print(F("match_label:res="));
88  Serial.println(res);
89  Serial.println();
90 
91  delay(2000);
92 }
93 
94 void print(uint8_t* rom)
95 {
96  // Print 1-Wire ROM identity code; family, serial number and crc
97  Serial.print(F("famly="));
98  if (rom[0] < 0x10) Serial.print('0');
99  Serial.print(rom[0], HEX);
100  Serial.print(F(",sn="));
101  for (size_t i = 1; i < OWI::ROM_MAX - 1; i++) {
102  if (rom[i] < 0x10) Serial.print('0');
103  Serial.print(rom[i], HEX);
104  if (i < OWI::ROM_MAX - 2) Serial.print(',');
105  }
106  Serial.print(F(",crc="));
107  uint8_t crc = rom[OWI::ROM_MAX - 1];
108  if (crc < 0x10) Serial.print('0');
109  Serial.print(crc, HEX);
110 }
virtual uint8_t read(uint8_t bits=CHARBITS)
Definition: OWI.h:72
int8_t alarm_search(uint8_t *code, int8_t last=FIRST)
Definition: OWI.h:261
bool match_label(uint8_t label)
Definition: OWI.h:274
Error during search.
Definition: OWI.h:192
virtual void write(uint8_t value, uint8_t bits=CHARBITS)
Definition: OWI.h:98
bool skip_rom()
Definition: OWI.h:248
void loop()
Definition: ROM.ino:14
void setup()
Definition: ROM.ino:8
void print(uint8_t *rom)
Definition: ROM.ino:94
int8_t search_rom(uint8_t family, uint8_t *code, int8_t last=FIRST)
Definition: OWI.h:204
Software::Serial< BOARD::D0 > Serial
Definition: Alarm.ino:8
Software::OWI< BOARD::D7 > owi
Definition: ROM.ino:6
bool read_rom(uint8_t *code)
Definition: OWI.h:222
Last position, search completed.
Definition: OWI.h:193
bool match_rom(uint8_t *code)
Definition: OWI.h:236
Start position of search.
Definition: OWI.h:191
static const size_t ROM_MAX
Definition: OWI.h:32