Skip to content

Instantly share code, notes, and snippets.

@lgh06
Last active May 6, 2021 11:32
Show Gist options
  • Save lgh06/199a78f868d8576f5364369e23e3e21f to your computer and use it in GitHub Desktop.
Save lgh06/199a78f868d8576f5364369e23e3e21f to your computer and use it in GitHub Desktop.
/*
将从机与arduino开发板通过IIC接口连接好。
SDA-->A4
SCK-->A5
上传以下代码至arudino,观察串口返回的结果。
Name: IICaddressFound.ino
Created: 2018/10/14 14:42:57
Author: ipenn
*/
#include <Wire.h> //(将 IIC 所需的Wire.h头文件包含进来)
// the setup function runs once when you press reset or power the boardvoid setup() {
Serial.begin(9600);//(开启串口,以接受结果)
Wire.begin();//(初始化IIC连接,作为主机无需参数)
for (int i = 0; i < 127; i++)//(IIC 地址从0~127(十进制),一共128个遍历一遍就知道结果了) {
Wire.beginTransmission(i);//(和地址i连接)
int error = Wire.endTransmission();//(结束和地址i的连接,并接受返回值(只可能是0~4其中的一个值)。其中0表示成功)
if (error == 0) {
//(如果返回值是0(成功)的话就返回地址,以十六进制)
Serial.println();
Serial.print("Device is found at : 0x");
Serial.println(i,HEX);
}
}
}
// the loop function runs over and over again until power down or reset
void loop() { }// loop 里什么都不干
@lgh06
Copy link
Author

lgh06 commented May 6, 2021

arduino uno R3
输出口5 接电阻和小灯 然后接回输出口上的GND

@lgh06
Copy link
Author

lgh06 commented May 6, 2021

Serial相关代码 指定波特率 把数据传回电脑

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment