I²C Library

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


  Important :

Library Routines

I2Cx_Init

Prototype

procedure I2Cx_Init(scl : longint);

Description

This function configures and initializes the desired I²C module with default settings.

This function enables the I²C module by setting the I2CEN bit. The rest of the bits in I²C control register remains unchanged. Default initialization (after reset) of I²C module is :

  • Continue operation in IDLE mode.
  • 7-bit slave address.
  • Slew rate control enabled for High Speed mode (400 kHz).
  • General call address disabled.
  • SCL clock stretching disabled.

As per the I²C standard, SCL clock may be 100 kHz or 400 kHz. However, the user can specify any clock rate up to 1 MHz.

Parameters
  • scl: requested serial clock rate.
Returns

Nothing.

Requires

MCU with the I²C module.

Example
// Initialize the I2C1 module with clock_rate of 100000
I2C1_Init(100000);
Notes
  • Refer to the MCU's datasheet for correct values of the scl in respect with Fosc.
  • 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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Init_Advanced

Prototype

procedure I2Cx_Init_Advanced(Fclk_Khz, scl : dword);

Description

This function configures and initializes the desired I²C module using Peripheral Bus Clock and default initialization settings.

As per the I²C standard, SCL clock may be 100 kHz or 400 kHz. However, the user can specify any clock rate up to 1 MHz.

Parameters
  • Fclk_Khz: Peripheral Bus Clock frequency in kHz.
  • scl: requested serial clock rate.
Returns

Nothing.

Requires

MCU with the I²C module.

Example
I2C1_Init_Advanced(50000, 100000);
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Start

Prototype

function I2Cx_Start() : word;

Description

Determines if the I²C bus is free and issues START signal.

Parameters

None.

Returns
  • 0 if I²C bus was successfully started.
  • 0xFFFF if I²C bus was not successfully started.
Requires

MCU with at least one I²C module.

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

Example
// Issue START signal
I2C1_Start();
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Restart

Prototype

function I2Cx_Restart() : word;

Description

Issues repeated START signal.

Parameters

None.

Returns
  • 0 if I²C bus was successfully restarted.
  • 0xFFFF if I²C bus was not successfully restarted.
Requires

MCU with at least one I²C module.

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

Example
// Issue RESTART signal
I2C1_Restart();
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Is_Idle

Prototype

function I2Cx_Is_Idle() : word;

Description

Waits for the I²C bus to become free. This is a blocking function.

Parameters

None.

Returns
  • 0 if I²C bus is free.
  • 1 if I²C bus is not free.
Requires

MCU with at least one I²C module.

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

Example
var data_ : byte;
...
if !(I2C1_Is_Idle)
  I2C1_Write(data_);
...
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Read

Prototype

function I2Cx_Read(ack : word) : byte;

Description

Reads a byte from the I²C bus.

Parameters
  • ack: acknowledge signal parameter. If this parameter is _I2C_ACK, acknowledge signal will be sent after reading, if its value is _I2C_NACK, not acknowledge signal will be sent after reading.
Returns

Received data.

Requires

MCU with at least one I²C module.

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

Also, START signal needs to be issued in order to use this function. See I2Cx_Start.

Example
var take : byte;
...
// Read data and send the not_acknowledge signal
take := I2C1_Read(_I2C_NACK);
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Write

Prototype

function I2Cx_Write(data_ : byte) : word;

Description

Sends data byte via the I²C bus.

Parameters
  • data_: data to be sent
Returns

  • 0 if there were no errors.
  • 1 if write collision was detected on the I²C bus.

Requires

MCU with at least one I²C module.

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

Also, START signal needs to be issued in order to use this function. See I2Cx_Start.

Example
var data_ : byte;
    error : word;
...
error := I2C1_Write(data_);
error := I2C1_Write(0xA3);
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

I2Cx_Stop

Prototype

procedure I2Cx_Stop();

Description

Issues STOP signal.

Parameters

None.

Returns

Nothing.

Requires

MCU with at least one I²C module.

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

Example
// Issue STOP signal
I2C1_Stop();
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 x in the routine prototype for a number from 1 to 5.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

Library Example

This code demonstrates working with the I²C library. Program sends data to EEPROM (data is written at the address 2). After that, program reads data from the same EEPROM address and displays it on PORTB for visual check. See the figure below how to interface the 24C02 to PIC32.

Copy Code To ClipboardCopy Code To Clipboard
program I2C_Simple;

var i, b : char;

procedure EEPROM_24C02_Init();
begin
  I2C2_Init(100000);
end;

//--------------- Writes data to 24C02 EEPROM - signle location
procedure EEPROM_24C02_WrSingle(wAddr : byte; wData : byte);
  begin
    I2C2_Start();           // issue I2C start signal
    I2C2_Write(0xA0);       // send byte via I2C  (command to 24cO2)
    I2C2_Write(wAddr);      // send byte (address of EEPROM location)
    I2C2_Write(wData);      // send data (data to be written)
    I2C2_Stop();
  end;

//--------------- Reads data from 24C02 EEPROM - single location (random)
function EEPROM_24C02_RdSingle(rAddr : byte) : byte;
  begin
    I2C2_Start();              // issue I2C start signal
    I2C2_Write(0xA0);          // send byte via I2C  (device address + W)
    I2C2_Write(rAddr);         // send byte (data address)
    I2C2_Restart();            // issue I2C signal repeated start
    I2C2_Write(0xA1);          // send byte (device address + R)
    result := I2C2_Read(1);     // Read the data (NO acknowledge)
    I2C2_Stop();
  end;

begin
  CHECON := 0x30;
  AD1PCFG := 0xFFFFFFFF;

  LATB := 0;                   // Set PORTB value to zero
  TRISB := 0;                  // Configure PORTB as output
  TRISA := 0;
  TRISD := 0;
  LATD := 0;
  LATF := 0;
  TRISF := 0;

  EEPROM_24C02_Init();                      // performs I2C initialization
  b := 0x00;
  for i := 0x00 to 0x80 do
    begin
      EEPROM_24C02_WrSingle(i,b);
      Inc(b);
      Delay_ms(5); //max vrednost za upis u eeprom
    end;
    
  for i := 0x00 to 0x80 do
    begin
      LATD := i;
      LATB := EEPROM_24C02_RdSingle(i);
      Delay_ms(100);
    end;
end.

Copyright (c) 2002-2012 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