TWI Library
TWI full master MSSP module is available with a number of AVR MCU models. mikroC PRO for AVR provides library which supports the master TWI mode.
Library Routines
TWI_Init
Prototype |
void TWI_Init(unsigned long clock); // for XMEGA family of MCUsvoid TWIx_Init(unsigned long clock); |
---|---|
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 |
char TWI_Busy(); // for XMEGA family of MCUschar TWIx_Busy(); |
---|---|
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 == 1) { ... |
TWI_Start
Prototype |
char TWI_Start(); // for XMEGA family of MCUschar TWIx_Start(); |
---|---|
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 == 1) { ... |
TWI_Read
Prototype |
char TWI_Read(char ack); // for XMEGA family of MCUschar TWIx_Read(char ack); |
---|---|
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 |
void TWI_Write(char data_); // for XMEGA family of MCUsvoid TWIx_Write(char data_); |
---|---|
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 |
void TWI_Stop(); // for XMEGA family of MCUsvoid 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 |
char TWI_Status(); // for XMEGA family of MCUschar TWIx_Status(); |
---|---|
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 |
void TWI_Close(); // for XMEGA family of MCUsvoid 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.
void main(){ 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(0u); // read data (NO acknowledge) TWI_Stop(); // issue TWI stop signal }
HW Connection
Interfacing 24c02 to AVR via TWI
What do you think about this topic ? Send us feedback!