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

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

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

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

sub function I2CM1_Read(dim dataIn as ^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 sub 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

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

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

sub function I2CM1_Write(dim dataOut as byte) as 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 sub 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

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

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

sub procedure I2CM_Set_Active(
  dim softReset_Ptr as ^TI2C_Soft_Reset_Ptr,
  dim setSlaveAddress_Ptr as ^TI2C_Set_Slave_Address_Ptr,
  dim write_Ptr as ^TI2C_Write_Ptr,
  dim write10Bit_Ptr as ^TI2C_Write_10Bit_Ptr,
  dim writeBytes_Ptr as ^TI2C_Write_Bytes_Ptr,
  dim read_Ptr as ^TI2C_Read_Ptr,
  dim receiveByte10Bit_Ptr as ^TI2C_Read_10Bit_Ptr,
  dim readBytes_Ptr as ^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 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
  • 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 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
  • 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 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
  • 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 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
  • 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 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
  • 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 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
  • 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 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
  • 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 sub function. See I2CM1_Init routine.

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