Fireduino Buy

Dual-Core Cortex-M3 processor, integrated high-quality audio Codec and WiFi module, have IOT expansion performance, perfectly compatible with the Arduino interface and Arduino IDE programming, and supports FireBlock graphics programming.

Wire

Update time:2018-04-13 Views:2896

Inter-Integrated Circuit

I2C is a multidirectional control bus,that is to say, multiple chips can be connected to the same bus structure,at the same time every chip can be used as a real-time data transmission control source.
SCL – Clock Line
SDA – Data Line
I2C bus is used in the various bus signal lines at least,with automatic addressing、Multi-master clock synchronization and arbitration, and other functions of the bus.Therefore,the computer system is very convenient and flexible use of I2C bus design,volume is small,and therefore is widely used in all kinds of practical application.
I2C serial bus and there are generally two signal lines,one of them is a two-way data SDA,the other is the clock line SCL.all to the I2C bus device serial data transmission through the SDA ,all the device of the clock line SCL received SCL on the bus.Any device can be as the Master ,also can be used as Slave,basically see is a transmission of a device.Due to the I2C addressing、start、response、end and so on mechanism,It is easy to distinguish between master-slave device.。

Wire

Fireduino I2C / TWI library allows you to communicate with I2C / TWI devices,Fireduino has two I2C interface with only support master mode。

Wire API

begin()

   Description
       The library Initialized, the I2C bus is added as a host, usually only called once. 

   Syntax
       Wire.begin()

   Parameters
      None 

   Returns 
       无

requestFrom(...)

   Description 
       To request data from the machine.This datas can be

retrieval by calling function vailable() and read()  

   Syntax
       Wire.requestFrom(addr,number);

   Parameters 
       address : 7-bit address,sent request  to this address
       quantity : The size of bytes requested  

   Reruens 
       The size of returns bytes.

beginTransmission(address)

   Description 
       began to transfer data with I2C slave accrrod to the address.you can call function write() to sort data and endTransmission() to transfer data after calling this function

   Syntax
       Wire.beginTransmission(addr);

   Parameter 
       address: the address of slave to transfer 

   Returns 
       None

endTransmission()

   Description  
       finish transmission,transfer the data form beginTransmission() and sort by write() .

   Syntax
       Wire.endTransmission()

   Parameter
       None

   Returns 值
       None

write()

   Description 
       for slave,write data and responses the host request。for host,sorts the data will be transferred(call it Between beginTransimission() and endTransmission())。 

   Syntax 
       Wire.write(value)
       Wire.write(string)
       Wire.write(data,length)

   Parameter 
       val: send one byte.
       str: send a string.
       data:send an array.
       length:the length of array

   Returns 
       byte:write() returns the number of wrote bytes,also confirm whether the data is wrote.

available()

   Description 
       return number of available bytes.for host ,call it after called requestFrom()

   Syntax
       Wire.available();

   Parameter 
       None

   Returns
       the number of available bytes.

read()

   Description 
       read a byte form host transfer to slave,call after calling requestFrom().

   Syntax 
       Wire.read()

   Parameter 
       None

   Returns  
       the next receive byte.

Wire Example

#include <Wire.h>  //add Libraries Wire Internal Arduino #define Register_ID 0#define Register_2D 0x2D#define Register_X0 0x32#define Register_X1 0x33#define Register_Y0 0x34#define Register_Y1 0x35#define Register_Z0 0x36#define Register_Z1 0x37 
 int ADXAddress = 0xA7>>1;  //Address Translationint reading = 0;int val = 0;int X0,X1,X_out;int Y0,Y1,Y_out;int Z1,Z0,Z_out;double Xg,Yg,Zg; void setup(){
  Serial.begin(9600);
  delay(100);
  Serial.println("setup");  
  Wire.begin();  //initialize I2C
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);
  Wire.endTransmission();} void loop(){
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_X0);
  Wire.write(Register_X1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {X0 = Wire.read();X1 = Wire.read();X1 = X1<<8;X_out = X0+X1;
  } 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Y0);
  Wire.write(Register_Y1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {Y0 = Wire.read();Y1 = Wire.read();Y1 = Y1<<8;Y_out = Y0+Y1;
  } 
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_Z0);
  Wire.write(Register_Z1);
  Wire.endTransmission();
  Wire.requestFrom(ADXAddress,2);
  if(Wire.available()<=2);
  {Z0 = Wire.read();Z1 = Wire.read();Z1 = Z1<<8;Z_out = Z0+Z1;
  } 
  Xg = X_out/256.00;//The output is converted to gravitational acceleration g, 2 digital precision decimal。
  Yg = Y_out/256.00;
  Zg = Z_out/256.00;
  Serial.println(Xg);
  Serial.println(Yg);
  Serial.println(Zg);
  Serial.println(X_out);
  Serial.println(Y_out);
  Serial.println(Z_out);
  delay(1000);  // Delay 1S }