Implementation

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

The master driver C-code consists of three files:
  1. TWI_Master.c
  2. TWI_Master.h
  3. Main.c

There is an example on how to use the driver in the main.c file. The TWI_Master.h file must be included in the main application and contains all function declarations, a define of the Bit Rate Register (TWBR), and defines for all TWI status codes. The TWBR must be set according to the description earlier in this application note. The TWI status code defines can be used to evaluate error messages and to take appropriate actions. The TWI_Master.c file contains all the driver functions.