TWI Library

TWI module is available with a number of 8051 MCU models. mikroC PRO for 8051 provides library which supports the master TWI mode.

Library Routines

TWI_Init

Prototype

void TWI_Init(unsigned long clock);

Returns

Nothing.

Description

Initializes TWI with desired clock (refer to device datasheet for correct values in respect with Fosc). Needs to be called before using other functions of TWI Library.

Requires

Library requires TWI module.

Example
TWI_Init(100000);

TWI_Busy

Prototype

char TWI_Busy();

Returns

Returns 0 if TWI start sequence is finished, 1 if TWI start sequnce is not finished.

Description

Signalizes the status of TWI bus.

Requires

TWI must be configured before using this function. See TWI_Init.

Example
if (TWI_Busy == 1) { 
...
}

TWI_Start

Prototype

char TWI_Start();

Returns

If there is no error function returns 0, otherwise returns 1.

Description

Determines if TWI bus is free and issues START signal.

Requires

TWI must be configured before using this function. See TWI_Init.

Example
if (TWI_Start == 1) { 
...

TWI_Read

Prototype

char TWI_Read(char ack);

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

TWI must be configured before using this function. See TWI_Init.

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

Example

Read data and send not acknowledge signal:

tmp = TWI_Read(0);

TWI_Write

Prototype

void TWI_Write(char data_);

Returns

Nothing.

Description

Sends data byte (parameter data_) via TWI bus.

Requires

TWI must be configured before using this function. See TWI_Init.

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

Example
TWI_Write(0xA3);

TWI_Stop

Prototype

void TWI_Stop();

Returns

Nothing.

Description

Issues STOP signal to TWI operation.

Requires

TWI must be configured before using this function. See TWI_Init.

Example
TWI_Stop();

TWI_Status

Prototype

char TWI_Status();

Returns

Returns value of status register (TWSR), the highest 5 bits.

Description

Returns status of TWI.

Requires

TWI must be configured before using this function. See TWI_Init.

Example
status = TWI_Status();

TWI_Close

Prototype

void TWI_Close();

Returns

Nothing.

Description

Closes TWI connection.

Requires

TWI must be configured before using this function. See TWI_Init.

Example
TWI_Close();

Library Example

This code demonstrates use of TWI Library procedures and functions. 8051 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 TWI from EEPROM and send its value to PORTA, to check if the cycle was successful. Check the figure below.

void main(){

  TWI_Init(100000);         // initialize TWI communication
  TWI_Start();              // issue TWI start signal
  TWI_Write(0xA2);          // send byte via TWI (device address + W)
  TWI_Write(2);             // send byte (address of EEPROM location)
  TWI_Write(0xAA);          // send data (data to be written)
  TWI_Stop();               // issue TWI stop signal

  Delay_100ms();

  TWI_Start();              // issue TWI start signal
  TWI_Write(0xA2);          // send byte via TWI (device address + W)
  TWI_Write(2);             // send byte (data address)
  TWI_Start();              // issue TWI signal repeated start
  TWI_Write(0xA3);          // send byte (device address + R)
  PORTA = TWI_Read(0u);     // read data (NO acknowledge)
  TWI_Stop();               // issue TWI stop signal
}

HW Connection

Interfacing 24c02 to 8051 via I²C

Interfacing 24c02 to 8051 via TWI

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