Handles I2C communications with external I2C devices. Also called TWI (two wire interface), Find the i2c specification at http://www.philips.com.
Devices are connected to two port pins on the chip. The i2c bus uses two signals SCK - clock and SDA - Data. Lines are active low and devices are ANDED together on the bus using open collector or open drain.
The internal pull up resistors in the AVR are used so no external pull up resistors are needed. Code has not been tested with multiple masters; the bus arbitration is not tested.
Hardware configuration is done in hardware.h. The following macros are needed by i2c.c:
First startbit, then slave address including LSB (read/write) then either a byte send or byte read. Send bytes to LCD example:
I2C_SendStartBit(); I2C_SendByte(LCD_DRIVER_ADDRESS); //I2C slave address I2C_SendByte(LCD_CONTROL_BYTE_WRITE_COMMAND); //This address LCD control bytes I2C_SendByte(LCD_SET_DDRAM_ADDRESS(LCD_GET_DDRAM_ADDR(x,y))); I2C_SendStopBit();
\
|
|
Initialize the I2C port pins.
|
|
|
Send a start bit Start bit is a 1 to 0 transition on SDA while SCL is high
________
SDA master \________
_________________
SDA slave ___/
__________
SCL ___/ \_____
|
|
|
Send a byte (address or data) to the slave parameter order used to select between sending LSB or MSB first
__________________________ ________
SDA slave \____/ ..
ack
___ ___ ___ ___ __________ ___ ___
SDA master ___/___X___X___..X___X___/ \___x___..
_ _ _ _ _ _ _ _
SCL ____/1\_/2\_/3\.._/7\_/8\_/9\____/1\_/2\..
msb lsb ack ^
|
[Can be held low by slave]
|
|
|
Reads a byte from the slave
_____________________ __________
SDA master __/ \____/ ..
ack
_______ ___ ___ ___ ______________ ___
SDA slave \__X___X___..X___X___/ \___x___..
_ _ _ _ _ _ _ _
SCL ____/1\_/2\_/3\.._/7\_/8\_/9\____/1\_/2\..
msb lsb ack
|
|
|
Generates a stop bit assumes SCL is low Stop bit is a 0 to 1 transition on SDA while SCL is high
____
SDA _____/
______
SCL ___/
|