Hardware Reference
In-Depth Information
Masters do not have an address because they are free to start communica-
tions whenever they want and automatically receive all responses. To declare
the Arduino as a master, call the Wire.begin() command, without an address
parameter.
Wire.begin(); // configure the Arduino as an I2C master
Master Communications
On most projects, the Arduino is coni gured as an I 2 C master, sending mes-
sages to slaves and listening to the responses. To create an I 2 C message, you
must follow several steps:
1. Begin the transmission.
2. Write the data.
3. End the transmission.
This creates a custom I 2 C message to a specii c slave. When a slave answers,
there is no encapsulation, and a write can be performed without beginning or
ending a transmission. Data requests are also encapsulated but are made by a
single function.
Sending Information
The I 2 C protocol specii es that master communication must be done in a single
transmission. To avoid breaks in the message, the message is i rst constructed
and completed before being sent.
To start sending data, the sketch must i rst begin a transmission structure by
using Wire.beginTransmission() . It takes one parameter, the destination address.
Wire.beginTransmission(address);
The sketch is then required to queue data, using Wire.write(). This function
can be called in three different ways. It can be called with a byte as the param-
eter to be appended to the queue. A string can be specii ed, in which case each
byte of the string will be appended. An array can be specii ed with a second
parameter, the length of data to send. Wire.write() will return the amount of
bytes appended to the message, but it's not necessary to read this.
Wire.write(value); // append a byte
Wire.write(string); // append a string
Wire.write(data, length); // append an array with a specified number
of bytes
number =Wire.write(string); // store the number of bytes appended in
a variable
 
Search WWH ::




Custom Search