2 Implementation

The implemented code in this application note is a pure slave driver. The TWI modules also support master operation. See AVR315: Using the TWI module as I2C master for a sample of a master driver. The master and slave drivers could be merged to one combined master and slave driver, but this is not the scope of this application note.

The slave driver c-code consists of three files:
  1. TWI_Slave.c
  2. TWI_Slave.h
  3. Main.c

There is an example on how to use the driver in the main.c file. The TWI_Slave.h file must be included in the main application and contains all function declarations and defines for all TWI status codes. The TWI status code defines can be used to evaluate error messages and to take appropriate actions. The file TWI_Slave.c contains all the driver functions.

Some devices have an additional TWI Address Mask Register (TWAMR) that enables a device to respond to several TWI slave addresses. A customized version of the standard implementation described here, is included in the application note attachment.