【Follow me第二季第4期】Arduino RP2040之i2c总线附i2c_scanner
本帖最后由 yilonglucky 于 2024-12-4 15:43 编辑<p>本期活动发放的开发板是Arduino Nano RP2040 Connect,这块主板上的主芯片是树莓派的RP2040。</p>
<p>RP2040是有两个独立的i2c控制器的。</p>
<p>而Arduino Nano是将i2c0复用到GPIO16和GPIO17使用的。</p>
<p>RP2040 GPIO功能图:</p>
<p> </p>
<p>在Arduino IDE中可以直接使用Wire库来使用这个i2c控制器。</p>
<p>根据Arduino Nano原理图知其已经连接了两个i2c slave,一个是加速度陀螺仪,一个是认证芯片,我另外连接了两个i2c slave。</p>
<p>前两者硬件原理图:</p>
<p> </p>
<p>在实际使用时,可以通过i2c_scanner来遍历i2c总线上的所有slave是否在位,并且将所有设备清单列出来。</p>
<p>代码如下:</p>
<pre>
<code class="language-cpp">// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not known.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 262013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for Serial Monitor
Serial.println("\nI2C Scanner");
}
void loop() {
int nDevices = 0;
Serial.println("Scanning...");
for (byte address = 1; address < 127; ++address) {
// The i2c_scanner uses the return value of
// the Wire.endTransmission to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.print(address, HEX);
Serial.println("!");
++nDevices;
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
} else {
Serial.println("done\n");
}
delay(5000);// Wait 5 seconds for next scan
}
</code></pre>
<p>这里注意,有一句:</p>
<p>while (!Serial); // Leonardo: wait for Serial Monitor</p>
<p>默认Serial对应的是2040开发板的USB接口对应的UART,这一句是指程序开始运行后等待用户打开该串口(可以使用IDE自带的串口监视器,我理解应该是内部有在检测UART电平是否被对面控制器拉高)。</p>
<p>实际运行效果如下:</p>
<p> </p>
<p>上图中</p>
<p>0x18是我外接的MCP9808温度检测</p>
<p>0x3C是我外接的SSD1306</p>
<p>0x60应该是主板自带MICROCHIP加密芯片ATECC608A,我没有找到这个地址的来源。实际应用应该是Arduino官网用来认证、下载、引导使用。</p>
<p>0x6A是主板自带ST加速度陀螺仪芯片LSM6DSOXTR,从硬件原理图中也能看到SA0接地,datasheet提示对应地址1101010b。不过IMU的代码把这里封装了,用户直接使用即可都不用关心实际地址是什么。</p>
<p> </p>
<p>疑问:默认使用i2c 0,怎么使用i2c 1呢?</p>
<div style="position: fixed; opacity: 1; height: 300px; width: 100%; font-size: 25px; text-align: center; bottom: 200px; left: 0px; display: none; flex-direction: column; justify-content: center; z-index: 1661789940; top: calc(50% + 0px);"> </div>
<div style="position: fixed; opacity: 1; height: 300px; width: 100%; font-size: 25px; text-align: center; bottom: 200px; left: 0px; display: none; flex-direction: column; justify-content: center; z-index: 1661789940; top: calc(50% + 0px);"> </div>
页:
[1]