| ||
|
Shopping Cart
|
| Home | MP3 Player | 8051 Tools | All Projects | PJRC Store | Site Map |
|
You are here:
Teensy
| Search PJRC |
|
|
Wire LibraryThe Wire library allows you to communicate with I2C devices, often also called "2 wire" or "TWI" (Two Wire Interface).Download: Wire is included with Arduino Hardware RequirementsI2C devices communicate with 2 signals, called SDA and SCL. Normally a 4.7K pullup resistor is connected between each signal and +5 volts. For short wires to only 1 device, you may not need these resistors.
![]() The Wire library is not compatible with Teensy 1.0. Basic UsageWire.begin()Begin using Wire in master mode, where you will initiate and control data transfers. This is the most common use when interfacing with most I2C peripheral chips. Wire.begin(address)Begin using Wire in slave mode, where you will respond at "address" when other I2C masters chips initiate communication. TransmittingWire.beginTransmission(address)Start a new transmission to a device at "address". Master mode is used. Wire.send(data)Send data. In master mode, beginTransmission must be called first. Wire.endTransmission()In master mode, this ends the transmission and causes all buffered data to be sent. ReceivingWire.requestFrom(address, count)Read "count" bytes from a device at "address". Master mode is used. Wire.available()Retuns the number of bytes available by calling receive. Wire.receive()Receive 1 byte. Responding in Slave ModeWire.OnReceive(myReceiveHandlerFunction)Causes "myReceiveHandlerFunction" to be called when a master device sends data. This only works in slave mode. Wire.OnRequest(myRequestHandlerFunction)Causes "myRequestHandlerFunction" to be called when a master device wishes to read data. This only works in slave mode. Example ProgramThis simple example uses a 24C256 I2C EEPROM. The first byte is read, incremented, and written back.
![]()
#include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); } void loop() { byte num; // set the 24C256 eeprom address to 0 Wire.beginTransmission(80); Wire.send(0); // address low byte Wire.send(0); // address high byte Wire.endTransmission(); // read 1 byte, from address 0 Wire.requestFrom(80, 1); while(Wire.available()) { num = Wire.receive(); } Serial.print("num = "); Serial.println(num, DEC); // increment num num = num + 1; // write "num" to 24C256 eeprom at address zero Wire.beginTransmission(80); Wire.send(0); // address low byte Wire.send(0); // address high byte Wire.send(num); // any more send starts writing Wire.endTransmission(); // next time loop runs, it should retrieve the // same number it wrote last time... even if you // shut off the power delay(5000); } Using AddressesMany datasheets will document I2C addresses as 8 bit numbers including a R/W bit. Here is an excerpt from the AT24C256B datasheet, the chip used in the example above.
![]() The Wire library requires addresses which do not include the R/W bit. Based only on the datasheet, you might conclude the address is 160 when writing and 161 when reading. The Wire library needs address 80 to communicate with this chip. The R/W bit is automatically created based on your use of the send or receive functions. More DetailsPlease refer to the official Wire library documentation for more details. |