diff options
| author | Abel Tim <abel@main.home> | 2024-06-22 09:17:50 +0200 |
|---|---|---|
| committer | Abel Tim <abel@main.home> | 2024-06-22 09:17:50 +0200 |
| commit | 4971e86d05c714ff3630ada8851938614128b412 (patch) | |
| tree | 35a40d4213bd8bcb47da35ef7a477ae37b28cf05 /debug/i2c_scanner | |
| parent | bec80cbf5f05e2faeeda15b7a8b372d1a3f8e465 (diff) | |
| download | robotica-4971e86d05c714ff3630ada8851938614128b412.tar.gz robotica-4971e86d05c714ff3630ada8851938614128b412.zip | |
update repo structure
update poster
add code flowchart
Diffstat (limited to 'debug/i2c_scanner')
| -rw-r--r-- | debug/i2c_scanner/i2c_scanner.ino | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/debug/i2c_scanner/i2c_scanner.ino b/debug/i2c_scanner/i2c_scanner.ino new file mode 100644 index 0000000..8b13954 --- /dev/null +++ b/debug/i2c_scanner/i2c_scanner.ino @@ -0,0 +1,93 @@ +/**
+i2c port scanner based on:
+ in wire library examples i2c_scanner
+ https://learn.adafruit.com/adafruit-pca9548-8-channel-stemma-qt-qwiic-i2c-multiplexer/arduino
+
+
+ *
+ */
+
+#include <Wire.h>
+
+#define PCAADDR 0x70 //i2c address of the i2c multiplexer
+
+
+void setup() {
+ Serial.begin(115200);
+ while (!Serial); // Leonardo: wait for Serial Monitor
+ Serial.println("\nI2C Scanner");
+
+}
+
+void pcaselect(uint8_t i) {
+ if (i > 7) return;
+
+ Wire.beginTransmission(PCAADDR);
+ Wire.write(1 << i);
+ Wire.endTransmission();
+}
+
+
+
+
+void scandirectports(){
+ Wire.begin();
+ int nDevices = 0;
+ Serial.println("Scanning for i2c ports on board...");
+
+ for (byte address = 1; address < 127; ++address) {
+ // The i2c_scanner uses the return value of
+ // the Wire.endTransmission to see if
+ // a device did acknowledge to the address.
+ Wire.beginTransmission(address);
+ byte error = Wire.endTransmission();
+
+ if (error == 0) {
+ Serial.print("I2C device found at address 0x");
+ if (address < 16) {
+ Serial.print("0");
+ }
+ Serial.print(address, HEX);
+ Serial.println(" !");
+
+ ++nDevices;
+ } else if (error == 4) {
+ Serial.print("Unknown error at address 0x");
+ if (address < 16) {
+ Serial.print("0");
+ }
+ Serial.println(address, HEX);
+ }
+ }
+ if (nDevices == 0) {
+ Serial.println("No I2C devices found\n");
+ } else {
+ Serial.println("done\n");
+ }
+ Serial.println("done scanning for i2c ports on board");
+}
+void scani2cmultiplexer(){
+ Serial.println("Scanning for i2c ports on i2c multiplexer...");
+ for (uint8_t t=0; t<8; t++) {
+ pcaselect(t);
+ Serial.print("PCA Port #"); Serial.println(t);
+
+ for (uint8_t addr = 0; addr<=127; addr++) {
+ if (addr == PCAADDR) continue;
+ Wire.beginTransmission(addr);
+ if (!Wire.endTransmission()) {
+ Serial.print("Found I2C 0x"); Serial.println(addr,HEX);
+ }
+ }
+ }
+ Serial.println("done scanning for i2c ports on i2c multiplexer");
+}
+
+
+
+void loop() {
+ scandirectports();
+ delay(1000);
+ scani2cmultiplexer();
+ delay(1000);
+}
|
