/*! \file i2csw.c \brief Software-driven I2C interface using port pins. */ //***************************************************************************** // // File Name : 'i2csw.c' // Title : Software-driven I2C interface using port pins // Author : Pascal Stang // Created : 11/22/2000 // Revised : 5/2/2002 // Version : 1.1 // Target MCU : Atmel AVR series // Editor Tabs : 4 // // This code is distributed under the GNU Public License // which can be found at http://www.gnu.org/licenses/gpl.txt // //***************************************************************************** #include #include "i2csw.h" // Standard I2C bit rates are: // 100KHz for slow speed // 400KHz for high speed //#define QDEL delay(5) // i2c quarter-bit delay //#define HDEL delay(10) // i2c half-bit delay // i2c quarter-bit delay #define QDEL asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); // i2c half-bit delay #define HDEL asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); #define I2C_SDL_LO cbi( SDAPORT, SDA) #define I2C_SDL_HI sbi( SDAPORT, SDA) #define I2C_SCL_LO cbi( SCLPORT, SCL); #define I2C_SCL_HI sbi( SCLPORT, SCL); #define I2C_SCL_TOGGLE HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO; #define I2C_START I2C_SDL_LO; QDEL; I2C_SCL_LO; #define I2C_STOP HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL; /* void i2ct(void) { HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO; } void i2cstart(void) { I2C_SDL_LO; QDEL; I2C_SCL_LO; } void i2cstop(void) { HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL; } #define I2C_SCL_TOGGLE i2ct(); #define I2C_START i2cstart(); #define I2C_STOP i2cstop(); */ UINT i2cPutbyte(u08 b) { int i; for (i=7;i>=0;i--) { if ( b & (1<=0;i--) { HDEL; I2C_SCL_HI; // clock HI c = inb(SDAPIN) & (1<