I²C Library
The I²C full master I²C module is available with a number of the MCU models. The mikroPascal PRO for FT90x provides a library which supports the master I²C mode.

- I²C library routines require you to specify the module you want to use. To select the desired I²C module, simply change the letter n in the routine prototype for a number from 0 to 5.
- Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
- Library routine prototypes may vary from family to family, so please pay attention to the desired routine section.
Library Routines
- I2CM1_Init
- I2CM1_Soft_Reset
- I2CM1_Set_Slave_Address
- I2CM1_Read
- I2CM1_Read_10Bit
- I2CM1_Read_Bytes
- I2CM1_Write
- I2CM1_Write_10Bit
- I2CM1_Write_Bytes
- I2CM_Set_Active
Generic Routines
- I2CM_Soft_Reset
- I2CM_Set_Slave_Address
- I2CM_Read
- I2CM_Read_10Bit
- I2CM_Read_Bytes
- I2CM_Write
- I2CM_Write_10Bit
- I2CM_Write_Bytes
I2CM1_Init
Prototype |
sub procedure I2CM1_Init(dim speedMode as byte, dim swap as byte) |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This sub function configures and initializes the I²C module with the standard user-defined settings. |
||||||||||||||||
Parameters |
|
||||||||||||||||
Returns |
Nothing. |
||||||||||||||||
Requires |
Routine requires MCU with the I²C module. |
||||||||||||||||
Example |
' Initialize the I2C module with the standard bitrate, swapping disabled I2CM1_Init(_I2CM_SPEED_MODE_STANDARD, _I2CM_SWAP_DISABLE) |
||||||||||||||||
Notes |
|
I2CM1_Soft_Reset
Prototype |
sub procedure I2CM1_Soft_Reset() |
---|---|
Description |
This sub function resets the whole I²C Master controller. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
' Reset I2C Master I2CM1_Soft_Reset() |
Notes |
|
I2CM1_Set_Slave_Address
Prototype |
sub procedure I2CM1_Set_Slave_Address(dim slaveAddress as byte) |
---|---|
Description |
Sets the address which the I²C Master will place on the bus, i.e. Slave address. |
Parameters |
|
Returns |
Nothing. |
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
' Set slave address I2CM1_Set_Slave_Address(0x0A) |
Notes |
|
I2CM1_Read
Prototype |
sub function I2CM1_Read(dim dataIn as ^byte) |
---|---|
Description |
Receives a single byte from the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Read(@bufferIn) ' read bytes and store to bufferIn. |
Notes |
|
I2CM1_Read_10Bit
Prototype |
sub function I2CM1_Read_10Bit(dim dataIn as ^byte, dim address10Bit as word) as byte |
---|---|
Description |
Receives a single byte from the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Read_10Bit(@bufferIn, address) ' read bytes and store to bufferIn. |
Notes |
|
I2CM1_Read_Bytes
Prototype |
sub function I2CM1_Read_Bytes(dim bufferIn as ^byte, dim numBytesToReceive as word) as byte |
---|---|
Description |
Receives multiple bytes from the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Read_Bytes(@bufferIn, 16) ' read 16 bytes and store it to bufferIn. |
Notes |
|
I2CM1_Write
Prototype |
sub function I2CM1_Write(dim dataOut as byte) as byte |
---|---|
Description |
Routine sends a single byte via the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Write(dataOut) ' write a byte |
Notes |
|
I2CM1_Write_10Bit
Prototype |
sub function I2CM1_Write_10Bit(dim dataOut as byte, dim address10Bit as word) as byte |
---|---|
Description |
Routine sends a single byte via the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Write_10Bit(@bufferOut, address) |
Notes |
|
I2CM1_Write_Bytes
Prototype |
sub function I2CM1_Write_Bytes(dim bufferOut as ^byte, dim numBytesToSend as word) as byte |
---|---|
Description |
Routine sends multiple bytes via the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Write_Bytes(@bufferOut, 16) |
Notes |
|
I2CM_Set_Active
Prototype |
sub procedure I2CM_Set_Active(
|
---|---|
Description |
Sets the active I²C module which will be used by the I²C routines. |
Parameters |
|
Returns |
Nothing. |
Requires |
Used I²C module must be initialized before using this sub function. See the I2CM1_Init routine. |
Example |
I2CM_Set_Active(@I2CM1_Soft_Reset, @I2CM1_Set_Slave_Address, @I2CM1_Write, @I2CM1_Write_10Bit, @I2CM1_Write_Bytes, @I2CM1_Read, @I2CM1_Read_10Bit, @I2CM1_Read_Bytes) ' Sets the I2CM1 module active |
Notes |
Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library. |
I2CM_Soft_Reset
Prototype |
sub procedure I2CM_Soft_Reset() |
---|---|
Description |
This sub procedure resets the whole I²C Master controller. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub procedure. See I2CM1_Init routine. |
Example |
' Reset I2C Master I2CM_Soft_Reset() |
Notes |
None. |
I2CM_Set_Slave_Address
Prototype |
sub procedure I2CM_Set_Slave_Address(dim slaveAddress as byte) |
---|---|
Description |
Sets the address which the I²C Master will place on the bus, i.e. Slave address. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
Nothing. |
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
' Set slave address I2CM_Set_Slave_Address(0x0A) |
Notes |
None. |
I2CM_Read
Prototype |
sub function I2CM_Read(dim dataIn as ^byte) This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
---|---|
Description |
Receives a single byte from the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM_Read(@dataIn) |
Notes |
None. |
I2CM_Read_10Bit
Prototype |
sub function I2CM_Read_10Bit(dim dataIn as ^byte, dim address10Bit as word) |
---|---|
Description |
Receives a single byte from the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM_Read_10Bit(@bufferIn, address) ' read bytes and store to bufferIn. |
Notes |
None. |
I2CM_Read_Bytes
Prototype |
sub function I2CM_Read_Bytes(dim bufferIn as byte, dim numBytesToReceive as word) |
---|---|
Description |
Receives multiple bytes from the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM_Read_Bytes(@bufferIn, 16) ' read 16 bytes and store it to bufferIn. |
Notes |
None. |
I2CM_Write
Prototype |
sub function I2CM_Write(dim dataOut as byte) as byte |
---|---|
Description |
Routine sends a single byte via the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM_Write(dataOut) ' write a byte |
Notes |
None. |
I2CM_Write_10Bit
Prototype |
sub function I2CM_Write_10Bit(dim dataOut as byte, dim address10Bit as word) as byte |
---|---|
Description |
Routine sends a single byte via the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Write_10Bit(@bufferOut, address) |
Notes |
None. |
I2CM_Write_Bytes
Prototype |
sub function I2CM_Write_Bytes(dim bufferOut as ^byte, dim numBytesToSend as word) as byte |
---|---|
Description |
Routine sends multiple bytes via the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2CM_Set_Active routine. |
Parameters |
|
Returns |
|
Requires |
Routine requires MCU with the I²C module. Used I²C module must be initialized before using this sub function. See I2CM1_Init routine. |
Example |
I2CM1_Write_Bytes(@bufferOut, 16) |
Notes |
None. |
What do you think about this topic ? Send us feedback!