TWI Library
TWI full master MSSP module is available with a number of AVR MCU models. mikroBasic PRO for AVR provides library which supports the master TWI mode.
Library Routines
TWI_Init
Prototype |
sub procedure TWI_Init(dim clock as longword) ' for XMEGA family of MCUssub procedure TWIx_Init(dim clock as longword) |
---|---|
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 |
sub function TWI_Busy() as byte ' for XMEGA family of MCUssub function TWIx_Busy() as 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 = 1) ... end if |
TWI_Start
Prototype |
sub function TWI_Start() as byte ' for XMEGA family of MCUssub function TWIx_Start() as byte |
---|---|
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) ... end if |
TWI_Read
Prototype |
sub function TWI_Read(dim ack as byte) as byte ' for XMEGA family of MCUssub function TWIx_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 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 |
sub procedure TWI_Write(dim data_ as byte) ' for XMEGA family of MCUssub procedure TWIx_Write(dim data_ as 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 |
sub procedure TWI_Stop() ' for XMEGA family of MCUssub procedure 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 |
sub function TWI_Status() as byte ' for XMEGA family of MCUssub function TWIx_Status() as 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 |
sub procedure TWI_Close() ' for XMEGA family of MCUssub procedure 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 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(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!