I2C Interface


Detailed Description

Implementation for communicating with I2C slaves using a polled I2C master implementation

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();

\

Files

Functions


Function Documentation

void I2C_Init void   )  [inline]
 

Initialize the I2C port pins.

void I2C_SendStartBit void   ) 
 

Send a start bit Start bit is a 1 to 0 transition on SDA while SCL is high

            ________
 SDA master         \________
            _________________
 SDA slave  ___/
            __________
        SCL ___/      \_____

bool I2C_SendByte uint8_t  byte  ) 
 

Send a byte (address or data) to the slave parameter order used to select between sending LSB or MSB first

Parameters:
byte Byte to send to slave
Return values:
1 if the slave did
0 if the slave didn't ack.
            __________________________      ________
 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]       

See also:
I2C_SendStartBit

I2C_SendStopBit

uint8_t I2C_GetByte bool  lastbyte  ) 
 

Reads a byte from the slave

Parameters:
lastbyte 1 if last byte read 0 if more bytes to be read
Returns:
byte read from slave
               _____________________      __________
 SDA master __/                     \____/          ..
                                     ack
            _______ ___ ___   ___ ______________ ___
 SDA slave      \__X___X___..X___X___/      \___x___..

                 _   _   _     _   _   _      _   _
       SCL  ____/1\_/2\_/3\.._/7\_/8\_/9\____/1\_/2\..

                msb               lsb ack  

void I2C_SendStopBit void   ) 
 

Generates a stop bit assumes SCL is low Stop bit is a 0 to 1 transition on SDA while SCL is high

           ____
 SDA _____/    
         ______
 SCL ___/      
See also:
I2C_SendStartBit

I2C_SendByte


Copyright 2002 Jörgen Birkler. For accelR8 Accelerometer on Mon Jan 19 21:43:20 2004 by Doxygen 1.3.5.