I2C總線的Arduino庫函數less
I2C即Inter-Integrated Circuit串行總線的縮寫,是PHILIPS公司推出的芯片間串行傳輸總線。它以1根串行數據線(SDA)和1根串行時鐘線(SCL)實現了雙工的同步數據傳輸。具備接口線少,控制方式簡化,器件封裝形式小,通訊速率較高等優勢。在主從通訊中,能夠有多個I2C總線器件同時接到I2C總線上,經過地址來識別通訊對象。ide
Arduino提供的I2C庫函數爲Wire.h。模塊化
I2C總線工做原理函數
SDA(串行數據線)和SCL(串行時鐘線)都是雙向I/O線,接口電路爲開漏輸出.需經過上拉電阻接電源VCC.當總線空閒時.兩根線都是高電平,鏈接總線的外同器件都是CMOS器件,輸出級也是開漏電路.在總線上消耗的電流很小,所以,總線上擴展的器件數量主要由電容負載來決定,由於每一個器件的總線接口都有必定的等效電容.而線路中電容會影響總線傳輸速度.當電容過大時,有可能形成傳輸錯誤.因此,其負載能力爲400pF,所以能夠估算出總線容許長度和所接器件數量。oop
主器件用於啓動總線傳送數據,併產生時鐘以開放傳送的器件,此時任何被尋址的器件均被認爲是從器件.在總線上主和從、發和收的關係不是恆定的,而取決於此時數據傳送方向。若是主機要發送數據給從器件,則主機首先尋址從器件,而後主動發送數據至從器件,最後由主機終止數據傳送;若是主機要接收從器件的數據,首先由主器件尋址從器件.而後主機接收從器件發送的數據,最後由主機終止接收過程。在這種狀況下.主機負責產生定時時鐘和終止數據傳送。 [2] ui
I2C總線特徵this
I2C總線特色能夠歸納以下:spa
(1)在硬件上,I2C總線只須要一根數據線和一根時鐘線兩根線,總線接口已經集成在芯片內部,不須要特殊的接口電路,並且片上接口電路的濾波器能夠濾去總線數據上的毛刺.所以I2C總線簡化了硬件電路PCB佈線,下降了系統成本,提升了系統可靠性。由於I2C芯片除了這兩根線和少許中斷線,與系統再沒有鏈接的線,用戶經常使用IC能夠很容易造成標準化和模塊化,便於重複利用。調試
(2)I2C總線是一個真正的多主機總線,若是兩個或多個主機同時初始化數據傳輸,能夠經過沖突檢測和仲裁防止數據破壞,每一個鏈接到總線上的器件都有惟一的地址,任何器件既能夠做爲主機也能夠做爲從機,但同一時刻只容許有一個主機。數據傳輸和地址設定由軟件設定,很是靈活。總線上的器件增長和刪除不影響其餘器件正常工做。rest
(3)I2C總線能夠經過外部連線進行在線檢測,便於系統故障診斷和調試,故障能夠當即被尋址,軟件也利於標準化和模塊化,縮短開發時問。
(4)鏈接到相同總線上的IC數量只受總線最大電容的限制,串行的8位雙向數據傳輸速率在標準模式下可達100Kbit/s,快速模式下可達400Kbit/s,高速模式下可達3.4Mbit/s。
(5)總線具備極低的電流消耗.抗高噪聲干擾,增長總線驅動器可使總線電容擴大10倍,傳輸距離達到15m;兼容不一樣電壓等級的器件,工做溫度範圍寬。
This library allows you to communicate with I2C / TWI devices. On the Arduino boards with the R3 layout (1.0 pinout), the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. The Arduino Due has two I2C / TWI interfaces SDA1 and SCL1 are near to the AREF pin and the additional one is on pins 20 and 21.
As a reference the table below shows where TWI pins are located on various Arduino boards. TWI引腳在不一樣板子上的位置。
Board I2C / TWI pins
Uno, Ethernet A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Due 20 (SDA), 21 (SCL), SDA1, SCL1
As of Arduino 1.0, the library inherits from the Stream functions, making it consistent with other read/write libraries. Because of this, send() and receive() have been replaced with read() and write().
There are both 7- and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it's being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8 bit address, you'll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8. Please note that a pull-up resistor is needed when connecting SDA/SCL pins. Please refer to the examples for more informations. MEGA 2560 board has pull-up resistors on pins 20 - 21 onboard.
I2C的Arduino庫函數
begin() 初始化總線,做爲主機或從機加入總線,帶地址參數就是從機,不帶就是主機
requestFrom() 主機向從機請求字節/數據
beginTransmission() 主機向從機傳送數據開始
endTransmission() 主機向從機傳送數據中止
write() 寫數據,從機向主機或主機向從機
available() 返回經過read()函數獲得的字節數
read() 讀取一個傳送的字節
SetClock() 修改I2C通訊的頻率,標準值是100KHz
onReceive() 註冊一個函數,當從機接收到主機的數據時此函數被調用
onRequest() 註冊一個函數,當主機向從機請求數據時此函數 被調用
I2C的Arduino庫函數詳解
Wire.begin() 初始化總線,做爲主機或從機加入總線,帶地址參數就是從機,不帶就是主機
Description 描述
Initiate the Wire library and join the I2C bus as a master or slave. This should normally be called only once.
Syntax 語法
Wire.begin(address)
Parameters 參數
address: the 7-bit slave address (optional); if not specified, join the bus as a master.
Returns 返回
None
Wire.requestFrom() 主機向從機請求字節/數據
Description
Used by the master to request bytes from a slave device. The bytes may then be retrieved with the available() and read() functions.
As of Arduino 1.0.1, requestFrom() accepts a boolean argument changing its behavior for compatibility with certain I2C devices.
If true, requestFrom() sends a stop message after the request, releasing the I2C bus.
If false, requestFrom() sends a restart message after the request. The bus will not be released, which prevents another master device from requesting between messages. This allows one master device to send multiple requests while in control.
The default value is true.
Syntax 語法
Wire.requestFrom(address, quantity)
Wire.requestFrom(address, quantity, stop)
Parameters
address: the 7-bit address of the device to request bytes from
quantity: the number of bytes to request
stop : boolean. true will send a stop message after the request, releasing the bus. false will continually send a restart after the request, keeping the connection active.
Returns
byte : the number of bytes returned from the slave device
Wire.beginTransmission(address) 主機向從機傳送數據開始
Description
Begin a transmission to the I2C slave device with the given address. Subsequently, queue bytes for transmission with the write() function and transmit them by calling endTransmission().
Parameters
address: the 7-bit address of the device to transmit to
Returns
None
Wire.endTransmission() 主機向從機傳送數據中止
Description
Ends a transmission to a slave device that was begun by beginTransmission() and transmits the bytes that were queued by write().
As of Arduino 1.0.1, endTransmission() accepts a boolean argument changing its behavior for compatibility with certain I2C devices.
If true, endTransmission() sends a stop message after transmission, releasing the I2C bus.
If false, endTransmission() sends a restart message after transmission. The bus will not be released, which prevents another master device from transmitting between messages. This allows one master device to send multiple transmissions while in control.
The default value is true.
Syntax
Wire.endTransmission()
Wire.endTransmission(stop)
Parameters
stop : boolean. true will send a stop message, releasing the bus after transmission. false will send a restart, keeping the connection active.
Returns
byte, which indicates the status of the transmission:
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() 寫數據,從機向主機或主機向從機
Description
Writes data from a slave device in response to a request from a master, or queues bytes for transmission from a master to slave device (in-between calls to beginTransmission() and endTransmission()).
Syntax
Wire.write(value)
Wire.write(string)
Wire.write(data, length)
Parameters
value: a value to send as a single byte
string: a string to send as a series of bytes
data: an array of data to send as bytes
length: the number of bytes to transmit
Returns
byte: write() will return the number of bytes written, though reading that number is optional
Example 舉例
#include <Wire.h>
byte val = 0;
void setup()
{ Wire.begin(); // join i2c bus }
void loop()
{
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
// device address is specified in datasheet
Wire.write(val); // sends value byte
Wire.endTransmission(); // stop transmitting
val++; // increment value
if(val == 64) // if reached 64th position (max)
{ val = 0; // start over from lowest value }
delay(500);
}
Wire.available() 返回經過read()函數獲得的字節數
Description
Returns the number of bytes available for retrieval with read(). This should be called on a master device after a call to requestFrom() or on a slave inside the onReceive() handler.
available() inherits from the Stream utility class.
Parameters
None
Returns
The number of bytes available for reading.
Wire.read() 讀取一個傳送的字節
Description
Reads a byte that was transmitted from a slave device to a master after a call to requestFrom() or was transmitted from a master to a slave. read() inherits from the Stream utility class.
Syntax
Wire.read()
Parameters
none
Returns
The next byte received
Example
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
Wire.setClock() 修改I2C通訊的頻率,標準值是100KHz
Description
This function modifies the clock frequency for I2C communication. I2C slave devices have no minimum working clock frequency, however 100KHz is usually the baseline.
Syntax
Wire.setClock(clockFrequency)
Parameters
clockFrequency: the value (in Hertz) of desired communication clock. Accepted values are 100000 (standard mode) and 400000 (fast mode). Some processors also support 10000 (low speed mode), 1000000 (fast mode plus) and 3400000 (high speed mode). Please refer to the specific processor documentation to make sure the desired mode is supported.
Returns
None
Wire.onReceive(handler) 註冊一個函數,當從機接收到主機的數據時,此函數被調用
Description
Registers a function to be called when a slave device receives a transmission from a master.
Parameters
handler: the function to be called when the slave receives data; this should take a single int parameter (the number of bytes read from the master) and return nothing, e.g.: void myHandler(int numBytes)
Returns
None
Wire.onRequest(handler) 註冊一個函數,當主機向從機請求數據時,此函數被調用
Description
Register a function to be called when a master requests data from this slave device.
Parameters
handler: the function to be called, takes no parameters and returns nothing, e.g.: void myHandler()
Returns
None