TWI Library

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

Library Routines

TWI_Init

Prototype

sub procedure TWI_Init(dim clock as longword)

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.

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

Requires

Library requires MSSP module on PORTB or PORTC.

Example
TWI_Init(100000)

TWI_Busy

Prototype

sub function TWI_Busy() as byte

Returns

Returns 0 if TWI start sequnce 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) 
    ...
end if

TWI_Start

Prototype

sub function TWI_Start() as byte

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) 
    ...
end if

TWI_Read

Prototype

sub function TWI_Read(dim ack as byte) as 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

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

sub procedure TWI_Write(dim data_ as byte)

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

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

sub function TWI_Status() as byte

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

sub procedure 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.

program TWI_Simple

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)
  P0 = TWI_Read(0)         ' read data (NO acknowledge)
  TWI_Stop()               ' issue TWI stop signal}
end.

HW Connection

Interfacing 24c02 to 8051 via TWI

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