I²C Library

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


  Important :

Library Routines

I2C1_Init

Prototype

procedure I2C1_Init(const clock : dword);

Returns

Nothing.

Description

Initializes I²C with desired clock (refer to device data sheet for correct values in respect with Fosc). Needs to be called before using other functions of I²C Library.

You don’t need to configure ports manually for using the module; library will take care of the initialization.

Requires

Library requires MSSP module.

  Note : Calculation of the I²C clock value is carried out by the compiler, as it would produce a relatively large code if performed on the libary level. Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.
Example
I2C1_Init(100000);

I2C1_Start

Prototype

function I2C1_Start() : byte;

Returns

If there is no error, function returns 0.

Description

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

Requires

I²C must be configured before using this function. See I2C1_Init.

Example
if (I2C1_Start() = 0) then
...

I2C1_Repeated_Start

Prototype

procedure I2C1_Repeated_Start();

Returns

Nothing.

Description

Issues repeated START signal.

Requires

I²C must be configured before using this function. See I2C1_Init.

Example
I2C1_Repeated_Start();

I2C1_Is_Idle

Prototype

function I2C1_Is_Idle() : byte;

Returns

Returns TRUE if I²C bus is free, otherwise returns FALSE.

Description

Tests if I²C bus is free.

Requires

I²C must be configured before using this function. See I2C1_Init.

Example
if (I2C1_Is_Idle) then
...

I2C1_Rd

Prototype

function I2C1_Rd(ack : byte) : byte;

Returns

Returns one byte from the slave.

Description

Reads one byte from the slave, and sends not acknowledge signal if parameter ack is 0, otherwise it sends acknowledge.

Requires

I²C must be configured before using this function. See I2C1_Init.

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

Example

Read data and send not acknowledge signal:

tmp := I2C1_Rd(0);

I2C1_Wr

Prototype

function I2C1_Wr(data : byte) : byte;

Returns

Returns 0 if there were no errors.

Description

Sends data byte (parameter data) via I²C bus.

Requires

I²C must be configured before using this function. See I2C1_Init.

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

Example
I2C1_Write($A3);

I2C1_Stop

Prototype

procedure I2C1_Stop();

Returns

Nothing.

Description

Issues STOP signal.

Requires

I²C must be configured before using this function. See I2C1_Init.

Example
I2C1_Stop();

Library Example

This code demonstrates use of I²C Library procedures and functions. PIC MCU is connected (SCL, SDA pins ) to 24c02 EEPROM. Program sends data to EEPROM (data is written at address 2). Then, we read data via I2C from EEPROM and send its value to PORTD, to check if the cycle was successful. Check the figure below.

Copy Code To ClipboardCopy Code To Clipboard
program I2C_Simple;

  begin
    ANSEL  := 0;            // Configure AN pins as digital I/O
    ANSELH := 0;
    PORTB := 0;
    TRISB := 0;             // Configure PORTB as output

    I2C1_Init(100000);      // initialize I2C communication
    I2C1_Start();           // issue I2C start signal
    I2C1_Wr(0xA2);          // send byte via I2C  (device address + W)
    I2C1_Wr(2);             // send byte (address of EEPROM location)
    I2C1_Wr(0xAA);          // send data (data to be written)
    I2C1_Stop();            // issue I2C stop signal

    Delay_100ms();

    I2C1_Start();           // issue I2C start signal
    I2C1_Wr(0xA2);          // send byte via I2C  (device address + W)
    I2C1_Wr(2);             // send byte (data address)
    I2C1_Repeated_Start();  // issue I2C signal repeated start
    I2C1_Wr(0xA3);          // send byte (device address + R)
    PORTB := I2C1_Rd(0);    // Read the data (NO acknowledge)
    I2C1_Stop();            // issue I2C stop signal
  end.

HW Connection

Interfacing 24c02 to PIC via I²C

Interfacing 24c02 to PIC via I²C

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