Component: I2C Master (Comms: Interface)
Author | Matrix Ltd. |
Version | 1.4 |
Category | Comms: Interface |
Contents
I2C Master component
Generic Two Wire I2C Communications Interface
Detailed description
Overview
The I2C bus is a medium speed communications bus which is usually best suited for talking between devices situated on the same circuit board. Due to the high frequency digital nature of the bus care should be taken to keep tracks as short as possible and as far away as possible from other sources of noise. A typical I2C bus consists of two signals, data and clock. The I2C bus usually consists of a single master device and then one or more slave devices. The master device initiates all the communications and can only communicate with a single device on the bus at a time by sending a unique device address as the first byte.
Each I2C transaction consists of a start and a stop as well as one or more data bytes made up of 8 clock cycles allowing the 8-bits of each byte to be transferred. Each byte send is followed by an Ack (acknowledged) or a Nak (not acknowledged) from the receiving device.
Pull up resistors
The I2C bus usually requires pull up resistors in the range of 4.7K to 10K between the two I2C signals and VCC. Some I2C devices have the pull up resistors built in so as to avoid external components.
The pull up resistors can be useful when interfacing a 5V microcontroller to a 3V3 sensor as the pull up resistor can be connected to 3V3 to eliminate the need for voltage level shifting.
Start / Restart / Stop
The Start, Restart and Stop operations are each states which the bus can be put into using the I2C specification.
Generic Write Transaction
A generic write transaction to a memory device might look something like this:
Start
Send External Device Address Byte (Write mode)
Send Internal Address Byte
Send Data Byte
Stop
Generic Read Transaction
A generic read transaction to a memory device might look something like this:
Start
Send External Device Address Byte (Write mode)
Send Internal Address Byte
Restart
Send External Device Address Byte (Read mode)
Read Data Byte
Stop
Examples
More information on I2C can be found here,
Matrix Flowcode Blog: Simplified communications I2C and SPI
Generic I2C EEPROM
Example file demonstrating how to read and write bytes from a generic I2C EEPROM device.
I2CEEPROM
Simulated I2C devices
This example uses the I2C master component combined with the DS1307 injector to simulate an I2C communications bus between the target Microcontroller and the virtual DS1307 device.
I2C DS1307 Example
The panel displays the current time from the DS1307 RAM which is populated to match the system time.
The I2C console shows the communications between the target microcontroller and simulated I2C device.
The DS1307 console shows the contents of RAM memory on the simulated I2C device.
Downloadable macro reference
![]() |
ReceiveByte |
Receives a byte from the I²C bus. | |
![]() |
Last |
Used to signify the last byte when streaming incoming data. 0=Not last byte, 1=Last Byte | |
![]() |
Return |
![]() |
Restart |
Outputs a restart condition onto the I²C bus. | |
![]() |
Return |
![]() |
Stop |
Outputs a stop condition onto the I²C bus. | |
![]() |
Return |
![]() |
Transaction_Uninit |
Uninitialise the I2C interface | |
![]() |
Return |
![]() |
Start |
Outputs a start condition onto the I²C bus. | |
![]() |
Return |
![]() |
Initialise |
Enables the I²C hardware and performs some initialization. Should be called at the start of the program or at least before any of the other I²C functions are called. | |
![]() |
Return |
![]() |
GetInt |
Gets an integer from the LUT | |
![]() |
Index |
![]() |
Return |
![]() |
SetFloatBits |
Sets the size of a float LUT entry | |
![]() |
Bits |
![]() |
Return |
![]() |
AddInt |
Int to add to the int array | |
![]() |
Value |
Int to add to the int array | |
![]() |
Return |
![]() |
AddFloat |
Add a float to the floating point LUT | |
[[File:]] - | Value |
Floating point values | |
![]() |
Return |
![]() |
Initialise |
Sets up the GSM module. Returns 0 for OK, 255 for no reply and 254 for command fail. | |
![]() |
Return |
![]() |
SampleAccel |
Samples the accelerometer 0 = no new data, 1 = new data available | |
![]() |
Return |
![]() |
Left |
Rotates the robot left - waits for the angle to be traversed before returning | |
![]() |
Angle |
Angle to turn in degrees | |
![]() |
Return |
![]() |
LCDDrawPixel |
Allows a single pixel on the LCD to be set or cleared | |
![]() |
X |
X Pixel 0-127 | |
![]() |
Y |
Y Pixel 0-31 | |
![]() |
State |
0=White (Off), 1=Black (On) | |
![]() |
Return |
![]() |
ServoSetPosition |
Allows one of the servo positions to be assigned | |
![]() |
Channel |
Range 0-3 | |
![]() |
Position |
Range 0-255 | |
![]() |
Return |
![]() |
CardBitmap |
Displays a monochrome bitmap read from the card on the LCD | |
![]() |
X |
Range 0-127 | |
![]() |
Y |
Range 0-31 | |
![]() |
Filename |
![]() |
Return |
![]() |
CardOpen |
Attempts to open an existing file on the SD card 255=Error, 1=File not found, 0=Open OK | |
![]() |
Filename |
![]() |
Return |
![]() |
Forwards |
Drives the robot forwards - waits for the distance to be traversed before returning | |
![]() |
Distance |
Distance in mm | |
![]() |
Return |
![]() |
ReadBearing |
Reads the compass bearing based on the degrees clockwise from magnetic north. | |
[[File:]] - | Return |
![]() |
GetConsoleHandle |
Gets the handle to the console allowing data displaying on the panel etc. | |
[[File:]] - | Return |
![]() |
LCDDrawLine |
Allows a single pixel width line to be drawn on the LCD | |
![]() |
X1 |
X Pixel 0-127 | |
![]() |
Y1 |
Y Pixel 0-31 | |
![]() |
X2 |
X Pixel 0-127 | |
![]() |
Y2 |
Y Pixel 0-31 | |
![]() |
Return |
![]() |
ServoAutoMoveToPosition |
Allows one of the servo positions to move gradually to a new position | |
![]() |
Channel |
Range 0-3 | |
![]() |
Position |
Range 0-255 | |
![]() |
Return |
![]() |
ReadAllValue |
Reads a single value stored from the read all command Index dictates which sensor value to read | |
![]() |
Index |
0=SW, 1-8=IR, 9-10=Line, 11=Light, 12=Mic, 13-15=Accel | |
![]() |
Return |
![]() |
LCDBacklight |
Allows the LCD backlight brighntess to be adjusted | |
![]() |
Intensity |
Range 0-100, 0=Off, 1=Min Brightness, 100=Max Brightness | |
![]() |
Return |
![]() |
LCDDrawRect |
Allows a rectangle to be drawn on the LCD | |
![]() |
X1 |
X Pixel 0-127 | |
![]() |
Y1 |
Y Pixel 0-31 | |
![]() |
X2 |
X Pixel 0-127 | |
![]() |
Y2 |
Y Pixel 0-31 | |
![]() |
Return |
![]() |
ReadLine |
Reads one of the IR line sensors 0-1 0=Left, 1=Right | |
![]() |
index |
0=Left, 1=Right | |
![]() |
Return |
![]() |
ServoDisable |
Allows one of the servo outputs 0-3 to be disabled | |
![]() |
Channel |
Range 0-3 | |
![]() |
Return |
![]() |
ServoEnable |
Allows one of the servo outputs 0-3 to be enabled | |
![]() |
Channel |
Range 0-3 | |
![]() |
Return |
![]() |
CardReadByte |
Reads a byte from the currently open file. Starts at the beginning of the file and auto increments to the end | |
![]() |
Return |
![]() |
CardErase |
Attempts to delete an existing file on the SD card 255=Error, 1=File not found, 0=Delete OK | |
![]() |
Filename |
![]() |
Return |
![]() |
CardWriteByte |
Appends a byte to the end of the currently open file | |
![]() |
Data |
![]() |
Return |
![]() |
EncoderRead |
Reads one of the motor encoder counters. Approx 0.328296mm of travel per encoder unit. | |
![]() |
index |
0=Left, 1=Right | |
![]() |
Return |
![]() |
SetMotors |
Sets the motor speeds without feedback Range -100 to 100 | |
[[File:]] - | Left |
Range -100 to 100 | |
[[File:]] - | Right |
Range -100 to 100 | |
![]() |
Return |
![]() |
Backwards |
Drives the robot backwards - waits for the distance to be traversed before returning | |
![]() |
Distance |
Distance in mm | |
![]() |
Return |
![]() |
EncoderReset |
Resets the motor encoder counters | |
![]() |
Return |
![]() |
LCDVerbose |
Allows the LCD to automatically report the API commands as they get processed. Default - verbose on | |
![]() |
State |
0=Off 1=On | |
![]() |
Return |
![]() |
SetProp |
Allows the COM port property to be set from a parent component. | |
![]() |
PropName |
![]() |
PropVal |
![]() |
Return |
![]() |
CardInit |
Attempts to startup the SD card 255=No Card, 254=Init Fail, 0=Init OK | |
![]() |
Return |
![]() |
GetProp |
Allows the property filter for the COM port list to be copied to a parent component. | |
![]() |
PropName |
![]() |
Return |
![]() |
LCDClear |
Clears the LCD | |
![]() |
Return |