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)
24 //Bits are cleared by output of low.
25 //Bits are set but input and pull-up resistor.
26 #define SETSDA P5DIR&=~SDA
27 #define CLRSDA P5DIR|=SDA
28 #define SETSCL P5DIR&=~SCL
29 #define CLRSCL P5DIR|=SCL
31 #define READSDA (P5IN&SDA?1:0)
32 #define SETBOTH P5DIR&=~(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
41 void I2C_WriteBit( unsigned char c )
61 unsigned char I2C_ReadBit()
68 unsigned char c = READSDA;
76 //! Inits bitbanging port, must be called before using the functions below
80 //Direction, not value, is used to set the value.
90 //! Send a START Condition
93 // set both to high at the same time
104 //! Send a STOP Condition
114 //! write a byte to the I2C slave device
115 unsigned char I2C_Write( unsigned char c )
119 I2C_WriteBit( c & 128 );
123 return I2C_ReadBit();
127 //! read a byte from the I2C slave device
128 unsigned char I2C_Read( unsigned char ack )
130 unsigned char res = 0;
135 res |= I2C_ReadBit();
149 //! Handles a monitor command.
150 void i2chandle(unsigned char app,
162 if(len>0) //optional parameter of length
164 if(len==0) //default value of 1
167 cmddata[i]=I2C_Read(1); //Always acknowledge
168 txdata(app,verb,len);
172 I2C_Write(cmddata[i]);