Home About Copyright and Disclaimer Contact Us Blog Entry Transforming your AVR Microcontroller to the I2C or TWI Slave I/O Expander Project September 27, 2009 by rwb, under Microcontroller. 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 Search This Site Custom Search Future Post Controlling the Motor is one of interesting topics in the embedded world especially for the robotics enthusiasts, on the next post we will learn the basic of motor electronic circuit as well as how to control it with microcontroller. Therefore don't miss it, stay tune on this blog ! ermicroblog Microcontrollers and Electronics Project Blog Transforming your AVR Microcontroller to the I2C or TWI Slave I/O E... file:///D:/Documents/SKRIPSI MAHMUDDIN - BATAN/www.ermicro... 1 of 18 10/30/2012 8:41 AM
18
Embed
Transforming your AVR Microcontroller to the I2C or TWI Slave I_O Expander Project _ ermicroblog
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Home About Copyright and Disclaimer Contact Us
Blog Entry
Transforming your AVR Microcontroller to the I2C or TWI Slave I/O
Expander ProjectSeptember 27, 2009 by rwb, under Microcontroller.
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
Search This Site
Custom Search
Future Post
Controlling the Motor is one ofinteresting topics in the embeddedworld especially for the roboticsenthusiasts, on the next post we willlearn the basic of motor electroniccircuit as well as how to control itwith microcontroller.
Therefore don't miss it, stay tune on
this blog !
ermicroblogMicrocontrollers and Electronics Project Blog
Transforming your AVR Microcontroller to the I2C or TWI Slave I/O E... file:///D:/Documents/SKRIPSI MAHMUDDIN - BATAN/www.ermicro...
1 of 18 10/30/2012 8:41 AM
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
Subscribe
Posts | Comments
Recommended Books
Teach Yourself Electricityand Elect...
Stan GibiliscoBest Price $4.84
or Buy New $23.07
Privacy Information
Transforming your AVR Microcontroller to the I2C or TWI Slave I/O E... file:///D:/Documents/SKRIPSI MAHMUDDIN - BATAN/www.ermicro...
2 of 18 10/30/2012 8:41 AM
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.
//***************************************************************************// File Name : i2cmaster.c// Version : 1.0// Description : AVR I2C Bus Master// Author : RWB// Target : AVRJazz Mega328 Board// Compiler : AVR-GCC 4.3.0; avr-libc 1.6.2 (WinAVR 20090313)// IDE : Atmel AVR Studio 4.17// Programmer : AVRJazz Mega328 STK500 v2.0 Bootloader// : AVR Visual Studio 4.17, STK500 programmer// Last Updated : 12 September 2009//***************************************************************************#include <avr/io.h>#include <util/delay.h>#include <compat/twi.h>
TWDR = regdata; // Store data in TWDR register i2c_state = 0; // Reset I2C State
}
TWCR |= (1<<TWINT); // Clear TWINT Flag break;
case TW_ST_DATA_NACK: // 0xC0: data transmitted, NACK received case TW_ST_LAST_DATA: // 0xC8: last data byte transmitted, ACK received case TW_BUS_ERROR: // 0x00: illegal start or stop condition default: TWCR |= (1<<TWINT); // Clear TWINT Flag i2c_state = 0; // Back to the Begining State }
// Enable Global Interrupt sei();}
int main(void){ unsigned char press_tm;
DDRB = 0xFE; // Set PORTB: PB0=Input, Others as Output PORTB = 0x00; DDRD = 0xFF; // Set PORTD to Output PORTD = 0x00; // Set All PORTD to Low
// Initial I2C Slave TWAR = MCP23008_ADDR & 0xFE; // Set I2C Address, Ignore I2C General Address 0x00 TWDR = 0x00; // Default Initial Value
// Simulated OLAT Return Mode: 0-Same as GPIO, 1-Use an alternative LED pattern abovevolatile unsigned char olat_mode;...... case GPIO: if (rw_status) { PORTD=regdata; // Write to GPIO - PORTD olat_reg=regdata; } else { regdata=PIND; // Read from GPIO - PORTD } break;
case OLAT: if (rw_status == 0) { // Read from Simulated OLAT Register if (olat_mode) {
regdata=led_pattern[iled++]; if (iled >= MAX_PATTERN)
iled = 0; } else {
regdata=olat_reg; } }...
As you notice from the C code above if the olat_mode variable equal to 1, then the MCP23008
Transforming your AVR Microcontroller to the I2C or TWI Slave I/O E... file:///D:/Documents/SKRIPSI MAHMUDDIN - BATAN/www.ermicro...
14 of 18 10/30/2012 8:41 AM
simulated OLAT register will return different LED pattern that is taken from the led_pattern[] variables.
The olat_mode variable is controlled by pressing the AVRJazz Mega168 board user switch read from the
ATMega168 PB0 port status inside the infinite for loop.
if (bit_is_clear(PINB, PB0)) { // if button is pressed _delay_us(100); // Wait for debouching if (bit_is_clear(PINB, PB0)) { press_tm++; if (press_tm > 100) olat_mode ^= 1; // Toggle the olat_mode }}
Compile and Download the I2C Master Code to the AVRJazz Mega168 board
Before compiling the code, we have to make sure the AVR Studio 4 configuration is set properly by
selecting menu project -> Configuration Option, the Configuration windows will appear as follow:
Make sure the Device selected is atmega168 and the Frequency use is 11059200 Hz.
After compiling and simulating our code we are ready to down load the code using the AVRJazz Mega168
bootloader facility. The bootloader program is activated by pressing the user switch and reset switch at
the same time; after releasing both switches, the 8 blue LED indicator will show that the bootloader
program is activate and ready to received command from Atmel AVR Studio 4 STK500 program.
Transforming your AVR Microcontroller to the I2C or TWI Slave I/O E... file:///D:/Documents/SKRIPSI MAHMUDDIN - BATAN/www.ermicro...
15 of 18 10/30/2012 8:41 AM
We choose the HEX file and press the Program Button to down load the code into the AVRJazz Mega168
board. Now you could enjoy the following video showing all the experiments we’ve done in this I2C slave
tutorial:
The Final Though
The usage of microcontroller’s based I2C slave device opening the enormous chances to build more
advance embedded system application, where you could attached multiple microcontroller and freeing
your master microcontroller (I2C master) to perform the main logic for your application, while the
co-microcontroller (I2C slave) could be programmed to do just a specific task such as advanced PWM
motor controller, multiple servo controller (robotics arm and leg), LCD controller (driving LCD display
through I2C), smart sensors and many more.
Last what I like the most is, you could mix and match the microcontroller brand and type such as Atmel
AVR families and Microchip PIC families, where you could take advantage of the features and strength of
both microcontrollers to support your embedded system application.
Bookmarks and Share
Transforming your AVR Microcontroller to the I2C or TWI Slave I/O E... file:///D:/Documents/SKRIPSI MAHMUDDIN - BATAN/www.ermicro...
16 of 18 10/30/2012 8:41 AM
02.02.10 #1
03.02.10 #2
07.07.11 #3
07.07.11 #4
Related Posts
Build Your Own Microcontroller Based PID Control Line Follower Robot (LFR) – Second Part
Using Maxim DS1307 Real Time Clock with Atmel AVR Microcontroller
How to use I2C-bus on the Atmel AVR Microcontroller
AVR Twinkle Twinkle Using PWM Project
AVR LCD Thermometer Using ADC and PWM Project
6 Responses to “Transforming your AVR Microcontroller to the I2C or TWI Slave I/O ExpanderProject”
Comment by mandomoose.
totally awesome. so much good stuff here and well explained
with examples and clear diagrams. Thank you for this
wonderful resource, and enjoyable music
Comment by rwb.
Thank you
Comment by jofre.
Thank you very much for sharing this , I think this is one of
the smaller codes I’ve seen for I2C.
There is only one typo on the master listing that makes the
I2C not work if the files are compiled as they are.
The bit manipulation on the I2CMaster listing to produce the