Articles Code About

Implementing an I2C Master with the Atmel ATMega Series

I2C sequence

I2C is a serial bus used for computer communications, developed by Philips. It's one of the most common ways of communicating between integrated circuits. For example, I2C is the defacto method of interacting with EEPROM chips. If you're developing firmware, you're going to run into I2C at some point.

Luckily, a lot of the Atmel MCU's have built in modules for working with I2C. For example, with the ATMega, there is what they call the two wire interface (TWI), which is compatible with I2C. With ATTiny, you have the Universal Serial Interface (USI), which is also I2C compatible. From what I gathered, I2C isn't mentioned much for patent reasons, but I'm sure you can agree it would be much easier to say that in the datasheet.

Atmel has been nice enough to provide reference implementations of I2C master and slave implementations, for both the TWI and USI. If you're interested in these, check out the application notes for the ATMega/ATTiny you're working with.

Since I've lately been in need of an I2C master driver for the ATMega series, I adapted an easy to use driver from the reference implementation provided by Atmel, with some heavy documentation to make it easier for you to understand what's going on.

You can find the github repository here. I would also recommend reading the TWI/USI sections of the respective MCU datasheets, which are very informative, and also read the documentation for the reference implementations (again, application notes). All of these resources combined, I'm confident you'll be well on your way to working as a successful I2C master (or slave).

Slave implementations are on the way. As always, contributors or welcomed.

Source on Github