I2C Master-slave in Assembly (ASM) ic2 với avr

I2C Master-slave in Assembly (ASM)

The I2C protocol is one of the most used protocols in hobby projects. But some might experience getting it to work can be hard. So I created someexample code to communicate between 2 AVR's, an ATmega32 as the master and ATmega8 as slave.
This is only one way I2C communication (master->slave) but I hope it gets you going, because I couldn't find a good headstart for the slave anywere. Offcourse you can use other types of AVR's as long as they have hardware I2C support. Atmel calls it TWI. Note that this is only an example that you can expand and modify yourself. Getting this to work in a real application requires some more coding by using interrupts for example. I'm working on a library that can be used in such circumstances.

This is what it can look like in a test setup. It looks chaotic, and it probably is, but in fact it's quite easy.

Master code

The master controls the I2C bus. In this example, the master can only transmit data.

Slave code

This example outputs the received data on portD but offcourse you can do anything you want with it.

Notes

To make this work, you have to connect the SLC and SDA pins of the master to the slaves. Pull both lines up with a resistor (of several kOhms). It should work with multiple slaves as long as you give them different addresses. For more information, see the datasheet of an AVR with TWI support. The entire I2C protocol is discussed in those. The main part of the masters code is also from there. 

0 nhận xét :

Đăng nhận xét