close

#Arduino #Arduino UNO #Arduino MEGA2560 #Arduino DUE #I2C #IIC #I2C LCD #1602 LCD #LCD

 

這裏我先用一張圖來解釋Arduino 專案的發展需要的工具

我們常常到購物網買一些Arduino的sensor。。。由於Sensor 百花齊放,做的人沒有提供資料,賣的人也不清楚,技術資料轉來轉去,大家一片混亂,拿I2C界面LCD來講,明明範例的位置是0x27,但是我們照着寫就是動不了,差半天才知道LCD模組的位置不同,現在我就提供大家一個方法I2C address Scan 程式有了它就不用再傷腦了。

 

完整的程式碼如下:

 

// I2C Scanner

// Written by Nick Gammon http://gammon.com.au/i2c

// Date: 20th April 2011

// 2016-09-30

//中文註解李進衛

//IIC 位址掃描程式

//適用於Arduino UNO,DUE,MEGA2560

// 技術參考網址 http://playground.arduino.cc/Main/I2cScanner

// 作者網址(他有詳細說明I2C通信格式與原理) : http://gammon.com.au/i2c

 

#include

 

void setup() {

  Serial.begin (115200);

 

  // Leonardo: wait for serial port to connect

  while (!Serial)

    {

    }

 

  Serial.println ();

  Serial.println ("I2C scanner. Scanning ...");

  byte count = 0;

 

  Wire.begin();

  for (byte i = 8; i < 120; i++) //地址掃描

  {

    Wire.beginTransmission (i); //對裝置發出I2C位址函數解釋請參考: https://www.arduino.cc/en/Reference/WireBeginTransmission

    if (Wire.endTransmission () == 0) //假如裝置有ACK 就顯示其位址資訊藉由RS232傳送出來

// 函數解釋請參考: https://www.arduino.cc/en/Reference/WireEndTransmission

      {

      Serial.print ("Found address: ");

      Serial.print (i, DEC);

      Serial.print (" (0x");

      Serial.print (i, HEX);

      Serial.println (")");

      count++;

      delay (1);  // maybe unneeded?

      } // end of good response

  } // end of for loop

  Serial.println ("Done.");

  Serial.print ("Found ");

  Serial.print (count, DEC);

  Serial.println (" device(s).");

}  // end of setup

 

void loop() {}

 

arrow
arrow
    創作者介紹
    創作者 pic5566 的頭像
    pic5566

    大紅龍

    pic5566 發表在 痞客邦 留言(0) 人氣()