Instructions
276Libraries
© 2013 Conrad Electronic
Sub I2C_Write(addr As Byte, hdr As ULong, hdr_len As Byte,
ByRef mem_addr As Byte, length As Word) As Byte
Description
First, up to 4 bytes of header data are written to the I2C device with address addr (I2C 7-bit address).
The header data is passed in hdr (dword), the number of bytes in hdr_len. The hdr_len may be zero,
means that is there is no header data transferred. There are always the first high-order bytes of the
header transmitted (big endian). After transferring the header, length bytes are written from the array
mem_addr to the I2C device.
The term header stands not for a specific I2C term, but for up to 4 bytes, that are transmitted to
the I2C device. Many I2C devices use such a header, e.g. as to index a register.
Parameter
addr address of I2C device
hdr up to 4 byte header data
hdr_len length of header
mem_addr array that is written to I2C device
length number of bytes that are transferred (exclusive header)
Return Parameter
-1 = transmission error
0 = successful
5.12.2.5 I2C Example
Example: EEPROM 24C64 read and write
// I2C device address = 0x50, Bit Rate 100kHz
// EEPROM has 16bit memory address
byte data[10];
void main(void)
{
// read 10 bytes from memory address 0x20 into array data[]
I2C_Read(0x50, 0x20, 2, data, 10);
// write 10 bytes from array data[] to EEPROM memory address 0x20
I2C_Write(0x50, 0x20, 2, data, 10);
}