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.


  Important :

Library Routines

Generic Routines

I2CM1_Init

Prototype

procedure I2CM1_Init(speedMode : byte; swap : byte);

Description

This function configures and initializes the I²C module with the standard user-defined settings.

Parameters
  • speedMode: desired I²C bus speed :
    Value Description
    _I2CM_SPEED_MODE_STANDARD Standard Speed Mode (up to 100kb/s).
    _I2CM_SPEED_MODE_FAST Fast mode (up to 400kb/s).
    _I2CM_SPEED_MODE_FAST_PLUS Fast-plus mode (up to 1Mb/s).
    _I2CM_SPEED_MODE_HIGH High-speed mode (up to 3.4Mb/s).
  • swap:
    Value Description
    _I2CM_SWAP_ENABLE Enable I²C Master and I²C Slave pad position swapping.
    _I2CM_SWAP_DISABLE Disable I²C Master and I²C Slave pad position swapping.
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
  • 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.

I2CM1_Soft_Reset

Prototype

procedure I2CM1_Soft_Reset();

Description

This 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 function. See I2CM1_Init routine.

Example
// Reset I2C Master
I2CM1_Soft_Reset();
Notes
  • 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.

I2CM1_Set_Slave_Address

Prototype

procedure I2CM1_Set_Slave_Address(slaveAddress : byte);

Description

Sets the address which the I²C Master will place on the bus, i.e. Slave address.

Parameters
  • slaveAddress: I²C slave address.
Returns

Nothing.

Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
// Set slave address
I2CM1_Set_Slave_Address(0x0A);
Notes
  • 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.

I2CM1_Read

Prototype

function I2CM1_Read(dataIn : ^byte);

Description

Receives a single byte from the I²C bus.

Parameters
  • dataIn: pointer to variable that will store the received byte.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM1_Read(@bufferIn);       // read bytes and store to bufferIn.
Notes
  • 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.

I2CM1_Read_10Bit

Prototype

function I2CM1_Read_10Bit(dataIn : ^byte; address10Bit : word) : byte;

Description

Receives a single byte from the I²C bus.

Parameters
  • dat: pointer to variable that will store the received data.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM1_Read_10Bit(@bufferIn, address);       // read bytes and store to bufferIn.
Notes
  • 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.

I2CM1_Read_Bytes

Prototype

function I2CM1_Read_Bytes(bufferIn : ^byte; numBytesToReceive : word) : byte;

Description

Receives multiple bytes from the I²C bus.

Parameters
  • bufferIn: pointer to a buffer that will store the received bytes.
  • numBytesToReceive: number of bytes to be received.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM1_Read_Bytes(@bufferIn, 16);       // read 16 bytes and store it to bufferIn.
Notes
  • 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.

I2CM1_Write

Prototype

function I2CM1_Write(dataOut : byte) : byte;

Description

Routine sends a single byte via the I²C bus.

Parameters
  • dataOut: byte to be sent.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM1_Write(dataOut);       // write a byte
Notes
  • 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.

I2CM1_Write_10Bit

Prototype

function I2CM1_Write_10Bit(dataOut : byte; address10Bit : word) : byte;

Description

Routine sends a single byte via the I²C bus.

Parameters
  • dataOut: byte to be sent.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM1_Write_10Bit(@bufferOut, address);
Notes
  • 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.

I2CM1_Write_Bytes

Prototype

function I2CM1_Write_Bytes(bufferOut : ^byte; numBytesToSend : word) : byte;

Description

Routine sends multiple bytes via the I²C bus.

Parameters
  • bufferOut: pointer to a buffer that holds bytes for sending.
  • numBytesToReceive: number of bytes to be sent.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM1_Write_Bytes(@bufferOut, 16);
Notes
  • 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.

I2CM_Set_Active

Prototype

procedure I2CM_Set_Active(
  softReset_Ptr : ^TI2C_Soft_Reset_Ptr;
  setSlaveAddress_Ptr : ^TI2C_Set_Slave_Address_Ptr;
  write_Ptr : ^TI2C_Write_Ptr;
  write10Bit_Ptr : ^TI2C_Write_10Bit_Ptr;
  writeBytes_Ptr : ^TI2C_Write_Bytes_Ptr;
  read_Ptr : ^TI2C_Read_Ptr;
  receiveByte10Bit_Ptr : ^TI2C_Read_10Bit_Ptr;
  readBytes_Ptr : ^TI2C_Read_Bytes_Ptr
);

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 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

procedure I2CM_Soft_Reset();

Description

This 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 procedure. See I2CM1_Init routine.

Example
// Reset I2C Master
I2CM_Soft_Reset();
Notes

None.

I2CM_Set_Slave_Address

Prototype

procedure I2CM_Set_Slave_Address(slaveAddress : 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
  • slaveAddress: I²C slave address.
Returns

Nothing.

Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
// Set slave address
I2CM_Set_Slave_Address(0x0A);
Notes

None.

I2CM_Read

Prototype

function I2CM_Read(dataIn : ^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
  • dataIn: pointer to variable that will store the received byte.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM_Read(@dataIn);
Notes

None.

I2CM_Read_10Bit

Prototype

function I2CM_Read_10Bit(dataIn : ^byte; address10Bit : 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
  • dat: pointer to variable that will store the received data.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM_Read_10Bit(@bufferIn, address);       // read bytes and store to bufferIn.
Notes

None.

I2CM_Read_Bytes

Prototype

function I2CM_Read_Bytes(bufferIn : byte; numBytesToReceive : 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
  • bufferIn: pointer to a buffer that will store the received bytes.
  • numBytesToReceive: number of bytes to be received.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM_Read_Bytes(@bufferIn, 16);       // read 16 bytes and store it to bufferIn.
Notes

None.

I2CM_Write

Prototype

function I2CM_Write(dataOut : byte) : 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
  • dataOut: byte to be sent.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM_Write(dataOut);       // write a byte
Notes

None.

I2CM_Write_10Bit

Prototype

function I2CM_Write_10Bit(dataOut : byte; address10Bit : word) : 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
  • dataOut: byte to be sent.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM_Write_10Bit(@bufferOut, address);
Notes

None.

I2CM_Write_Bytes

Prototype

function I2CM_Write_Bytes(bufferOut : ^byte; numBytesToSend : word) : 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
  • bufferOut: pointer to a buffer that holds bytes for sending.
  • numBytesToReceive: number of bytes to be sent.
Returns
  • _I2CM_STATUS_OK if there were no errors.
  • _I2CM_STATUS_ERROR if an error occured during transmission.
  • _I2CM_STATUS_ARB_LOST if arbitration was lost.
Requires

Routine requires MCU with the I²C module.

Used I²C module must be initialized before using this function. See I2CM1_Init routine.

Example
I2CM_Write_Bytes(@bufferOut, 16);
Notes

None.

Copyright (c) 2002-2015 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code