当前位置:网站首页>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();
边栏推荐
- Eco express micro engine system has supported one click deployment to cloud hosting
- How to use mitmproxy
- Frequently asked questions about jetpack compose and material you
- Spark overview
- Verifying downloaded files using sha256 files
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- Community theory | kotlin flow's principle and design philosophy
- Current situation analysis of Devops and noops
- [C language] simple implementation of mine sweeping game
- 网络相关知识(硬件工程师)
猜你喜欢
如何使用MITMPROXy
51单片机——ADC讲解(A/D转换、D/A转换)
AttributeError: ‘str‘ object has no attribute ‘decode‘
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
ROS create workspace
经典文献阅读之--Deformable DETR
The official zero foundation introduction jetpack compose Chinese course is coming!
Eco express micro engine system has supported one click deployment to cloud hosting
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
亚马逊aws数据湖工作之坑1
随机推荐
Generic classes and parameterized classes of SystemVerilog
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
ESP8266与STC8H8K单片机联动——天气时钟
Picture clipping plug-in cropper js
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
[C language] screening method for prime numbers
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Database learning summary 5
LeetCode 83. 删除排序链表中的重复元素
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Web页面用户分步操作引导插件driver.js
Web page user step-by-step operation guide plug-in driver js
Use some common functions of hbuilderx
BGP 路由优选规则和通告原则
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
社区说|Kotlin Flow 的原理与设计哲学
Frequently asked questions about jetpack compose and material you
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
ZABBIX server trap command injection vulnerability (cve-2017-2824)