DUE I2C Bus

I2C Bus

I2C Bus is a way to transfer data between 2 digital circuits. There is typically one master circuit that is in charge of initiating transfers. And then there are one or more slaves on the bus. The BrainPad MicroComputer is always the bus master. A slave can be an accelerometer or motor controller.

When a master talks to a slave, it needs to know the slave’s address. Each slave must have its own unique address. The master then commands the addressed slave to either read or write data. I2cBytes() is used for transferring the data. The function needs 2 arrays, one for the outgoing data and one for the incoming data.

BrainBot, for example, has headlights that can be controlled using 4 values sent over I2C. The slave address of the robot is 0x01, and then the 4 values must be 0x01 followed by the three color levels (Red, Green, Blue). Do not confuse the 0x01 in the first sent value with the slave address, which happens to also be 0x01.

This example will use the Red (reflected as 0xff), while keeping the Green and Blue in the off state (which is 0x00). By the way, these hex numbers are just how codes are used to present values. You can simply use 0 instead of 0x00 and 255 instead of 0xff.

Dim a[4] # We will write 4 bytes
Dim b[0] # We do not need to read
a[0]= 0x01
a[1]= 0xff
a[2]= 0x00
a[3]= 0x00
I2cBytes(0x01, a, 5, b, 0)

Restart Condition

Every I2C transaction starts with a START condition, then the slave’s address, then data, and finally the STOP condition. Some sensors require a continuous write-read transaction with no STOP in the middle. Those transactions require have a RESTART condition instead. When I2cBytes() is called with both read and write, it automatically sends RESTART apropiately.


BrainStorm

Where is the “bus” in I2C bus? Data is delivered down the wire by one as a stream of data. In computer world, this is called a data bus. What other busses are used around us? Think of how a Keyboard is wired to a PC.

Content Licensing
Newsletter

Twitter Feed
Hot off the press!