Skip to content

Instantly share code, notes, and snippets.

@wwng2333
Last active July 22, 2023 11:06
Show Gist options
  • Save wwng2333/9f307ef6d63096fd49562b9357200cdd to your computer and use it in GitHub Desktop.
Save wwng2333/9f307ef6d63096fd49562b9357200cdd to your computer and use it in GitHub Desktop.
IIC搜索
#include <Wire.h>
#define PY32 0xA0
void setup() {
// put your setup code here, to run once:
Serial.begin(76880);
Wire.begin(12, 13);
}
void loop() {
// put your main code here, to run repeatedly:
PY32_Read();
delay(500);
}
uint16_t PY32_Read() {
byte data[2] = { 0 }, i = 0;
uint16_t ret = 0;
Wire.beginTransmission(PY32 >> 1);
Wire.requestFrom(PY32 >> 1, 2);
while (Wire.available()) data[i++] = Wire.read();
ret = data[0] | (data[1] << 8);
Serial.println(ret, HEX);
return ret;
}
#include <Wire.h>
#include "SlowSoftI2CMaster.h"
#define PY32 0xA0
SlowSoftI2CMaster si = SlowSoftI2CMaster(12, 13);
void setup() {
Wire.begin(5, 4);
Serial.begin(74880);
Serial.println(F("Intializing ..."));
if (!si.i2c_init())
Serial.println(F("Initialization error. SDA or SCL are low"));
else
Serial.println(F("...done"));
}
void loop(void)
{
uint8_t add = 0;
int found = false;
Serial.println("Scanning ...");
Serial.println(" 8-bit 7-bit addr");
// try read
do {
if (si.i2c_start(add | I2C_READ)) {
found = true;
si.i2c_read(true);
si.i2c_stop();
Serial.print("Read: 0x");
if (add < 0x0F) Serial.print(0, HEX);
Serial.print(add+I2C_READ, HEX);
Serial.print(" 0x");
if (add>>1 < 0x0F) Serial.print(0, HEX);
Serial.println(add>>1, HEX);
} else si.i2c_stop();
add += 2;
} while (add);
// try write
add = 0;
do {
if (si.i2c_start(add | I2C_WRITE)) {
found = true;
si.i2c_stop();
Serial.print("Write: 0x");
if (add < 0x0F) Serial.print(0, HEX);
Serial.print(add+I2C_WRITE, HEX);
Serial.print(" 0x");
if (add>>1 < 0x0F) Serial.print(0, HEX);
Serial.println(add>>1, HEX);
} else si.i2c_stop();
si.i2c_stop();
add += 2;
} while (add);
if (!found) Serial.println(F("No I2C device found."));
Serial.println("Done\n\n");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment