1 //GoodFET I2C Master Application
4 //Higher level left to client application.
6 //Based upon a neighborly example at
7 //http://codinglab.blogspot.com/2008/10/i2c-on-avr-using-bit-banging.html
22 #define I2CDELAY(x) delay(x<<4)
25 //2xx only, need 1xx compat code
26 #define CLRSDA P5OUT&=~SDA
27 #define SETSDA P5OUT|=SDA
28 #define CLRSCL P5OUT&=~SCL
29 #define SETSCL P5OUT|=SCL
31 #define READSDA (P5IN&SDA?1:0)
32 #define SETBOTH P5OUT|=(SDA|SCL)
34 #define I2C_DATA_HI() SETSDA
35 #define I2C_DATA_LO() CLRSDA
37 #define I2C_CLOCK_HI() SETSCL
38 #define I2C_CLOCK_LO() CLRSCL
40 #warning "Using internal resistors. Won't work on 161x devices."
42 //! Inits bitbanging port, must be called before using the functions below
47 //Direction, not value, is used to set the value.
60 void I2C_WriteBit( unsigned char c )
80 unsigned char I2C_ReadBit()
87 unsigned char c = READSDA;
96 //! Send a START Condition
99 // set both to high at the same time
110 //! Send a STOP Condition
120 //! write a byte to the I2C slave device
121 unsigned char I2C_Write( unsigned char c )
125 I2C_WriteBit( c & 0x80 );
129 return I2C_ReadBit();
133 //! read a byte from the I2C slave device
134 unsigned char I2C_Read( unsigned char ack )
136 unsigned char res = 0;
141 res |= I2C_ReadBit();
155 //! Handles a monitor command.
156 void i2chandle(unsigned char app,
168 if(len>0) //optional parameter of length
170 if(!len) //default value of 1
173 cmddata[i]=I2C_Read(1); //Always acknowledge
174 txdata(app,verb,len);
179 cmddata[0]+=I2C_Write(cmddata[i]);