Noduino I2C Scan
来自Jack's Lab
(版本间的差异)
(→编译烧写) |
(→代码示例) |
||
| 第102行: | 第102行: | ||
<source lang=cpp> | <source lang=cpp> | ||
| + | #include "noduino.h" | ||
| + | void setup() | ||
| + | { | ||
| + | serial_begin(115200); | ||
| + | wire_begin(); | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | byte error, address; | ||
| + | int nDevices; | ||
| + | |||
| + | serial_printf("I2C Scanning...\r\n"); | ||
| + | |||
| + | nDevices = 0; | ||
| + | for (address = 1; address < 127; address++) { | ||
| + | // The i2c_scanner uses the return value of | ||
| + | // the wire_endTransmisstion to see if | ||
| + | // a device did acknowledge to the address. | ||
| + | wire_beginTransmission(address); | ||
| + | error = wire_endTransmission(); | ||
| + | |||
| + | if (error == 0) { | ||
| + | serial_printf("I2C device found at address 0x%02X\r\n", address); | ||
| + | nDevices++; | ||
| + | } else if (error == 4) { | ||
| + | serial_printf("Unknow error at address 0x%02X\r\n", address); | ||
| + | } | ||
| + | } | ||
| + | if (nDevices == 0) | ||
| + | serial_printf("No I2C devices found\r\n"); | ||
| + | else | ||
| + | serial_printf("done\r\n"); | ||
| + | |||
| + | delay(25*1000); | ||
| + | } | ||
</source> | </source> | ||
2016年8月28日 (日) 09:46的版本
目录 |
1 开发环境准备
根据您的系统,选择如下快速指南:
- Getting Started with Noduino SDK on Linux
- Getting Started with Noduino SDK on Mac OS X
- Getting Started with Noduino SDK on Windows
2 硬件准备
所有 I2C 设备,都接在:
- SCL --> D9
- SDA --> D8
3.3V 供电
保证连在总线上的所有设备地址不冲突!
3 编译烧写
进入你的 i2c-scan 目录:
$ cd /PATH/TO/noduino-sdk/examples/noduino/i2c-scan
编译、上传一步完成:
$ make flash
不同平台下,你的串口设备号可能不一样,确认一下你的串口设备,Linux 下 Falcon 开发板可能被识别为 /dev/ttyUSB1(dmesg 查看),则:
$ make flash ESPPORT=/dev/ttyUSB1
Windows 下 Falcon 开发板可能被识别为 COM6(控制面板 --> 设备管理器里查看),则:
$ make flash ESPPORT=COM6
4 查看结果
上传完之后,用串口工具,用 115200 的波特率打开串口,即可看到串口在持续输出传感器测得的值:
Windows 下可用串口调试助手这类插看
Linux 下推荐使用 picocom:
$ sudo apt-get install picocom $ picocom -b 115200 /dev/ttyUSB1 picocom v1.7 port is : /dev/ttyUSB2 flowcontrol : none baudrate is : 115200 parity is : none databits are : 8 escape is : C-a local echo is : no noinit is : no noreset is : no nolock is : no send_cmd is : sz -vv receive_cmd is : rz -vv imap is : omap is : emap is : crcrlf,delbs, Terminal ready ...... ......
Ctrl + a q 退出 picocom
5 代码示例
#include "noduino.h"
void setup()
{
serial_begin(115200);
wire_begin();
}
void loop()
{
byte error, address;
int nDevices;
serial_printf("I2C Scanning...\r\n");
nDevices = 0;
for (address = 1; address < 127; address++) {
// The i2c_scanner uses the return value of
// the wire_endTransmisstion to see if
// a device did acknowledge to the address.
wire_beginTransmission(address);
error = wire_endTransmission();
if (error == 0) {
serial_printf("I2C device found at address 0x%02X\r\n", address);
nDevices++;
} else if (error == 4) {
serial_printf("Unknow error at address 0x%02X\r\n", address);
}
}
if (nDevices == 0)
serial_printf("No I2C devices found\r\n");
else
serial_printf("done\r\n");
delay(25*1000);
}
6 扩展阅读
- 更多问题参考: