The I2C bus (read as I squared C) is one of the most important embedded system serial bus interface first introduced by Philips in 1980; using just two lines called SCL (serial clock) and SDA (serial data) respectively make the I2C bus is a perfect choice to provide additional I/O capabilities to your microcontroller project without changing your microcontroller type and design in order to increase the I/O port pins.
Today there are plenty choices of I2C slave I/O port expander devices available on the market such as Philips PCF8574 and Microchip MCP23008 for addressable 8-bit general I/O ports which capable of handling I2C standard bus speed of 100Khz or fast speed mode of 400Khz; the Microchip MCP23008 even can handle up to 1.7Mhz bus speed. You could read more information about using I2C interface on my previous posted blog How to use I2C-bus on the Atmel AVR Microcontroller.
The usage of I2C slave devices is not merely for the I/O expander but it also use to expand your microcontroller capabilities as well as it’s functionalities such as the I2C real time clock from Maxim DS1307 (Using Maxim DS1307 Real Time Clock with Atmel AVR Microcontroller), Microchip TC1321 10-bit digital to analog converter (DAC), Microchip MCP2331 12-bit analog to digital converter (ADC), Microchip MCP9801 high accuracy temperature sensor and Microchip 24AA128 16KB EEPROM.
Although there are many types of sophisticated I2C slave devices available on the market, sometimes we need the I2C slave device which has the capabilities beyond that, something more powerful, more flexible and yet easy to be configured; the answer to this requirement is to use the microcontroller it self as the I2C slave device, that is why most of the midrange class microcontrollers have the I2C slave peripheral feature build inside it. This is what we are going to learn on this tutorial, where the principal we learn here could be applied to other microcontroller type as well.
The following is the list of hardware and software used in this tutorial:
- AVRJazz Mega328 board from ermicro as the I2C master controller which base on the AVR ATmega328P microcontroller (board schema).
- JazzMate MCP23008 board from ermicro which base on the Microchip MCP23008 8-bit I2C I/O expander.
- AVRJazz Mega168 board from ermicro as the I2C slave device which base on the AVR ATmega168 microcontroller (board schema).
- WinAVR for the GNU’s C compiler
- Atmel AVR Studio 4 for the coding and debugging environment.
- STK500 programmer from AVR Studio 4, using the AVRJazz Mega328 and AVRJazz Mega168 boards STK500 v2.0 bootloader facility.
The MCP23008 8-bit I/O Expander I2C slave
The Microchip MCP23008 will be a perfect model for our I2C slave device as it has the 3-bit configurable address (000 to 111) which provides up to 7 devices that could be attached to the I2C bus which give total of 56 ports. MCP23008 also come with 11 internal register to control its operation
As the I2C slave device, mean the MCP23008 will be the passive device that depends on the I2C master device to initiate the communication. On this tutorial I will use the AVRJazz Mega328 learning board as the I2C master controller; the following is the C code for the I2C master controller.
For more detail: Transforming your AVR Microcontroller to the I2C or TWI Slave I/O Expander Project