Skip to content

Instantly share code, notes, and snippets.

@castleberrysam
Created December 20, 2016 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save castleberrysam/90abf7f0d173f4fc28990f51d10faf78 to your computer and use it in GitHub Desktop.
Save castleberrysam/90abf7f0d173f4fc28990f51d10faf78 to your computer and use it in GitHub Desktop.
What is I2C?
- Communication protocol
- Two wires (SDA, SCL) also needs GND
- Serial, data is transmitted one bit at a time
- Half duplex, one device talks at a time
- Synchronous, must receive response before moving on
- Master/slave, master controls clock and who can talk
- 1 byte word size, every ninth bit contains ACK or NACK
- 100kb/s baud rate, but it allows slave to stretch the clock by pulling SCL low
- SDA can only change when SCL is low (except start/stop)
- All devices have a 7-bit address (0 -> 127)
- Usually read/write registers on the device (register addresses)
Typical I2C Communication Flow
1. Master sends start sequence (drive SDA low while SCL is high)
2. All slaves listen for their address
3. Master sends slave address + read/write bit
4. All slaves inspect the address to determine if it is their address
5. The addressed slave sends an ACK and all other slaves ignore
6. Master sends the register address
7. The addressed slave sends an ACK
8. Master sends data one byte at a time waiting for an ACK every 9th bit
9. If master receives a NACK, then it aborts the communication
10. Master sends stop sequence (drive SDA high while SCL is high)
Additional I2C Communication Flow Rules
- You cannot read and write in the same transmission
- A read usually requires at least two transmissions
. A write to point to the register that you want to read
. A read to read the register
- Most I2C devices will auto-increment the register pointer
- Read/write bit: 0 means write, 1 means read
- 9th bit: 0 means ACK, 1 means NACK
Prerequisites for I2C Success
- Read vendor's data sheet
- Understand binary
- Understand hexadecimal
- Understand bit shifting
- Understand bitwise operations
- Understand signed vs. unsigned data types
- Understand big endianness vs. little endianness
Vendor Data Sheet
- Look for I2C section
- Search for "slave address" or "I2C address"
- Are there control registers?
- What are the different configurations available?
- Are there data registers?
- What data is stored in which register?
- Does the device need to be calibrated?
- Is there a way to test that the device is functioning correctly?
Big Endian vs. Little Endian
- Remember that the value of bit positions gets larger as you move from right to left
- For a multi-byte value
. The leftmost byte is the Most Significant Byte (MSB)
. The rightmost byte is the Least Significant Byte (LSB)
- Big endian means that the multi-byte value is arranged MSB to LSB
- Little endian means that the multi-byte value is arranged LSB to MSB
Device Addressing Example
- Slave address: decimal 36, hex 0x24, binary 0100100
- Write address: decimal 72, hex 0x48, binary 01001000
- Read address: decimal 73, hex 0x49, binary 01001001
WpilibJ I2C Class
- Uses a different I2C class for each slave device
- Constructor takes port and slave address (port = onboard or expansion)
- addressOnly verifies device responds to specified slave address
- read reads from registers
- readOnly reads from a device without registers
- transaction reads and writes to registers
- verifySensor verifies the value in a register
- write writes to a single register
- writeBulk writes to multiple registers
Some Tips
- Don't try to send too much data
- Don't put too many I2C devices on the same bus
- Make sure you allow appropriate time between readings
- Keep your wiring as short as you can
- Make sure devices are calibrated
- Consider ways to power cycle devices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment