【arduino库文件】-wire.h

As a reference the table below shows where TWI pins are located on various Arduino boards.

BoardI2C / TWI pins
Uno, EthernetA4 (SDA), A5 (SCL)
Mega256020 (SDA), 21 (SCL)
Leonardo2 (SDA), 3 (SCL)
Due20 (SDA), 21 (SCL), SDA1, SCL1

不论是 IIC/TWI, 或是 SPI, 以及硬串口、软串口甚至 Serial.print 都是要靠中断来帮忙处理, 如果你把中断禁止了, 那 这些都无法动作了 !

I2C库文件wire

背景
使用 Arduino 例程的时候发现,官方的描述不太详细,走了些弯路。特此,写篇文章记录下。

Arduino 的 I2C 相关函数
Arduino 的封装库真的是非非常的棒,I2C 就只有 10 个 API 函数。I2C 所用的库,称为:Wire Library。详细的描述可以看这个官方地址:

https://www.arduino.cc/en/Reference/Wire

下面我会介绍部分的 API 函数。

begin

begin 函数用于初始化 Wrie Library 并以 Master 或 Slave 的身份加入 I2C 总线上。begin 函数没有返回值。调用 begin 函数有两种形式:

begin():无输入参数,表示以 Master 形式加入总线。
begin( address ):有输入参数,表示以从机形式加入总线,设备地址为address(7-bit)

beginTransmission

beginTransmission 函数用于启动一次 Master write to Slave 操作。值得注意的是这个函数的调用并不会产生 Start 信号 和发送 Slave Address,仅是实现通知 Arduino后面要启动 Master write to Slave 操作。

beginTransmission 函数调用后,(再调用 write 函数进行数据写入), 最后再调用 endTransmission 函数方能产生 Start 信号 和发送 Slave Address 及通讯时序。
beginTransmission 函数调用形式:

beginTransmission(address)
1

write

write 函数用于向 Slave 写入数据。共有 3 种调用形式:

write(value) :写入单字节
write(string) :写入字符串
write(data, length) :写入 length 个字节

endTransmission

endTransmission 函数用于结束一次 Master write to Slave 操作。前面在介绍 beginTransmission 的时候也介绍过了,如果不在后面使用 endTransmission 函数, 总线上不会产生 Master write to Slave 的时序。

endTransmission 函数的调用十分有意思。endTransmission 函数可输入参数。

endTransmission(0):当输入参数为 0 时,将在通讯结束后,不产生 STOP 信号。
endTransmission(!0):当输入参数为 !0 时,在通讯结束后,生成 STOP 信号。(释放总线)
endTransmission():当无输入参数时,在通讯结束后,产生 STOP 信号。(释放总线)
因为我设计的产品程序是使用 DUMMY WRITE 时序,就是这个不产生 STOP 信号卡了我半天的时间(这是我要写本文的原因……)。而官方中,并没有详细介绍这个输入参数…

同时,endTransmission 函数时具有返回值的:

0:success
1:data too long to fit in transmit buffer
2:received NACK on transmit of address
3:received NACK on transmit of data
4:other error
有个地方需要注意的:当通讯过程中,出现异常后,异常后的 write 操作将被终止,直接结束通讯,具体的是否出现异常,只需要看 endTransmission 的返回值即可。

requestFrom

requestFrom 函数用于实现 Master Read From Slave 操作。调用形式有 2 种:

requestFrom(address, quantity):从 address 设备读取 quantity 个字节,结束后,产生 STOP 信号
requestFrom(address, quantity, stop) :从 address 设备读取 quantity 个字节,结束后,依据 stop 的值确定是否产生 STOP 信号。
stop = 0:不产生 STOP 信号
stop != 0:产生 STOP 信号
requestFrom 函数具有返回值(表示从 address 设备读取到的字节数)。

available
available 函数用于统计 Master Read From Slave 操作后, read 缓存区剩余的字节数。每当缓存区的数据被读走 1 个字节,available 函数的返回值减一。通常 available 函数会搭配着 read 函数使用。

read
read 函数用于在 Master Read From Slave 操作后,读取缓存区的数据。

例程
下面的例程,是我提供给客户的案例程序。程序上传至了 GitHub:
https://github.com/TFmini/TFmini-I2C-MasterExample_Arduino

通讯时序如下图所示:

通讯时序
节选代码段:

#include <Wire.h> // I2C head file

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  // Initiate the Wire library and join the I2C bus as a master or Slave.
  Wire.begin(); 
  Serial.print("Ready to Read TFmini\r\n");
  delay(10);
}

void loop() {
  // put your main code here, to run repeatedly:
  byte i = 0;
  byte rx_Num = 0;  // the bytes of received by I2C
  byte rx_buf[7] = {0}; // received buffer by I2C

  Wire.beginTransmission(7); // Begin a transmission to the I2C Slave device with the given address.
  Wire.write(1); // Reg's Address_H
  Wire.write(2); // Reg's Address_L
  Wire.write(7); // Data Length
  Wire.endTransmission(0);  // Send a START Sign

  // Wire.requestFrom(AA,BB);receive the data form slave.
  // AA: Slave Address ; BB: Data Bytes 
  rx_Num = Wire.requestFrom(0x07, 7); 

  // Wire.available: Retuens the number of bytes available for retrieval with read().
  while( Wire.available())
  {
      rx_buf[i] = Wire.read(); // received one byte
      i++;
  }

}

总结

作者:Cherry0_0Wu
来源:CSDN
原文:https://blog.csdn.net/XiuHua_Wu/article/details/82691173
版权声明:本文为博主原创文章,转载请附上博文链接!

根据引用\[1\]中提供的设置,您可以在您的setting.json文件中添加以下设置来解决找不到WProgram.h文件的错误: "arduino.path": "C:/Program Files (x86)/Arduino/", "arduino.additionalUrls": "", "arduino.logLevel": "info", "arduino.enableUSBDetection": true, "C_Cpp.intelliSenseEngine": "Tag Parser" 这些设置将指定Arduino的安装路径,并启用USB检测。此外,还可以使用"C_Cpp.intelliSenseEngine": "Tag Parser"来指定C/C++的智能感知引擎。 如果您需要下载Wire.h头文件,您可以在Arduino的安装路径中找到它。根据引用\[2\]中提供的示例代码,Wire.h头文件是用于I2C通信的文件。您可以在Arduino的安装路径中的libraries文件夹中找到Wire文件夹,其中包含Wire.h头文件。 请注意,根据引用\[3\]中的描述,如果您使用的是Arduino 1.8.9版本,您可能需要对util.js文件进行修改。具体来说,您需要注释掉common路径下util.js文件的第215行的编码判断语句。但是,请注意,这个问题可能只适用于特定的Arduino版本,其他版本可能没有这个问题。 总结起来,要下载ArduinoWire.h头文件,您可以在Arduino的安装路径中找到它。同时,根据您的具体情况,您可能需要根据引用\[1\]和引用\[3\]中提供的设置和修改来解决相关问题。 #### 引用[.reference_title] - *1* *3* [vscode调用Arduino踩坑记](https://blog.csdn.net/yanggengzhen/article/details/103900182)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *4* *5* [Arduino Wire.h(IIC/ I2C)语法](https://blog.csdn.net/weixin_30855761/article/details/99946024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值