* This document is outdated because EZ-Builder now converts the address for you. This document may be referenced for educational purposes.
i2c is not entirely about binary. But the address sure is! That is because most addresses are defined as 7 bit values. And if you keep reading, you'll see that changes need to be done to use that value in a byte; because Bytes are 8 bits. Your i2c device datasheet will define the address as a 7-bit value (128 possible addresses).
In an 8-bit i2c packet, the LSB (least significant bit, which is furthest to the right) determines the direction of data. So if you have a 7-bit address to convert to 8 bit, shift the 7-bit address over to the left by one position, thus freeing up the furthest bit on the right.
Example if your i2c 7-bit address is 0001010 converts into:
00010100 <- 8-bit packet for writing
00010101 <- 8-bit packet for reading
Each position of a binary number repesents a value. In an 8 bit byte, you can have a maximum value of 255 (total of 256 positions). Remember we count 0 as a number, don't we? So yes, it starts at 0 and counts to 255.
Here are the values of a binary number from LSB to MSB
LSB = Least Significant bit (Bit furthest to the right)
MSB = Most Significant bit (Bit furthest to the left)
Bit 0 - one
Bit 1 - two
Bit 2 - four
Bit 3 - eight
Bit 4 - sixteen
Bit 5 - thirty-two
Bit 6 - sixty-four
Bit 7 - one hundred twenty-eight
00000001 = 1
00000010 = 2
00000011 = 3
00000100 = 4
00000101 = 5
10000000 = 128
10000001 = 129
10000010 = 130
LSB and MSB
Binary is not about counting. We only apply values to binary so we have a decimal notation to reference the configuration of bits (switches). It is easier for a human to understand the number 13 then to remember 1101.
MSB is the Most Significant Bit. That means the bit that would have the highest decimal value. The MSB is the bit at the furthest LEFT.
LSB is the Least Significant Bit. That is the bit that has the lowest decimal value. The LSB is the bit furthest to the RIGHT.
For example, 10000001 is the value 129 in decimal. The MSB and the LSB are both set to 1. The value of the LSB is 1, and the value of the MSB is 128. 1 + 128 = 129.
Every i2c device has an address. Look in the datasheet for the address. Also pay attention if the address is defined as 7 bit or 8 bit. If the address is defined as 7 bit, then you will need to prepare the address to be used for i2c.
The LSB in the address is used for the direction of data 0 (Write) and 1 (Read). The rest of the 7 bits is where you put the address. Use the Windows Calculator to find the value that shifted your bits over by one to set the Write or Read bit. You can simply multiple your address by 2 to get a 0 (Write) on the first bit. Or multiple your address by 2 and add 1 to get 1 (Read) on the first bit.
For example, the default BlinkM 7-bit address is 0x09. In order to send that over the wire, you need to shift 0x09 to the left by 1 bit. You can do that easily by multiplying 0x09 x 2. Which now equals 0x12. So 0x12 is the WRITE address of the 0x09 BlinkM Device.
What about READ? Well, the read bit (LSB) will need to be set to a 1. The easiest way to do that is (0x09 x 2) + 1. And the answer is 0x13. So the READ address of a 0x09 BlinkM Device is 0x13.
Hex Case Sensitivity
The hex value is NOT case sensitive - You can use upper or lower case in the value, it doesn't matter. 0xFF equals 0xff. However, 0XFF is not hex. The X is always lower case in HEX. A Google search will help you understand why
This bit of code will change a BlinkM with the default address 0x09 to the color blue
I2CWrite(0, 0x12, 0x6e, 0x00, 0x00, 0xff)
Characters vs Strings
You may also not use hex and use characters instead. Like most programming languages, characters are defined with single quotes ' and strings are defined with double quotes ". You can use the letter 'n' instead of 0x6e in this peice of code...
I2CWrite(0, 0x12, 'n', 0x00, 0x00, 0xff)
i2c Wire Length and Pull Up Resistors
If you modify or extend the wire on the i2c device, keep the individual wires (GND, +5, SDA, SCL) from being twisted together. You will notice i2c cables are flat, much like an older IDE Hard Drive Cable. Building your own i2c extension cable is not easy with regular wire. You can try different pull-up resistors, which decreases the sensitivity of your communication, but also removes noise. Ideally you should be using a special i2c cable.
Pull-Up resistors are connected between +5 to SDA and +5 to SCL. They are required for i2c device communication. The EZ-B has 2 pull-up resistors on board for the i2c. If a long thick wire is used (specifically if twisted), you may need to add 2 more resistors. The pull-ups range from 1k ohm to 10k ohm. You may need to expirement with a variety of resistors to find your ideal value.
For understanding of i2c, please read this well written document: Click To Read