【Arduino】I2C Master 实现

#背景
前几天,有个海外的客户在使用 Arduino 连接产品(I2C接口)的时候,发现有异常。为了方便,我自己写了个 Arduino 的例程。使用 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) 

##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++;
  }
  
  // 原来下面是我的处理程序,不放上来了。反正上面的就是通讯相关的部分
}

#总结
在几年前,我买了个 Arduino 的开发板,一直没有用,要不是这次,我也不会真正的用上 Arduino。实话实说,Arduino 真的是十分好用,重点是巨简单,整个编程过程就像是儿童编程一样。

有意思!
这里写图片描述

猜你喜欢

转载自blog.csdn.net/XiuHua_Wu/article/details/82691173