2 \author Travis Goodspeed
4 GoodFET I2C Master Application
10 //Higher level left to client application.
12 //Based upon a neighborly example at
13 //http://codinglab.blogspot.com/2008/10/i2c-on-avr-using-bit-banging.html
28 #define I2CDELAY(x) delay(x<<4)
31 //2xx only, need 1xx compat code
32 #define CLRSDA P5OUT&=~SDA
33 #define SETSDA P5OUT|=SDA
34 #define CLRSCL P5OUT&=~SCL
35 #define SETSCL P5OUT|=SCL
37 #define READSDA (P5IN&SDA?1:0)
38 #define SETBOTH P5OUT|=(SDA|SCL)
40 #define I2C_DATA_HI() SETSDA
41 #define I2C_DATA_LO() CLRSDA
43 #define I2C_CLOCK_HI() SETSCL
44 #define I2C_CLOCK_LO() CLRSCL
46 //#warning "Using internal resistors. Won't work on 161x devices."
48 //! Inits bitbanging port, must be called before using the functions below
53 //Direction, not value, is used to set the value.
67 void I2C_WriteBit( unsigned char c )
87 unsigned char I2C_ReadBit()
94 unsigned char c = READSDA;
103 //! Send a START Condition
106 // set both to high at the same time
117 //! Send a STOP Condition
127 //! write a byte to the I2C slave device
128 unsigned char I2C_Write( unsigned char c )
132 I2C_WriteBit( c & 0x80 );
136 return I2C_ReadBit();
140 //! read a byte from the I2C slave device
141 unsigned char I2C_Read( unsigned char ack )
143 unsigned char res = 0;
148 res |= I2C_ReadBit();
162 //! Handles a monitor command.
163 void i2chandle(unsigned char app,
175 if(len>0) //optional parameter of length
177 if(!len) //default value of 1
180 cmddata[i]=I2C_Read(1); //Always acknowledge
181 txdata(app,verb,len);
186 cmddata[0]+=I2C_Write(cmddata[i]);