Arduino-TWI
Two-Wire Interface (TWI) library for Arduino
Scanner.ino
Go to the documentation of this file.
1 #include "TWI.h"
2 
3 // Configure: TWI bus manager (software or hardware)
4 #define USE_SOFTWARE_TWI
5 
6 #if defined(USE_SOFTWARE_TWI)
7 #include "GPIO.h"
8 #include "Software/TWI.h"
9 #if defined(ARDUINO_attiny)
10 #include "Software/Serial.h"
11 Software::Serial<BOARD::D0> Serial;
13 #else
14 #if defined(SAM)
16 #else
18 #endif
19 #endif
20 
21 #else
22 // Configure: Hardware TWI bus clock frequency (100 or 400 kHz)
23 #include "Hardware/TWI.h"
24 Hardware::TWI twi(100000UL);
25 // Hardware::TWI twi(400000UL);
26 #endif
27 
28 void setup()
29 {
30  Serial.begin(57600);
31  while (!Serial);
32 }
33 
34 void loop()
35 {
36  // Scan twi device addresses. Print address of all devices
37  // that respond to a write request
38  int i = 0;
39  for (uint8_t addr = 3; addr < 128; addr++) {
40  TWI::Device dev(twi, addr);
41  dev.acquire();
42  int res = dev.write(NULL);
43  dev.release();
44  if (res < 0) continue;
45  Serial.print(i++);
46  Serial.print(F(":addr=0x"));
47  if (addr < 0x10) Serial.print(0);
48  Serial.println(addr, HEX);
49  }
50  Serial.println();
51  delay(5000);
52 }
bool release()
Definition: TWI.h:62
void loop()
Definition: Scanner.ino:34
int write(const void *buf, size_t count)
Definition: TWI.h:84
void setup()
Definition: Scanner.ino:28
bool acquire()
Definition: TWI.h:52
Software::TWI< BOARD::D18, BOARD::D19 > twi
Definition: Scanner.ino:17