YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2065|回复: 1
打印 上一主题 下一主题

arduino IIC地址查询查询/I2C地址扫描查询 Arduino I2C Scanner

[复制链接]

签到天数: 867 天

[LV.10]以坛为家III

跳转到指定楼层
楼主
发表于 2018-5-18 14:18:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Arduino I2C Scanner
IIC液晶使用贴中,我们的网友:hwn444 贡献的IIC地址查询程序,这里我们再放一个IIC地址扫描程序,功能都一样,就是在串口输出时做了些事情。。。
废话少说直接看程序:
[C] 纯文本查看 复制代码
/**
   Arduino I2C Scanner
   v. 1.0
   Copyright (C) 2017 Robert Ulbricht
   [url=http://www.arduinoslovakia.eu]http://www.arduinoslovakia.eu[/url]
   I2C Scanner inspired by Raspberry Pi.
   IDE: 1.8.3 or higher
   Board: Arduino Uno, Mega, Pro Mini
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <[url=http://www.gnu.org/licenses/>.]http://www.gnu.org/licenses/>.[/url]
*/

#include <Wire.h>

void setup() {
  Wire.begin();

  Serial.begin(115200);
  Serial.println("I2C Scanner");

}

void loop() {
  uint8_t error, address, line = 1;
  int nDevices = 0;

  Serial.println("     0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");
  Serial.print  ("00:         ");

  // [url=https://learn.adafruit.com/i2c-addresses/the-list]https://learn.adafruit.com/i2c-addresses/the-list[/url]
  for (address = 0x03; address < 0x78; address++ ) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      printHex(address);
      nDevices++;
    }
    else if (error == 4)
      Serial.print(" ER");
    else
      Serial.print(" --");

    if ((address + 1) % 16 == 0) {
      Serial.println();
      Serial.print(line);
      Serial.print("0:");
      line++;
    }

  }
  if (nDevices == 0)
    Serial.println("\nNo I2C devices found\n");
  else {
    Serial.print("\nFound ");
    Serial.print(nDevices);
    Serial.println(" devices\n");
  }

  delay(5000);           // wait 5 seconds for next scan
}

void printHex(uint8_t address) {
  Serial.print(" ");
  if (address < 16)
    Serial.print("0");
  Serial.print(address, HEX);
}



输出效果(图中硬件IIC 地址为 0x68):
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-11-23 17:39 , Processed in 0.051619 second(s), 28 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表