TWI Library
TWI full master MSSP module is available with a number of AVR MCU models. mikroPascal PRO for AVR provides library which supports the master TWI mode.
Library Routines
TWI_Init
Prototype |
procedure TWI_Init(clock : dword); // for XMEGA family of MCUsprocedure TWIx_Init(clock : dword); |
---|---|
Returns |
Nothing. |
Description |
Initializes TWI with desired You don’t need to configure ports manually for using the module; library will take care of the initialization. For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
Requires |
Library requires MCU with TWI module. |
Example |
TWI_Init(100000); |
TWI_Busy
Prototype |
function TWI_Busy() : byte; // for XMEGA family of MCUsfunction TWIx_Busy() : byte; |
---|---|
Returns |
|
Description |
Signalizes the status of TWI bus. For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
Requires |
TWI must be configured before using this function. See TWI_Init. |
Example |
if (TWI_Busy = 0) begin ... end; |
TWI_Start
Prototype |
function TWI_Start() : char; // for XMEGA family of MCUsfunction TWIx_Start() : char; |
---|---|
Returns |
|
Description |
Determines if TWI bus is free and issues START signal. For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
Requires |
TWI must be configured before using this function. See TWI_Init. |
Example |
if (TWI_Start = 0) begin ... end; |
TWI_Read
Prototype |
function TWI_Read(ack : byte) : byte; // for XMEGA family of MCUsfunction TWIx_Read(ack : byte) : byte; |
---|---|
Returns |
Returns one byte from the slave. |
Description |
Reads one byte from the slave, and sends not acknowledge signal if parameter For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
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 |
procedure TWI_Write(data_ : byte); // for XMEGA family of MCUsprocedure TWIx_Write(data_ : byte); |
---|---|
Returns |
Nothing. |
Description |
Sends data byte (parameter For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
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 |
procedure TWI_Stop(); // for XMEGA family of MCUsprocedure TWIx_Stop(); |
---|---|
Returns |
Nothing. |
Description |
Issues STOP signal to TWI operation. For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
Requires |
TWI must be configured before using this function. See TWI_Init. |
Example |
TWI_Stop(); |
TWI_Status
Prototype |
function TWI_Status() : byte; // for XMEGA family of MCUsfunction TWIx_Status() : byte; |
---|---|
Returns |
Returns value of status register (TWSR), the highest 5 bits. |
Description |
Returns status of TWI. For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
Requires |
TWI must be configured before using this function. See TWI_Init. |
Example |
status := TWI_Status(); |
TWI_Close
Prototype |
procedure TWI_Close(); // for XMEGA family of MCUsprocedure TWIx_Close(); |
---|---|
Returns |
Nothing. |
Description |
Closes TWI connection. For XMEGA family of MCUs change the X in the routine prototype with C, D, E or F. |
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. AVR 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; begin DDRA := 0xFF; // configure PORTA as output 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(0); // read data (NO acknowledge) TWI_Stop(); // issue TWI stop signal} end.
HW Connection
Interfacing 24c02 to AVR via TWI
What do you think about this topic ? Send us feedback!