I²C Library

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


  Important :

Library Routines

Generic Routines

I2CM1_Init

Prototype

void I2CM1_Init(char speedMode, char swap);

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

void 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

void I2CM1_Set_Slave_Address(char slaveAddress);

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

char I2CM1_Read(char *dataIn);

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

char I2CM1_Read_10Bit(char *dataIn, unsigned int address10Bit);

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

char I2CM1_Read_Bytes(char *bufferIn, unsigned int numBytesToReceive);

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

char I2CM1_Write(char dataOut);

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

char I2CM1_Write_10Bit(char dataOut, unsigned int address10Bit);

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

char I2CM1_Write_Bytes(char *bufferOut, unsigned int numBytesToSend);

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

void I2CM_Set_Active(
  void (*softReset_Ptr)(void),
  void (*setSlaveAddress_Ptr)(char slaveAddress),
  char (*write_Ptr)(char dataOut),
  char (*write10Bit_Ptr)(char dataOut, unsigned int address10Bit),
  char (*writeBytes_Ptr)(char *buffer, unsigned int numBytesToSend),
  char (*read_Ptr)(char *dataIn),
  char (*receiveByte10Bit_Ptr)(char *dataIn, unsigned int address10Bit),
  char (*readBytes_Ptr)(char *buffer, unsigned int numBytesToReceive)
);

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

void I2CM_Soft_Reset();

Description

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

Example
// Reset I2C Master
I2CM_Soft_Reset();
Notes

None.

I2CM_Set_Slave_Address

Prototype

void I2CM_Set_Slave_Address(char slaveAddress);

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

char I2CM_Read(char *dataIn);

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

char I2CM_Read_10Bit(char *dataIn, unsigned int address10Bit);

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

char I2CM_Read_Bytes(char *bufferIn, unsigned int numBytesToReceive);

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

char I2CM_Write(char dataOut);

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

char I2CM_Write_10Bit(char dataOut, unsigned int address10Bit);

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

char I2CM_Write_Bytes(char *bufferOut, unsigned int numBytesToSend);

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