当前位置:网站首页>Arduino Wire 库使用
Arduino Wire 库使用
2022-07-02 06:08:00 【谢娘蓝桥】
1. 扫描所有IIC 从设备
// --------------------------------------
// 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 26 2013
// 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
}
2.写数据
Wire.beginTransmission(CLD_ADDR1);
Wire.write(&TAS6424_Write_data_1_4[i][0], 2);
Wire.endTransmission();3.指定从设备寄存器读取数据
//CLD_ADDR1 从设备地址
//读取一个数据,
//reg data 从设备地址
//地址位宽 1
//0 --不发送stop
Wire.requestFrom((uint8_t)CLD_ADDR1, 1,reg_addr,1,0);
while (Wire.available()) {
c = Wire.read(); // 以字符形式接受并读取从机发来的一个字节的数据
}
Serial.println(c, HEX); // 串口输出该字符
Wire.endTransmission();边栏推荐
- Generics and generic constraints of typescript
- 外部中断无法进入,删代码再还原就好......记录这个想不到的bug
- 让每一位开发者皆可使用机器学习技术
- Let every developer use machine learning technology
- [C language] screening method for prime numbers
- Ros2 --- lifecycle node summary
- 使用HBuilderX的一些常用功能
- Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
- LeetCode 83. 删除排序链表中的重复元素
- Mathematical statistics and machine learning
猜你喜欢

官方零基础入门 Jetpack Compose 的中文课程来啦!

复杂 json数据 js前台解析 详细步骤《案例:一》

Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes

Redis Key-Value数据库【初级】

锐捷EBGP 配置案例

Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table

神机百炼3.54-染色法判定二分图

uni-app开发中遇到的问题(持续更新)

Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken

Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
随机推荐
STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
如何使用MITMPROXy
谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
ESP8266与STC8H8K单片机联动——天气时钟
Comment utiliser mitmproxy
Detailed explanation of BGP message
Zhuanzhuanben - LAN construction - Notes
mock-用mockjs模拟后台返回数据
神机百炼3.52-Prim
Generic classes and parameterized classes of SystemVerilog
Regular expression summary
LeetCode 47. 全排列 II
使用sha256文件验证下载的文件
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
Google Play Academy 组队 PK 赛,正式开赛!
Memcached installation
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
495. Timo attack
Redis Key-Value数据库【初级】
AttributeError: ‘str‘ object has no attribute ‘decode‘