|
|

楼主 |
发表于 2013-6-20 13:35:32
|
显示全部楼层
使用如下代码扫描I2C地址:
- //*****************************************
- //
- // I2C Scaner code
- //
- //*****************************************
- // Scan the I2C bus between addresses from_addr and to_addr.
- // On each address, call the callback function with the address and result.
- // If result==0, address was found, otherwise, address wasn't found
- // (can use result to potentially get other status on the I2C bus, see twi.c)
- // Assumes Wire.begin() has already been called
- void scanI2CBus(byte from_addr, byte to_addr,
- void(*callback)(byte address, byte result) )
- {
- byte rc;
- byte data = 0; // not used, just an address to feed to twi_writeTo()
- for( byte addr = from_addr; addr <= to_addr; addr++ ) {
- rc = twi_writeTo(addr, &data, 0, 1, 0);
- if(rc==0) callback( addr, rc );
- }
- }
- // Called when address is found in scanI2CBus()
- // Feel free to change this as needed
- // (like adding I2C comm code to figure out what kind of I2C device is there)
- void scanFunc( byte addr, byte result ) {
-
-
- Serial.print("addr: ");
- Serial.print(addr,DEC);
- Serial.print("\t HEX: 0x");
- Serial.print(addr,HEX);
- Serial.println( (result==0) ? "\t found!":" ");
- if(addr==80) Serial.println("24cXX on connect!");
- // Serial.print( (addr%4) ? "\t":"\n");
- }
复制代码
结果如下:
想请教为什么是4个地址呢 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|